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){
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 {
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_));
232 void add(size_type row, size_type col) {
233 indices_[row].insert(col);
238 size_type entries = 0;
239 for (size_type i=0; i<rows_; i++)
245 size_type
rows()
const {
return rows_;}
248 size_type
cols()
const {
return cols_;}
260 return indices_[row].storage();
265 return indices_[row].size();
274 template <
class MatrixType>
275 void import(
const MatrixType& m, size_type rowOffset=0, size_type colOffset=0) {
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);
305 for (size_type row=0; row<rows_; row++)
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_;
324 size_type rows_, cols_;
325 size_type maxVectorSize_;
Stores the nonzero entries for creating a sparse matrix.
Definition: matrixindexset.hh:181
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
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
auto overload(F &&... f)
Create an overload set.
Definition: overloadset.hh:61
Dune namespace.
Definition: alignedallocator.hh:13
constexpr std::integral_constant< std::size_t, sizeof...(II)> size(std::integer_sequence< T, II... >)
Return the size of the sequence.
Definition: integersequence.hh:75
constexpr std::bool_constant<((II==value)||...)> contains(std::integer_sequence< T, II... >, std::integral_constant< T, value >)
Checks whether or not a given sequence contains a value.
Definition: integersequence.hh:137