-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinvoke_intseq.h
More file actions
175 lines (151 loc) · 6.23 KB
/
invoke_intseq.h
File metadata and controls
175 lines (151 loc) · 6.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#ifndef INVOKE_INSEQ_H
#define INVOKE_INSEQ_H
#include <concepts>
#include <functional>
#include <type_traits>
#include <utility>
#include <vector>
namespace Invoke {
template <class T> struct is_integer_sequence : std::false_type {};
template <class T, T... Ints>
struct is_integer_sequence<std::integer_sequence<T, Ints...>> : std::true_type {
};
template <class T>
constexpr bool is_integer_sequence_v = is_integer_sequence<T>::value;
// Szablon który zwraca ten sam typ, jeśli nie jest std::integer_sequence,
// w przeciwnym przypadku std::integral_constant<T, 0>
template <class T> struct Cast_integer_sequence {
using type = T;
};
template <class T, T... Ints>
struct Cast_integer_sequence<std::integer_sequence<T, Ints...>> {
using type = std::integral_constant<T, 0>;
};
// Szablon, który liczy iloczyn rozmiarów std::integer_sequence występujących w
// T...
template <class... T> struct Count_result_size {
static constexpr size_t value = 1;
};
template <class U, class... T> struct Count_result_size<U, T...> {
static constexpr size_t value = Count_result_size<T...>::value;
};
template <std::integral U, U... Ints, class... T>
struct Count_result_size<std::integer_sequence<U, Ints...>, T...> {
static constexpr size_t value =
Count_result_size<T...>::value * sizeof...(Ints);
};
// Szablon, który przyjmuje N-tą wartości z Ints.
template <std::size_t N, std::integral T, T... Ints> struct GetNth {
using value_type = std::integral_constant<T, 0>;
};
template <std::size_t N, std::integral T, T Int, T... Ints>
requires(N > 0)
struct GetNth<N, T, Int, Ints...> {
using value_type = typename GetNth<N - 1, T, Ints...>::value_type;
};
template <std::size_t N, std::integral T, T Int, T... Ints>
requires(N == 0)
struct GetNth<N, T, Int, Ints...> {
using value_type = typename std::integral_constant<T, Int>;
};
// Specjalizacje głównego szablonu rozwiązującego zadanie.
// Oblicza wynik, który powinien znaleźć się na <Target> pozycji w wyniku.
// Przesuwa argumenty cyklicznie <ilość argumentów> razy, postępując odpowienio
// w zależności od przypadku.
// Wersja używana w najgłębszym wywołaniu rekurencyjnym, wywołuje std::invoke
template <std::size_t Offset, std::size_t Target, class F, class... T>
struct Invoke_intseq {
static constexpr decltype(auto) _invoke_intseq(F &&f, T &&...args) {
static_assert(Offset == sizeof...(args));
if constexpr (std::is_void_v<std::invoke_result_t<
F, typename Cast_integer_sequence<T>::type...>>) {
std::invoke(std::forward<F>(f), std::forward<T>(args)...);
} else {
return std::invoke(std::forward<F>(f), std::forward<T>(args)...);
}
}
};
// Pierwszy argument nie jest typu std::integer_sequence, przesuwamy cyklicznie
// argumenty
template <std::size_t Offset, std::size_t Target, class F, class V, class... T>
requires(!is_integer_sequence_v<V> && Offset != sizeof...(T) + 1)
struct Invoke_intseq<Offset, Target, F, V, T...> {
static constexpr decltype(auto) _invoke_intseq(F &&f, V &&arg, T &&...args) {
return Invoke_intseq<Offset + 1, Target, F, T..., V>::_invoke_intseq(
std::forward<F>(f), std::forward<T>(args)..., std::forward<V>(arg));
}
};
// Pierwszy argument jest typu std::integer_sequence, wybieram wartość stojącą
// na odpowieniej pozycji, następnie przesuwam argumenty cyklicznie podmieniając
// std::integer_sequence na std::integtal constant.
template <std::size_t Offset, std::size_t Target, class F, std::integral V,
V... Ints, class... T>
struct Invoke_intseq<Offset, Target, F, std::integer_sequence<V, Ints...>,
T...> {
static constexpr decltype(auto)
_invoke_intseq(F &&f, std::integer_sequence<V, Ints...>, T &&...args) {
constexpr size_t Int =
typename GetNth<Target / Count_result_size<T...>::value, V,
Ints...>::value_type();
return Invoke_intseq<Offset + 1, Target % Count_result_size<T...>::value, F,
T..., std::integral_constant<V, Int>>::
_invoke_intseq(std::forward<F>(f), std::forward<T>(args)...,
std::integral_constant<V, Int>());
}
};
template <class T> decltype(auto) constexpr wrap_if_needed(T &&t) {
if constexpr (std::is_reference_v<T>) {
return std::reference_wrapper<std::remove_reference_t<T>>(t);
} else {
return std::forward<T>(t);
}
}
template <class F, class... T> struct Invoker {
using result_type =
std::invoke_result_t<F, typename Cast_integer_sequence<T>::type...>;
static constexpr decltype(auto) invoke(F &&f, T &&...args) {
constexpr size_t result_size = Invoke::Count_result_size<T...>::value;
if constexpr (result_size == 0) {
if constexpr (std::is_void_v<result_type>)
return;
else
return std::array<result_type, 0>();
}
auto res = iteration_helper(std::forward<F>(f),
std::make_index_sequence<result_size>(),
std::forward<T>(args)...);
if constexpr (std::is_void_v<result_type>) {
return;
} else if constexpr (((!is_integer_sequence_v<T>)&&...)) {
return static_cast<result_type>(res[0]);
} else {
return res;
}
}
template <std::size_t... Is>
static constexpr decltype(auto)
iteration_helper(F &&f, std::index_sequence<Is...>, T &&...args) {
constexpr size_t result_size = Invoke::Count_result_size<T...>::value;
using wrap_reference = std::conditional_t<
std::is_reference_v<result_type>,
std::reference_wrapper<std::remove_reference_t<result_type>>,
result_type>;
if constexpr (std::is_void_v<result_type>) {
(Invoke_intseq<0, Is, F, T...>::_invoke_intseq(std::forward<F>(f),
std::forward<T>(args)...),
...);
return 0;
} else {
return std::array<wrap_reference, result_size>(
{wrap_if_needed(Invoke_intseq<0, Is, F, T...>::_invoke_intseq(
std::forward<F>(f), std::forward<T>(args)...))...});
}
}
};
} // namespace Invoke
template <class F, class... T>
constexpr decltype(auto) invoke_intseq(F &&f, T &&...args) {
return Invoke::Invoker<F, T...>::invoke(std::forward<F>(f),
std::forward<T>(args)...);
}
#endif