Skip to content

Use sparseRange range generator from dune-common

Sander, Oliver requested to merge introduce-sparserange into master

Currently, it is not possible to use range-based for to loop over the entries of a BCRSMatrix object. That is because the iterator provided by the matrix row has a method 'index', which returns the current matrix column, and this method is not accessible from a range-based for loop. As a consequence, normal iterator loops have to to be used, which make the code clumsy.

Now C++17 has brought structured binding, which offers a very elegant way around the problem. There is a proposal in dune-common for a global method 'sparseRange', which turns an iterator range with a custom iterator. This iterator, when dereferenced, returns std::pair<reference,int>. Hence it is possible to write

for (auto [entry, i] : sparseRange(matrixRow))
  std::cout << "column " << j << " contains " << entry << std::endl;

in lieu of

auto it = matrixRow.begin()
auto endIt = matrixRow.end()
for (; it!=endIt; ++it)
  std::cout << "column " << it.index() << " contains " << *it << std::endl;

However, besides the use of the 'index' method there is nothing matrix-specific or dune-istl-specific here.

Edited by Sander, Oliver

Merge request reports