/
mp.hpp
24472 lines (20577 loc) · 711 KB
/
mp.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
// SPDX-FileCopyrightText: 2023 Jonathan Poelen <jonathan.poelen@gmail.com>
// SPDX-FileCopyrightText: 2024 Jonathan Poelen <jonathan.poelen@gmail.com>
// SPDX-License-Identifier: MIT
// Amalgamated version of https://github.com/jonathanpoelen/jln.mp
#pragma once
#ifndef JLN_MP_HPP
#define JLN_MP_HPP
#include <cstddef>
#include <cstdint>
#include <limits>
#include <type_traits>
#include <utility>
namespace jln::mp
{
/// \ingroup functional
/// \treturn \value
struct identity
{
template<class x>
using f = x;
};
namespace emp
{
template<class x>
using identity = x;
}
/// \ingroup number
using int_ = std::intmax_t;
using uint_ = std::uintmax_t;
template<int_ v>
struct number { static const int_ value = v; };
using true_ = number<1>;
using false_ = number<0>;
#ifndef JLN_MP_ENABLE_TPL_AUTO
# if defined(__cpp_nontype_template_parameter_auto)
# if __cpp_nontype_template_parameter_auto >= 201606L
# define JLN_MP_ENABLE_TPL_AUTO 1
# endif
# endif
#endif
#ifndef JLN_MP_ENABLE_TPL_AUTO
# define JLN_MP_ENABLE_TPL_AUTO 0
#endif
#if JLN_MP_ENABLE_TPL_AUTO
# define JLN_MP_TPL_AUTO_OR_INT auto
# define JLN_MP_TPL_AUTO_OR(T) auto
#else
# define JLN_MP_TPL_AUTO_OR_INT ::jln::mp::int_
# define JLN_MP_TPL_AUTO_OR(T) T
#endif
}
namespace jln::mp
{
/// \ingroup list
template<class... xs>
class list {};
} // namespace jln::mp
// Compiler type
//@{
// cuda
#if defined(__CUDACC__)
# ifndef JLN_MP_CUDA
# define JLN_MP_CUDA (__CUDACC_VER_MAJOR__ * 100 + __CUDACC_VER_MINOR__)
# endif
# if defined(__clang__)
# ifndef JLN_MP_HOST_COMPILER_CLANG
# define JLN_MP_HOST_COMPILER_CLANG (__clang_major__ * 100 + __clang_minor__)
# endif
# elif defined(__GNUC__)
# ifndef JLN_MP_HOST_COMPILER_GCC
# define JLN_MP_HOST_COMPILER_GCC (__GNUC__ * 100 + __GNUC_MINOR__)
# endif
# endif
// clang-cl
#elif defined(_MSC_VER) && defined(__clang__)
# ifndef JLN_MP_MSVC_LIKE
# define JLN_MP_MSVC_LIKE _MSC_VER
# endif
# ifndef JLN_MP_CLANG_CL
# define JLN_MP_CLANG_CL (__clang_major__ * 100 + __clang_minor__)
# endif
// clang
#elif defined(__clang__)
# ifndef JLN_MP_CLANG
# define JLN_MP_CLANG (__clang_major__ * 100 + __clang_minor__)
# endif
// msvc
#elif defined(_MSC_VER)
# ifndef JLN_MP_MSVC_LIKE
# define JLN_MP_MSVC_LIKE _MSC_VER
# endif
# ifndef JLN_MP_MSVC
# define JLN_MP_MSVC _MSC_VER
# endif
# ifndef JLN_MP_MEMOIZED_ALIAS
# define JLN_MP_MEMOIZED_ALIAS 1
# endif
// gcc
#elif defined(__GNUC__)
# ifndef JLN_MP_GCC
# define JLN_MP_GCC (__GNUC__ * 100 + __GNUC_MINOR__)
# endif
# ifndef JLN_MP_OPTIMIZED_ALIAS
# define JLN_MP_OPTIMIZED_ALIAS 1
# endif
# ifndef JLN_MP_MEMOIZED_ALIAS
# define JLN_MP_MEMOIZED_ALIAS 1
# endif
# ifndef JLN_MP_FAST_ALIAS_ON_VARIABLE_TEMPLATE
# define JLN_MP_FAST_ALIAS_ON_VARIABLE_TEMPLATE 1
# endif
#endif
// clang like
#if defined(__clang__)
# ifndef JLN_MP_CLANG_LIKE
# define JLN_MP_CLANG_LIKE (__clang_major__ * 100 + __clang_minor__)
# endif
#endif
// apple
#if defined(__APPLE__) && JLN_MP_CLANG
# ifndef JLN_MP_APPLE_CLANG
# define JLN_MP_APPLE_CLANG JLN_MP_CLANG
# endif
#endif
#ifndef JLN_MP_CLANG_LIKE
# define JLN_MP_CLANG_LIKE 0
#endif
#ifndef JLN_MP_APPLE_CLANG
# define JLN_MP_APPLE_CLANG 0
#endif
#ifndef JLN_MP_MSVC_LIKE
# define JLN_MP_MSVC_LIKE 0
#endif
#ifndef JLN_MP_CLANG_CL
# define JLN_MP_CLANG_CL 0
#endif
#ifndef JLN_MP_CLANG
# define JLN_MP_CLANG 0
#endif
#ifndef JLN_MP_MSVC
# define JLN_MP_MSVC 0
#endif
#ifndef JLN_MP_GCC
# define JLN_MP_GCC 0
#endif
#ifndef JLN_MP_CUDA
# define JLN_MP_CUDA 0
#endif
#ifndef JLN_MP_HOST_COMPILER_CLANG
# define JLN_MP_HOST_COMPILER_CLANG 0
#endif
#ifndef JLN_MP_HOST_COMPILER_GCC
# define JLN_MP_HOST_COMPILER_GCC 0
#endif
#ifndef JLN_MP_OPTIMIZED_ALIAS
# define JLN_MP_OPTIMIZED_ALIAS 0
#endif
#ifndef JLN_MP_MEMOIZED_ALIAS
# define JLN_MP_MEMOIZED_ALIAS 0
#endif
#ifndef JLN_MP_FAST_ALIAS_ON_VARIABLE_TEMPLATE
# define JLN_MP_FAST_ALIAS_ON_VARIABLE_TEMPLATE 0
#endif
//@}
#define JLN_MP_WORKAROUND(symbol, test) ((symbol) != 0 && ((symbol) test))
#if defined(__has_builtin)
# define JLN_MP_HAS_BUILTIN(name) __has_builtin(name)
#else
# define JLN_MP_HAS_BUILTIN(name) 0
#endif
// Pragma
//@{
#define JLN_MP_PRAGMA(x) JLN_MP_PRAGMA_I(x)
#ifdef _MSC_VER
# define JLN_MP_PRAGMA_I(x) __pragma(x)
#else
# define JLN_MP_PRAGMA_I(x) _Pragma(#x)
#endif
//@}
// Features
//@{
#ifndef JLN_MP_FEATURE_CONCEPTS
# ifdef __cpp_concepts
# if __cpp_concepts >= 201907L
# define JLN_MP_FEATURE_CONCEPTS 1
# endif
# endif
# ifndef JLN_MP_FEATURE_CONCEPTS
# define JLN_MP_FEATURE_CONCEPTS 0
# endif
#endif
//@}
// Diagnostic
//@{
#if JLN_MP_CLANG_LIKE || JLN_MP_GCC
# define JLN_MP_DIAGNOSTIC_PUSH() _Pragma("GCC diagnostic push")
# define JLN_MP_DIAGNOSTIC_POP() _Pragma("GCC diagnostic pop")
# define JLN_MP_DIAGNOSTIC_MSVC_IGNORE(X)
# define JLN_MP_DIAGNOSTIC_GCC_IGNORE(X) JLN_MP_PRAGMA_I(GCC diagnostic ignored X)
# define JLN_MP_DIAGNOSTIC_MSVC_WARNING(X)
# define JLN_MP_DIAGNOSTIC_GCC_WARNING(X) JLN_MP_PRAGMA_I(GCC diagnostic warning X)
# define JLN_MP_DIAGNOSTIC_MSVC_ERROR(X)
# define JLN_MP_DIAGNOSTIC_GCC_ERROR(X) JLN_MP_PRAGMA_I(GCC diagnostic error X)
# ifdef __clang__
# define JLN_MP_DIAGNOSTIC_GCC_ONLY_IGNORE(X)
# define JLN_MP_DIAGNOSTIC_CLANG_IGNORE JLN_MP_DIAGNOSTIC_GCC_IGNORE
# define JLN_MP_DIAGNOSTIC_GCC_ONLY_WARNING(X)
# define JLN_MP_DIAGNOSTIC_CLANG_WARNING JLN_MP_DIAGNOSTIC_GCC_WARNING
# define JLN_MP_DIAGNOSTIC_GCC_ONLY_ERROR(X)
# define JLN_MP_DIAGNOSTIC_CLANG_ERROR JLN_MP_DIAGNOSTIC_GCC_ERROR
# else
# define JLN_MP_DIAGNOSTIC_GCC_ONLY_IGNORE JLN_MP_DIAGNOSTIC_GCC_IGNORE
# define JLN_MP_DIAGNOSTIC_CLANG_IGNORE(X)
# define JLN_MP_DIAGNOSTIC_GCC_ONLY_WARNING JLN_MP_DIAGNOSTIC_GCC_WARNING
# define JLN_MP_DIAGNOSTIC_CLANG_WARNING(X)
# define JLN_MP_DIAGNOSTIC_GCC_ONLY_ERROR JLN_MP_DIAGNOSTIC_GCC_ERROR
# define JLN_MP_DIAGNOSTIC_CLANG_ERROR(X)
# endif
#elif JLN_MP_MSVC
# define JLN_MP_DIAGNOSTIC_PUSH() __pragma(warning(push))
# define JLN_MP_DIAGNOSTIC_POP() __pragma(warning(pop))
# define JLN_MP_DIAGNOSTIC_MSVC_IGNORE(X) JLN_MP_PRAGMA_I(warning(disable:X))
# define JLN_MP_DIAGNOSTIC_GCC_ONLY_IGNORE(X)
# define JLN_MP_DIAGNOSTIC_GCC_IGNORE(X)
# define JLN_MP_DIAGNOSTIC_MSVC_WARNING(X) JLN_MP_PRAGMA_I(warning(4:X))
# define JLN_MP_DIAGNOSTIC_GCC_ONLY_WARNING(X)
# define JLN_MP_DIAGNOSTIC_GCC_WARNING(X)
# define JLN_MP_DIAGNOSTIC_MSVC_ERROR(X) JLN_MP_PRAGMA_I(error(X))
# define JLN_MP_DIAGNOSTIC_GCC_ONLY_ERROR(X)
# define JLN_MP_DIAGNOSTIC_GCC_ERROR(X)
# if JLN_MP_CLANG_LIKE
# define JLN_MP_DIAGNOSTIC_CLANG_IGNORE(X) JLN_MP_PRAGMA_I(GCC diagnostic ignored X)
# define JLN_MP_DIAGNOSTIC_CLANG_WARNING(X) JLN_MP_PRAGMA_I(GCC diagnostic warning X)
# define JLN_MP_DIAGNOSTIC_CLANG_ERROR(X) JLN_MP_PRAGMA_I(GCC diagnostic error X)
# else
# define JLN_MP_DIAGNOSTIC_CLANG_IGNORE(X)
# define JLN_MP_DIAGNOSTIC_CLANG_WARNING(X)
# define JLN_MP_DIAGNOSTIC_CLANG_ERROR(X)
# endif
#else
# define JLN_MP_DIAGNOSTIC_PUSH()
# define JLN_MP_DIAGNOSTIC_POP()
# define JLN_MP_DIAGNOSTIC_MSVC_IGNORE(X)
# define JLN_MP_DIAGNOSTIC_GCC_IGNORE(X)
# define JLN_MP_DIAGNOSTIC_GCC_ONLY_IGNORE(X)
# define JLN_MP_DIAGNOSTIC_CLANG_IGNORE(X)
# define JLN_MP_DIAGNOSTIC_MSVC_WARNING(X)
# define JLN_MP_DIAGNOSTIC_GCC_WARNING(X)
# define JLN_MP_DIAGNOSTIC_GCC_ONLY_WARNING(X)
# define JLN_MP_DIAGNOSTIC_CLANG_WARNING(X)
# define JLN_MP_DIAGNOSTIC_MSVC_ERROR(X)
# define JLN_MP_DIAGNOSTIC_GCC_ERROR(X)
# define JLN_MP_DIAGNOSTIC_GCC_ONLY_ERROR(X)
# define JLN_MP_DIAGNOSTIC_CLANG_ERROR(X)
#endif
//@}
// special diagnostic
//@{
#if JLN_MP_CLANG >= 1600
# define JLN_MP_DIAGNOSTIC_IGNORE_UNSAFE_BUFFER_USAGE() \
JLN_MP_DIAGNOSTIC_CLANG_IGNORE("-Wunsafe-buffer-usage")
#else
# define JLN_MP_DIAGNOSTIC_IGNORE_UNSAFE_BUFFER_USAGE()
#endif
//@}
// special wrapper for specialization of variable template or struct
//@{
#if JLN_MP_FAST_ALIAS_ON_VARIABLE_TEMPLATE
# define JLN_MP_NUMBER_FROM_VARIABLE_TEMPLATE_OR_TYPE(expr_v, expr_impl, ...) \
number<expr_v<__VA_ARGS__>>
#else
# define JLN_MP_NUMBER_FROM_VARIABLE_TEMPLATE_OR_TYPE(expr_v, expr_impl, ...) \
typename expr_impl<__VA_ARGS__>::type
#endif
#define JLN_MP_NUMBER_FROM_REGULAR_VARIABLE_TEMPLATE_OR_TYPE(name, ...) \
JLN_MP_NUMBER_FROM_VARIABLE_TEMPLATE_OR_TYPE(emp::name##_v, detail::name##_impl, __VA_ARGS__)
//@}
#define JLN_MP_UNPACK(...) __VA_ARGS__
#ifndef JLN_MP_ENABLE_DEBUG
# define JLN_MP_ENABLE_DEBUG 0
#endif
// useless with gcc and msvc since it displays all the transformations
#if JLN_MP_GCC || JLN_MP_MSVC || JLN_MP_MEMOIZED_ALIAS
# undef JLN_MP_ENABLE_DEBUG
# define JLN_MP_ENABLE_DEBUG 0
#endif
#ifndef JLN_MP_MAX_CALL_ELEMENT
# define JLN_MP_MAX_CALL_ELEMENT 10000
#endif
/// \defgroup list List
/// \defgroup algorithm Algorithm
/// \defgroup number Number
/// \defgroup value Value
/// \defgroup functional Functional
/// \defgroup utility Utility
/// \defgroup trait Trait
/// \defgroup search Search
/// \defgroup group Group
// Fast convertion to bool.
// If the value is more likely to be a bool, preferred bool(expr)
#if JLN_MP_GCC
# define JLN_MP_RAW_EXPR_TO_BOOL(...) __VA_ARGS__ ? true : false
# define JLN_MP_RAW_EXPR_TO_BOOL_NOT(...) __VA_ARGS__ ? false : true
# define JLN_MP_EXPR_TO_BOOL(...) __VA_ARGS__ ? true : false
# define JLN_MP_EXPR_TO_BOOL_NOT(...) __VA_ARGS__ ? false : true
#elif JLN_MP_CLANG
# define JLN_MP_RAW_EXPR_TO_BOOL(...) !!__VA_ARGS__
# define JLN_MP_RAW_EXPR_TO_BOOL_NOT(...) !__VA_ARGS__
# define JLN_MP_EXPR_TO_BOOL(...) !!(__VA_ARGS__)
# define JLN_MP_EXPR_TO_BOOL_NOT(...) !(__VA_ARGS__)
#else
# define JLN_MP_RAW_EXPR_TO_BOOL(...) bool(__VA_ARGS__)
# define JLN_MP_RAW_EXPR_TO_BOOL_NOT(...) !__VA_ARGS__
# define JLN_MP_EXPR_TO_BOOL(...) bool(__VA_ARGS__)
# define JLN_MP_EXPR_TO_BOOL_NOT(...) !(__VA_ARGS__)
#endif
namespace jln::mp
{
/// \ingroup utility
template<bool>
struct conditional_c;
template<>
struct conditional_c<true>
{
template<class true_value, class false_value>
using f = true_value;
};
template<>
struct conditional_c<false>
{
template<class true_value, class false_value>
using f = false_value;
};
template<class bool_>
using conditional = conditional_c<JLN_MP_RAW_EXPR_TO_BOOL(bool_::value)>;
namespace emp
{
template<class bool_, class true_value, class false_value>
using conditional = typename mp::conditional_c<JLN_MP_RAW_EXPR_TO_BOOL(bool_::value)>
::template f<true_value, false_value>;
template<bool bool_, class true_value, class false_value>
using conditional_c = typename mp::conditional_c<bool_>
::template f<true_value, false_value>;
}
/// Value that is not available.
/// This type is used in `smp` for a contract that is not respected.
struct na {};
#if !JLN_MP_MEMOIZED_ALIAS || JLN_MP_MSVC
# if !JLN_MP_CUDA
/// \cond
namespace detail
{
template<class F, class... Params>
struct memoizer_impl;
}
/// \endcond
/// \ingroup functional
/// Memoization version of \link call.
template<class C, class... xs>
using memoize_call = typename detail::memoizer_impl<C, xs...>::type;
/// Memoize a call to \c C::f<xs...>.
template<class C>
struct memoize
{
template<class... xs>
using f = typename detail::memoizer_impl<C, xs...>::type;
};
# else // if JLN_MP_CUDA
/// \cond
namespace detail
{
template<class dummy, class F, class... Params>
struct memoizer_impl;
}
/// \endcond
/// \ingroup functional
/// Memoization version of \link call.
template<class C, class... xs>
using memoize_call = typename detail::memoizer_impl<void, C, xs...>::type;
/// Memoize a call to \c C::f<xs...>.
template<class C>
struct memoize
{
template<class... xs>
using f = typename detail::memoizer_impl<void, C, xs...>::type;
};
# endif
# define JLN_MP_MEMOIZE(...) ::jln::mp::memoize<__VA_ARGS__>
#else // if JLN_MP_MEMOIZED_ALIAS && !JLN_MP_MSVC
template<class C, class... xs>
using memoize_call = typename conditional_c<!sizeof...(xs)>
::template f<C, C>
::template f<xs...>;
template<class C>
struct memoize
{
template<class... xs>
using f = typename conditional_c<!sizeof...(xs)>
::template f<C, C>
::template f<xs...>;
};
# define JLN_MP_MEMOIZE(...) __VA_ARGS__
#endif
}
// # if !JLN_MP_CUDA
// # endif
/// \cond
#if !JLN_MP_MEMOIZED_ALIAS || JLN_MP_MSVC
namespace jln::mp::detail
{
struct uncallable_function
{
using try_type = na;
};
template<class T>
struct memoize_result
{
using type = T;
using try_type = T;
};
# if !JLN_MP_CUDA
template<class F, class... Params>
memoize_result<
typename conditional_c<!sizeof...(Params)>
::template f<F, F>
::template f<Params...>
> memoized_call(list<Params...>*);
template<class F>
uncallable_function memoized_call(void*);
template<class C, class... Params>
struct memoizer_impl : decltype(memoized_call<C>(static_cast<list<Params...>*>(nullptr)))
{};
#else // if JLN_MP_CUDA
template<class dummy, class F, class... Params>
struct memoizer_impl : uncallable_function
{};
template<class> using void_t = void;
template<class F, class... Params>
struct memoizer_impl<void_t<typename F::template f<Params...>>, F, Params...>
: memoize_result<typename F::template f<Params...>>
{};
# endif
}
#else // if JLN_MP_MEMOIZED_ALIAS && !JLN_MP_MSVC
namespace jln::mp::detail
{
struct uncallable_function
{};
}
#endif
/// \endcond
namespace jln::mp
{
/// \cond
namespace detail
{
template<bool> struct dcallf;
template<bool> struct dcall_c;
template<bool> struct dcallf_c;
class too_many_arguments_error {};
}
/// \endcond
/// \ingroup functional
#define JLN_MP_IDENT(...) __VA_ARGS__
#if ! JLN_MP_ENABLE_DEBUG
#define JLN_MP_TRACE_F(...) __VA_ARGS__
#define JLN_MP_TRACE_TYPENAME
#define JLN_MP_CALL_TRACE(C, ...) typename C::template f<__VA_ARGS__>
#define JLN_MP_CALL_TRACE_T(C, ...) typename C::template f<__VA_ARGS__>
#define JLN_MP_CALL_TRACE_0_ARG(...) typename __VA_ARGS__::template f<>
#else
// does not compile without memoize with msvc...
#define JLN_MP_TRACE_F(...) memoize<__VA_ARGS__>
#define JLN_MP_TRACE_TYPENAME typename
#if JLN_MP_CUDA
#define JLN_MP_CALL_TRACE(C, ...) \
typename ::jln::mp::detail::memoizer_impl<void, C, __VA_ARGS__>::type
#define JLN_MP_CALL_TRACE_T(C, ...) \
typename ::jln::mp::detail::memoizer_impl<void, typename C, __VA_ARGS__>::type
#define JLN_MP_CALL_TRACE_0_ARG(...) \
typename ::jln::mp::detail::memoizer_impl<void, __VA_ARGS__>::type
#else // if !JLN_MP_CUDA
#define JLN_MP_CALL_TRACE(C, ...) \
typename ::jln::mp::detail::memoizer_impl<C, __VA_ARGS__>::type
#define JLN_MP_CALL_TRACE_T(C, ...) \
typename ::jln::mp::detail::memoizer_impl<typename C, __VA_ARGS__>::type
#define JLN_MP_CALL_TRACE_0_ARG(...) \
typename ::jln::mp::detail::memoizer_impl<__VA_ARGS__>::type
#endif
#endif
#if JLN_MP_CLANG_LIKE
# define JLN_MP_MEMOIZE_CALL(C, ...) ::jln::mp::memoize_call<C, __VA_ARGS__>
# define JLN_MP_MEMOIZE_CALL_F(C, ...) ::jln::mp::memoize_call<C, __VA_ARGS__>
#else
# define JLN_MP_MEMOIZE_CALL(C, ...) typename JLN_MP_TRACE_F(C)::template f<__VA_ARGS__>
# define JLN_MP_MEMOIZE_CALL_F(C, ...) JLN_MP_TRACE_F(C)::template f<__VA_ARGS__>
#endif
#ifdef JLN_MP_DOXYGENATING
template<class C, class... xs>
using call = C::f<xs...>;
#define JLN_MP_DCALL_TRACE_XS(xs, C, ...) call<C, __VA_ARGS__>
#define JLN_MP_DCALL_TRACE_XS_0(xs, C) call<__VA_ARGS__>
#define JLN_MP_DCALL_V_TRACE_XS(xs, C, ...) call<C, __VA_ARGS__>
#define JLN_MP_FORCE_DCALL_TRACE_XS(xs, C, ...) call<C, __VA_ARGS__>
#define JLN_MP_FORCE_DCALL_V_TRACE_XS(xs, C, ...) call<C, __VA_ARGS__>
#define JLN_MP_DCALLF_XS(xs, F, ...) F<__VA_ARGS__>
#define JLN_MP_DCALLF_V_XS(xs, F, ...) F<__VA_ARGS__>
#define JLN_MP_DCALLF_C_XS(xs, F, ...) F<__VA_ARGS__>
#else
#define JLN_MP_DCALLF_XS(xs, C, ...) \
typename detail::dcallf<sizeof...(xs) < JLN_MP_MAX_CALL_ELEMENT> \
::template f<C, __VA_ARGS__>
#define JLN_MP_DCALLF_XS_0(xs, C) \
typename detail::dcallf<sizeof...(xs) < JLN_MP_MAX_CALL_ELEMENT> \
::template f<C>
#define JLN_MP_DCALLF_V_XS(xs, C, ...) \
detail::dcallf<sizeof...(xs) < JLN_MP_MAX_CALL_ELEMENT> \
::template f<C, __VA_ARGS__>
#define JLN_MP_DCALLF_C_XS(xs, C, ...) \
typename detail::dcallf_c<sizeof...(xs) < JLN_MP_MAX_CALL_ELEMENT> \
::template f<C, __VA_ARGS__>
# if JLN_MP_MSVC
template<class C, class... xs>
using call = typename detail::memoizer_impl<C, xs...>::type;
# define JLN_MP_DCALL_TRACE_XS(xs, C, ...) \
typename ::jln::mp::detail::memoizer_impl<C, __VA_ARGS__>::type
# define JLN_MP_FORCE_DCALL_TRACE_XS(xs, C, ...) \
typename JLN_MP_FORCE_DCALL_V_TRACE_XS(xs, C, __VA_ARGS__)
# define JLN_MP_DCALL_TRACE_XS_0(xs, C) \
typename ::jln::mp::detail::memoizer_impl< \
typename ::jln::mp::conditional_c< \
sizeof...(xs) < JLN_MP_MAX_CALL_ELEMENT> \
::template f<C, C> \
>::type
# define JLN_MP_DCALL_V_TRACE_XS(xs, C, ...) \
::jln::mp::detail::memoizer_impl<C, __VA_ARGS__>::type
# define JLN_MP_FORCE_DCALL_V_TRACE_XS(xs, C, ...) \
::jln::mp::detail::memoizer_impl< \
typename ::jln::mp::conditional_c< \
sizeof...(xs) < JLN_MP_MAX_CALL_ELEMENT> \
::template f<C, C>, \
__VA_ARGS__ \
>::type
# else
template<class C, class... xs>
using call = typename conditional_c<sizeof...(xs) < JLN_MP_MAX_CALL_ELEMENT>
::template f<C, detail::too_many_arguments_error>
::template f<xs...>;
# define JLN_MP_DCALL_TRACE_XS(xs, C, ...) \
typename ::jln::mp::conditional_c<sizeof...(xs) < JLN_MP_MAX_CALL_ELEMENT> \
::template f<JLN_MP_TRACE_F(C), ::jln::mp::detail::too_many_arguments_error> \
::template f<__VA_ARGS__>
# define JLN_MP_FORCE_DCALL_TRACE_XS JLN_MP_DCALL_TRACE_XS
# define JLN_MP_DCALL_TRACE_XS_0(xs, C) \
typename ::jln::mp::conditional_c<sizeof...(xs) < JLN_MP_MAX_CALL_ELEMENT> \
::template f<JLN_MP_TRACE_F(C), ::jln::mp::detail::too_many_arguments_error> \
::template f<>
# define JLN_MP_DCALL_V_TRACE_XS(xs, C, ...) \
::jln::mp::conditional_c<sizeof...(xs) < JLN_MP_MAX_CALL_ELEMENT> \
::template f<JLN_MP_TRACE_F(C), ::jln::mp::detail::too_many_arguments_error> \
::template f<__VA_ARGS__>
# define JLN_MP_FORCE_DCALL_V_TRACE_XS JLN_MP_DCALL_V_TRACE_XS
# endif
#endif
#define JLN_MP_CALLER_XS(xs, ...) \
::jln::mp::conditional_c<sizeof...(xs) < JLN_MP_MAX_CALL_ELEMENT> \
::template f<__VA_ARGS__, ::jln::mp::detail::too_many_arguments_error>
#if JLN_MP_MSVC
# define JLN_MP_MSVC_FIX_CALL(C, ...) \
::jln::mp::detail::raw_call<typename JLN_MP_IDENT C, __VA_ARGS__>
# define JLN_MP_MSVC_FIX_CALL_T(C, ...) \
::jln::mp::detail::raw_call<JLN_MP_IDENT C, __VA_ARGS__>
#else
# define JLN_MP_MSVC_FIX_CALL(C, ...) typename JLN_MP_IDENT C::template f<__VA_ARGS__>
# define JLN_MP_MSVC_FIX_CALL_T(C, ...) typename JLN_MP_IDENT C::template f<__VA_ARGS__>
#endif
template<class C, JLN_MP_TPL_AUTO_OR_INT... xs>
using call_c = typename detail::dcall_c<sizeof...(xs) < JLN_MP_MAX_CALL_ELEMENT>
::template f<C, xs...>;
template<class C, class... xs>
using call_t = typename call<C, xs...>::type;
}
/// \cond
namespace jln::mp::detail
{
template<>
struct dcall_c<true>
{
template<class C, JLN_MP_TPL_AUTO_OR_INT...xs>
using f = typename C::template f<xs...>;
};
template<>
struct dcallf<true>
{
template<template<class...> class F, class... xs>
using f = F<xs...>;
};
template<>
struct dcallf_c<true>
{
template<template<JLN_MP_TPL_AUTO_OR_INT...> class F, JLN_MP_TPL_AUTO_OR_INT...xs>
using f = F<xs...>;
};
// useful to work around msvc bugs
template<class F, class... xs>
using raw_call = typename F::template f<xs...>;
template<class C>
struct call_trace_xs_0
{
template<class... xs>
using f = JLN_MP_DCALL_TRACE_XS_0(xs, C);
};
template<class C>
struct call_trace_xs
{
template<class... xs>
using f = JLN_MP_DCALL_TRACE_XS(xs, C, xs...);
};
struct call_trace_c0_arg
{
template<class C, class...>
using f = JLN_MP_CALL_TRACE_0_ARG(C);
};
}
/// \endcond
namespace jln::mp
{
/// \ingroup functional
/// Makes a \function from a \lazymetafunction.
/// \treturn \value
/// \see lift
template<template<class...> class F, class C = identity>
struct lift_t
{
template<class... xs>
using f = JLN_MP_CALL_TRACE(C, JLN_MP_DCALLF_XS(xs, F, xs...)::type);
};
/// Makes a \function from a \metafunction.
/// \treturn \value
/// \see lift_t
template<template<class...> class F, class C = identity>
struct lift
{
template<class... xs>
using f = JLN_MP_CALL_TRACE(C, JLN_MP_DCALLF_XS(xs, F, xs...));
};
/// \cond
template<template<class...> class F>
struct lift_t<F, identity>
{
template<class... xs>
using f = JLN_MP_DCALLF_XS(xs, F, xs...)::type;
};
template<template<class...> class F>
struct lift<F, identity>
{
template<class... xs>
using f = JLN_MP_DCALLF_XS(xs, F, xs...);
};
/// \endcond
/// Makes a \function from a \lazymetafunction.
/// \treturn \value
/// \see lift
template<template<JLN_MP_TPL_AUTO_OR_INT...> class F, class C = identity>
struct lift_v_t
{
template<class... xs>
using f = JLN_MP_CALL_TRACE(C, JLN_MP_DCALLF_C_XS(xs, F, xs::value...)::type);
};
/// Makes a \function from a \metafunction.
/// \treturn \value
/// \see lift_t
template<template<JLN_MP_TPL_AUTO_OR_INT...> class F, class C = identity>
struct lift_v
{
template<class... xs>
using f = JLN_MP_CALL_TRACE(C, JLN_MP_DCALLF_C_XS(xs, F, xs::value...));
};
/// \cond
template<template<JLN_MP_TPL_AUTO_OR_INT...> class F>
struct lift_v_t<F, identity>
{
template<class... xs>
using f = JLN_MP_DCALLF_C_XS(xs, F, xs::value...)::type;
};
template<template<JLN_MP_TPL_AUTO_OR_INT...> class F>
struct lift_v<F, identity>
{
template<class... xs>
using f = JLN_MP_DCALLF_C_XS(xs, F, xs::value...);
};
/// Makes a \function from a \lazymetafunction.
/// \treturn \value
/// \see lift_t
template<template<JLN_MP_TPL_AUTO_OR_INT...> class F, class C = identity>
struct lift_v_c_t
{
template<JLN_MP_TPL_AUTO_OR_INT... xs>
using f = JLN_MP_CALL_TRACE(C, JLN_MP_DCALLF_C_XS(xs, F, xs...)::type);
};
/// Makes a \function from a \metafunction.
/// \treturn \value
/// \see lift_t
template<template<JLN_MP_TPL_AUTO_OR_INT...> class F, class C = identity>
struct lift_v_c
{
template<JLN_MP_TPL_AUTO_OR_INT... xs>
using f = JLN_MP_CALL_TRACE(C, JLN_MP_DCALLF_C_XS(xs, F, xs...));
};
/// \cond
template<template<JLN_MP_TPL_AUTO_OR_INT...> class F>
struct lift_v_c_t<F, identity>
{
template<JLN_MP_TPL_AUTO_OR_INT... xs>
using f = JLN_MP_DCALLF_C_XS(xs, F, xs...)::type;
};
template<template<JLN_MP_TPL_AUTO_OR_INT...> class F>
struct lift_v_c<F, identity>
{
template<JLN_MP_TPL_AUTO_OR_INT... xs>
using f = JLN_MP_DCALLF_C_XS(xs, F, xs...);
};
/// \endcond
#if JLN_MP_GCC
# define JLN_MP_LIFT_WRAP_IMPL(fn, fn_t) ::jln::mp::lift_t<fn>
#else
# define JLN_MP_LIFT_WRAP_IMPL(fn, fn_t) ::jln::mp::lift<fn_t>
#endif
#define JLN_MP_LIFT_WRAP(fn) JLN_MP_LIFT_WRAP_IMPL(fn, fn##_t)
/// \endcond
/// \ingroup list
/// \treturn \link list
using listify = lift<list>;
/// \cond
template<>
struct lift<list, identity>
{
template<class... xs>
using f = list<xs...>;
};
/// \endcond
/// \cond
namespace detail
{
template<class C, class L, class... xs> struct _unpack {};
template<class C, class L, class... xs> struct _unpack_append {};
#if JLN_MP_ENABLE_TPL_AUTO
template<class C, class L, class... xs> struct _unpack_v {};
template<class C, class L, class... xs> struct _unpack_append_v {};
#endif
}
/// \endcond
/// \ingroup utility
/// Turns a \typelist into a \sequence of those types.
/// \semantics
/// \code
/// unpack<F>::f<typelist<xs...>, ys...> == F::f<ys..., xs...>
/// \endcode
/// \treturn \sequence
template<class C>
struct unpack
{
template<class seq, class... xs>
using f = typename detail::_unpack<C, seq, xs...>::type;
};
/// Turns a \typelist into a \sequence of those types.
/// \semantics
/// \code
/// unpack_append<F>::f<typelist<xs...>, ys...> == F::f<xs..., ys...>
/// \endcode
/// \treturn \sequence
template<class C>
struct unpack_append
{
template<class seq, class... xs>
using f = typename detail::_unpack_append<C, seq, xs...>::type;
};
#if JLN_MP_ENABLE_TPL_AUTO
/// Turns a \typelist into a \sequence of those types.
/// \semantics
/// \code
/// unpack<F>::f<valuelist<xs...>, ys...> == F::f<ys..., xs...>
/// \endcode
/// \treturn \sequence
template<class C>
struct unpack_v
{
template<class seq, class... xs>
using f = typename detail::_unpack_v<C, seq, xs...>::type;
};
/// Turns a \typelist into a \sequence of those types.
/// \semantics
/// \code
/// unpack_append<F>::f<valuelist<xs...>, ys...> == F::f<xs..., ys...>
/// \endcode
/// \treturn \sequence
template<class C>
struct unpack_append_v
{
template<class seq, class... xs>
using f = typename detail::_unpack_append_v<C, seq, xs...>::type;
};
#endif
namespace emp
{
template<class L, class C, class... xs>
using unpack = typename detail::_unpack<C, L, xs...>::type;
template<class L, class C, class... xs>
using unpack_append = typename detail::_unpack_append<C, L, xs...>::type;
template<class L, class C, int_... xs>
using unpack_c = typename detail::_unpack<C, L, number<xs>...>::type;
template<class L, class C, int_... xs>
using unpack_append_c = typename detail::_unpack_append<C, L, number<xs>...>::type;
#if JLN_MP_ENABLE_TPL_AUTO
template<class L, class C, class... xs>
using unpack_v = typename detail::_unpack_v<C, L, xs...>::type;
template<class L, class C, class... xs>
using unpack_append_v = typename detail::_unpack_append_v<C, L, xs...>::type;
template<class L, class C, int_... xs>
using unpack_c_v = typename detail::_unpack_v<C, L, number<xs>...>::type;
template<class L, class C, int_... xs>
using unpack_append_c_v = typename detail::_unpack_append_v<C, L, number<xs>...>::type;
#endif
}
} // namespace jln::mp
/// \cond
namespace jln::mp::detail
{
template<class C, template<class...> class Seq, class... ys, class... xs>
struct _unpack<C, Seq<ys...>, xs...>
{
using type = typename C::template f<xs..., ys...>;
};