-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpack_utils.hpp
2506 lines (1826 loc) · 89.2 KB
/
pack_utils.hpp
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ------------------------------------------------
#pragma once
// ------------------------------------------------
#include <cstddef>
#include <concepts>
#include <utility>
#include <tuple>
#include <memory>
// ------------------------------------------------
namespace kaixo {
// ------------------------------------------------
constexpr std::size_t npos = static_cast<std::size_t>(-1);
// ------------------------------------------------
// Combine Filters using logical and
template<template<class> class ...Filter>
struct filter_and {
template<class Ty>
struct type {
constexpr static bool value = (static_cast<bool>(Filter<Ty>::value) && ...);
};
};
// ------------------------------------------------
// Combine Filters using logical or
template<template<class> class ...Filter>
struct filter_or {
template<class Ty>
struct type {
constexpr static bool value = (static_cast<bool>(Filter<Ty>::value) || ...);
};
};
// ------------------------------------------------
// Invert Filter using logical not
template<template<class> class Filter>
struct filter_invert {
template<class Ty>
struct type {
constexpr static bool value = !static_cast<bool>(Filter<Ty>::value);
};
};
// ------------------------------------------------
// Reverse the Sorter
template<template<class, class> class Sorter>
struct sorter_reverse {
template<class A, class B>
struct type {
constexpr static bool value = !Sorter<A, B>::value;
};
};
// ------------------------------------------------
// Only apply Transform if Filter matches
template<template<class> class Filter, template<class> class Transform>
struct conditional_transform {
template<class Ty>
struct _impl;
template<class Ty>
requires (Filter<Ty>::value)
struct _impl<Ty> {
using type = Transform<Ty>;
};
template<class Ty>
requires (!Filter<Ty>::value)
struct _impl<Ty> {
using type = Ty;
};
template<class Ty>
using type = typename _impl<Ty>::type;
};
// ------------------------------------------------
// ------------------------------------------------
// ------------------------------------------------
template<class ...Tys>
struct pack;
// ------------------------------------------------
namespace detail {
template<std::size_t I>
struct index_as_type {
constexpr static std::size_t value = I;
};
template<class>
struct to_indices;
template<std::size_t ...Is>
struct to_indices<pack<index_as_type<Is>...>> {
using type = std::index_sequence<Is...>;
};
template<class>
struct indices_as_pack;
template<std::size_t ...Ns>
struct indices_as_pack<std::index_sequence<Ns...>> {
using type = pack<index_as_type<Ns>...>;
};
}
// ------------------------------------------------
template<class Pack>
struct pack_size;
template<class ...Tys>
struct pack_size<pack<Tys...>> {
constexpr static std::size_t value = sizeof...(Tys);
};
// Size of Pack
template<class Pack>
constexpr std::size_t pack_size_v = pack_size<Pack>::value;
// ------------------------------------------------
template<std::size_t I, class Ty>
struct pack_element;
template<class Ty, class ...Tys>
struct pack_element<0, pack<Ty, Tys...>> {
using type = Ty;
};
template<std::size_t I, class Ty, class ...Tys>
struct pack_element<I, pack<Ty, Tys...>> {
using type = typename pack_element<I - 1, pack<Tys...>>::type;
};
// Ith element in Pack
template<std::size_t I, class Pack>
using pack_element_t = typename pack_element<I, Pack>::type;
// ------------------------------------------------
template<std::size_t N, class Indices>
struct indices_element {
constexpr static std::size_t value = pack_element<N, typename detail::indices_as_pack<Indices>::type>::type::value;
};
// Ith element in Indices
template<std::size_t I, class Indices>
constexpr std::size_t indices_element_v = indices_element<I, Indices>::value;
// ------------------------------------------------
template<class Type, class Pack>
struct pack_contains {
constexpr static bool value = false;
};
template<class Type, class ...Tys>
struct pack_contains<Type, pack<Type, Tys...>> {
constexpr static bool value = true;
};
template<class Type, class Ty, class ...Tys>
struct pack_contains<Type, pack<Ty, Tys...>> {
constexpr static bool value = pack_contains<Type, pack<Tys...>>::value;
};
// Pack contains Type
template<class Type, class Pack>
constexpr bool pack_contains_v = pack_contains<Type, Pack>::value;
// ------------------------------------------------
template<class Types, class Pack>
struct pack_contains_all;
template<class ...Types, class ...Tys>
struct pack_contains_all<pack<Types...>, pack<Tys...>> {
constexpr static bool value = (pack_contains<Types, pack<Tys...>>::value && ...);
};
// Pack contains all in Types
template<class Types, class Pack>
constexpr bool pack_contains_all_v = pack_contains_all<Types, Pack>::value;
// ------------------------------------------------
template<class Types, class Pack>
struct pack_contains_any;
template<class ...Types, class ...Tys>
struct pack_contains_any<pack<Types...>, pack<Tys...>> {
constexpr static bool value = (pack_contains<Types, pack<Tys...>>::value || ...);
};
// Pack contains any in Types
template<class Types, class Pack>
constexpr bool pack_contains_any_v = pack_contains_any<Types, Pack>::value;
// ------------------------------------------------
template<template<class> class Filter, class Pack>
struct pack_find;
template<template<class> class Filter, class ...Tys>
struct pack_find<Filter, pack<Tys...>> {
constexpr static bool value = (Filter<Tys>::value || ...);
};
// Find match for Filter in Pack
template<template<class> class Filter, class Pack>
constexpr bool pack_find_v = pack_find<Filter, Pack>::value;
// ------------------------------------------------
template<class Type, class Pack>
struct pack_count;
template<class Type, class ...Tys>
struct pack_count<Type, pack<Type, Tys...>> {
constexpr static std::size_t value = 1 + pack_count<Type, pack<Tys...>>::value;
};
template<class Type, class Ty, class ...Tys>
struct pack_count<Type, pack<Ty, Tys...>> {
constexpr static std::size_t value = pack_count<Type, pack<Tys...>>::value;
};
template<class Type>
struct pack_count<Type, pack<>> {
constexpr static std::size_t value = 0;
};
// Number of occurences of Type in Pack
template<class Type, class Pack>
constexpr std::size_t pack_count_v = pack_count<Type, Pack>::value;
// ------------------------------------------------
template<class Types, class Pack>
struct pack_count_all;
template<class ...Types, class ...Tys>
struct pack_count_all<pack<Types...>, pack<Tys...>> {
constexpr static std::size_t value = (pack_count<Types, pack<Tys...>>::value + ... + 0);
};
// Sum of the number of occurences of all Types in Pack
template<class Types, class Pack>
constexpr std::size_t pack_count_all_v = pack_count_all<Types, Pack>::value;
// ------------------------------------------------
template<class Pack>
struct pack_count_unique;
template<class ...Tys>
struct pack_count_unique<pack<Tys...>> {
template<class T>
constexpr static double _unq_cnt = 1.0 / (std::same_as<T, Tys> +...);
constexpr static std::size_t value = static_cast<std::size_t>((_unq_cnt<Tys> +... + 0) + 0.5);
};
// Count unique elements in Pack
template<class Pack>
constexpr std::size_t pack_count_unique_v = pack_count_unique<Pack>::value;
// ------------------------------------------------
template<template<class> class Filter, class Pack>
struct pack_count_filter;
template<template<class> class Filter, class ...Tys>
struct pack_count_filter<Filter, pack<Tys...>> {
constexpr static std::size_t value = (static_cast<bool>(Filter<Tys>::value) + ... + 0);
};
// Count number of matches for Filter in Pack
template<template<class> class Filter, class Pack>
constexpr std::size_t pack_count_filter_v = pack_count_filter<Filter, Pack>::value;
// ------------------------------------------------
template<class Type, std::size_t I, class Pack>
struct pack_nth_index_of;
template<class Type, std::size_t I, class Ty, class ...Tys>
struct pack_nth_index_of<Type, I, pack<Ty, Tys...>> {
constexpr static std::size_t _last = pack_nth_index_of<Type, I, pack<Tys...>>::value;
constexpr static std::size_t value = _last == npos ? npos : _last + 1;
};
template<class Type, std::size_t I, class ...Tys>
struct pack_nth_index_of<Type, I, pack<Type, Tys...>> {
constexpr static std::size_t _last = pack_nth_index_of<Type, I - 1, pack<Tys...>>::value;
constexpr static std::size_t value = _last == npos ? npos : _last + 1;
};
template<class Type, class ...Tys>
struct pack_nth_index_of<Type, 0, pack<Type, Tys...>> {
constexpr static std::size_t value = 0;
};
template<class Type, std::size_t N>
struct pack_nth_index_of<Type, N, pack<>> {
constexpr static bool _found = false;
constexpr static std::size_t value = npos;
};
// Index of the Nth occurence of Type in Pack
template<class Type, std::size_t N, class Pack>
constexpr std::size_t pack_nth_index_of_v = pack_nth_index_of<Type, N, Pack>::value;
// ------------------------------------------------
template<class Types, std::size_t N, class Pack>
struct pack_nth_index_of_any;
template<class ...Types, std::size_t N, class Ty, class ...Tys>
requires (!(std::same_as<Types, Ty> || ...))
struct pack_nth_index_of_any<pack<Types...>, N, pack<Ty, Tys...>> {
constexpr static std::size_t _last = pack_nth_index_of_any<pack<Types...>, N, pack<Tys...>>::value;
constexpr static std::size_t value = _last == npos ? npos : _last + 1;
};
template<class ...Types, std::size_t N, class Ty, class ...Tys>
requires (std::same_as<Types, Ty> || ...)
struct pack_nth_index_of_any<pack<Types...>, N, pack<Ty, Tys...>> {
constexpr static std::size_t _last = pack_nth_index_of_any<pack<Types...>, N - 1, pack<Tys...>>::value;
constexpr static std::size_t value = _last == npos ? npos : _last + 1;
};
template<class ...Types, class Ty, class ...Tys>
requires (std::same_as<Types, Ty> || ...)
struct pack_nth_index_of_any<pack<Types...>, 0, pack<Ty, Tys...>> {
constexpr static std::size_t value = 0;
};
template<class Types, std::size_t N>
struct pack_nth_index_of_any<Types, N, pack<>> {
constexpr static bool _found = false;
constexpr static std::size_t value = npos;
};
// Index of the Nth occurence of any of Types in Pack
template<class Types, std::size_t N, class Pack>
constexpr std::size_t pack_nth_index_of_any_v = pack_nth_index_of_any<Types, N, Pack>::value;
// ------------------------------------------------
template<template<class> class Filter, std::size_t N, class Pack>
struct pack_nth_index_filter;
template<template<class> class Filter, std::size_t N, class Ty, class ...Tys>
requires (!Filter<Ty>::value)
struct pack_nth_index_filter<Filter, N, pack<Ty, Tys...>> {
constexpr static std::size_t _last = pack_nth_index_filter<Filter, N, pack<Tys...>>::value;
constexpr static std::size_t value = _last == npos ? npos : _last + 1;
};
template<template<class> class Filter, std::size_t N, class Ty, class ...Tys>
requires (Filter<Ty>::value)
struct pack_nth_index_filter<Filter, N, pack<Ty, Tys...>> {
constexpr static std::size_t _last = pack_nth_index_filter<Filter, N - 1, pack<Tys...>>::value;
constexpr static std::size_t value = _last == npos ? npos : _last + 1;
};
template<template<class> class Filter, class Ty, class ...Tys>
requires (Filter<Ty>::value)
struct pack_nth_index_filter<Filter, 0, pack<Ty, Tys...>> {
constexpr static std::size_t value = 0;
};
template<template<class> class Filter, std::size_t N>
struct pack_nth_index_filter<Filter, N, pack<>> {
constexpr static bool _found = false;
constexpr static std::size_t value = npos;
};
// Index of the Nth match of Filter in Pack
template<template<class> class Filter, std::size_t N, class Pack>
constexpr std::size_t pack_nth_index_filter_v = pack_nth_index_filter<Filter, N, Pack>::value;
// ------------------------------------------------
template<class Type, class Pack>
struct pack_index_of {
constexpr static std::size_t value = pack_nth_index_of<Type, 0, Pack>::value;
};
// Index of the first occurence of Type in Pack
template<class Type, class Pack>
constexpr std::size_t pack_index_of_v = pack_index_of<Type, Pack>::value;
// ------------------------------------------------
template<class Types, class Pack>
struct pack_index_of_any {
constexpr static std::size_t value = pack_nth_index_of_any<Types, 0, Pack>::value;
};
// Index of the first occurence of any of Types in Pack
template<class Types, class Pack>
constexpr std::size_t pack_index_of_any_v = pack_index_of_any<Types, Pack>::value;
// ------------------------------------------------
template<template<class> class Filter, class Pack>
struct pack_index_filter {
constexpr static std::size_t value = pack_nth_index_filter<Filter, 0, Pack>::value;
};
// Index of the first match of Filter in Pack
template<template<class> class Filter, class Pack>
constexpr std::size_t pack_index_filter_v = pack_index_filter<Filter, Pack>::value;
// ------------------------------------------------
template<class Type, class Pack>
struct pack_first_index_of {
constexpr static std::size_t value = pack_nth_index_of<Type, 0, Pack>::value;
};
// Index of the first occurence of Type in Pack
template<class Type, class Pack>
constexpr std::size_t pack_first_index_of_v = pack_first_index_of<Type, Pack>::value;
// ------------------------------------------------
template<class Types, class Pack>
struct pack_first_index_of_any {
constexpr static std::size_t value = pack_nth_index_of_any<Types, 0, Pack>::value;
};
// Index of the first occurence of any of Types in Pack
template<class Types, class Pack>
constexpr std::size_t pack_first_index_of_any_v = pack_first_index_of_any<Types, Pack>::value;
// ------------------------------------------------
template<template<class> class Filter, class Pack>
struct pack_first_index_filter {
constexpr static std::size_t value = pack_nth_index_filter<Filter, 0, Pack>::value;
};
// Index of the first match of Filter in Pack
template<template<class> class Filter, class Pack>
constexpr std::size_t pack_first_index_filter_v = pack_first_index_filter<Filter, Pack>::value;
// ------------------------------------------------
template<class Type, class Pack>
struct pack_last_index_of {
constexpr static std::size_t value = pack_nth_index_of<Type, pack_count<Type, Pack>::value - 1, Pack>::value;
};
// Index of the last occurence of Type in Pack
template<class Type, class Pack>
constexpr std::size_t pack_last_index_of_v = pack_last_index_of<Type, Pack>::value;
// ------------------------------------------------
template<class Types, class Pack>
struct pack_last_index_of_any {
constexpr static std::size_t value = pack_nth_index_of_any<Types, pack_count_all<Types, Pack>::value - 1, Pack>::value;
};
// Index of the last occurence of any of Types in Pack
template<class Types, class Pack>
constexpr std::size_t pack_last_index_of_any_v = pack_last_index_of_any<Types, Pack>::value;
// ------------------------------------------------
template<template<class> class Filter, class Pack>
struct pack_last_index_filter {
constexpr static std::size_t value = pack_nth_index_filter<Filter, pack_count_filter<Filter, Pack>::value - 1, Pack>::value;
};
// Index of the last match of Filter in Pack
template<template<class> class Filter, class Pack>
constexpr std::size_t pack_last_index_filter_v = pack_last_index_filter<Filter, Pack>::value;
// ------------------------------------------------
template<class Type, class Pack>
struct pack_indices_of {
template<class>
struct _impl;
template<std::size_t ...Is>
struct _impl<std::index_sequence<Is...>> {
using type = std::index_sequence<pack_nth_index_of<Type, Is, Pack>::value...>;
};
using type = typename _impl<std::make_index_sequence<pack_count<Type, Pack>::value>>::type;
};
// All indices of Type in Pack
template<class Type, class Pack>
using pack_indices_of_t = pack_indices_of<Type, Pack>::type;
// ------------------------------------------------
template<class Types, class Pack>
struct pack_indices_of_all {
template<class>
struct _impl;
template<std::size_t ...Is>
struct _impl<std::index_sequence<Is...>> {
using type = std::index_sequence<pack_nth_index_of_any<Types, Is, Pack>::value...>;
};
using type = typename _impl<std::make_index_sequence<pack_count_all<Types, Pack>::value>>::type;
};
// All indices of all Types in Pack
template<class Types, class Pack>
using pack_indices_of_all_t = pack_indices_of_all<Types, Pack>::type;
// ------------------------------------------------
template<template<class> class Filter, class Pack>
struct pack_indices_filter {
template<class>
struct _impl;
template<std::size_t ...Is>
struct _impl<std::index_sequence<Is...>> {
using type = std::index_sequence<pack_nth_index_filter<Filter, Is, Pack>::value...>;
};
using type = typename _impl<std::make_index_sequence<pack_count_filter<Filter, Pack>::value>>::type;
};
// All indices of all matches of Filter in Pack
template<template<class> class Filter, class Pack>
using pack_indices_filter_t = pack_indices_filter<Filter, Pack>::type;
// ------------------------------------------------
template<template<class> class Filter, class Pack>
struct pack_indices_not_filter {
template<class>
struct _impl;
template<std::size_t ...Is>
struct _impl<std::index_sequence<Is...>> {
using type = std::index_sequence<pack_nth_index_filter<filter_invert<Filter>::type, Is, Pack>::value...>;
};
using type = typename _impl<std::make_index_sequence<pack_count_filter<filter_invert<Filter>::type, Pack>::value>>::type;
};
// All indices of all non-matches of Filter in Pack
template<template<class> class Filter, class Pack>
using pack_indices_not_filter_t = pack_indices_not_filter<Filter, Pack>::type;
// ------------------------------------------------
template<class Type, class Pack>
struct pack_indices_not_of {
template<class Other>
struct _impl {
constexpr static bool value = !std::same_as<Other, Type>;
};
using type = typename pack_indices_filter<_impl, Pack>::type;
};
// All indices not of Type in Pack
template<class Type, class Pack>
using pack_indices_not_of_t = pack_indices_not_of<Type, Pack>::type;
// ------------------------------------------------
template<class Types, class Pack>
struct pack_indices_not_of_all {
template<class Other>
struct _impl {
constexpr static bool value = !pack_contains<Other, Types>::value;
};
using type = typename pack_indices_filter<_impl, Pack>::type;
};
// All indices not of all Types in Pack
template<class Types, class Pack>
using pack_indices_not_of_all_t = pack_indices_not_of_all<Types, Pack>::type;
// ------------------------------------------------
namespace detail {
template<class Nums, std::size_t Index = 0, std::size_t ...Result>
struct remove_number_if_not_same_as_index;
template<std::size_t Num, std::size_t ...Nums, std::size_t Index, std::size_t ...Result>
struct remove_number_if_not_same_as_index<std::index_sequence<Num, Nums...>, Index, Result...> {
using type = typename remove_number_if_not_same_as_index<
std::index_sequence<Nums...>, Index + 1, Result...>::type;
};
template<std::size_t ...Nums, std::size_t Index, std::size_t ...Result>
struct remove_number_if_not_same_as_index<std::index_sequence<Index, Nums...>, Index, Result...> {
using type = typename remove_number_if_not_same_as_index<
std::index_sequence<Nums...>, Index + 1, Result..., Index>::type;
};
template<std::size_t Index, std::size_t ...Result>
struct remove_number_if_not_same_as_index<std::index_sequence<>, Index, Result...> {
using type = std::index_sequence<Result...>;
};
}
template<class Pack>
struct pack_first_indices {
template<class>
struct _impl;
template<std::size_t ...Is>
struct _impl<std::index_sequence<Is...>> {
using _first_indices = std::index_sequence<pack_first_index_of<typename pack_element<Is, Pack>::type, Pack>::value...>;
using type = typename detail::remove_number_if_not_same_as_index<_first_indices>::type;
};
using type = typename _impl<std::make_index_sequence<pack_size<Pack>::value>>::type;
};
// First indices of all elements in Pack
template<class Pack>
using pack_first_indices_t = typename pack_first_indices<Pack>::type;
// ------------------------------------------------
template<class Pack>
struct pack_last_indices {
template<class>
struct _impl;
template<std::size_t ...Is>
struct _impl<std::index_sequence<Is...>> {
using _first_indices = std::index_sequence<pack_last_index_of<typename pack_element<Is, Pack>::type, Pack>::value...>;
using type = typename detail::remove_number_if_not_same_as_index<_first_indices>::type;
};
using type = typename _impl<std::make_index_sequence<pack_size<Pack>::value>>::type;
};
// Last indices of all elements in Pack
template<class Pack>
using pack_last_indices_t = typename pack_last_indices<Pack>::type;
// ------------------------------------------------
template<std::size_t N, class Pack>
struct pack_nth_indices {
template<class>
struct _impl;
template<std::size_t ...Is>
struct _impl<std::index_sequence<Is...>> {
using _first_indices = std::index_sequence<pack_nth_index_of<typename pack_element<Is, Pack>::type, N, Pack>::value...>;
using type = typename detail::remove_number_if_not_same_as_index<_first_indices>::type;
};
using type = typename _impl<std::make_index_sequence<pack_size<Pack>::value>>::type;
};
// Nth indices of all elements in Pack
template<std::size_t N, class Pack>
using pack_nth_indices_t = typename pack_nth_indices<N, Pack>::type;
// ------------------------------------------------
template<class Indices, class Pack>
struct pack_at_indices;
template<std::size_t ...Is, class Pack>
struct pack_at_indices<std::index_sequence<Is...>, Pack> {
using type = pack<typename pack_element<Is, Pack>::type...>;
};
// Create new pack by selecting Indices from Pack
template<class Indices, class Pack>
using pack_at_indices_t = typename pack_at_indices<Indices, Pack>::type;
// ------------------------------------------------
template<class Pack>
struct pack_unique {
using type = typename pack_at_indices<typename pack_first_indices<Pack>::type, Pack>::type;
};
// Only keep unique elements in Pack
template<class Pack>
using pack_unique_t = typename pack_unique<Pack>::type;
// ------------------------------------------------
template<std::size_t I, class Pack>
struct pack_drop;
template<std::size_t I, class Ty, class ...Tys>
struct pack_drop<I, pack<Ty, Tys...>> {
using type = typename pack_drop<I - 1, pack<Tys...>>::type;
};
template<class Pack>
struct pack_drop<0, Pack> {
using type = Pack;
};
// Drop the first I elements of Pack
template<std::size_t I, class Pack>
using pack_drop_t = typename pack_drop<I, Pack>::type;
// ------------------------------------------------
template<template<class> class Filter, class Pack>
struct pack_drop_while;
template<template<class> class Filter, class Ty, class ...Tys>
requires (Filter<Ty>::value)
struct pack_drop_while<Filter, pack<Ty, Tys...>> {
using type = typename pack_drop_while<Filter, pack<Tys...>>::type;
};
template<template<class> class Filter, class Ty, class ...Tys>
requires (!Filter<Ty>::value)
struct pack_drop_while<Filter, pack<Ty, Tys...>> {
using type = pack<Ty, Tys...>;
};
template<template<class> class Filter>
struct pack_drop_while<Filter, pack<>> {
using type = pack<>;
};
// Drop elements from Pack while Filter matches
template<template<class> class Filter, class Pack>
using pack_drop_while_t = typename pack_drop_while<Filter, Pack>::type;
// ------------------------------------------------
template<template<class> class Filter, class Pack>
struct pack_drop_until;
template<template<class> class Filter, class Ty, class ...Tys>
requires (!Filter<Ty>::value)
struct pack_drop_until<Filter, pack<Ty, Tys...>> {
using type = typename pack_drop_until<Filter, pack<Tys...>>::type;
};
template<template<class> class Filter, class Ty, class ...Tys>
requires (Filter<Ty>::value)
struct pack_drop_until<Filter, pack<Ty, Tys...>> {
using type = pack<Ty, Tys...>;
};
template<template<class> class Filter>
struct pack_drop_until<Filter, pack<>> {
using type = pack<>;
};
// Drop elements from Pack until Filter matches
template<template<class> class Filter, class Pack>
using pack_drop_until_t = typename pack_drop_until<Filter, Pack>::type;
// ------------------------------------------------
template<std::size_t I, class Pack>
struct pack_take {
template<class>
struct _impl;
template<std::size_t ...Is>
struct _impl<std::index_sequence<Is...>> {
using type = pack<typename pack_element<Is, Pack>::type...>;
};
using type = typename _impl<std::make_index_sequence<I>>::type;
};
// Take the first I elements of Pack
template<std::size_t I, class Pack>
using pack_take_t = typename pack_take<I, Pack>::type;
// ------------------------------------------------
template<template<class> class Filter, class Pack>
struct pack_take_while {
template<std::size_t Take>
struct _impl {
using type = typename pack_take<Take, Pack>::type;
};
template<>
struct _impl<npos> {
using type = Pack;
};
constexpr static std::size_t _first_non_match = pack_first_index_filter<filter_invert<Filter>::type, Pack>::value;
using type = typename _impl<_first_non_match>::type;
};
// Take elements from Pack while Filter matches
template<template<class> class Filter, class Pack>
using pack_take_while_t = typename pack_take_while<Filter, Pack>::type;
// ------------------------------------------------
template<template<class> class Filter, class Pack>
struct pack_take_until {
template<std::size_t Take>
struct _impl {
using type = typename pack_take<Take, Pack>::type;
};
template<>
struct _impl<npos> {
using type = Pack;
};
constexpr static std::size_t _first_non_match = pack_first_index_filter<Filter, Pack>::value;
using type = typename _impl<_first_non_match>::type;
};
// Take elements from Pack until Filter matches
template<template<class> class Filter, class Pack>
using pack_take_until_t = typename pack_take_until<Filter, Pack>::type;
// ------------------------------------------------
template<std::size_t I, class Pack>
struct pack_drop_last {
using type = typename pack_take<pack_size<Pack>::value - I, Pack>::type;
};
// Drop the last I elements of Pack
template<std::size_t I, class Pack>
using pack_drop_last_t = typename pack_drop_last<I, Pack>::type;
// ------------------------------------------------
template<template<class> class Filter, class Pack>
struct pack_drop_last_while {
constexpr static std::size_t _last_match = pack_last_index_filter<filter_invert<Filter>::type, Pack>::value;
using type = typename pack_take<_last_match + 1, Pack>::type;
};
// Drop elements from the end of Pack while Filter matches
template<template<class> class Filter, class Pack>
using pack_drop_last_while_t = typename pack_drop_last_while<Filter, Pack>::type;
// ------------------------------------------------
template<template<class> class Filter, class Pack>
struct pack_drop_last_until {
constexpr static std::size_t _last_match = pack_last_index_filter<Filter, Pack>::value;
using type = typename pack_take<_last_match + 1, Pack>::type;
};
// Drop elements from the end of Pack until Filter matches
template<template<class> class Filter, class Pack>
using pack_drop_last_until_t = typename pack_drop_last_until<Filter, Pack>::type;
// ------------------------------------------------
template<std::size_t I, class Pack>
struct pack_take_last {
using type = typename pack_drop<pack_size<Pack>::value - I, Pack>::type;
};
// Take the last I elements of Pack
template<std::size_t I, class Pack>
using pack_take_last_t = typename pack_take_last<I, Pack>::type;
// ------------------------------------------------
template<template<class> class Filter, class Pack>
struct pack_take_last_while {
constexpr static std::size_t _last_match = pack_last_index_filter<filter_invert<Filter>::type, Pack>::value;
using type = typename pack_drop<_last_match + 1, Pack>::type;
};
// Take elements from the end of Pack while Filter matches
template<template<class> class Filter, class Pack>
using pack_take_last_while_t = typename pack_take_last_while<Filter, Pack>::type;
// ------------------------------------------------
template<template<class> class Filter, class Pack>
struct pack_take_last_until {
constexpr static std::size_t _last_match = pack_last_index_filter<Filter, Pack>::value;
using type = typename pack_drop<_last_match + 1, Pack>::type;
};
// Take elements from the end of Pack until Filter matches
template<template<class> class Filter, class Pack>
using pack_take_last_until_t = typename pack_take_last_until<Filter, Pack>::type;
// ------------------------------------------------
template<class ...Packs>
struct pack_concat;
template<class ...As, class ...Bs, class ...Packs>
struct pack_concat<pack<As...>, pack<Bs...>, Packs...> {
using type = typename pack_concat<pack<As..., Bs...>, Packs...>::type;
};
template<class Pack>
struct pack_concat<Pack> {
using type = Pack;
};
template<>
struct pack_concat<> {
using type = pack<>;
};
// Concat all types in all Packs to single pack
template<class ...Packs>
using pack_concat_t = typename pack_concat<Packs...>::type;
// ------------------------------------------------
template<std::size_t I, class Pack>
struct pack_erase {
using type = typename pack_concat<typename pack_take<I, Pack>::type, typename pack_drop<I + 1, Pack>::type>::type;
};
// Erase index I from Pack
template<std::size_t I, class Pack>
using pack_erase_t = typename pack_erase<I, Pack>::type;
// ------------------------------------------------
namespace detail {
template<class Indices, class Pack, class Result = pack<>, std::size_t Index = 0>
struct pack_erase_all;
template<std::size_t ...Is, class Ty, class ...Tys, class ...Result, std::size_t Index>
requires ((Is != Index) && ...) // Index not in erase
struct pack_erase_all<std::index_sequence<Is...>, pack<Ty, Tys...>, pack<Result...>, Index> {
using type = typename pack_erase_all<std::index_sequence<Is...>, pack<Tys...>, pack<Result..., Ty>, Index + 1>::type;
};
template<std::size_t ...Is, class Ty, class ...Tys, class Result, std::size_t Index>
requires ((Is == Index) || ...) // Index in erase
struct pack_erase_all<std::index_sequence<Is...>, pack<Ty, Tys...>, Result, Index> {
using type = typename pack_erase_all<std::index_sequence<Is...>, pack<Tys...>, Result, Index + 1>::type;
};
template<class Indices, class Result, std::size_t Index>
struct pack_erase_all<Indices, pack<>, Result, Index> {
using type = Result;
};
}
template<class Indices, class Pack>
struct pack_erase_all {
using type = typename detail::pack_erase_all<Indices, Pack>::type;
};
// Erase all Indices from Pack
template<class Indices, class Pack>
using pack_erase_all_t = typename pack_erase_all<Indices, Pack>::type;
// ------------------------------------------------
template<class Type, std::size_t I, class Pack>
struct pack_insert {
using type = typename pack_concat<typename pack_take<I, Pack>::type, pack<Type>, typename pack_drop<I, Pack>::type>::type;
};
// Insert Type in Pack at index I
template<class Type, std::size_t I, class Pack>
using pack_insert_t = typename pack_insert<Type, I, Pack>::type;
// ------------------------------------------------
template<class Types, std::size_t I, class Pack>
struct pack_insert_all {