AMDiS  0.3
The Adaptive Multi-Dimensional Simulation Toolbox
VectorBase.hpp
1 #pragma once
2 
3 #include <iterator>
4 
5 #include <dune/common/rangeutilities.hh>
6 #include <dune/functions/functionspacebases/flatmultiindex.hh>
7 
8 #include <amdis/common/FakeContainer.hpp>
9 #include <amdis/utility/MappedRangeView.hpp>
10 
11 namespace AMDiS
12 {
14  template <class Derived>
15  class VectorBase
16  {
17  private:
18  Derived const& asDerived() const
19  {
20  return static_cast<Derived const&>(*this);
21  }
22 
23  Derived& asDerived()
24  {
25  return static_cast<Derived&>(*this);
26  }
27 
28  protected:
31  VectorBase() = default;
32 
33  public:
34  void finish() { /* do nothing */ }
35  void synchronize() { /* do nothing */ }
36 
38  template <class MultiIndex, class ValueType, class Assign>
39  void insert(MultiIndex const& idx, ValueType const& value, Assign assign)
40  {
41  assign(value, asDerived().at(idx));
42  }
43 
45  template <class IndexRange, class OutputIterator>
46  void gather(IndexRange const& localInd, OutputIterator buffer) const
47  {
48  for (auto const& idx : localInd)
49  *buffer++ = asDerived().at(idx);
50  }
51 
52 
54  template <class IndexRange, class LocalVec, class Assign>
55  void scatter(IndexRange const& localInd, LocalVec const& vec, FakeContainer<bool,true>, Assign assign)
56  {
57  auto vec_it = std::begin(vec);
58  for (auto const& idx : localInd)
59  assign(*vec_it++, asDerived().at(idx));
60  }
61 
64  template <class IndexRange, class LocalVec, class MaskRange, class Assign>
65  void scatter(IndexRange const& localInd, LocalVec const& vec, MaskRange const& mask, Assign assign)
66  {
67  auto vec_it = std::begin(vec);
68  auto mask_it = std::begin(mask);
69  auto ind_it = std::begin(localInd);
70  for (; vec_it != std::end(vec); ++vec_it, ++mask_it, ++ind_it) {
71  if (*mask_it)
72  assign(*vec_it, asDerived().at(*ind_it));
73  }
74  }
75 
76 
78  template <class IndexRange, class Func>
79  void forEach(IndexRange const& localInd, Func&& f) const
80  {
81  for (auto const& idx : localInd)
82  f(idx, asDerived().at(idx));
83  }
84 
86  template <class IndexRange, class Func>
87  void forEach(IndexRange const& localInd, Func&& f)
88  {
89  for (auto const& idx : localInd)
90  f(idx, asDerived().at(idx));
91  }
92 
93 
95  template <class Func>
96  void forEach(Func&& f)
97  {
98  forEach(mappedRangeView(Dune::range(asDerived().size()),
99  [](auto i) { return Dune::Functions::FlatMultiIndex<decltype(i)>{i}; }), FWD(f));
100  }
101 
103  template <class Func>
104  void forEach(Func&& f) const
105  {
106  forEach(mappedRangeView(Dune::range(asDerived().size()),
107  [](auto i) { return Dune::Functions::FlatMultiIndex<decltype(i)>{i}; }), FWD(f));
108  }
109  };
110 
111 } // end namespace AMDiS
void forEach(IndexRange const &localInd, Func &&f)
Apply the functor f to all entries of the list of multi-indices localInd.
Definition: VectorBase.hpp:87
constexpr bool MultiIndex
A multi-index type.
Definition: Concepts.hpp:150
void forEach(Func &&f)
Apply the functor f to all entries of the container.
Definition: VectorBase.hpp:96
Contains all classes needed for solving linear and non linear equation systems.
Definition: AdaptBase.hpp:6
void scatter(IndexRange const &localInd, LocalVec const &vec, MaskRange const &mask, Assign assign)
Definition: VectorBase.hpp:65
void gather(IndexRange const &localInd, OutputIterator buffer) const
Gather values from the vector into the output range buffer decribed as an output iterator.
Definition: VectorBase.hpp:46
auto mappedRangeView(R &&range, F const &f)
Create a MappedRangeView.
Definition: MappedRangeView.hpp:445
VectorBase()=default
void forEach(IndexRange const &localInd, Func &&f) const
Apply the functor f to all entries of the list of multi-indices localInd.
Definition: VectorBase.hpp:79
void insert(MultiIndex const &idx, ValueType const &value, Assign assign)
Insert or add value at position idx, using the passed assign functor.
Definition: VectorBase.hpp:39
A container-like data-structure not storing anything and with empty implementations in many container...
Definition: FakeContainer.hpp:34
CRTP base class for flat vector backends.
Definition: VectorBase.hpp:15
void scatter(IndexRange const &localInd, LocalVec const &vec, FakeContainer< bool, true >, Assign assign)
Scatter the values from the local vec into the container using the passed assign functor.
Definition: VectorBase.hpp:55
void forEach(Func &&f) const
Apply the functor f to all entries of the container.
Definition: VectorBase.hpp:104