-
Notifications
You must be signed in to change notification settings - Fork 0
/
tuple_utl.h
499 lines (407 loc) · 14.4 KB
/
tuple_utl.h
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
#ifndef TUPLE_UTL_H
#define TUPLE_UTL_H
#include <tuple>
#include <type_traits>
#include <optional>
#include <functional>
#include <algorithm>
using uint =unsigned int;
namespace tutl {
template<typename M,typename T>
T && repeater_fun(T &&t){
return std::forward<T>(t);
}
template<size_t M,typename T>
T && repeater_fun(T &&t){
return std::forward<T>(t);
}
template<std::size_t Begin,std::size_t End,typename T, size_t ...i>
inline constexpr std::tuple<std::tuple_element_t<Begin+i,std::remove_pointer_t<T>>...> * get_arguments(std::index_sequence<i...>){
return nullptr;
};
template<std::size_t Begin,std::size_t End,typename ...Arg>
inline constexpr auto Get_arguments(){
return get_arguments<Begin,End,std::tuple<Arg...>>(std::make_index_sequence<End-Begin>{});
}
template<std::size_t N,typename ...Arg>
inline constexpr auto Get_last_arguments(){
return Get_arguments<sizeof... (Arg)-N,sizeof... (Arg),Arg...>();
}
template<std::size_t N,typename ...Arg>
inline constexpr auto Get_first_arguments(){
return Get_arguments<0,N,Arg...>();
}
template<int N,char const t[]>
constexpr int to_zero(){
if constexpr (t[N]=='\0'){
return N;
}else {
return to_zero<N+1,t>();
}
}
template<char const t[],size_t ...i>
constexpr auto char_to_char(std::index_sequence<i...>){
constexpr char m[sizeof... (i)]={t[i]...};
return m;
}
template<char const t[]>
constexpr auto Char_to_char(){
return char_to_char(t,std::make_index_sequence<to_zero<0,t>()>());
}
template<size_t ...i>
constexpr auto char_to_array(const char t[],std::index_sequence<i...>){
return std::array<char,sizeof... (i)>{t[i]...};
}
template<char const t[]>
constexpr auto Char_to_array(){
return char_to_array<t>(std::make_index_sequence<to_zero<0,t>()>());
}
template<std::size_t N>
constexpr auto Char_to_array(const char t[]){
return char_to_array(t,std::make_index_sequence<N>());
}
template<typename T,size_t ...i>
constexpr auto to_array(T t,std::index_sequence<i...>){
return std::array<T,sizeof... (i)>{repeater_fun<i>(t)...};
}
template<typename T,std::size_t N>
constexpr auto To_array(T t){
return to_array<T>(t,std::make_index_sequence<N>());
}
template<std::size_t N>
static constexpr std::size_t day_on_month(std::size_t day){
std::size_t day_on_monthe[12]={31,28,31,30,31,30,31,31,30,31,30,31};
if constexpr(N==366){
day_on_monthe[1]=29;
}
std::size_t k=0;
for (std::size_t i=0;i<12;i++) {
k+=day_on_monthe[i];
if(day<k){
return i;
}
}
return day;
}
template<std::size_t N>
static constexpr std::size_t month_days(std::size_t month){
std::size_t day_on_monthe[12]={31,28,31,30,31,30,31,31,30,31,30,31};
if constexpr(N==366){
day_on_monthe[1]=29;
}
return day_on_monthe[month];
}
template<typename F,size_t ...i>
static constexpr auto to_months(F f,std::index_sequence<i...>){
return std::array<std::size_t,sizeof...(i)>{f(i)...};
}
template<std::size_t N,typename F>
static constexpr std::array<std::size_t,N> To_months(F f){
return to_months(f,std::make_index_sequence<N>());
}
inline static std::array<std::size_t,365> months=To_months<365>(day_on_month<365>);
inline static std::array<std::size_t,13> day_to_months={0,31,59,90,120,151,181,212,243,270,304,334,365};
inline static std::size_t to_month(std::size_t day){
return months[day];
}
inline static std::size_t to_day(std::size_t month){
return day_to_months[month];
}
//
template <std::size_t size,typename T,size_t... i>
constexpr inline int arrayDublicate(T S,std::array<T,size> t,std::index_sequence<i...>) {
int k=0;
(((t[i]==S)?k++:false),...);
return k;
}
template <std::size_t size,typename T>
constexpr inline int ArrayDublicate(T S,std::array<T,size> t) {
return arrayDublicate(S,t,std::make_index_sequence<size>());
}
template <typename T,size_t... i>
constexpr inline int ischeck_dublicate_to_array(T t,std::index_sequence<i...>){
int k=0;
((((ArrayDublicate(t[i],t))>1)?k++:false),...);
return k;
}
template <std::size_t size,typename T>
constexpr inline bool isCheck_Dublicate_to_Array(std::array<T,size> t) {
return ischeck_dublicate_to_array(t,std::make_index_sequence<size>());
}
template <auto S, typename T,size_t... i>
constexpr inline int arraySearch(T t,std::index_sequence<i...>) {
int k=-1;
(((t[i]==S)?k=i:false),...);
return k;
}
template <auto S,std::size_t size,typename T>
constexpr inline int ArraySearch(std::array<T,size> t) {
return arraySearch<S>(t,std::make_index_sequence<size>());
}
template <std::size_t size,typename T,size_t... i>
constexpr inline bool ischeck_dublicate_to_array(std::array<T,size> t,std::index_sequence<i...>) {
int k=0;
(((ArraySearch<t[i]>(t)==0)?true:k++),...);
return k;
}
template <std::size_t size,typename T,size_t... i>
constexpr inline bool isCheck_Dublicate_to_Array(std::array<T,size> t) {
return ischeck_dublicate_to_array(t,std::make_index_sequence<size>());
}
template <typename T,typename F, size_t... i>
inline auto arrayTransform(T&& t,F &&f, std::index_sequence<i...>) {
return std::array<decltype (f(std::forward<T>(t)[0])), sizeof... (i)>{f(std::forward<T>(t)[i])...};
}
template <typename T,std::size_t size,typename F>
inline auto ArrayTransform(std::array<T, size> && t,F &&f) {
return arrayTransform(std::forward<std::array<T, size>>(t),std::forward<F>(f),std::make_index_sequence<size>());
}
template <typename T,typename F, size_t... i>
inline auto arrayArg(T&& t,F &&f, std::index_sequence<i...>) {
return f(std::forward<T>(t)[i]...);
}
template <typename T,std::size_t size,typename F>
inline auto ArrayArg(std::array<T, size> && t,F &&f) {
return arrayArg(std::forward<std::array<T, size>>(t),std::forward<F>(f),std::make_index_sequence<size>());
}
template <typename T,typename F, size_t... i>
constexpr inline void tupleForeach(T&& t,F &&f, std::index_sequence<i...>) {
((f(std::get<i>(std::forward<T>(t)))),...);
}
template <typename T,typename F>
constexpr inline void TupleForeach(T&& t,F &&f) {
tupleForeach(std::forward<T>(t),std::forward<F>(f),std::make_index_sequence<std::tuple_size<std::remove_reference_t<T>>::value>());
}
template <typename T,typename S, size_t... i>
constexpr inline int get_tuple_type_number(std::index_sequence<i...>) noexcept{
int k=-1;
int j=-1;
auto t=std::make_tuple((std::is_same<std::tuple_element_t<i,std::remove_reference_t<T>>,S>())...);
TupleForeach(t,[&k,&j](bool t){j++;(t==1)?k=j:k=k;});
return k;
}
template <typename T,typename S>
constexpr inline int Get_tuple_type_number() noexcept{
return get_tuple_type_number<T,S>(std::make_index_sequence<std::tuple_size_v<std::remove_reference_t<T>>>());
}
template<typename Type,typename T,typename K>
constexpr auto * Get_tuple_type_element(K &s){
constexpr int id_element=Get_tuple_type_number<T,Type>();
if constexpr(id_element<0){
static_assert (id_element<0,";Error_tuple;" );
int * t=nullptr;
return t;
}else{
return &std::get<id_element>(s);
}
}
template<typename Type,typename T>
constexpr auto * Get_tuple_type_element_t(T &s){
constexpr int id_element=Get_tuple_type_number<std::remove_reference_t<T>,Type>();
if constexpr(id_element<0){
static_assert (id_element<0,";Error_tuple;" );
int * t=nullptr;
return t;
}else{
return &std::get<id_element>(s);
}
}
template <typename T,auto type, size_t... i>
constexpr inline int get_tuple_type_number(std::index_sequence<i...>) noexcept{
int k=-1;
int j=-1;
auto t=std::make_tuple((std::tuple_element_t<i,std::remove_reference_t<T>>::type==type)...);
TupleForeach(t,[&k,&j](bool t){j++;(t==1)?k=j:k=k;});
return k;
}
template <typename T,auto type>
constexpr inline int Get_tuple_type_number() noexcept{
return get_tuple_type_number<T,type>(std::make_index_sequence<std::tuple_size_v<std::remove_reference_t<T>>>());
}
template<auto type,typename T,typename K>
constexpr auto * Get_tuple_type_element(K &s){
constexpr int id_element=Get_tuple_type_number<T,type>();
if constexpr(id_element<0){
static_assert (id_element<0,";Error_tuple;" );
int * t=nullptr;
return t;
}else{
return &std::get<id_element>(s);
}
}
template<typename Type,typename T>
constexpr bool isGet_tuple_type_element(){
constexpr int id_element=Get_tuple_type_number<T,Type>();
if constexpr(id_element<0){
return false;
}
return true;
}
template<typename Type,typename S,typename Args,typename Data, typename F>
constexpr void Construction_found_and_activation(Data &&t,F &&f){
if constexpr(std::is_constructible_v<Type,S>){
std::forward<F>(f)(*Get_tuple_type_element<Type,Args>(std::forward<Data>(t)));
}
}
template<typename T>
constexpr T s(size_t i){
return {};
};
template<typename T,size_t ...i>
constexpr auto _create_tuple(std::index_sequence<i...>){
return std::tuple_cat(std::make_tuple(s<T>(i))...);
}
template<int N,typename T>
constexpr auto create_tuple(){
return _create_tuple<T>(std::make_index_sequence<N>());
}
template<typename T>
constexpr T s(T t,size_t i){
return t;
};
template<typename ...T,size_t ...i>
constexpr auto _create_tuple(std::tuple<T...> t,std::index_sequence<i...>){
return std::tuple_cat(s(t,i)...);
}
template<int N,typename ...T>
constexpr auto create_tuple(T ...t){
auto tt=std::tuple_cat(std::make_tuple(t...));
if constexpr(N<0){
return std::tuple<>{};
}else{
return _create_tuple(tt,std::make_index_sequence<N>());
}
}
template<typename J,typename ...T,size_t ...i>
constexpr auto tuple_to_array(std::tuple<T...> t,std::index_sequence<i...>){
return std::array<J,sizeof...(T)>{std::get<i>(t)...};
}
template<typename J,typename ...T>
constexpr auto Tuple_to_array(std::tuple<T...> t){
return tuple_to_array<J>(t,std::make_index_sequence<sizeof...(T)>());
}
template<size_t ...i>
constexpr auto make_index_to_array(std::index_sequence<i...>){
return std::array<int,sizeof...(i)>{i...};
}
template<int N>
constexpr auto Make_index_to_array(){
if constexpr(N<0){
return std::tuple<>{};
}else{
return make_index_to_array(std::make_index_sequence<N>());
}
}
template<typename T>
constexpr auto Type_to(T value){
return value;
}
template < template <typename...> class Template, typename T >
struct is_instantiation_of_tuple : std::false_type {};
template < template <typename...> class Template, typename... Args >
struct is_instantiation_of_tuple< Template, Template<Args...> > : std::true_type {};
template<typename T>
constexpr auto Type_to_tuple(T value){
if constexpr(is_instantiation_of_tuple<std::tuple,decltype (Type_to<T>({}))>()){
return Type_to(value);
}else{
return std::tuple<T>(value);
}
}
template<typename T>
uint constexpr _cout_arg(){
if constexpr(is_instantiation_of_tuple<std::tuple,T>()){
return std::tuple_size<T>();
}else{
return 1;
}
}
template<typename ...Arg>
constexpr uint cout_arg(){
uint cout_arg__=sizeof...(Arg);
((cout_arg__+=_cout_arg<decltype(Type_to(Arg{}))>()-1),...);
return cout_arg__;
}
template<typename ...Arg>
constexpr auto type_disclosure(){
return std::tuple_cat(Type_to_tuple<Arg>({})...);
}
template<typename ...Arg>
constexpr auto type_disclosure(Arg...arg){
return std::tuple_cat(Type_to_tuple<Arg>(arg)...);
}
template<auto indef,auto data_>
struct KAD{//Key_And_Data
inline static constexpr decltype (indef) type=indef;
inline static constexpr decltype (data_) data=data_;
};
template<auto indef,typename Data>
struct KAAT{//Key_And_Atribute_Type
inline static constexpr decltype (indef) type=indef;
using Type_Data=Data;
std::optional<Data> data;
void inline set_data(Data data_){
data.emplace( data_ );
}
};
template<typename T,typename Data>
struct TAAT{//Type_And_Atribute_Type
using type=T;
static T Type();
std::optional<Data> data;
void inline set_data(Data & data_){
data.emplace( data_ );
}
};
template <typename ...Arg>
class Type_to_data{
std::tuple<Arg...> types{};
public:
Type_to_data(){}
// template<typename F>
// Type_to_data(F &f):types(fun_void<Arg>(f())...){}
template<auto type,typename T>
constexpr void set_data(T data_){
TupleForeach(types,[ &data_](auto & t){
if constexpr(std::remove_reference_t<decltype (t)>::type==type){
t.set_data(data_);
}
});
}
template<typename Type,typename T>
constexpr void set_data(T data_){
TupleForeach(types,[ &data_](auto &t){
if constexpr(std::is_same<typename std::remove_reference_t<decltype (t)>::type,Type>()){
t.set_data(data_);
}
});
}
template<auto type>
inline constexpr auto * get_element(){
return Get_tuple_type_element<type,std::tuple<Arg...>>(types);
}
template<auto type>
inline constexpr auto & get_element_data(){
return *Get_tuple_type_element<type,std::tuple<Arg...>>(types)->data;
}
template<typename Type>
constexpr auto * get_element(){
return Get_tuple_type_element<std::remove_reference_t<Type>,std::tuple<decltype(Arg::Type()) ...>>(types);
}
template<typename Type>
constexpr auto & get_element_data(){
return *Get_tuple_type_element<std::remove_reference_t<Type>,std::tuple<decltype(Arg::Type()) ...>>(types)->data;
}
constexpr auto & get_data(){ return types;}
const auto & get_data_const()const{ return types;}
};
template<typename Array,typename ...Arg>
inline void array_to_for(Array & array,Arg &...arg){//синтаксический сахар
std::for_each(array.begin(),array.end(),[&arg...](auto &t){t(arg...);});
}
template<typename ...Arg>
inline std::function<void(Arg ...)> get_to_for_array_fun(std::vector<std::function<void(Arg ...)>> & array){//синтаксический сахар
return [array](Arg ...arg){array_to_for(array,arg...);};
}
}
#endif // TUPLE_UTL_H