Skip to content

Draft: Add a replacement for std::vector

Simon Praetorius requested to merge feature/vector into master

(this is just an experiment and not intended to be merged)

Summary

This MR provides a container Dune::Vector with a similar interface as std::vector to be used as internal storage data structure in other dune vector and matrix types.

Rationale

The container std::vector is most of the time the best choice for storing contiguous data. Unfortunately, it has one design flaw that must be preserved for backwards compatibility reasons: The std::vector<bool> specialization. Instead of storing regular bools in the vector, these values are compressed resulting in the need for special proxy reference and pointer types. This change in interface is the reason why in some places in Dune we need to use a std::vector<char> to represent a bool vector. This is not very intuitive. Also, using the regular bool type even in containers using std::vector under the hood, e.g., BlockVector or VariableBlockVector from dune-istl, results in difficult to understand error messages.

Code that does not work

We cannot instantiate a BlockVector and VariableBlockVector (from dune-istl) with bool as element type:

Dune::BlockVector<bool> mask_vector(10); 
  // ERROR in dune/istl/bvector.hh:559:30: use of deleted function 
  // ‘void std::vector<bool, _Alloc>::data() [with _Alloc = std::allocator<bool>]’
  // this->p = storage_.data();

I tried to think about solutions to this issue. The solution I came up with are: writing out own std::vector-like class, as simple as possible, with an interface as close as possible to std::vector, to be used in some container data-structures, e.g. DynamicVector, mdarray, BlockVector...

In this (experimental) MR, I have provided such a vector implementation: Dune::Vector. The difference to a std::vector is the missing push_back and insert functions and the capacity is the same as the size.

In a second implementation in vector2.hh, I have changed this to inherit the implementation from std::Vector for types unequal to bool. An only for bool I do a specialization that provides the regular std::vector interface.

Discussion

Do you have another, maybe simpler, idea how to circumvent the problem with std::vector<bool>?

Related branches

In dune-istl I have created connected branches with the same name feature/vector to test the BlockVector and VariableBlockVector containers with this new Dune::Vector.

Edited by Simon Praetorius

Merge request reports

Loading