AMDiS  2.10
The Adaptive Multi-Dimensional Simulation Toolbox
Map.hpp
1 #pragma once
2 
3 #include <array>
4 #include <tuple>
5 #include <vector>
6 
7 #include <dune/common/tuplevector.hh>
8 #include <amdis/common/Apply.hpp>
9 
10 namespace AMDiS {
11 namespace Recursive {
12 
13 template <class T>
14 struct Map;
15 
18 
25 template <class F, class T>
26 auto map(F&& f, T const& t)
27 {
28  return Map<T>::impl(f,t);
29 }
30 
31 
32 // specializations for container types
33 
35 template <class T>
36 struct Map
37 {
38  template <class F>
39  static auto impl(F&& f, T const& t)
40  {
41  return f(t);
42  }
43 };
44 
45 template <class T, std::size_t n>
46 struct Map<std::array<T,n>>
47 {
48  template <class F>
49  static auto impl(F&& f, std::array<T,n> const& a)
50  {
51  return Ranges::applyIndices<n>([&](auto... ii) {
52  return std::array{Recursive::map(f,a[ii])...}; });
53  }
54 };
55 
56 template <class... TT>
57 struct Map<std::tuple<TT...>>
58 {
59  template <class F>
60  static auto impl(F&& f, std::tuple<TT...> const& t)
61  {
62  return Ranges::apply([&](auto const&... ti) {
63  return std::tuple{Recursive::map(f,ti)...}; }, t);
64  }
65 };
66 
67 template <class T1, class T2>
68 struct Map<std::pair<T1,T2>>
69 {
70  template <class F>
71  static auto impl(F&& f, std::pair<T1,T2> const& t)
72  {
73  return std::pair{Recursive::map(f,t.first), Recursive::map(f,t.second)};
74  }
75 };
76 
77 template <class... TT>
78 struct Map<Dune::TupleVector<TT...>>
79 {
80  template <class F>
81  static auto impl(F&& f, Dune::TupleVector<TT...> const& t)
82  {
83  return Ranges::apply([&](auto const&... ti) {
84  return Dune::makeTupleVector(Recursive::map(f,ti)...); }, t);
85  }
86 };
87 
88 template <class T>
89 struct Map<std::vector<T>>
90 {
91  template <class F>
92  static auto impl(F&& f, std::vector<T> const& v)
93  {
94  using U = TYPEOF(Recursive::map(f,std::declval<T>()));
95  std::vector<U> out;
96  out.reserve(v.size());
97  for (std::size_t i = 0; i < v.size(); ++i)
98  out.emplace_back(Recursive::map(f,v[i]));
99  return out;
100  }
101 };
102 
103 }} // end namespace AMDiS::Recursive
Definition: FieldMatVec.hpp:12
Definition: AdaptBase.hpp:6
Default implementation of the recursive map function.
Definition: Map.hpp:14