-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathflux.hpp
14595 lines (11709 loc) · 444 KB
/
flux.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
// Copyright (c) 2022 Tristan Brindle (tcbrindle at gmail dot com)
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#ifndef FLUX_HPP_INCLUDED
#define FLUX_HPP_INCLUDED
// Copyright (c) 2024 Tristan Brindle (tcbrindle at gmail dot com)
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#ifndef FLUX_ADAPTOR_HPP_INCLUDED
#define FLUX_ADAPTOR_HPP_INCLUDED
// Copyright (c) 2023 Tristan Brindle (tcbrindle at gmail dot com)
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#ifndef FLUX_ADAPTOR_ADJACENT_HPP_INCLUDED
#define FLUX_ADAPTOR_ADJACENT_HPP_INCLUDED
// Copyright (c) 2022 Tristan Brindle (tcbrindle at gmail dot com)
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#ifndef FLUX_CORE_HPP_INCLUDED
#define FLUX_CORE_HPP_INCLUDED
// Copyright (c) 2022 Tristan Brindle (tcbrindle at gmail dot com)
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#ifndef FLUX_CORE_CONCEPTS_HPP_INCLUDED
#define FLUX_CORE_CONCEPTS_HPP_INCLUDED
// Copyright (c) 2023 Tristan Brindle (tcbrindle at gmail dot com)
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#ifndef FLUX_CORE_UTILS_HPP_INCLUDED
#define FLUX_CORE_UTILS_HPP_INCLUDED
#include <compare>
#include <concepts>
#include <type_traits>
// Copyright (c) 2023 Tristan Brindle (tcbrindle at gmail dot com)
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#ifndef FLUX_CORE_ASSERT_HPP_INCLUDED
#define FLUX_CORE_ASSERT_HPP_INCLUDED
// Copyright (c) 2023 Tristan Brindle (tcbrindle at gmail dot com)
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#ifndef FLUX_CORE_CONFIG_HPP_INCLUDED
#define FLUX_CORE_CONFIG_HPP_INCLUDED
// Copyright (c) 2022 Tristan Brindle (tcbrindle at gmail dot com)
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#ifndef FLUX_MACROS_HPP_INCLUDED
#define FLUX_MACROS_HPP_INCLUDED
#include <version>
#define FLUX_VERSION_MAJOR 0
#define FLUX_VERSION_MINOR 4
#define FLUX_VERSION_PATCH 0
#define FLUX_VERSION_DEVEL 1 // 0 => Release, 1 => development post Major.Minor.Patch
#define FLUX_VERSION \
(FLUX_VERSION_MAJOR * 100'000 \
+ FLUX_VERSION_MINOR * 1'000 \
+ FLUX_VERSION_PATCH * 10 \
+ FLUX_VERSION_DEVEL)
#define FLUX_FWD(x) static_cast<decltype(x)&&>(x)
#define FLUX_DECLVAL(...) ((static_cast<__VA_ARGS__(*)()noexcept>(nullptr))())
#if defined(__GNUC__)
# define FLUX_ALWAYS_INLINE [[gnu::always_inline]] inline
#elif defined(_MSC_VER)
# define FLUX_ALWAYS_INLINE __forceinline
#else
# define FLUX_ALWAYS_INLINE inline
#endif
#define FLUX_NO_UNIQUE_ADDRESS [[no_unique_address]]
#define FLUX_FOR(_flux_var_decl_, ...) \
if (auto&& _flux_seq_ = __VA_ARGS__; true) \
for (auto _flux_cur_ = ::flux::first(_flux_seq_); \
!::flux::is_last(_flux_seq_, _flux_cur_); \
::flux::inc(_flux_seq_, _flux_cur_)) \
if (_flux_var_decl_ = ::flux::read_at(_flux_seq_, _flux_cur_); true)
#define FLUX_ASSERT(cond) (::flux::assert_(cond, "assertion '" #cond "' failed"))
#define FLUX_DEBUG_ASSERT(cond) (::flux::assert_(!::flux::config::enable_debug_asserts || (cond), "assertion '" #cond "' failed"));
#ifdef FLUX_MODULE_INTERFACE
#define FLUX_EXPORT export
#else
#define FLUX_EXPORT
#endif
#endif // FLUX_MACROS_HPP_INCLUDED
#include <concepts>
#include <cstddef>
#include <type_traits>
#define FLUX_ERROR_POLICY_TERMINATE 1
#define FLUX_ERROR_POLICY_UNWIND 2
#define FLUX_ERROR_POLICY_FAIL_FAST 3
#define FLUX_OVERFLOW_POLICY_ERROR 10
#define FLUX_OVERFLOW_POLICY_WRAP 11
#define FLUX_OVERFLOW_POLICY_IGNORE 12
#define FLUX_DIVIDE_BY_ZERO_POLICY_ERROR 100
#define FLUX_DIVIDE_BY_ZERO_POLICY_IGNORE 101
#define FLUX_INTEGER_CAST_POLICY_CHECKED 1001
#define FLUX_INTEGER_CAST_POLICY_UNCHECKED 1002
// Default error policy is terminate
#define FLUX_DEFAULT_ERROR_POLICY FLUX_ERROR_POLICY_TERMINATE
// Default overflow policy is error in debug builds, wrap in release builds
#ifdef NDEBUG
# define FLUX_DEFAULT_OVERFLOW_POLICY FLUX_OVERFLOW_POLICY_WRAP
#else
# define FLUX_DEFAULT_OVERFLOW_POLICY FLUX_OVERFLOW_POLICY_ERROR
#endif // NDEBUG
// Default divide by zero policy is error in debug builds, ignore in release builds
#ifdef NDEBUG
# define FLUX_DEFAULT_DIVIDE_BY_ZERO_POLICY FLUX_DIVIDE_BY_ZERO_POLICY_IGNORE
#else
# define FLUX_DEFAULT_DIVIDE_BY_ZERO_POLICY FLUX_DIVIDE_BY_ZERO_POLICY_ERROR
#endif // NDEBUG
// Select which error policy to use
#if defined(FLUX_TERMINATE_ON_ERROR)
# define FLUX_ERROR_POLICY FLUX_ERROR_POLICY_TERMINATE
#elif defined(FLUX_UNWIND_ON_ERROR)
# define FLUX_ERROR_POLICY FLUX_ERROR_POLICY_UNWIND
#elif defined(FLUX_FAIL_FAST_ON_ERROR)
# define FLUX_ERROR_POLICY FLUX_ERROR_POLICY_FAIL_FAST
#else
# define FLUX_ERROR_POLICY FLUX_DEFAULT_ERROR_POLICY
#endif // FLUX_TERMINATE_ON_ERROR
// Default integer cast policy is checked in debug builds, unchecked in release builds
#ifdef NDEBUG
# define FLUX_DEFAULT_INTEGER_CAST_POLICY FLUX_INTEGER_CAST_POLICY_UNCHECKED
#else
# define FLUX_DEFAULT_INTEGER_CAST_POLICY FLUX_INTEGER_CAST_POLICY_CHECKED
#endif // NDEBUG
// Should we print an error message before terminating?
#ifndef FLUX_PRINT_ERROR_ON_TERMINATE
# define FLUX_PRINT_ERROR_ON_TERMINATE 1
#endif // FLUX_PRINT_ERROR_ON_TERMINATE
// Should we test debug assertions?
#ifndef FLUX_ENABLE_DEBUG_ASSERTS
# ifdef NDEBUG
# define FLUX_ENABLE_DEBUG_ASSERTS 0
# else
# define FLUX_ENABLE_DEBUG_ASSERTS 1
# endif
#endif
// Select which overflow policy to use
#if defined(FLUX_ERROR_ON_OVERFLOW)
# define FLUX_OVERFLOW_POLICY FLUX_OVERFLOW_POLICY_ERROR
#elif defined(FLUX_WRAP_ON_OVERFLOW)
# define FLUX_OVERFLOW_POLICY FLUX_OVERFLOW_POLICY_WRAP
#elif defined(FLUX_IGNORE_OVERFLOW)
# define FLUX_OVERFLOW_POLICY FLUX_OVERFLOW_POLICY_IGNORE
#else
# define FLUX_OVERFLOW_POLICY FLUX_DEFAULT_OVERFLOW_POLICY
#endif // FLUX_ERROR_ON_OVERFLOW
// Select which divide by zero policy to use
#if defined(FLUX_ERROR_ON_DIVIDE_BY_ZERO)
# define FLUX_DIVIDE_BY_ZERO_POLICY FLUX_DIVIDE_BY_ZERO_POLICY_ERROR
#elif defined(FLUX_IGNORE_DIVIDE_BY_ZERO)
# define FLUX_DIVIDE_BY_ZERO_POLICY FLUX_DIVIDE_BY_ZERO_POLICY_IGNORE
#else
# define FLUX_DIVIDE_BY_ZERO_POLICY FLUX_DEFAULT_DIVIDE_BY_ZERO_POLICY
#endif // FLUX_ERROR_ON_DIVIDE_BY_ZERO
// Select which integer cast policy to use
#if defined(FLUX_CHECKED_INTEGER_CASTS)
# define FLUX_INTEGER_CAST_POLICY FLUX_INTEGER_CAST_POLICY_CHECKED
#elif defined(FLUX_UNCHECKED_INTEGER_CASTS)
# define FLUX_INTEGER_CAST_POLICY FLUX_INTEGER_CAST_POLICY_UNCHECKED
#else
# define FLUX_INTEGER_CAST_POLICY FLUX_DEFAULT_INTEGER_CAST_POLICY
#endif
// Should we try to use static bounds checking?
#if !defined(FLUX_DISABLE_STATIC_BOUNDS_CHECKING)
# if defined(__has_cpp_attribute) && defined(__has_builtin)
# if __has_builtin(__builtin_constant_p) && __has_cpp_attribute(gnu::error)
# define FLUX_HAVE_GCC_STATIC_BOUNDS_CHECKING 1
# endif
# endif
#endif // FLUX_DISABLE_STATIC_BOUNDS_CHECKING
// Default int_t is ptrdiff_t
#define FLUX_DEFAULT_INT_TYPE std::ptrdiff_t
// Select which int type to use
#ifndef FLUX_INT_TYPE
#define FLUX_INT_TYPE FLUX_DEFAULT_INT_TYPE
#endif
namespace flux {
FLUX_EXPORT
enum class error_policy {
terminate = FLUX_ERROR_POLICY_TERMINATE,
unwind = FLUX_ERROR_POLICY_UNWIND,
fail_fast = FLUX_ERROR_POLICY_FAIL_FAST
};
FLUX_EXPORT
enum class overflow_policy {
ignore = FLUX_OVERFLOW_POLICY_IGNORE,
wrap = FLUX_OVERFLOW_POLICY_WRAP,
error = FLUX_OVERFLOW_POLICY_ERROR
};
FLUX_EXPORT
enum class divide_by_zero_policy {
ignore = FLUX_DIVIDE_BY_ZERO_POLICY_IGNORE,
error = FLUX_DIVIDE_BY_ZERO_POLICY_ERROR
};
FLUX_EXPORT
enum class integer_cast_policy {
checked = FLUX_INTEGER_CAST_POLICY_CHECKED,
unchecked = FLUX_INTEGER_CAST_POLICY_UNCHECKED
};
namespace config {
FLUX_EXPORT
using int_type = FLUX_INT_TYPE;
static_assert(std::signed_integral<int_type> && (sizeof(int_type) >= sizeof(std::ptrdiff_t)),
"Custom FLUX_INT_TYPE must be a signed integer type at least as large as ptrdiff_t");
FLUX_EXPORT
inline constexpr error_policy on_error = static_cast<error_policy>(FLUX_ERROR_POLICY);
FLUX_EXPORT
inline constexpr overflow_policy on_overflow = static_cast<overflow_policy>(FLUX_OVERFLOW_POLICY);
FLUX_EXPORT
inline constexpr divide_by_zero_policy on_divide_by_zero = static_cast<divide_by_zero_policy>(FLUX_DIVIDE_BY_ZERO_POLICY);
FLUX_EXPORT
inline constexpr integer_cast_policy on_integer_cast = static_cast<integer_cast_policy>(FLUX_INTEGER_CAST_POLICY);
FLUX_EXPORT
inline constexpr bool print_error_on_terminate = FLUX_PRINT_ERROR_ON_TERMINATE;
FLUX_EXPORT
inline constexpr bool enable_debug_asserts = FLUX_ENABLE_DEBUG_ASSERTS;
} // namespace config
} // namespace flux
#endif
#include <cstdio>
#include <exception>
#include <source_location>
#include <stdexcept>
#include <type_traits>
#if defined(__has_builtin)
# if __has_builtin(__builtin_trap)
# define FLUX_HAS_BUILTIN_TRAP 1
# endif
#elif defined(_MSC_VER)
# include <intrin.h>
# define FLUX_HAS_FASTFAIL 1
#endif
namespace flux {
FLUX_EXPORT
struct unrecoverable_error : std::logic_error {
explicit inline unrecoverable_error(char const* msg) : std::logic_error(msg) {}
};
namespace detail {
struct runtime_error_fn {
private:
[[noreturn]]
FLUX_ALWAYS_INLINE
static void fail_fast()
{
#if FLUX_HAS_BUILTIN_TRAP
__builtin_trap();
#elif FLUX_HAS_FASTFAIL
__fastfail(7); // FAST_FAIL_FATAL_APP_EXIT
#else
std::abort();
#endif
}
[[noreturn]]
static void unwind(const char* msg, std::source_location loc)
{
char buf[1024];
std::snprintf(buf, 1024, "%s:%u: Fatal error: %s",
loc.file_name(), loc.line(), msg);
throw unrecoverable_error(buf);
}
[[noreturn]]
static void terminate(const char* msg, std::source_location loc)
{
if constexpr (config::print_error_on_terminate) {
std::fprintf(stderr, "%s:%u: Fatal error: %s\n",
loc.file_name(), loc.line(), msg);
}
std::terminate();
}
public:
[[noreturn]]
FLUX_ALWAYS_INLINE
void operator()(char const* msg,
std::source_location loc = std::source_location::current()) const
{
if constexpr (config::on_error == error_policy::fail_fast) {
fail_fast();
} else if constexpr (config::on_error == error_policy::unwind) {
unwind(msg, loc);
} else {
terminate(msg, loc);
}
}
};
}
FLUX_EXPORT inline constexpr auto runtime_error = detail::runtime_error_fn{};
#ifdef FLUX_HAVE_GCC_STATIC_BOUNDS_CHECKING
[[gnu::error("out-of-bounds sequence access detected")]]
void static_bounds_check_failed(); // not defined
#endif
namespace detail {
struct assert_fn {
inline constexpr void operator()(bool cond, char const* msg,
std::source_location loc = std::source_location::current()) const
{
if (cond) {
return;
} else {
runtime_error(msg, std::move(loc));
}
}
};
struct bounds_check_fn {
inline constexpr void operator()(bool cond, std::source_location loc = std::source_location::current()) const
{
if (!std::is_constant_evaluated()) {
assert_fn{}(cond, "out of bounds sequence access", std::move(loc));
}
}
};
struct indexed_bounds_check_fn {
template <typename T>
inline constexpr void operator()(T idx, T limit,
std::source_location loc = std::source_location::current()) const
{
if (!std::is_constant_evaluated()) {
#ifdef FLUX_HAVE_GCC_STATIC_BOUNDS_CHECKING
if (__builtin_constant_p(idx) && __builtin_constant_p(limit)) {
if (idx < T{0} || idx >= limit) {
static_bounds_check_failed();
}
}
#endif
assert_fn{}(idx >= T{0} && idx < limit, "out-of-bounds sequence access", loc);
}
}
};
} // namespace detail
FLUX_EXPORT inline constexpr auto assert_ = detail::assert_fn{};
FLUX_EXPORT inline constexpr auto bounds_check = detail::bounds_check_fn{};
FLUX_EXPORT inline constexpr auto indexed_bounds_check = detail::indexed_bounds_check_fn{};
} // namespace flux
#endif // FLUX_CORE_ASSERT_HPP_INCLUDED
namespace flux {
/*
* Useful helpers
*/
FLUX_EXPORT
template <typename From, typename To>
concept decays_to = std::same_as<std::remove_cvref_t<From>, To>;
FLUX_EXPORT
template <typename T, typename U>
concept same_decayed = std::same_as<std::remove_cvref_t<T>,
std::remove_cvref_t<U>>;
namespace detail {
struct copy_fn {
template <typename T>
[[nodiscard]]
constexpr auto operator()(T&& arg) const
noexcept(std::is_nothrow_convertible_v<T, std::decay_t<T>>)
-> std::decay_t<T>
{
return FLUX_FWD(arg);
}
};
} // namespace detail
FLUX_EXPORT inline constexpr auto copy = detail::copy_fn{};
namespace detail {
template <typename T, typename... U>
concept any_of = (std::same_as<T, U> || ...);
template <typename T, typename Cat>
concept compares_as = std::same_as<std::common_comparison_category_t<T, Cat>, Cat>;
template <typename Fn, typename T, typename U, typename Cat>
concept ordering_invocable_ =
std::regular_invocable<Fn, T, U> &&
compares_as<std::decay_t<std::invoke_result_t<Fn, T, U>>, Cat>;
} // namespace detail
FLUX_EXPORT
template <typename Fn, typename T, typename U, typename Cat = std::partial_ordering>
concept ordering_invocable =
detail::ordering_invocable_<Fn, T, U, Cat> &&
detail::ordering_invocable_<Fn, U, T, Cat> &&
detail::ordering_invocable_<Fn, T, T, Cat> &&
detail::ordering_invocable_<Fn, U, U, Cat>;
} // namespace flux
#endif
#include <compare>
#include <concepts>
#include <cstdint>
#include <functional>
#include <initializer_list>
#include <tuple>
#include <type_traits>
// clang-format off
// Workaround GCC12 ICE in sequence concept definition below
#if defined(__GNUC__) && !defined(__clang__) && (__GNUC__ < 13)
#define FLUX_COMPILER_IS_GCC12
#endif
#if defined(__cpp_lib_ranges_zip) && (__cpp_lib_ranges_zip >= 202110L)
#define FLUX_HAVE_CPP23_TUPLE_COMMON_REF
#endif
namespace flux {
/*
* Cursor concepts
*/
FLUX_EXPORT
template <typename Cur>
concept cursor = std::movable<Cur>;
FLUX_EXPORT
template <typename Cur>
concept regular_cursor = cursor<Cur> && std::regular<Cur>;
FLUX_EXPORT
template <typename Cur>
concept ordered_cursor =
regular_cursor<Cur> &&
std::totally_ordered<Cur>;
/*
* Sequence concepts and associated types
*/
FLUX_EXPORT
template <typename T>
struct sequence_traits;
FLUX_EXPORT
struct default_sequence_traits;
namespace detail {
template <typename T>
using traits_t = sequence_traits<std::remove_cvref_t<T>>;
} // namespace detail
FLUX_EXPORT
template <typename Seq>
using cursor_t = decltype(detail::traits_t<Seq>::first(FLUX_DECLVAL(Seq&)));
FLUX_EXPORT
template <typename Seq>
using element_t = decltype(detail::traits_t<Seq>::read_at(FLUX_DECLVAL(Seq&), FLUX_DECLVAL(cursor_t<Seq> const&)));
namespace detail {
template <typename T>
concept has_element_type = requires { typename element_t<T>; };
template <has_element_type T>
struct value_type { using type = std::remove_cvref_t<element_t<T>>; };
template <has_element_type T>
requires requires { typename traits_t<T>::value_type; }
struct value_type<T> { using type = typename traits_t<T>::value_type; };
} // namespace detail
FLUX_EXPORT
template <typename Seq>
using value_t = typename detail::value_type<Seq>::type;
FLUX_EXPORT
using distance_t = flux::config::int_type;
FLUX_EXPORT
using index_t = flux::config::int_type;
FLUX_EXPORT
template <typename Seq>
using rvalue_element_t = decltype(detail::traits_t<Seq>::move_at(FLUX_DECLVAL(Seq&), FLUX_DECLVAL(cursor_t<Seq> const&)));
FLUX_EXPORT
template <typename Seq>
using common_element_t = std::common_reference_t<element_t<Seq>, value_t<Seq>&>;
FLUX_EXPORT
template <typename Seq>
using const_element_t = std::common_reference_t<value_t<Seq> const&&, element_t<Seq>>;
namespace detail {
template <typename B>
concept boolean_testable =
std::convertible_to<B, bool> &&
requires (B&& b) {
{ !FLUX_FWD(b) } -> std::convertible_to<bool>;
};
template <typename T>
using with_ref = T&;
template <typename T>
concept can_reference = requires { typename with_ref<T>; };
template <typename Seq, typename Traits = sequence_traits<std::remove_cvref_t<Seq>>>
concept sequence_requirements =
requires (Seq& seq) {
{ Traits::first(seq) } -> cursor;
} &&
requires (Seq& seq, cursor_t<Seq> const& cur) {
{ Traits::is_last(seq, cur) } -> boolean_testable;
{ Traits::read_at(seq, cur) } -> can_reference;
} &&
requires (Seq& seq, cursor_t<Seq>& cur) {
{ Traits::inc(seq, cur) };
};
template <typename Seq, typename Traits = sequence_traits<std::remove_cvref_t<Seq>>>
concept sequence_concept =
sequence_requirements<Seq> &&
requires (Seq& seq, cursor_t<Seq> const& cur) {
{ Traits::read_at_unchecked(seq, cur) } -> std::same_as<element_t<Seq>>;
{ Traits::move_at(seq, cur) } -> can_reference;
{ Traits::move_at_unchecked(seq, cur) } -> std::same_as<rvalue_element_t<Seq>>;
} &&
#ifndef FLUX_COMPILER_IS_GCC12
requires (Seq& seq, bool (*pred)(element_t<Seq>)) {
{ Traits::for_each_while(seq, pred) } -> std::same_as<cursor_t<Seq>>;
} &&
#endif
#ifdef FLUX_HAVE_CPP23_TUPLE_COMMON_REF
std::common_reference_with<element_t<Seq>&&, value_t<Seq>&> &&
std::common_reference_with<rvalue_element_t<Seq>&&, value_t<Seq> const&> &&
#endif
std::common_reference_with<element_t<Seq>&&, rvalue_element_t<Seq>&&>;
} // namespace detail
FLUX_EXPORT
template <typename Seq>
concept sequence = detail::sequence_concept<Seq>;
namespace detail {
template <typename>
inline constexpr bool disable_multipass = false;
template <typename T>
requires requires { T::disable_multipass; } &&
decays_to<decltype(T::disable_multipass), bool>
inline constexpr bool disable_multipass<T> = T::disable_multipass;
} // namespace detail
FLUX_EXPORT
template <typename Seq>
concept multipass_sequence =
sequence<Seq> && regular_cursor<cursor_t<Seq>> &&
!detail::disable_multipass<detail::traits_t<Seq>>;
namespace detail {
template <typename Seq, typename Traits = sequence_traits<std::remove_cvref_t<Seq>>>
concept bidirectional_sequence_requirements =
requires (Seq& seq, cursor_t<Seq>& cur) {
{ Traits::dec(seq, cur) };
};
} // namespace detail
FLUX_EXPORT
template <typename Seq>
concept bidirectional_sequence = multipass_sequence<Seq> && detail::bidirectional_sequence_requirements<Seq>;
namespace detail {
template <typename Seq, typename Traits = sequence_traits<std::remove_cvref_t<Seq>>>
concept random_access_sequence_requirements =
ordered_cursor<cursor_t<Seq>> &&
requires (Seq& seq, cursor_t<Seq>& cur, distance_t offset) {
{ Traits::inc(seq, cur, offset) };
} &&
requires (Seq& seq, cursor_t<Seq> const& cur) {
{ Traits::distance(seq, cur, cur) } -> std::convertible_to<distance_t>;
};
} // namespace detail
FLUX_EXPORT
template <typename Seq>
concept random_access_sequence =
bidirectional_sequence<Seq> &&
detail::random_access_sequence_requirements<Seq>;
namespace detail {
template <typename Seq, typename Traits = sequence_traits<std::remove_cvref_t<Seq>>>
concept bounded_sequence_requirements =
requires (Seq& seq) {
{ Traits::last(seq) } -> std::same_as<cursor_t<Seq>>;
};
} // namespace detail
FLUX_EXPORT
template <typename Seq>
concept bounded_sequence = sequence<Seq> && detail::bounded_sequence_requirements<Seq>;
namespace detail {
template <typename Seq, typename Traits = sequence_traits<std::remove_cvref_t<Seq>>>
concept contiguous_sequence_requirements =
std::is_lvalue_reference_v<element_t<Seq>> &&
std::same_as<value_t<Seq>, std::remove_cvref_t<element_t<Seq>>> &&
requires (Seq& seq) {
{ Traits::data(seq) } -> std::same_as<std::add_pointer_t<element_t<Seq>>>;
};
} // namespace detail
FLUX_EXPORT
template <typename Seq>
concept contiguous_sequence =
random_access_sequence<Seq> &&
bounded_sequence<Seq> &&
detail::contiguous_sequence_requirements<Seq>;
namespace detail {
template <typename Seq, typename Traits = sequence_traits<std::remove_cvref_t<Seq>>>
concept sized_sequence_requirements =
requires (Seq& seq) {
{ Traits::size(seq) } -> std::convertible_to<distance_t>;
};
} // namespace detail
FLUX_EXPORT
template <typename Seq>
concept sized_sequence = sequence<Seq> && detail::sized_sequence_requirements<Seq>;
FLUX_EXPORT
template <typename Seq, typename T>
concept writable_sequence_of =
sequence<Seq> &&
requires (element_t<Seq> elem, T&& item) {
{ elem = FLUX_FWD(item) } -> std::same_as<element_t<Seq>&>;
};
namespace detail {
template <typename>
inline constexpr bool is_infinite_seq = false;
template <typename T>
requires requires { T::is_infinite; } &&
decays_to<decltype(T::is_infinite), bool>
inline constexpr bool is_infinite_seq<T> = T::is_infinite;
}
FLUX_EXPORT
template <typename Seq>
concept infinite_sequence =
sequence<Seq> &&
detail::is_infinite_seq<detail::traits_t<Seq>>;
FLUX_EXPORT
template <typename Seq>
concept read_only_sequence =
sequence<Seq> &&
std::same_as<element_t<Seq>, const_element_t<Seq>>;
FLUX_EXPORT
template <typename Seq>
concept const_iterable_sequence =
// Seq and Seq const must both be sequences
sequence<Seq> && sequence<Seq const> &&
// Seq and Seq const must have the same cursor and value types
std::same_as<cursor_t<Seq>, cursor_t<Seq const>> &&
std::same_as<value_t<Seq>, value_t<Seq const>> &&
// Seq and Seq const must have the same const_element type
#ifdef FLUX_HAVE_CPP23_TUPLE_COMMON_REF
std::same_as<const_element_t<Seq>, const_element_t<Seq const>> &&
#endif
// Seq and Seq const must model the same extended sequence concepts
(multipass_sequence<Seq> == multipass_sequence<Seq const>) &&
(bidirectional_sequence<Seq> == bidirectional_sequence<Seq const>) &&
(random_access_sequence<Seq> == random_access_sequence<Seq const>) &&
(contiguous_sequence<Seq> == contiguous_sequence<Seq const>) &&
(bounded_sequence<Seq> == bounded_sequence<Seq const>) &&
(sized_sequence<Seq> == sized_sequence<Seq const>) &&
(infinite_sequence<Seq> == infinite_sequence<Seq const>) &&
// If Seq is read-only, Seq const must be read-only as well
(!read_only_sequence<Seq> || read_only_sequence<Seq const>);
namespace detail {
template <typename T, typename R = std::remove_cvref_t<T>>
constexpr bool is_ilist = false;
template <typename T, typename E>
constexpr bool is_ilist<T, std::initializer_list<E>> = true;
template <typename Seq>
concept rvalue_sequence =
std::is_object_v<Seq> &&
std::move_constructible<Seq> &&
sequence<Seq>;
template <typename Seq>
concept trivially_copyable_sequence =
std::copyable<Seq> &&
std::is_trivially_copyable_v<Seq> &&
sequence<Seq>;
}
FLUX_EXPORT
template <typename Seq>
concept adaptable_sequence =
(detail::rvalue_sequence<Seq>
|| (std::is_lvalue_reference_v<Seq> &&
detail::trivially_copyable_sequence<std::decay_t<Seq>>)) &&
!detail::is_ilist<Seq>;
FLUX_EXPORT
template <typename D>
struct inline_sequence_base;
namespace detail {
template <typename T, typename U>
requires (!std::same_as<T, inline_sequence_base<U>>)
void derived_from_inline_sequence_base_test(T const&, inline_sequence_base<U> const&);
template <typename T>
concept derived_from_inline_sequence_base = requires(T t) {
derived_from_inline_sequence_base_test(t, t);
};
} // namespace detail
/*
* Default sequence_traits implementation
*/
struct default_sequence_traits {
template <typename Self>
requires detail::sequence_requirements<Self>
static constexpr auto read_at_unchecked(Self& self, cursor_t<Self> const& cur)
-> decltype(detail::traits_t<Self>::read_at(self, cur))
{
return detail::traits_t<Self>::read_at(self, cur);
}
template <typename Self>
requires detail::sequence_requirements<Self>
static constexpr auto move_at(Self& self, cursor_t<Self> const& cur)
-> std::conditional_t<std::is_lvalue_reference_v<element_t<Self>>,
std::add_rvalue_reference_t<std::remove_reference_t<element_t<Self>>>,
element_t<Self>>
{
using Traits = detail::traits_t<Self>;
if constexpr (std::is_lvalue_reference_v<element_t<Self>>) {
return std::move(Traits::read_at(self, cur));
} else {
return Traits::read_at(self, cur);
}
}
template <typename Self>
requires detail::sequence_requirements<Self>
static constexpr auto move_at_unchecked(Self& self, cursor_t<Self> const& cur)
-> decltype(detail::traits_t<Self>::move_at(self, cur))
{
return detail::traits_t<Self>::move_at(self, cur);
}
template <typename Self>
requires detail::random_access_sequence_requirements<Self> &&
detail::bounded_sequence_requirements<Self>
static constexpr auto size(Self& self) -> distance_t
{
using Traits = detail::traits_t<Self>;
return Traits::distance(self, Traits::first(self), Traits::last(self));
}
template <typename Self, typename Pred>
requires detail::sequence_requirements<Self>
static constexpr auto for_each_while(Self& self, Pred&& pred) -> cursor_t<Self>
{
using Traits = detail::traits_t<Self>;
auto cur = Traits::first(self);
if constexpr (bounded_sequence<Self> && regular_cursor<cursor_t<Self>>) {
auto const last = Traits::last(self);
while (cur != last) {
if (!std::invoke(pred, Traits::read_at(self, cur))) {
break;
}
Traits::inc(self, cur);
}
} else {
while (!Traits::is_last(self, cur)) {
if (!std::invoke(pred, Traits::read_at(self, cur))) {
break;
}
Traits::inc(self, cur);
}
}
return cur;
}
};
namespace detail {
template <typename T>
concept has_nested_sequence_traits =
requires { typename T::flux_sequence_traits; } &&
std::is_class_v<typename T::flux_sequence_traits>;
}
template <typename T>
requires detail::has_nested_sequence_traits<T>
struct sequence_traits<T> : T::flux_sequence_traits {};
namespace detail {
template <typename O>
concept optional_like =
std::default_initializable<O> &&
std::movable<O> &&
requires (O& o) {
{ static_cast<bool>(o) };
{ *o } -> flux::detail::can_reference;
};
}
} // namespace flux
#endif // FLUX_CORE_CONCEPTS_HPP_INCLUDED
// Copyright (c) 2022 Tristan Brindle (tcbrindle at gmail dot com)
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#ifndef FLUX_CORE_DEFAULT_IMPLS_HPP_INCLUDED
#define FLUX_CORE_DEFAULT_IMPLS_HPP_INCLUDED
// Copyright (c) 2023 Tristan Brindle (tcbrindle at gmail dot com)
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#ifndef FLUX_CORE_NUMERIC_HPP_INCLUDED
#define FLUX_CORE_NUMERIC_HPP_INCLUDED
#include <flux/core/detail/jtckdint.h>
#include <climits>
#include <cstdint>
#include <limits>
#include <utility>
namespace flux::num {
FLUX_EXPORT
template <typename T>
concept integral =
std::integral<T> &&
!flux::detail::any_of<T, bool, char, wchar_t, char8_t, char16_t, char32_t>;
FLUX_EXPORT
template <typename T>
concept signed_integral = integral<T> && std::signed_integral<T>;
FLUX_EXPORT
template <typename T>
concept unsigned_integral = integral<T> && std::unsigned_integral<T>;
FLUX_EXPORT
template <integral T>
struct overflow_result {
T value;
bool overflowed;
};
namespace detail {
template <integral To>