GamR  0.0.0
GammaROOT
Loading...
Searching...
No Matches
Tuples.hh
Go to the documentation of this file.
1#ifndef GAMROOT_UTILS_TUPLES_HH
2#define GAMROOT_UTILS_TUPLES_HH
3
4#pragma GCC diagnostic push
5#pragma GCC diagnostic ignored "-Wunused-variable"
6// auto l causes some complaints but has a purpose for enforcing
7// iteration order
8
9/* STD */
10#include <tuple>
11
12namespace GamR {
13 namespace Utils {
14 // https://ideone.com/j6dNE5
15 // https://stackoverflow.com/questions/16387354/template-tuple-calling-a-function-on-each-element
16 // Basically a missing feature in the language until C++17
17 // Though be warned, in C++17 std::apply might have randomised order of
18 // iteration This should be first element to last as expected.
19#ifndef DOXYGEN_SHOULD_SKIP_THIS
20
21 template <int... Is>
22 struct seq {
23 };
24
25 template <int N, int... Is>
26 struct gen_seq : gen_seq<N - 1, N - 1, Is...> {
27 };
28
29 template <int... Is>
30 struct gen_seq<0, Is...> : seq<Is...> {
31 };
32
33 template <typename T, typename F, int... Is>
34 void for_each(T &&t, F f, seq<Is...>)
35 {
36 auto l = {(f(std::get<Is>(t)), 0)...};
37 }
38
39 // Logical extension of above but for two tuples
40 template <typename T1, typename T2, typename F, int... Is>
41 void for_both(T1 &&t1, T2 &&t2, F f, seq<Is...>)
42 {
43 auto l = {(f(std::get<Is>(t1), std::get<Is>(t2)), 0)...};
44
45 }
46 template <typename T1, typename T2, typename F, int... Is, typename N>
47 void for_both(T1 &&t1, T2 &&t2, F f, seq<Is...>, N n)
48 {
49 auto l = {(f(std::get<Is>(t1), std::get<Is>(t2), n), 0)...};
50 }
51
52
53#endif
54
66 template <typename... Ts, typename F>
67 void for_each_in_tuple(std::tuple<Ts...> &t, F f)
68 {
69 for_each(t, f, gen_seq<sizeof...(Ts)>());
70 }
71
82 template <typename T1, typename T2, typename F>
83 void for_both_in_tuple(T1 &t1, T2 &t2, F f)
84 {
85 for_both(t1, t2, f, gen_seq<std::tuple_size<T1>::value>());
86 }
87
88 template <typename T1, typename T2, typename F, typename N>
89 void for_both_in_tuple(T1 &t1, T2 &t2, F f, N n)
90 {
91 for_both(t1, t2, f, gen_seq<std::tuple_size<T1>::value>(), n);
92 }
93
94 } // namespace Utils
95} // namespace GamR
96
97#pragma GCC diagnostic pop
98
99#endif
void for_each_in_tuple(std::tuple< Ts... > &t, F f)
Applies function on each element of tuple.
Definition Tuples.hh:67
void for_both_in_tuple(T1 &t1, T2 &t2, F f)
Applies function taking both elements of tuple as arguments.
Definition Tuples.hh:83
Definition Gain.cc:19