AMDiS  0.3
The Adaptive Multi-Dimensional Simulation Toolbox
RecursiveApply.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 {
12  namespace Recursive
13  {
15  template <class T>
16  struct Apply
17  {
18  template <class F>
19  static auto impl(F&& f, T const& t)
20  {
21  return f(t);
22  }
23  };
24 
27 
34  template <class F, class T>
35  auto apply(F&& f, T const& t)
36  {
37  return Apply<T>::impl(f,t);
38  }
39 
40 
41  // specializations for container types
42 
43  template <class T, std::size_t n>
44  struct Apply<std::array<T,n>>
45  {
46  template <class F>
47  static auto impl(F&& f, std::array<T,n> const& a)
48  {
49  return Ranges::applyIndices<n>([&](auto... ii) {
50  return std::array{Recursive::apply(f,a[ii])...}; });
51  }
52  };
53 
54  template <class... TT>
55  struct Apply<std::tuple<TT...>>
56  {
57  template <class F>
58  static auto impl(F&& f, std::tuple<TT...> const& t)
59  {
60  return Ranges::apply([&](auto const&... ti) {
61  return std::tuple{Recursive::apply(f,ti)...}; }, t);
62  }
63  };
64 
65  template <class T1, class T2>
66  struct Apply<std::pair<T1,T2>>
67  {
68  template <class F>
69  static auto impl(F&& f, std::pair<T1,T2> const& t)
70  {
71  return std::pair{Recursive::apply(f,t.first),
72  Recursive::apply(f,t.second)};
73  }
74  };
75 
76  template <class... TT>
77  struct Apply<Dune::TupleVector<TT...>>
78  {
79  template <class F>
80  static auto impl(F&& f, Dune::TupleVector<TT...> const& t)
81  {
82  return Ranges::apply([&](auto const&... ti) {
83  return Dune::makeTupleVector(Recursive::apply(f,ti)...); }, t);
84  }
85  };
86 
87  template <class T>
88  struct Apply<std::vector<T>>
89  {
90  template <class F>
91  static auto impl(F&& f, std::vector<T> const& v)
92  {
93  using U = TYPEOF(Recursive::apply(f,std::declval<T>()));
94  std::vector<U> out;
95  out.reserve(v.size());
96  for (std::size_t i = 0; i < v.size(); ++i)
97  out.emplace_back(Recursive::apply(f,v[i]));
98  return out;
99  }
100  };
101 
102  } // end namespace Recursive
103 } // end namespace AMDiS
Definition: FieldMatVec.hpp:12
Contains all classes needed for solving linear and non linear equation systems.
Definition: AdaptBase.hpp:6
Default implementation of the recursive map function.
Definition: RecursiveApply.hpp:16