5#ifndef DUNE_ISTL_MATRIXINDEXSET_HH
6#define DUNE_ISTL_MATRIXINDEXSET_HH
15#include <dune/common/overloadset.hh>
38 using Index = std::uint_least32_t;
42 class FlatSet :
public std::vector<Index>
44 using Base = std::vector<Index>;
49 void insert(
const Index& value) {
50 auto it = std::lower_bound(begin(), end(), value);
51 if ((it == end() or (*it != value)))
52 Base::insert(it, value);
54 bool contains(
const Index& value)
const {
55 return std::binary_search(begin(), end(), value);
59 using RowIndexSet = std::variant<FlatSet, std::set<Index>>;
95 indices_.resize(rows_, FlatSet());
102 indices_.resize(rows_, FlatSet());
113 return std::visit(Dune::overload(
115 [&](std::set<size_type>& set) {
121 [&](FlatSet& sortedVector) {
122 if (sortedVector.size() < maxVectorSize_)
123 sortedVector.insert(
col);
124 else if (not sortedVector.contains(
col))
126 std::set<size_type> set(sortedVector.begin(), sortedVector.end());
128 indices_[row] = std::move(set);
158 return indices_[row];
163 return std::visit([&](
const auto& rowIndices) {
164 return rowIndices.size();
174 template <
class MatrixType>
177 typedef typename MatrixType::row_type RowType;
178 typedef typename RowType::ConstIterator ColumnIterator;
180 for (
size_type rowIdx=0; rowIdx<m.N(); rowIdx++) {
182 const RowType& row = m[rowIdx];
184 ColumnIterator cIt = row.begin();
185 ColumnIterator cEndIt = row.end();
187 for(; cIt!=cEndIt; ++cIt)
188 add(rowIdx+rowOffset, cIt.index()+colOffset);
199 template <
class MatrixType>
202 matrix.setSize(rows_, cols_);
203 matrix.setBuildMode(MatrixType::random);
206 matrix.setrowsize(row,
rowsize(row));
208 matrix.endrowsizes();
210 for (
size_type row=0; row<rows_; row++) {
211 std::visit([&](
const auto& rowIndices) {
212 matrix.setIndicesNoSort(row, rowIndices.begin(), rowIndices.end());
222 std::vector<RowIndexSet> indices_;
Col col
Definition matrixmatrix.hh:351
Definition allocator.hh:11
Stores the nonzero entries for creating a sparse matrix.
Definition matrixindexset.hh:37
void resize(size_type rows, size_type cols)
Reset the size of an index set.
Definition matrixindexset.hh:99
static constexpr size_type defaultMaxVectorSize
Default value for maxVectorSize.
Definition matrixindexset.hh:76
Index size_type
Definition matrixindexset.hh:63
size_type rows() const
Return the number of rows.
Definition matrixindexset.hh:143
void exportIdx(MatrixType &matrix) const
Initializes a BCRSMatrix with the indices contained in this MatrixIndexSet.
Definition matrixindexset.hh:200
void add(size_type row, size_type col)
Add an index to the index set.
Definition matrixindexset.hh:112
MatrixIndexSet(size_type rows, size_type cols, size_type maxVectorSize=defaultMaxVectorSize)
Constructor setting the matrix size.
Definition matrixindexset.hh:93
MatrixIndexSet(size_type maxVectorSize=defaultMaxVectorSize) noexcept
Constructor with custom maxVectorSize.
Definition matrixindexset.hh:83
const auto & columnIndices(size_type row) const
Return column indices of entries in given row.
Definition matrixindexset.hh:157
size_type rowsize(size_type row) const
Return the number of entries in a given row.
Definition matrixindexset.hh:162
size_type size() const
Return the number of entries.
Definition matrixindexset.hh:135
size_type cols() const
Return the number of columns.
Definition matrixindexset.hh:146