-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvutils.h
More file actions
326 lines (254 loc) · 10.9 KB
/
vutils.h
File metadata and controls
326 lines (254 loc) · 10.9 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#ifndef NUMBER_TYPE_H
#define NUMBER_TYPE_H
#include "vdefines.h"
#include <array>
#include <cstddef>
#include <exception>
#include <functional>
#include <type_traits>
template <typename... Types>
struct variant;
namespace details {
template <typename... Types>
struct vstorage;
}
class bad_variant_access : std::exception {
const char* message = "bad variant access";
public:
bad_variant_access() noexcept = default;
explicit bad_variant_access(const char* str) noexcept : message(str) {}
virtual const char* what() const noexcept override {
return message;
}
};
namespace details {
template <size_t Index, typename... Types>
struct get_type;
template <typename First, typename... Tail>
struct get_type<0, First, Tail...> {
using type = First;
};
template <size_t Index, typename First, typename... Tail>
struct get_type<Index, First, Tail...> {
using type = typename get_type<Index - 1, Tail...>::type;
};
template <size_t Index>
struct get_type<Index> {
using type = void;
};
template <size_t Index, typename... Types>
using get_type_t = typename get_type<Index, Types...>::type;
template <typename... Types>
struct pack_size : std::integral_constant<size_t, sizeof...(Types)> {};
} // namespace details
template <typename Variant>
struct variant_size;
template <typename... Types>
struct variant_size<const variant<Types...>> : variant_size<variant<Types...>> {};
template <typename... Types>
struct variant_size<volatile variant<Types...>> : variant_size<variant<Types...>> {};
template <typename... Types>
struct variant_size<const volatile variant<Types...>> : variant_size<variant<Types...>> {};
template <typename... Types>
struct variant_size<variant<Types...>> : details::pack_size<Types...> {};
template <typename... Types>
struct variant_size<details::vstorage<Types...>> : details::pack_size<Types...> {};
template <typename Variant>
inline constexpr size_t variant_size_v = variant_size<Variant>::value;
template <size_t Index, typename... Types>
struct variant_alternative;
template <size_t Index, typename... Types>
struct variant_alternative<Index, variant<Types...>> {
using type = details::get_type_t<Index, Types...>;
};
template <size_t Index, typename... Types>
struct variant_alternative<Index, const variant<Types...>> {
using type = const details::get_type_t<Index, Types...>;
};
template <size_t Index, typename... Types>
struct variant_alternative<Index, volatile variant<Types...>> {
using type = volatile details::get_type_t<Index, Types...>;
};
template <size_t Index, typename... Types>
struct variant_alternative<Index, const volatile variant<Types...>> {
using type = const volatile details::get_type_t<Index, Types...>;
};
template <size_t Index, typename Variant>
using variant_alternative_t = typename variant_alternative<Index, Variant>::type;
namespace details {
template <size_t Index, typename Type>
struct position_type {
static constexpr size_t index = Index;
using type = Type;
};
template <typename T, size_t Index, typename... Vars>
struct best_construct_type;
template <typename T, size_t Index, typename Var>
struct best_construct_type<T, Index, Var> {
static position_type<Index, Var> choose(Var) requires is_valid_conversion<T, Var>;
};
template <typename T, size_t Index, typename Var, typename... Types>
struct best_construct_type<T, Index, Var, Types...> : best_construct_type<T, Index + 1, Types...> {
using best_construct_type<T, Index + 1, Types...>::choose;
static position_type<Index, Var> choose(Var) requires is_valid_conversion<T, Var>;
};
template <typename T, typename... Types>
using chosen_construct_type = decltype(best_construct_type<T, 0, Types...>::choose(std::declval<T>()));
template <typename T, typename... Types>
struct count_of;
template <typename T>
struct count_of<T> {
static constexpr size_t count = 0;
};
template <typename T, typename Last>
struct count_of<T, Last> {
static constexpr size_t count = (std::is_same_v<T, Last>) ? 1 : 0;
};
template <typename T, typename Head, typename... Types>
struct count_of<T, Head, Types...> {
static constexpr size_t count = ((std::is_same_v<T, Head>) ? 1 : 0) + count_of<T, Types...>::count;
};
template <typename T, typename... Types>
inline constexpr size_t count_of_v = count_of<T, Types...>::count;
template <typename Searching, size_t Index, typename... Types>
struct find_first;
template <typename Searching, size_t Index>
struct find_first<Searching, Index> {
static constexpr size_t index = -1;
};
template <typename Searching, size_t Index, typename Last>
struct find_first<Searching, Index, Last> {
static constexpr size_t index = (std::is_same_v<Searching, Last>) ? Index : -1;
};
template <typename Searching, size_t Index, typename Head, typename... Types>
struct find_first<Searching, Index, Head, Types...> {
static constexpr size_t index =
(std::is_same_v<Searching, Head>) ? Index : find_first<Searching, Index + 1, Types...>::index;
};
template <typename T, typename... Types>
inline constexpr size_t find_first_v = find_first<T, 0, Types...>::index;
} // namespace details
template <typename T, typename... Types>
constexpr bool holds_alternative(variant<Types...> const& v) noexcept requires(details::count_of_v<T, Types...> == 1) {
return details::find_first_v<T, Types...> == v.index();
}
template <size_t Index, typename... Types>
constexpr variant_alternative_t<Index, variant<Types...>>& get(variant<Types...>& v) {
if (Index == v.index()) {
return v.storage.get(in_place_index<Index>);
}
throw bad_variant_access{};
}
template <size_t Index, typename... Types>
constexpr const variant_alternative_t<Index, variant<Types...>>& get(variant<Types...> const& v) {
if (Index == v.index()) {
return v.storage.get(in_place_index<Index>);
}
throw bad_variant_access{};
}
template <size_t Index, typename... Types>
constexpr variant_alternative_t<Index, variant<Types...>>&& get(variant<Types...>&& v) {
return std::move(get<Index>(v));
}
template <size_t Index, typename... Types>
constexpr const variant_alternative_t<Index, variant<Types...>>&& get(variant<Types...> const&& v) {
return std::move(get<Index>(v));
}
template <typename T, typename... Types, size_t Index = details::find_first_v<T, Types...>>
constexpr T& get(variant<Types...>& v) {
if (holds_alternative<T>(v)) {
return v.storage.get(in_place_index<Index>);
}
throw bad_variant_access{};
}
template <typename T, typename... Types, size_t Index = details::find_first_v<T, Types...>>
constexpr const T& get(variant<Types...> const& v) {
if (holds_alternative<T>(v)) {
return v.storage.get(in_place_index<Index>);
}
throw bad_variant_access{};
}
template <typename T, typename... Types, size_t Index = details::find_first_v<T, Types...>>
constexpr variant_alternative_t<Index, variant<Types...>>&& get(variant<Types...>&& v) {
return std::move(get<Index>(v));
}
template <typename T, typename... Types, size_t Index = details::find_first_v<T, Types...>>
constexpr const variant_alternative_t<Index, variant<Types...>>&& get(variant<Types...> const&& v) {
return std::move(get<Index>(v));
}
template <size_t Index, typename... Types>
constexpr std::add_pointer_t<variant_alternative_t<Index, variant<Types...>>> get_if(variant<Types...>* v) noexcept {
return (v != nullptr && Index == v->index()) ? std::addressof(get<Index>(*v)) : nullptr;
}
template <size_t Index, typename... Types>
constexpr std::add_pointer_t<const variant_alternative_t<Index, variant<Types...>>>
get_if(const variant<Types...>* v) noexcept {
return (v != nullptr && Index == v->index()) ? std::addressof(get<Index>(*v)) : nullptr;
}
template <typename T, typename... Types, size_t Index = details::find_first_v<T, Types...>>
constexpr std::add_pointer_t<T> get_if(variant<Types...>* v) noexcept {
return get_if<Index>(v);
}
template <typename T, typename... Types, size_t Index = details::find_first_v<T, Types...>>
constexpr std::add_pointer_t<const T> get_if(const variant<Types...>* v) noexcept {
return get_if<Index>(v);
}
namespace details {
template <typename... Types>
constexpr std::array<std::common_type_t<Types...>, sizeof...(Types)> array_ctor(Types&&... args) noexcept {
return {std::forward<Types>(args)...};
}
template <typename T>
constexpr T& in_impl(T& m) noexcept {
return m;
}
template <typename T, typename... Vars>
constexpr auto& in_impl(T& m, size_t index, Vars... indexes) noexcept {
return in_impl(m[index], indexes...);
}
template <typename T, typename... Types, size_t... Vars>
constexpr auto create_func_matrix_base(std::index_sequence<Vars...>) noexcept {
return [](T func, Types... tp) { return std::invoke(std::forward<T>(func), get<Vars>(std::forward<Types>(tp))...); };
}
template <typename T, typename... Types, size_t... Vars1, size_t... Vars2, typename... Vars3>
constexpr auto create_func_matrix_base(std::index_sequence<Vars1...>, std::index_sequence<Vars2...>,
Vars3... tail) noexcept {
return array_ctor(create_func_matrix_base<T, Types...>(std::index_sequence<Vars1..., Vars2>(), tail...)...);
};
template <typename T, size_t... Vars>
constexpr auto create_func_matrix_base_in(std::index_sequence<Vars...>) noexcept {
return [](T func) { return std::invoke(std::forward<T>(func), (std::integral_constant<size_t, Vars>())...); };
}
template <typename T, size_t... Vars1, size_t... Vars2, typename... Vars3>
constexpr auto create_func_matrix_base_in(std::index_sequence<Vars1...>, std::index_sequence<Vars2...>,
Vars3... tail) noexcept {
return array_ctor(create_func_matrix_base_in<T>(std::index_sequence<Vars1..., Vars2>(), tail...)...);
};
template <typename T, typename... Types>
constexpr auto create_matrix() noexcept {
return create_func_matrix_base<T, Types...>(
std::index_sequence<>(), std::make_index_sequence<variant_size_v<std::remove_reference_t<Types>>>()...);
}
template <typename T, typename... Types>
constexpr auto create_matrix_by_index() noexcept {
return create_func_matrix_base_in<T>(std::index_sequence<>(),
std::make_index_sequence<variant_size_v<std::remove_reference_t<Types>>>()...);
}
template <typename T, typename... Types>
inline constexpr auto matrix_by_index_v = create_matrix_by_index<T, Types...>();
template <typename T, typename... Types>
constexpr decltype(auto) visit_by_index(T&& visitor, Types&&... vars) {
return in_impl(matrix_by_index_v<T&&, Types&&...>, vars.index()...)(std::forward<T>(visitor));
}
template <typename T, typename... Types>
inline constexpr auto matrix_v = create_matrix<T, Types...>();
} // namespace details
template <typename T, typename... Types>
constexpr decltype(auto) visit(T&& visitor, Types&&... vars) {
if ((vars.valueless_by_exception() || ...)) {
throw bad_variant_access();
}
return in_impl(details::matrix_v<T&&, Types&&...>, vars.index()...)(std::forward<T>(visitor),
std::forward<Types>(vars)...);
}
#endif