AMDiS  0.3
The Adaptive Multi-Dimensional Simulation Toolbox
ForEach.hpp
1 #pragma once
2 
3 #include <initializer_list>
4 
5 #include <amdis/common/Index.hpp>
6 #include <amdis/common/Range.hpp>
7 
8 namespace AMDiS
9 {
10  namespace Ranges
11  {
12  template <class Functor, class... Args>
13  constexpr void forVariadic(Functor&& f, Args&&... args)
14  {
15  (f(FWD(args)),...);
16  }
17 
18  template <std::size_t... I, class Tuple, class Functor>
19  constexpr void forEach(std::index_sequence<I...>, Tuple&& tuple, Functor&& f)
20  {
21  using std::get;
22  (f(get<I>(tuple)),...);
23  }
24 
25  template <class Tuple, class Functor>
26  constexpr void forEach(Tuple&& tuple, Functor&& f)
27  {
28  Ranges::forEach(std::make_index_sequence<static_size_v<Tuple>>{}, FWD(tuple), FWD(f));
29  }
30 
31  template <class T, std::size_t n, class Functor>
32  constexpr void forEach(std::array<T,n> const& a, Functor&& f)
33  {
34  for (auto const& ai : a)
35  f(ai);
36  }
37 
38  template <class T, class Functor>
39  constexpr void forEach(std::vector<T> const& v, Functor&& f)
40  {
41  for (auto const& vi : v)
42  f(vi);
43  }
44 
45 
46  template <std::size_t I0 = 0, std::size_t... I, class Functor>
47  constexpr void forIndices(std::index_sequence<I...>, Functor&& f)
48  {
49  (f(index_t<I0+I>{}),...);
50  }
51 
52  template <std::size_t I0, std::size_t I1, class Functor>
53  constexpr void forIndices(index_t<I0>, index_t<I1>, Functor&& f)
54  {
55  Ranges::forIndices<I0>(std::make_index_sequence<std::size_t(I1-I0)>{}, FWD(f));
56  }
57 
58  template <std::size_t N, class Functor>
59  constexpr void forIndices(index_t<N>, Functor&& f)
60  {
61  Ranges::forIndices(std::make_index_sequence<N>{}, FWD(f));
62  }
63 
64  template <std::size_t I0, std::size_t I1, class Functor>
65  constexpr void forIndices(Functor&& f)
66  {
67  Ranges::forIndices<I0>(std::make_index_sequence<std::size_t(I1-I0)>{}, FWD(f));
68  }
69 
70  template <std::size_t N, class Functor>
71  constexpr void forIndices(Functor&& f)
72  {
73  Ranges::forIndices<0>(std::make_index_sequence<N>{}, FWD(f));
74  }
75 
76  } // end namespace Ranges
77 } // end namespace AMDiS
constexpr bool Functor
A Functor is a function F with signature Signature.
Definition: Concepts.hpp:134
Contains all classes needed for solving linear and non linear equation systems.
Definition: AdaptBase.hpp:6