9 #include <fei_FillableMat.hpp>
10 #include <fei_EqnBuffer.hpp>
11 #include <fei_CSVec.hpp>
16 FillableMat::FillableMat()
23 FillableMat::FillableMat(
EqnBuffer& eqnbuf)
27 std::vector<int>& eqnNums = eqnbuf.
eqnNumbers();
28 int numEqns = eqnNums.size();
29 std::vector<fei::CSVec*>& eqns = eqnbuf.
eqns();
31 for(
int i=0; i<numEqns; ++i) {
34 int rowlen = row_vec->size();
35 int* indices = &(row_vec->indices()[0]);
36 double* coefs = &(row_vec->coefs()[0]);
38 for(
int j=0; j<rowlen; ++j) {
39 putCoef(row, indices[j], coefs[j]);
45 FillableMat::~FillableMat()
48 iter = matdata_.begin(), iter_end = matdata_.end();
49 for(; iter!=iter_end; ++iter) {
50 vecpool_.destroy(iter->second);
51 vecpool_.deallocate(iter->second, 1);
57 FillableMat::operator=(
const FillableMat& src)
61 FillableMat::const_iterator
65 for(; s_iter != s_end; ++s_iter) {
66 int row = s_iter->first;
67 const CSVec* srow = s_iter->second;
68 const std::vector<int>& s_ind = srow->indices();
69 const std::vector<double>& s_coef = srow->coefs();
71 for(
size_t i=0; i<s_ind.size(); ++i) {
73 double coef = s_coef[i];
75 putCoef(row, col, coef);
84 FillableMat::setValues(
double value)
87 iter = matdata_.begin(), iter_end = matdata_.end();
89 for(; iter != iter_end; ++iter) {
90 set_values(*(iter->second), value);
96 FillableMat::createPosition(
int row,
int col)
98 sumInCoef(row, col, 0.0);
102 FillableMat::feipoolmat::iterator
103 insert_row(FillableMat::feipoolmat& matdata,
104 FillableMat::feipoolmat::iterator iter,
110 CSVec* vptr = vecpool.allocate(1);
111 vecpool.construct(vptr, dummy);
113 if (vptr->indices().capacity() == 0) {
114 vptr->indices().reserve(16);
115 vptr->coefs().reserve(16);
118 return matdata.insert(iter, std::make_pair(row, vptr));
123 FillableMat::sumInCoef(
int row,
int col,
double coef)
125 CSVec* rowvec = create_or_getRow(row);
127 add_entry(*rowvec, col, coef);
132 FillableMat::putCoef(
int row,
int col,
double coef)
134 CSVec* rowvec = create_or_getRow(row);
136 put_entry(*rowvec, col, coef);
141 FillableMat::sumInRow(
int row,
const int* cols,
const double* coefs,
144 CSVec* rowvec = create_or_getRow(row);
146 for(
unsigned i=0; i<len; ++i) {
147 add_entry(*rowvec, cols[i], coefs[i]);
153 FillableMat::putRow(
int row,
const int* cols,
const double* coefs,
156 CSVec* rowvec = create_or_getRow(row);
158 for(
unsigned i=0; i<len; ++i) {
159 put_entry(*rowvec, cols[i], coefs[i]);
165 FillableMat::getNumRows()
const
167 return matdata_.size();
172 FillableMat::hasRow(
int row)
const
174 feipoolmat::const_iterator iter = matdata_.find(row);
175 return iter != matdata_.end();
180 FillableMat::getRow(
int row)
const
182 feipoolmat::const_iterator iter = matdata_.lower_bound(row);
184 if (iter == matdata_.end() || iter->first != row) {
185 throw std::runtime_error(
"fei::FillableMat: row not found.");
193 FillableMat::create_or_getRow(
int row)
195 feipoolmat::iterator iter = matdata_.lower_bound(row);
197 if (iter == matdata_.end() || iter->first != row) {
198 iter = insert_row(matdata_, iter, row, vecpool_);
209 iter = matdata_.begin(), iter_end = matdata_.end();
210 for(; iter!=iter_end; ++iter) {
211 vecpool_.destroy(iter->second);
212 vecpool_.deallocate(iter->second, 1);
220 FillableMat::operator==(
const FillableMat& rhs)
const
222 if (getNumRows() != rhs.getNumRows())
return false;
224 FillableMat::const_iterator
228 FillableMat::const_iterator rhs_it = rhs.begin();
230 for(; this_it != this_end; ++this_it, ++rhs_it) {
231 int this_row = this_it->first;
232 int rhs_row = rhs_it->first;
233 if (this_row != rhs_row)
return false;
235 const CSVec* this_row_vec = this_it->second;
236 const CSVec* rhs_row_vec = rhs_it->second;
238 if (*this_row_vec != *rhs_row_vec)
return false;
246 FillableMat::operator!=(
const FillableMat& rhs)
const
248 return !(*
this == rhs);
252 void print(std::ostream& os,
const FillableMat& mat)
254 FillableMat::const_iterator
255 irow = mat.begin(), irowend = mat.end();
256 for(; irow!=irowend; ++irow) {
257 int row = irow->first;
258 const CSVec* vec = irow->second;
259 const std::vector<int>& v_ind = vec->indices();
260 const std::vector<double>& v_coef = vec->coefs();
261 os <<
"row " << row <<
": ";
262 for(
size_t i=0; i<v_ind.size(); ++i) {
263 os <<
"("<<v_ind[i]<<
","<<v_coef[i]<<
") ";
274 FillableMat::const_iterator
275 r_iter = mat.begin(),
278 for(; r_iter != r_end; ++r_iter) {
279 CSVec* row = r_iter->second;
289 rows.resize(mat.getNumRows());
291 FillableMat::const_iterator
292 m_iter = mat.begin(),
296 for(; m_iter!=m_end; ++m_iter) {
297 rows[offset++] = m_iter->first;
std::vector< fei::CSVec * > & eqns()
std::vector< int > & eqnNumbers()
void get_row_numbers(const FillableMat &mat, std::vector< int > &rows)
int count_nnz(const FillableMat &mat)
void print(std::ostream &os, const FillableMat &mat)