Dune Core Modules (2.9.0)

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