5#ifndef DUNE_ISTL_MATRIXINDEXSET_HH
6#define DUNE_ISTL_MATRIXINDEXSET_HH
15#include <dune/common/overloadset.hh>
41 using Index = std::uint_least32_t;
42 class Vector :
public std::vector<Index> {
44 size_type maxVectorSize_;
45 friend class RowIndexSet;
49 static Index getMaxVectorSize(
const RowIndexSet::Vector& v) {
50 return v.maxVectorSize_;
54 using size_type = Index;
67 static constexpr size_type defaultMaxVectorSize = 2048;
75 RowIndexSet(size_type maxVectorSize = defaultMaxVectorSize) : storage_{Vector()}
77 std::get<Vector>(storage_).maxVectorSize_ = maxVectorSize;
88 void insert(size_type
col){
89 std::visit(Dune::overload(
91 [&](std::set<size_type>& set) {
97 [&](Vector& sortedVector) {
98 auto it = std::lower_bound(sortedVector.cbegin(), sortedVector.cend(),
col);
99 if (it == sortedVector.cend() or (*it !=
col)) {
100 if (sortedVector.size() < getMaxVectorSize(sortedVector)) {
101 sortedVector.insert(it,
col);
103 std::set<size_type> set(sortedVector.cbegin(), sortedVector.cend());
105 storage_ = std::move(set);
118 bool contains(
const Index&
col)
const {
119 return std::visit(Dune::overload(
120 [&](
const std::set<Index>& set) {
121 return set.contains(
col);
123 [&](
const std::vector<Index>& sortedVector) {
124 return std::binary_search(sortedVector.cbegin(), sortedVector.cend(),
col);
134 const auto& storage()
const {
143 size_type size()
const {
144 return std::visit([&](
const auto& rowIndices) {
145 return rowIndices.size();
153 std::visit([&](
auto& rowIndices) {
159 std::variant<Vector, std::set<Index>> storage_;
183 using size_type =
typename Impl::RowIndexSet::size_type;
215 indices_.resize(rows_, Impl::RowIndexSet(maxVectorSize_));
222 indices_.resize(rows_, Impl::RowIndexSet(maxVectorSize_));
233 indices_[row].insert(
col);
260 return indices_[row].storage();
265 return indices_[row].size();
274 template <
class MatrixType>
277 typedef typename MatrixType::row_type RowType;
278 typedef typename RowType::ConstIterator ColumnIterator;
280 for (
size_type rowIdx=0; rowIdx<m.N(); rowIdx++) {
282 const RowType& row = m[rowIdx];
284 ColumnIterator cIt = row.begin();
285 ColumnIterator cEndIt = row.end();
287 for(; cIt!=cEndIt; ++cIt)
288 add(rowIdx+rowOffset, cIt.index()+colOffset);
299 template <
class MatrixType>
302 matrix.setSize(rows_, cols_);
303 matrix.setBuildMode(MatrixType::random);
306 matrix.setrowsize(row,
rowsize(row));
308 matrix.endrowsizes();
310 for (
size_type row=0; row<rows_; row++) {
311 std::visit([&](
const auto& rowIndices) {
312 matrix.setIndicesNoSort(row, rowIndices.begin(), rowIndices.end());
313 }, indices_[row].storage());
322 std::vector<Impl::RowIndexSet> indices_;
Definition allocator.hh:11
MatrixSparsityPatternGatherScatter< M, I >::ColIter MatrixSparsityPatternGatherScatter< M, I >::col
Definition matrixredistribute.hh:591
void resize(size_type rows, size_type cols)
Reset the size of an index set.
Definition matrixindexset.hh:219
static constexpr size_type defaultMaxVectorSize
Default value for maxVectorSize.
Definition matrixindexset.hh:196
typename Impl::RowIndexSet::size_type size_type
Definition matrixindexset.hh:183
size_type rows() const
Return the number of rows.
Definition matrixindexset.hh:245
void exportIdx(MatrixType &matrix) const
Initializes a BCRSMatrix with the indices contained in this MatrixIndexSet.
Definition matrixindexset.hh:300
void add(size_type row, size_type col)
Add an index to the index set.
Definition matrixindexset.hh:232
MatrixIndexSet(size_type rows, size_type cols, size_type maxVectorSize=defaultMaxVectorSize)
Constructor setting the matrix size.
Definition matrixindexset.hh:213
MatrixIndexSet(size_type maxVectorSize=defaultMaxVectorSize) noexcept
Constructor with custom maxVectorSize.
Definition matrixindexset.hh:203
const auto & columnIndices(size_type row) const
Return column indices of entries in given row.
Definition matrixindexset.hh:259
size_type rowsize(size_type row) const
Return the number of entries in a given row.
Definition matrixindexset.hh:264
size_type size() const
Return the number of entries.
Definition matrixindexset.hh:237
size_type cols() const
Return the number of columns.
Definition matrixindexset.hh:248