-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcore.hh
1357 lines (1011 loc) · 33.7 KB
/
core.hh
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
// Copyright (c) 2022 Mikael Simonsson <https://mikaelsimonsson.com>.
// SPDX-License-Identifier: BSL-1.0
// # Core functionality
#pragma once
#include <cassert> // assert
#include <compare> // common_comparison_category, *_ordering, ...
#include <concepts> // same_as
#include <cstddef> // nullptr_t, ptrdiff_t, size_t
#include <cstdint> // int*_t, uint*_t, *_MAX, ...
#include <initializer_list> // initializer_list
#include <iterator> // begin, end, iterator_traits, *_iterator_tag
#include <limits> // numeric_limits
#include <tuple> // ignore, tuple, tuple_element, tuple_size
#include <type_traits> // is_*, make_*signed, ...
#include <utility> // as_const, declval, exchange, forward, get, move, pair, swap
// ## Requirements
// ### C++20
static_assert(__cplusplus >= 202002L, "C++20 support is required.");
// ### Standard 64-bit
static_assert(__CHAR_BIT__ == 8, "char must be 8-bit.");
static_assert(sizeof(void*) == 8, "Pointer size must be 8 bytes (64-bit).");
static_assert(sizeof(std::size_t) == 8, "std::size_t must be 8 bytes (64-bit).");
static_assert(std::is_same_v<std::uint8_t, unsigned char>, "std::uint8_t must be unsigned char.");
// ### Little-endian
#if !defined(__BYTE_ORDER__) || !defined(__ORDER_LITTLE_ENDIAN__)
#error "Both __BYTE_ORDER__ and __ORDER_LITTLE_ENDIAN__ must be defined."
#elif __BYTE_ORDER__ != __ORDER_LITTLE_ENDIAN__
#error "Little-endian required."
#endif
// ## Macros
// ### SNN_ADDRESS_SANITIZER_BOOL
// https://clang.llvm.org/docs/AddressSanitizer.html
// #conditional-compilation-with-has-feature-address-sanitizer
#if defined(__has_feature)
#if __has_feature(address_sanitizer)
#define SNN_ADDRESS_SANITIZER_BOOL true
#endif
// https://gcc.gnu.org/onlinedocs/cpp/Common-Predefined-Macros.html
#elif defined(__SANITIZE_ADDRESS__)
#if __SANITIZE_ADDRESS__
#define SNN_ADDRESS_SANITIZER_BOOL true
#endif
#endif
#if !defined(SNN_ADDRESS_SANITIZER_BOOL)
#define SNN_ADDRESS_SANITIZER_BOOL false
#endif
// ### snn_assert & snn_should[_if_not_fuzzing]
// Including constants SNN_ASSERT_BOOL and SNN_SHOULD_BOOL.
#if defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION)
#if defined(__OPTIMIZE__)
#define snn_assert(e) (__builtin_expect(!!(e), 1) ? (void)0 : __builtin_trap())
#else
#define snn_assert(e) assert(e)
#endif
#define SNN_ASSERT_BOOL true
#define snn_should(e) snn_assert(e)
#define SNN_SHOULD_BOOL true
#define snn_should_if_not_fuzzing(e) ((void)0)
#else // !defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION)
#if defined(NDEBUG)
#define snn_assert(e) ((void)0)
#define SNN_ASSERT_BOOL false
#elif defined(__OPTIMIZE__)
#define snn_assert(e) (__builtin_expect(!!(e), 1) ? (void)0 : __builtin_trap())
#define SNN_ASSERT_BOOL true
#else
#define snn_assert(e) assert(e)
#define SNN_ASSERT_BOOL true
#endif
#if defined(NDEBUG) || defined(__OPTIMIZE__)
#define snn_should(e) ((void)0)
#define SNN_SHOULD_BOOL false
#else
#define snn_should(e) assert(e)
#define SNN_SHOULD_BOOL true
#endif
#define snn_should_if_not_fuzzing(e) snn_should(e)
#endif
// ### SNN_DIAGNOSTIC_[...]
#if defined(__clang__) && defined(__clang_major__) && __clang_major__ >= 16
#define SNN_DIAGNOSTIC_PUSH _Pragma("clang diagnostic push")
#define SNN_DIAGNOSTIC_POP _Pragma("clang diagnostic pop")
#define SNN_DIAGNOSTIC_IGNORE_UNSAFE_BUFFER_USAGE \
_Pragma("clang diagnostic ignored \"-Wunsafe-buffer-usage\"")
#else
#define SNN_DIAGNOSTIC_PUSH
#define SNN_DIAGNOSTIC_POP
#define SNN_DIAGNOSTIC_IGNORE_UNSAFE_BUFFER_USAGE
#endif
// ### SNN_INT128_BOOL
#if defined(__SIZEOF_INT128__) && defined(_LIBCPP_VERSION)
#define SNN_INT128_BOOL true
#else
// In GNU C++ Standard Library (libstdc++) `std::is_integral_v<__int128_t>` is `false`.
#define SNN_INT128_BOOL false
#endif
namespace snn
{
// ## Types
using usize = std::size_t;
using isize = std::int64_t;
using uptr = std::uintptr_t;
using iptr = std::intptr_t;
using iptrdiff = std::ptrdiff_t;
using u64 = std::uint64_t;
using u32 = std::uint32_t;
using u16 = std::uint16_t;
using u8 = std::uint8_t;
using u32fast = std::uint_fast32_t;
using u16fast = std::uint_fast16_t;
using u8fast = std::uint_fast8_t;
using i64 = std::int64_t;
using i32 = std::int32_t;
using i16 = std::int16_t;
using i8 = std::int8_t;
using i32fast = std::int_fast32_t;
using i16fast = std::int_fast16_t;
using i8fast = std::int_fast8_t;
using byte = std::uint8_t;
#if SNN_INT128_BOOL
using u128 = __uint128_t;
using i128 = __int128_t;
#endif
// ## Promises
namespace promise
{
struct has_capacity_t final
{
explicit has_capacity_t() = default;
};
inline constexpr has_capacity_t has_capacity;
struct has_value_t final
{
explicit has_value_t() = default;
};
inline constexpr has_value_t has_value;
struct is_sorted_t final
{
explicit is_sorted_t() = default;
};
inline constexpr is_sorted_t is_sorted;
struct is_utf8_t final
{
explicit is_utf8_t() = default;
};
inline constexpr is_utf8_t is_utf8;
struct is_valid_t final
{
explicit is_valid_t() = default;
};
inline constexpr is_valid_t is_valid;
struct no_overlap_t final
{
explicit no_overlap_t() = default;
};
inline constexpr no_overlap_t no_overlap;
struct not_empty_t final
{
explicit not_empty_t() = default;
};
inline constexpr not_empty_t not_empty;
struct null_terminated_t final
{
explicit null_terminated_t() = default;
};
inline constexpr null_terminated_t null_terminated;
struct within_bounds_t final
{
explicit within_bounds_t() = default;
};
inline constexpr within_bounds_t within_bounds;
}
// ## Meta helpers & tags
namespace meta
{
// ### Tags
// #### inplace
using inplace_t = std::in_place_t;
inline constexpr inplace_t inplace;
// #### internal
struct internal_t final
{
explicit internal_t() = default;
};
inline constexpr internal_t internal;
// #### iterators
struct iterators_t final
{
explicit iterators_t() = default;
};
inline constexpr iterators_t iterators;
// ### Validation
// #### all
template <template <typename> typename Is, typename... Ts>
struct all : public std::bool_constant<(Is<Ts>::value && ...)>
{
};
template <template <typename> typename Is, typename... Ts>
inline constexpr bool all_v = all<Is, Ts...>::value;
// #### any
template <template <typename> typename Is, typename... Ts>
struct any : public std::bool_constant<(Is<Ts>::value || ...)>
{
};
template <template <typename> typename Is, typename... Ts>
inline constexpr bool any_v = any<Is, Ts...>::value;
// #### none
template <template <typename> typename Is, typename... Ts>
struct none : public std::bool_constant<(!Is<Ts>::value && ...)>
{
};
template <template <typename> typename Is, typename... Ts>
inline constexpr bool none_v = none<Is, Ts...>::value;
// ### Miscellaneous
// #### always_false
template <typename>
inline constexpr bool always_false = false;
// #### index
template <usize Index>
using index_t = std::integral_constant<usize, Index>;
template <usize Index>
inline constexpr index_t<Index> index;
// #### type
template <typename T>
using type_t = std::type_identity<T>;
template <typename T>
inline constexpr type_t<T> type;
}
// ## Bounds tags
namespace bounds
{
// ### mask
struct mask_t final
{
explicit mask_t() = default;
};
inline constexpr mask_t mask;
}
// ## Container tags
namespace container
{
struct do_not_initialize_t final
{
explicit do_not_initialize_t() = default;
};
inline constexpr do_not_initialize_t do_not_initialize;
struct fill_t final
{
explicit fill_t() = default;
};
inline constexpr fill_t fill;
struct reserve_t final
{
explicit reserve_t() = default;
};
inline constexpr reserve_t reserve;
}
// ## Type traits
// ### is_strict_integral
// "strict_integral" requirements:
// * The type must be a power-of-two integral.
// * The type must use all `sizeof(T) * __CHAR_BIT__` bits.
// * The type must support `std::make_signed`.
// * The type must support `std::make_unsigned`.
// * The type must support `std::numeric_limits`.
// Notable types that are not included are `bool` and `_BitInt(N)`.
namespace detail
{
template <typename T>
struct is_strict_integral_strict : public std::false_type
{
};
template <>
struct is_strict_integral_strict<char> : public std::true_type
{
};
template <>
struct is_strict_integral_strict<signed char> : public std::true_type
{
};
template <>
struct is_strict_integral_strict<unsigned char> : public std::true_type
{
};
template <>
struct is_strict_integral_strict<wchar_t> : public std::true_type
{
};
template <>
struct is_strict_integral_strict<char8_t> : public std::true_type
{
};
template <>
struct is_strict_integral_strict<char16_t> : public std::true_type
{
};
template <>
struct is_strict_integral_strict<char32_t> : public std::true_type
{
};
template <>
struct is_strict_integral_strict<short> : public std::true_type
{
};
template <>
struct is_strict_integral_strict<unsigned short> : public std::true_type
{
};
template <>
struct is_strict_integral_strict<int> : public std::true_type
{
};
template <>
struct is_strict_integral_strict<unsigned int> : public std::true_type
{
};
template <>
struct is_strict_integral_strict<long> : public std::true_type
{
};
template <>
struct is_strict_integral_strict<unsigned long> : public std::true_type
{
};
template <>
struct is_strict_integral_strict<long long> : public std::true_type
{
};
template <>
struct is_strict_integral_strict<unsigned long long> : public std::true_type
{
};
#if SNN_INT128_BOOL
template <>
struct is_strict_integral_strict<i128> : public std::true_type
{
};
template <>
struct is_strict_integral_strict<u128> : public std::true_type
{
};
#endif
}
template <typename T>
struct is_strict_integral : public detail::is_strict_integral_strict<std::remove_cv_t<T>>
{
};
template <typename T>
inline constexpr bool is_strict_integral_v = is_strict_integral<T>::value;
// ## Core concepts
// ### arithmetic
template <typename T>
concept arithmetic = std::is_arithmetic_v<T>;
// ### convertible_to
template <typename From, typename To>
concept convertible_to = std::is_convertible_v<From, To>;
// ### floating_point
template <typename T>
concept floating_point = std::is_floating_point_v<T>;
// ### pointer
template <typename T>
concept pointer = std::is_pointer_v<T>;
// ### same_as
using std::same_as;
// ### sane
// This library assumes that all types are "sane":
// * If a type is copy constructible it must also be move constructible.
// * If a type is copy assignable it must also be move assignable.
// * If a type is move constructible it must also be nothrow move constructible.
// * If a type is move assignable it must also be nothrow move assignable.
// clang-format off
template <typename T>
concept sane = (!std::is_copy_constructible_v<T> || std::is_move_constructible_v<T>) &&
(!std::is_copy_assignable_v<T> || std::is_move_assignable_v<T>) &&
(!std::is_move_constructible_v<T> || std::is_nothrow_move_constructible_v<T>) &&
(!std::is_move_assignable_v<T> || std::is_nothrow_move_assignable_v<T>);
// clang-format on
// ### value_type_or
// `value_type_or<T&>` will accept `T` or `T&`, but not `const T&`.
// `value_type_or<const T&>` will accept `T` or `const T&`, but not `T&`.
template <typename T, typename U>
concept value_type_or = std::is_same_v<T, U> || std::is_same_v<T, std::remove_cvref_t<U>>;
// ## Constructible concepts
// ### brace_constructible_from
template <typename T, typename... Args>
concept brace_constructible_from = requires(Args&&... args) { T{std::forward<Args>(args)...}; };
// ### constructible_from
template <typename T, typename... Args>
concept constructible_from = std::is_constructible_v<T, Args...>;
// ### constructible_from_iterators
template <typename T>
concept constructible_from_iterators = requires(T& v) { //
T{meta::iterators, v.begin(), v.end()};
};
// ### implicitly_default_constructible
namespace detail
{
template <typename T>
void takes_single_value_of(T);
}
template <typename T>
concept implicitly_default_constructible = requires { detail::takes_single_value_of<T>({}); };
// ### explicitly_default_constructible
template <typename T>
concept explicitly_default_constructible =
std::is_default_constructible_v<T> && !implicitly_default_constructible<T>;
// ## Callable concepts
// ### callable
// Callable without the overhead of `std::invoke(...)`.
template <typename Fn, typename... Args>
concept callable = requires(Fn&& fn, Args&&... args) { //
std::forward<Fn>(fn)(std::forward<Args>(args)...);
};
// ### predicate
// Callable that doesn't modify the parameters, with a boolean-testable return value.
template <typename P, typename... Args>
concept predicate = requires(P& p, const Args&... args) {
{ !p(args...) } -> same_as<bool>;
};
// ## Integral concepts
// ### integral
template <typename T>
concept integral = std::is_integral_v<T>;
// ### strict_integral
template <typename T>
concept strict_integral = integral<T> && is_strict_integral_v<T>;
// ### strict_integral_min
template <typename T, usize BitCount>
concept strict_integral_min = strict_integral<T> && (sizeof(T) * __CHAR_BIT__) >= BitCount;
// ### signed_integral
template <typename T>
concept signed_integral = integral<T> && std::is_signed_v<T>;
// ### strict_signed_integral
template <typename T>
concept strict_signed_integral = signed_integral<T> && strict_integral<T>;
// ### unsigned_integral
template <typename T>
concept unsigned_integral = integral<T> && !signed_integral<T>;
// ### strict_unsigned_integral
template <typename T>
concept strict_unsigned_integral = unsigned_integral<T> && strict_integral<T>;
// ### character
#if defined(__CHAR_UNSIGNED__)
template <typename T>
concept character = strict_unsigned_integral<T> && same_as<const T, const char>;
#else
template <typename T>
concept character = strict_signed_integral<T> && same_as<const T, const char>;
#endif
// ### octet
template <typename T>
concept octet = strict_integral<T> && sizeof(T) == 1;
// ### same_signedness_as
template <typename T, typename U>
concept same_signedness_as =
integral<T> && integral<U> && (std::is_signed_v<T> == std::is_signed_v<U>);
// ## Iterable concepts
// ### legacy_iterable
template <typename T>
concept legacy_iterable = requires(T& v) {
{ v.begin() } -> same_as<decltype(v.end())>;
};
// ## Non-type concepts
// ### power_of_two
template <usize N>
concept power_of_two = N > 0 && (N & (N - 1)) == 0;
// ## Has concepts
// ### has_append_inplace
template <typename T, typename... Args>
concept has_append_inplace =
requires(T& v, Args&&... args) { v.append_inplace(std::forward<Args>(args)...); };
// ### has_at
template <typename T>
concept has_at = requires(T& v) { v.at(usize{}, promise::within_bounds); };
// ### has_contiguous_iterator
template <typename T>
concept has_contiguous_iterator = requires(T& v) {
{ v.begin() } -> pointer;
};
// ### has_count
template <typename T>
concept has_count = requires(T& v) {
{ v.count() } -> unsigned_integral;
};
// ### has_drop_back
template <typename T>
concept has_drop_back = requires(T& v) { v.drop_back(promise::not_empty); };
// ### has_drop_front
template <typename T>
concept has_drop_front = requires(T& v) { v.drop_front(promise::not_empty); };
// ### has_to
template <typename From, typename To>
concept has_to = requires(const From& v) { v.template to<To>(); };
// ## Type traits
// ### deep_const
namespace detail
{
template <typename T>
struct deep_const_strict
{
using type = T;
};
template <typename T>
struct deep_const_strict<T&>
{
using type = const T&;
};
template <typename T>
struct deep_const_strict<T&&>
{
using type = const T&&;
};
template <typename T>
struct deep_const_strict<T*>
{
using type = const T*;
};
}
template <typename T>
struct deep_const : public detail::deep_const_strict<std::remove_cv_t<T>>
{
};
template <typename T>
using deep_const_t = typename deep_const<T>::type;
// ### front_value
template <typename T>
struct front_value
{
using type = std::remove_cvref_t<decltype(std::declval<T>().front(promise::not_empty))>;
};
template <typename T>
using front_value_t = typename front_value<T>::type;
// ### is_trivially_relocatable
// A type `T` is trivially relocatable if `std::is_trivially_copyable_v<T>` is true or if it has
// a member type `trivially_relocatable_type` which is `T`.
namespace detail
{
template <typename, typename = void>
struct has_trivially_relocatable_type_member : public std::false_type
{
};
template <typename T>
struct has_trivially_relocatable_type_member<
T, std::void_t<typename T::trivially_relocatable_type>>
: public std::is_same<typename T::trivially_relocatable_type, T>
{
};
template <typename T>
struct is_trivially_relocatable_strict
: public std::bool_constant<std::is_trivially_copyable_v<T> ||
has_trivially_relocatable_type_member<T>::value>
{
};
}
template <typename T>
struct is_trivially_relocatable
: public detail::is_trivially_relocatable_strict<std::remove_cv_t<T>>
{
};
template <typename T>
inline constexpr bool is_trivially_relocatable_v = is_trivially_relocatable<T>::value;
// ### not_deduced
template <typename T>
struct not_deduced
{
using type = T;
};
template <typename T>
using not_deduced_t = typename not_deduced<T>::type;
// ### promote_integral
// Promote integral type to minimum N-bit integral type with the same sign.
namespace detail
{
template <usize BitCount, bool IsSigned>
struct promote_integral_lookup
{
};
template <>
struct promote_integral_lookup<8, true>
{
using type = i8;
};
template <>
struct promote_integral_lookup<8, false>
{
using type = u8;
};
template <>
struct promote_integral_lookup<16, true>
{
using type = i16;
};
template <>
struct promote_integral_lookup<16, false>
{
using type = u16;
};
template <>
struct promote_integral_lookup<32, true>
{
using type = i32;
};
template <>
struct promote_integral_lookup<32, false>
{
using type = u32;
};
template <>
struct promote_integral_lookup<64, true>
{
using type = i64;
};
template <>
struct promote_integral_lookup<64, false>
{
using type = u64;
};
#if SNN_INT128_BOOL
template <>
struct promote_integral_lookup<128, true>
{
using type = i128;
};
template <>
struct promote_integral_lookup<128, false>
{
using type = u128;
};
#endif
template <usize BitCount, bool IsSigned>
using promote_integral_lookup_t =
typename promote_integral_lookup<BitCount, IsSigned>::type;
}
template <integral Int, usize BitCount>
struct promote_integral
{
private:
static constexpr usize int_bit_count_ = sizeof(Int) * __CHAR_BIT__;
static constexpr usize bit_count_ = int_bit_count_ < BitCount ? BitCount : int_bit_count_;
public:
using type = detail::promote_integral_lookup_t<bit_count_, std::is_signed_v<Int>>;
};
template <integral Int, usize BitCount>
using promote_integral_t = typename promote_integral<Int, BitCount>::type;
// ### remove_cv_rvalue_ref
template <typename T>
struct remove_cv_rvalue_ref
{
using type = std::remove_cvref_t<T>;
};
template <typename T>
struct remove_cv_rvalue_ref<T&>
{
using type = T&;
};
template <typename T>
using remove_cv_rvalue_ref_t = typename remove_cv_rvalue_ref<T>::type;
// ### trivially_relocatable_if
// Provides the first type if the types following it are all trivially relocatable,
// or `void` if not.
template <typename T, typename... Ts>
struct trivially_relocatable_if
: public std::conditional<meta::all_v<is_trivially_relocatable, Ts...>, T, void>
{
static_assert(sizeof...(Ts) > 0, "No types to check if trivially relocatable.");
};
template <typename T, typename... Ts>
using trivially_relocatable_if_t = typename trivially_relocatable_if<T, Ts...>::type;
// ## Range concepts
// ### input_range
template <typename T>
concept input_range = has_drop_front<T>;
// ### forward_range
template <typename T>
concept forward_range = input_range<T> && std::is_copy_constructible_v<T>;
// ### bidirectional_range
template <typename T>
concept bidirectional_range = forward_range<T> && has_drop_back<T>;
// ### random_access_range
template <typename T>
concept random_access_range = bidirectional_range<T> && has_at<T>;
// ### contiguous_range
template <typename T>
concept contiguous_range = random_access_range<T> && has_contiguous_iterator<T>;
// ## Initializer list
// Make accessible for unqualified lookup.
using std::initializer_list;
// Alias (remember that alias templates are currently not deduced by Clang).
template <typename T>
using init_list = initializer_list<T>;
// ## Constants
namespace constant
{
// ### General
inline constexpr usize bits_per_byte = __CHAR_BIT__;
inline constexpr usize dynamic_count = SIZE_MAX;
inline constexpr usize npos = SIZE_MAX;
template <typename T>
inline constexpr T value_initialized{};
// ### Exit status
namespace exit
{
inline constexpr int failure = 1;
inline constexpr int success = 0;
}
// ### Integral limit
template <integral Int>
struct limit final
{
static constexpr Int min = std::numeric_limits<Int>::min();
static constexpr Int max = std::numeric_limits<Int>::max();
};
// ### Floating point limit
namespace fp
{
template <floating_point Fp>
struct limit final
{
static constexpr Fp min_negative = std::numeric_limits<Fp>::lowest();
static constexpr Fp min_positive = std::numeric_limits<Fp>::min();
static constexpr Fp max = std::numeric_limits<Fp>::max();
static constexpr Fp infinity = std::numeric_limits<Fp>::infinity();
static constexpr Fp nan = std::numeric_limits<Fp>::quiet_NaN();
};
}
// ### Byte sizes
namespace size
{
// #### Metric
template <strict_integral_min<16> Int>
inline constexpr Int kilobyte = 1'000; // kB
template <strict_integral_min<32> Int>
inline constexpr Int megabyte = 1'000'000; // MB
template <strict_integral_min<32> Int>
inline constexpr Int gigabyte = 1'000'000'000; // GB
template <strict_integral_min<64> Int>
inline constexpr Int terabyte = 1'000'000'000'000; // TB
template <strict_integral_min<64> Int>