FEI Version of the Day
fei_CSVec.cpp
00001 /*--------------------------------------------------------------------*/
00002 /*    Copyright 2005 Sandia Corporation.                              */
00003 /*    Under the terms of Contract DE-AC04-94AL85000, there is a       */
00004 /*    non-exclusive license for use of this work by or on behalf      */
00005 /*    of the U.S. Government.  Export of this program may require     */
00006 /*    a license from the United States Government.                    */
00007 /*--------------------------------------------------------------------*/
00008 
00009 #include <fei_CSVec.hpp>
00010 #include <algorithm>
00011 
00012 namespace fei {
00013 
00014 CSVec::CSVec(const FillableVec& invec)
00015  : indices_(invec.size()),
00016    coefs_(invec.size())
00017 {
00018   operator=(invec);
00019 }
00020 
00021 CSVec::CSVec(unsigned sz)
00022  : indices_(sz, 0),
00023    coefs_(sz, 0.0)
00024 {
00025 }
00026 
00027 CSVec::~CSVec()
00028 {
00029 }
00030 
00031 CSVec&
00032 CSVec::operator=(const CSVec& invec)
00033 {
00034   indices_ = invec.indices_;
00035   coefs_ = invec.coefs_;
00036 
00037   return *this;
00038 }
00039 
00040 CSVec&
00041 CSVec::operator=(const FillableVec& invec)
00042 {
00043   indices_.resize(invec.size());
00044   coefs_.resize(invec.size());
00045 
00046   FillableVec::const_iterator iter = invec.begin(), iter_end = invec.end();
00047 
00048   unsigned i=0;
00049   for(; iter != iter_end; ++iter, ++i) {
00050     indices_[i] = iter->first;
00051     coefs_[i] = iter->second;
00052   }
00053 
00054   return *this;
00055 }
00056 
00057 void add_entry(CSVec& vec, int eqn, double coef)
00058 {
00059   std::vector<int>& v_ind = vec.indices();
00060   std::vector<double>& v_coef = vec.coefs();
00061 
00062   std::vector<int>::iterator
00063     iter = std::lower_bound(v_ind.begin(), v_ind.end(), eqn);
00064 
00065   size_t offset = iter - v_ind.begin();
00066 
00067   if (iter == v_ind.end() || *iter != eqn) {
00068     v_ind.insert(iter, eqn);
00069     v_coef.insert(v_coef.begin()+offset, coef);
00070   }
00071   else {
00072     v_coef[offset] += coef;
00073   }
00074 }
00075 
00076 void put_entry(CSVec& vec, int eqn, double coef)
00077 {
00078   std::vector<int>& v_ind = vec.indices();
00079   std::vector<double>& v_coef = vec.coefs();
00080 
00081   std::vector<int>::iterator
00082     iter = std::lower_bound(v_ind.begin(), v_ind.end(), eqn);
00083 
00084   size_t offset = iter - v_ind.begin();
00085 
00086   if (iter == v_ind.end() || *iter != eqn) {
00087     v_ind.insert(iter, eqn);
00088     v_coef.insert(v_coef.begin()+offset, coef);
00089   }
00090   else {
00091     v_coef[offset] = coef;
00092   }
00093 }
00094 
00095 void remove_entry(CSVec& vec, int eqn)
00096 {
00097   std::vector<int>& v_ind = vec.indices();
00098   std::vector<double>& v_coef = vec.coefs();
00099 
00100   std::vector<int>::iterator
00101     iter = std::lower_bound(v_ind.begin(), v_ind.end(), eqn);
00102 
00103   if (iter != v_ind.end() && *iter == eqn) {
00104     v_ind.erase(iter);
00105 
00106     size_t offset = iter - v_ind.begin();
00107     std::vector<double>::iterator coef_iter = v_coef.begin()+offset;
00108     v_coef.erase(coef_iter);
00109   }
00110 }
00111 
00112 void set_values(CSVec& vec, double scalar)
00113 {
00114   std::fill(vec.coefs().begin(), vec.coefs().end(), scalar);
00115 }
00116 
00117 void add_CSVec_CSVec(const CSVec& u, CSVec& v)
00118 {
00119   const std::vector<int>& indices = u.indices();
00120   const std::vector<double>& coefs = u.coefs();
00121 
00122   for(size_t i=0; i<indices.size(); ++i) {
00123     add_entry(v, indices[i], coefs[i]);
00124   }
00125 }
00126 
00127 }//namespace fei
00128 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends