AMDiS 2.10
The Adaptive Multi-Dimensional Simulation Toolbox
ForEach.hpp
1#pragma once
2
3#include <initializer_list>
4#include <tuple>
5#include <type_traits>
6
7#include <amdis/common/Index.hpp>
8
9namespace AMDiS
10{
11 namespace Ranges
12 {
13 template <class Functor, class... Args>
14 constexpr void forVariadic(Functor&& f, Args&&... args)
15 {
16 (f(FWD(args)),...);
17 }
18
19 template <std::size_t... I, class Tuple, class Functor>
20 constexpr void forEach(std::index_sequence<I...>, Tuple&& tuple, Functor&& f)
21 {
22 using std::get;
23 (f(get<I>(tuple)),...);
24 }
25
26 template <class... T, class Functor>
27 constexpr void forEach(std::tuple<T...> const& tuple, Functor&& f)
28 {
29 Ranges::forEach(std::make_index_sequence<sizeof...(T)>{}, tuple, FWD(f));
30 }
31
32 template <class... T, class Functor>
33 constexpr void forEach(std::tuple<T...>& tuple, Functor&& f)
34 {
35 Ranges::forEach(std::make_index_sequence<sizeof...(T)>{}, tuple, FWD(f));
36 }
37
38 template <class Range, class Functor>
39 constexpr auto forEach(Range&& r, Functor&& f)
40 -> std::void_t<decltype((std::begin(r),std::end(r)))>
41 {
42 for (auto& ri : r)
43 f(ri);
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:133