AMDiS  0.3
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 Functor, class... Args>
39  constexpr decltype(auto) applyVariadic(Functor&& f, Args&&... args)
40  {
41  return Impl_::applyImpl(FWD(f), std::forward_as_tuple(args...),
42  std::make_index_sequence<sizeof...(Args)>{});
43  }
44 
45  template <std::size_t N, class Functor>
46  constexpr decltype(auto) applyIndices(Functor&& f)
47  {
48  return Impl_::applyIndicesImpl(FWD(f), index_t<0>{},
49  std::make_index_sequence<N>{});
50  }
51 
52  template <std::size_t I0, std::size_t I1, class Functor>
53  constexpr decltype(auto) applyIndices(Functor&& f)
54  {
55  return Impl_::applyIndicesImpl(FWD(f), index_t<I0>{},
56  std::make_index_sequence<I1-I0>{});
57  }
58 
59  template <class Functor, std::size_t N>
60  constexpr decltype(auto) applyIndices(Functor&& f, index_t<N>)
61  {
62  return Impl_::applyIndicesImpl(FWD(f), index_t<0>{},
63  std::make_index_sequence<N>{});
64  }
65 
66  template <class Functor, std::size_t I0, std::size_t I1>
67  constexpr decltype(auto) applyIndices(Functor&& f, index_t<I0>, index_t<I1>)
68  {
69  return Impl_::applyIndicesImpl(FWD(f), index_t<I0>{},
70  std::make_index_sequence<I1-I0>{});
71  }
72 
73  } // end namespace Ranges
74 } // end namespace AMDiS
constexpr bool Functor
A Functor is a function F with signature Signature.
Definition: Concepts.hpp:134
Definition: FieldMatVec.hpp:12
Contains all classes needed for solving linear and non linear equation systems.
Definition: AdaptBase.hpp:6
std::integral_constant< std::size_t, I > index_t
A wrapper for std::size_t type.
Definition: Index.hpp:31