AMDiS  2.10
The Adaptive Multi-Dimensional Simulation Toolbox
Apply.hpp
1 #pragma once
2 
3 #include <tuple>
4 #include <type_traits>
5 #include <utility>
6 
7 #include <amdis/common/Index.hpp>
8 #include <amdis/common/StaticSize.hpp>
9 #include <amdis/common/TypeTraits.hpp>
10 
11 namespace AMDiS
12 {
13  namespace Ranges
14  {
15  namespace Impl_
16  {
17  template <class Functor, class Tuple, std::size_t... I>
18  constexpr decltype(auto) applyImpl(Functor&& f, Tuple&& t, std::index_sequence<I...>)
19  {
20  using std::get;
21  return f(get<I>(FWD(t))...);
22  }
23 
24  template <class Functor, std::size_t I0, std::size_t... I>
25  constexpr decltype(auto) applyIndicesImpl(Functor&& f, index_t<I0>, std::index_sequence<I...>)
26  {
27  return f(index_t<I0+I>{}...);
28  }
29  } // namespace Impl_
30 
31  template <class F, class Tuple>
32  constexpr decltype(auto) apply(F&& f, Tuple&& t)
33  {
34  return Impl_::applyImpl(FWD(f), FWD(t),
35  std::make_index_sequence<static_size_v<Tuple>>{});
36  }
37 
38  template <class F, std::size_t... I>
39  constexpr decltype(auto) apply(F&& f, std::index_sequence<I...> seq)
40  {
41  return Impl_::applyIndicesImpl(FWD(f), index_t<0>{}, seq);
42  }
43 
44  template <class Functor, class... Args>
45  constexpr decltype(auto) applyVariadic(Functor&& f, Args&&... args)
46  {
47  return Impl_::applyImpl(FWD(f), std::forward_as_tuple(args...),
48  std::make_index_sequence<sizeof...(Args)>{});
49  }
50 
51  template <std::size_t N, class Functor>
52  constexpr decltype(auto) applyIndices(Functor&& f)
53  {
54  return Impl_::applyIndicesImpl(FWD(f), index_t<0>{},
55  std::make_index_sequence<N>{});
56  }
57 
58  template <std::size_t I0, std::size_t I1, class Functor>
59  constexpr decltype(auto) applyIndices(Functor&& f)
60  {
61  return Impl_::applyIndicesImpl(FWD(f), index_t<I0>{},
62  std::make_index_sequence<I1-I0>{});
63  }
64 
65  template <class Functor, std::size_t N>
66  constexpr decltype(auto) applyIndices(Functor&& f, index_t<N>)
67  {
68  return Impl_::applyIndicesImpl(FWD(f), index_t<0>{},
69  std::make_index_sequence<N>{});
70  }
71 
72  template <class Functor, std::size_t I0, std::size_t I1>
73  constexpr decltype(auto) applyIndices(Functor&& f, index_t<I0>, index_t<I1>)
74  {
75  return Impl_::applyIndicesImpl(FWD(f), index_t<I0>{},
76  std::make_index_sequence<I1-I0>{});
77  }
78 
79  } // end namespace Ranges
80 } // end namespace AMDiS
constexpr bool Functor
A Functor is a function F with signature Signature.
Definition: Concepts.hpp:133
Definition: FieldMatVec.hpp:12
Definition: AdaptBase.hpp:6