RecursiveApply.hpp 2.75 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#pragma once

#include <array>
#include <tuple>
#include <vector>

#include <dune/common/tuplevector.hh>
#include <amdis/common/Apply.hpp>

namespace AMDiS
{
  namespace Recursive
  {
    /// Default implementation of the recursive \ref map function.
    template <class T>
    struct Apply
    {
      template <class F>
      static auto impl(F&& f, T const& t)
      {
        return f(t);
      }
    };

    /// \brief Recursive application of a transformation functor `f` to a hierarchic
    /// container of containers, returning the transformed container.
    /**
     * This utility function applies the given functor `f` to the "leaf" entries in
     * a hierarchic container that returns a transformed container. Therefore, the
     * container is traversed recursively, using specializations of the `Map<Container>::apply`
     * class method. If no specialization is provided, the function is applied to the
     * whole container or leaf entry, respectively.
     **/
    template <class F, class T>
    auto apply(F&& f, T const& t)
    {
      return Apply<T>::impl(f,t);
    }


    // specializations for container types

    template <class T, std::size_t n>
    struct Apply<std::array<T,n>>
    {
      template <class F>
      static auto impl(F&& f, std::array<T,n> const& a)
      {
        return Ranges::applyIndices<n>([&](auto... ii) {
          return std::array{Recursive::apply(f,a[ii])...}; });
      }
    };

    template <class... TT>
    struct Apply<std::tuple<TT...>>
    {
      template <class F>
      static auto impl(F&& f, std::tuple<TT...> const& t)
      {
        return Ranges::apply([&](auto const&... ti) {
          return std::tuple{Recursive::apply(f,ti)...}; }, t);
      }
    };

    template <class T1, class T2>
    struct Apply<std::pair<T1,T2>>
    {
      template <class F>
      static auto impl(F&& f, std::pair<T1,T2> const& t)
      {
        return std::pair{Recursive::apply(f,t.first),
                         Recursive::apply(f,t.second)};
      }
    };

    template <class... TT>
    struct Apply<Dune::TupleVector<TT...>>
    {
      template <class F>
      static auto impl(F&& f, Dune::TupleVector<TT...> const& t)
      {
        return Ranges::apply([&](auto const&... ti) {
          return Dune::makeTupleVector(Recursive::apply(f,ti)...); }, t);
      }
    };

    template <class T>
    struct Apply<std::vector<T>>
    {
      template <class F>
      static auto impl(F&& f, std::vector<T> const& v)
      {
        using U = TYPEOF(Recursive::apply(f,std::declval<T>()));
        std::vector<U> out;
        out.reserve(v.size());
        for (std::size_t i = 0; i < v.size(); ++i)
          out.emplace_back(Recursive::apply(f,v[i]));
        return out;
      }
    };

  } // end namespace Recursive
} // end namespace AMDiS