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
10namespace AMDiS {
11namespace Recursive {
12
13template <class T>
14struct Map;
15
18
25template <class F, class T>
26auto map(F&& f, T const& t)
27{
28 return Map<T>::impl(f,t);
29}
30
31
32// specializations for container types
33
35template <class T>
36struct Map
37{
38 template <class F>
39 static auto impl(F&& f, T const& t)
40 {
41 return f(t);
42 }
43};
44
45template <class T, std::size_t n>
46struct 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
56template <class... TT>
57struct 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
67template <class T1, class T2>
68struct 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
77template <class... TT>
78struct 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
88template <class T>
89struct 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
Default implementation of the recursive map function.
Definition: Map.hpp:37