Dune Core Modules (2.6.0)

matrixindexset.hh
1// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2// vi: set et ts=4 sw=2 sts=2:
3#ifndef DUNE_ISTL_MATRIXINDEXSET_HH
4#define DUNE_ISTL_MATRIXINDEXSET_HH
5
6#include <vector>
7#include <set>
8
9namespace Dune {
10
11
14 {
15
16 public:
17 typedef std::size_t size_type;
18
20 MatrixIndexSet() : rows_(0), cols_(0)
21 {}
22
24 MatrixIndexSet(size_type rows, size_type cols) : rows_(rows), cols_(cols) {
25 indices_.resize(rows_);
26 }
27
29 void resize(size_type rows, size_type cols) {
30 rows_ = rows;
31 cols_ = cols;
32 indices_.resize(rows_);
33 }
34
36 void add(size_type i, size_type j) {
37 indices_[i].insert(j);
38 }
39
41 size_type size() const {
42 size_type entries = 0;
43 for (size_type i=0; i<rows_; i++)
44 entries += indices_[i].size();
45
46 return entries;
47 }
48
50 size_type rows() const {return rows_;}
51
52
54 size_type rowsize(size_type row) const {return indices_[row].size();}
55
62 template <class MatrixType>
63 void import(const MatrixType& m, size_type rowOffset=0, size_type colOffset=0) {
64
65 typedef typename MatrixType::row_type RowType;
66 typedef typename RowType::ConstIterator ColumnIterator;
67
68 for (size_type rowIdx=0; rowIdx<m.N(); rowIdx++) {
69
70 const RowType& row = m[rowIdx];
71
72 ColumnIterator cIt = row.begin();
73 ColumnIterator cEndIt = row.end();
74
75 for(; cIt!=cEndIt; ++cIt)
76 add(rowIdx+rowOffset, cIt.index()+colOffset);
77
78 }
79
80 }
81
87 template <class MatrixType>
88 void exportIdx(MatrixType& matrix) const {
89
90 matrix.setSize(rows_, cols_);
91 matrix.setBuildMode(MatrixType::random);
92
93 for (size_type i=0; i<rows_; i++)
94 matrix.setrowsize(i, indices_[i].size());
95
96 matrix.endrowsizes();
97
98 for (size_type i=0; i<rows_; i++) {
99
100 typename std::set<size_type>::iterator it = indices_[i].begin();
101 for (; it!=indices_[i].end(); ++it)
102 matrix.addindex(i, *it);
103
104 }
105
106 matrix.endindices();
107
108 }
109
110 private:
111
112 std::vector<std::set<size_type> > indices_;
113
114 size_type rows_, cols_;
115
116 };
117
118
119} // end namespace Dune
120
121#endif
Stores the nonzero entries in a sparse matrix.
Definition: matrixindexset.hh:14
void resize(size_type rows, size_type cols)
Reset the size of an index set.
Definition: matrixindexset.hh:29
MatrixIndexSet()
Default constructor.
Definition: matrixindexset.hh:20
void add(size_type i, size_type j)
Add an index to the index set.
Definition: matrixindexset.hh:36
size_type rows() const
Return the number of rows.
Definition: matrixindexset.hh:50
void exportIdx(MatrixType &matrix) const
Initializes a BCRSMatrix with the indices contained in this MatrixIndexSet.
Definition: matrixindexset.hh:88
MatrixIndexSet(size_type rows, size_type cols)
Constructor setting the matrix size.
Definition: matrixindexset.hh:24
size_type rowsize(size_type row) const
Return the number of entries in a given row.
Definition: matrixindexset.hh:54
size_type size() const
Return the number of entries.
Definition: matrixindexset.hh:41
Dune namespace.
Definition: alignedallocator.hh:10
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Nov 24, 23:30, 2024)