dune-istl  2.2.1
matrixindexset.hh
Go to the documentation of this file.
1 #ifndef DUNE_MATRIX_INDEX_SET_HH
2 #define DUNE_MATRIX_INDEX_SET_HH
3 
4 #include <vector>
5 #include <set>
6 
7 namespace Dune {
8 
9 
12 {
13 
14 public:
15  typedef std::size_t size_type;
16 
18  MatrixIndexSet() : rows_(0), cols_(0)
19  {}
20 
22  MatrixIndexSet(size_type rows, size_type cols) : rows_(rows), cols_(cols) {
23  indices_.resize(rows_);
24  }
25 
28  rows_ = rows;
29  cols_ = cols;
30  indices_.resize(rows_);
31  }
32 
34  void add(size_type i, size_type j) {
35  indices_[i].insert(j);
36  }
37 
39  size_type size() const {
40  size_type entries = 0;
41  for (size_type i=0; i<rows_; i++)
42  entries += indices_[i].size();
43 
44  return entries;
45  }
46 
48  size_type rows() const {return rows_;}
49 
50 
52  size_type rowsize(size_type row) const {return indices_[row].size();}
53 
60  template <class MatrixType>
61  void import(const MatrixType& m, size_type rowOffset=0, size_type colOffset=0) {
62 
63  typedef typename MatrixType::row_type RowType;
64  typedef typename RowType::ConstIterator ColumnIterator;
65 
66  for (size_type rowIdx=0; rowIdx<m.N(); rowIdx++) {
67 
68  const RowType& row = m[rowIdx];
69 
70  ColumnIterator cIt = row.begin();
71  ColumnIterator cEndIt = row.end();
72 
73  for(; cIt!=cEndIt; ++cIt)
74  add(rowIdx+rowOffset, cIt.index()+colOffset);
75 
76  }
77 
78  }
79 
85  template <class MatrixType>
86  void exportIdx(MatrixType& matrix) const {
87 
88  matrix.setSize(rows_, cols_);
89  matrix.setBuildMode(MatrixType::random);
90 
91  for (size_type i=0; i<rows_; i++)
92  matrix.setrowsize(i, indices_[i].size());
93 
94  matrix.endrowsizes();
95 
96  for (size_type i=0; i<rows_; i++) {
97 
98  typename std::set<size_type>::iterator it = indices_[i].begin();
99  for (; it!=indices_[i].end(); ++it)
100  matrix.addindex(i, *it);
101 
102  }
103 
104  matrix.endindices();
105 
106  }
107 
108 private:
109 
110  std::vector<std::set<size_type> > indices_;
111 
112  size_type rows_, cols_;
113 
114 };
115 
116 
117 } // end namespace Dune
118 
119 #endif