FEI Version of the Day
fei_CSVec.hpp
00001 #ifndef _fei_CSVec_hpp_
00002 #define _fei_CSVec_hpp_
00003 
00004 /*--------------------------------------------------------------------*/
00005 /*    Copyright 2005 Sandia Corporation.                              */
00006 /*    Under the terms of Contract DE-AC04-94AL85000, there is a       */
00007 /*    non-exclusive license for use of this work by or on behalf      */
00008 /*    of the U.S. Government.  Export of this program may require     */
00009 /*    a license from the United States Government.                    */
00010 /*--------------------------------------------------------------------*/
00011 
00012 #include <fei_macros.hpp>
00013 #include <vector>
00014 #include <algorithm>
00015 
00016 namespace fei {
00017 
00024 class CSVec {
00025  public:
00026   CSVec(unsigned sz=0);
00027   virtual ~CSVec();
00028 
00029   CSVec& operator=(const CSVec& invec);
00030 
00031   std::vector<int>& indices() {return indices_;}
00032   const std::vector<int>& indices() const {return indices_;}
00033   std::vector<double>& coefs() {return coefs_;}
00034   const std::vector<double>& coefs() const {return coefs_;}
00035 
00036   size_t size() const {return indices_.size();}
00037 
00038   void clear() { indices_.clear(); coefs_.clear(); }
00039 
00040   bool operator==(const CSVec& rhs) const {
00041     return indices_==rhs.indices_ && coefs_==rhs.coefs_;
00042   }
00043 
00044   bool operator!=(const CSVec& rhs) const {
00045     return indices_!=rhs.indices_ || coefs_!=rhs.coefs_;
00046   }
00047 
00048  private:
00049   std::vector<int> indices_;
00050   std::vector<double> coefs_;
00051 };//class CSVec
00052 
00053 inline
00054 void add_entry(CSVec& vec, int eqn, double coef)
00055 {
00056   std::vector<int>& v_ind = vec.indices();
00057   std::vector<double>& v_coef = vec.coefs();
00058 
00059   std::vector<int>::iterator
00060     iter = std::lower_bound(v_ind.begin(), v_ind.end(), eqn);
00061 
00062   size_t offset = iter - v_ind.begin();
00063 
00064   if (iter == v_ind.end() || *iter != eqn) {
00065     v_ind.insert(iter, eqn);
00066     v_coef.insert(v_coef.begin()+offset, coef);
00067   }
00068   else {
00069     v_coef[offset] += coef;
00070   }
00071 }
00072 
00073 
00074 void add_entries(CSVec& vec, int num, const int* eqns, const double* coefs);
00075 
00076 void put_entry(CSVec& vec, int eqn, double coef);
00077 
00078 double get_entry(const CSVec& vec, int eqn);
00079 
00080 void remove_entry(CSVec& vec, int eqn);
00081 
00082 void set_values(CSVec& vec, double scalar);
00083 
00086 void add_CSVec_CSVec(const CSVec& u, CSVec& v);
00087 
00088 }//namespace fei
00089 
00090 #endif
00091 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends