-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathrange_algorithm_support.hpp
1097 lines (946 loc) · 47.4 KB
/
range_algorithm_support.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) Microsoft Corporation.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#pragma once
#include <cassert>
#include <concepts>
#include <cstddef>
#include <functional>
#include <ranges>
#include <span>
#include <type_traits>
#include <utility>
#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__)
namespace ranges = std::ranges;
template <class>
inline constexpr bool always_false = false;
namespace detail {
static constexpr bool permissive() {
return false;
}
template <class>
struct DependentBase {
static constexpr bool permissive() {
return true;
}
};
template <class T>
struct Derived : DependentBase<T> {
static constexpr bool test() {
return permissive();
}
};
} // namespace detail
constexpr bool is_permissive = detail::Derived<int>::test();
template <class T>
inline constexpr T* nullptr_to = nullptr;
template <bool>
struct borrowed { // borrowed<true> is a borrowed_range; borrowed<false> is not
int* begin() const;
int* end() const;
};
template <>
inline constexpr bool ranges::enable_borrowed_range<borrowed<true>> = true;
struct boolish {
bool value_ = true;
constexpr operator bool() const noexcept {
return value_;
}
[[nodiscard]] constexpr boolish operator!() const noexcept {
return {!value_};
}
};
namespace test {
using std::assignable_from, std::conditional_t, std::convertible_to, std::copy_constructible, std::derived_from,
std::exchange, std::ptrdiff_t, std::span;
using output = std::output_iterator_tag;
using input = std::input_iterator_tag;
using fwd = std::forward_iterator_tag;
using bidi = std::bidirectional_iterator_tag;
using random = std::random_access_iterator_tag;
using contiguous = std::contiguous_iterator_tag;
template <class T>
void operator&(T&&) {
STATIC_ASSERT(always_false<T>);
}
template <class T, class U>
void operator,(T&&, U&&) {
STATIC_ASSERT(always_false<T>);
}
enum class CanDifference : bool { no, yes };
enum class CanCompare : bool { no, yes };
enum class ProxyRef : bool { no, yes };
enum class IsWrapped : bool { no, yes };
template <class T>
[[nodiscard]] constexpr bool to_bool(T const t) noexcept {
STATIC_ASSERT(std::is_enum_v<T> && std::same_as<std::underlying_type_t<T>, bool>);
return static_cast<bool>(t);
}
template <class Element, IsWrapped Wrapped = IsWrapped::yes>
class sentinel {
Element* ptr_ = nullptr;
public:
sentinel() = default;
constexpr explicit sentinel(Element* ptr) noexcept : ptr_{ptr} {}
[[nodiscard]] constexpr Element* peek() const noexcept {
return ptr_;
}
using _Prevent_inheriting_unwrap = sentinel;
using unwrap = sentinel<Element, IsWrapped::no>;
// clang-format off
[[nodiscard]] constexpr auto _Unwrapped() const noexcept requires (to_bool(Wrapped)) {
return unwrap{ptr_};
}
// clang-format on
static constexpr bool _Unwrap_when_unverified = true;
// clang-format off
constexpr void _Seek_to(unwrap const& s) noexcept requires (to_bool(Wrapped)) {
ptr_ = s.peek();
}
// clang-format on
[[nodiscard]] friend constexpr boolish operator==(sentinel const s, Element* const ptr) noexcept {
return {s.ptr_ == ptr};
}
[[nodiscard]] friend constexpr boolish operator==(Element* const ptr, sentinel const s) noexcept {
return {s.ptr_ == ptr};
}
[[nodiscard]] friend constexpr boolish operator!=(sentinel const s, Element* const ptr) noexcept {
return !(s == ptr);
}
[[nodiscard]] friend constexpr boolish operator!=(Element* const ptr, sentinel const s) noexcept {
return !(s == ptr);
}
[[nodiscard]] friend constexpr ptrdiff_t operator-(sentinel const s, Element* const ptr) noexcept {
return s.ptr_ - ptr;
}
[[nodiscard]] friend constexpr ptrdiff_t operator-(Element* const ptr, sentinel const s) noexcept {
return ptr - s.ptr_;
}
};
// clang-format off
template <class T, class U>
concept CanEq = requires(T const& t, U const& u) {
{ t == u } -> convertible_to<bool>;
};
template <class T, class U>
concept CanNEq = requires(T const& t, U const& u) {
{ t != u } -> convertible_to<bool>;
};
template <class T, class U>
concept CanLt = requires(T const& t, U const& u) {
{ t < u } -> convertible_to<bool>;
};
template <class T, class U>
concept CanLtE = requires(T const& t, U const& u) {
{ t <= u } -> convertible_to<bool>;
};
template <class T, class U>
concept CanGt = requires(T const& t, U const& u) {
{ t > u } -> convertible_to<bool>;
};
template <class T, class U>
concept CanGtE = requires(T const& t, U const& u) {
{ t >= u } -> convertible_to<bool>;
};
// clang-format on
template <class Category, class Element>
class proxy_reference {
Element& ref_;
using Value = std::remove_cv_t<Element>;
public:
constexpr explicit proxy_reference(Element& r) : ref_{r} {}
proxy_reference(proxy_reference const&) = default;
constexpr proxy_reference const& operator=(proxy_reference const& that) const
requires assignable_from<Element&, Element&> {
ref_ = that.ref_;
return *this;
}
// clang-format off
constexpr operator Element&() const requires derived_from<Category, input> {
return ref_;
}
template <class T>
requires (!std::same_as<std::remove_cvref_t<T>, proxy_reference> && assignable_from<Element&, T>)
constexpr void operator=(T&& val) const {
ref_ = std::forward<T>(val);
}
// clang-format on
template <class Cat, class Elem>
[[nodiscard]] constexpr boolish operator==(proxy_reference<Cat, Elem> that) const
requires CanEq<Element, Elem> {
return {ref_ == that.peek()};
}
template <class Cat, class Elem>
[[nodiscard]] constexpr boolish operator!=(proxy_reference<Cat, Elem> that) const
requires CanNEq<Element, Elem> {
return {ref_ != that.peek()};
}
template <class Cat, class Elem>
[[nodiscard]] constexpr boolish operator<(proxy_reference<Cat, Elem> that) const requires CanLt<Element, Elem> {
return {ref_ < that.peek()};
}
template <class Cat, class Elem>
[[nodiscard]] constexpr boolish operator>(proxy_reference<Cat, Elem> that) const requires CanGt<Element, Elem> {
return {ref_ > that.peek()};
}
template <class Cat, class Elem>
[[nodiscard]] constexpr boolish operator<=(proxy_reference<Cat, Elem> that) const
requires CanLtE<Element, Elem> {
return {ref_ <= that.peek()};
}
template <class Cat, class Elem>
[[nodiscard]] constexpr boolish operator>=(proxy_reference<Cat, Elem> that) const
requires CanGtE<Element, Elem> {
return {ref_ >= that.peek()};
}
// clang-format off
[[nodiscard]] friend constexpr boolish operator==(proxy_reference r, Value const& val)
requires CanEq<Element, Value> {
return {r.ref_ == val};
}
[[nodiscard]] friend constexpr boolish operator==(Value const& val, proxy_reference r)
requires CanEq<Element, Value> {
return {r.ref_ == val};
}
[[nodiscard]] friend constexpr boolish operator!=(proxy_reference r, Value const& val)
requires CanNEq<Element, Value> {
return {r.ref_ != val};
}
[[nodiscard]] friend constexpr boolish operator!=(Value const& val, proxy_reference r)
requires CanNEq<Element, Value> {
return {r.ref_ != val};
}
[[nodiscard]] friend constexpr boolish operator<(Value const& val, proxy_reference r)
requires CanLt<Value, Element> {
return {val < r.ref_};
}
[[nodiscard]] friend constexpr boolish operator<(proxy_reference r, Value const& val)
requires CanLt<Element, Value> {
return {r.ref_ < val};
}
[[nodiscard]] friend constexpr boolish operator>(Value const& val, proxy_reference r)
requires CanGt<Value, Element> {
return {val > r.ref_};
}
[[nodiscard]] friend constexpr boolish operator>(proxy_reference r, Value const& val)
requires CanGt<Element, Value> {
return {r.ref_ > val};
}
[[nodiscard]] friend constexpr boolish operator<=(Value const& val, proxy_reference r)
requires CanLtE<Value, Element> {
return {val <= r.ref_};
}
[[nodiscard]] friend constexpr boolish operator<=(proxy_reference r, Value const& val)
requires CanLtE<Element, Value> {
return {r.ref_ <= val};
}
[[nodiscard]] friend constexpr boolish operator>=(Value const& val, proxy_reference r)
requires CanGtE<Value, Element> {
return {val >= r.ref_};
}
[[nodiscard]] friend constexpr boolish operator>=(proxy_reference r, Value const& val)
requires CanGtE<Element, Value> {
return {r.ref_ >= val};
}
// clang-format on
[[nodiscard]] constexpr Element& peek() const noexcept {
return ref_;
}
};
template <class Ref>
struct common_reference {
Ref ref_;
common_reference(Ref r) : ref_{static_cast<Ref>(r)} {}
// clang-format off
template <class Cat, class Elem>
requires convertible_to<Elem&, Ref>
common_reference(proxy_reference<Cat, Elem> pref) : ref_{pref.peek()} {}
// clang-format on
};
} // namespace test
// clang-format off
template <class Cat, class Elem, class U, template <class> class TQuals, template <class> class UQuals>
requires std::common_reference_with<Elem&, UQuals<U>>
struct std::basic_common_reference<::test::proxy_reference<Cat, Elem>, U, TQuals, UQuals> {
using type = common_reference_t<Elem&, UQuals<U>>;
};
template <class T, class Cat, class Elem, template <class> class TQuals, template <class> class UQuals>
requires std::common_reference_with<TQuals<T>, Elem&>
struct std::basic_common_reference<T, ::test::proxy_reference<Cat, Elem>, TQuals, UQuals> {
using type = common_reference_t<TQuals<T>, Elem&>;
};
// clang-format on
namespace test {
// clang-format off
template <class Category, class Element,
// Model sized_sentinel_for along with sentinel?
CanDifference Diff = CanDifference{derived_from<Category, random>},
// Model sentinel_for with self (and sized_sentinel_for if Diff; implies copyable)?
CanCompare Eq = CanCompare{derived_from<Category, fwd>},
// Use a ProxyRef reference type (instead of Element&)?
ProxyRef Proxy = ProxyRef{!derived_from<Category, contiguous>},
// Interact with the STL's iterator unwrapping machinery?
IsWrapped Wrapped = IsWrapped::yes>
requires (to_bool(Eq) || !derived_from<Category, fwd>)
&& (!to_bool(Proxy) || !derived_from<Category, contiguous>)
class iterator {
Element* ptr_;
template <class T>
static constexpr bool at_least = derived_from<Category, T>;
using ReferenceType = conditional_t<to_bool(Proxy), proxy_reference<Category, Element>, Element&>;
public:
// output iterator operations
iterator() = default;
constexpr explicit iterator(Element* ptr) noexcept : ptr_{ptr} {}
constexpr iterator(iterator&& that) noexcept : ptr_{exchange(that.ptr_, nullptr)} {}
constexpr iterator& operator=(iterator&& that) noexcept {
ptr_ = exchange(that.ptr_, nullptr);
return *this;
}
[[nodiscard]] constexpr Element* peek() const noexcept {
return ptr_;
}
[[nodiscard]] constexpr ReferenceType operator*() const noexcept {
return ReferenceType{*ptr_};
}
[[nodiscard]] constexpr boolish operator==(sentinel<Element, Wrapped> const& s) const noexcept {
return boolish{ptr_ == s.peek()};
}
[[nodiscard]] friend constexpr boolish operator==(
sentinel<Element, Wrapped> const& s, iterator const& i) noexcept {
return i == s;
}
[[nodiscard]] constexpr boolish operator!=(sentinel<Element, Wrapped> const& s) const noexcept {
return !(*this == s);
}
[[nodiscard]] friend constexpr boolish operator!=(
sentinel<Element, Wrapped> const& s, iterator const& i) noexcept {
return !(i == s);
}
constexpr iterator& operator++() & noexcept {
++ptr_;
return *this;
}
constexpr iterator operator++(int) & noexcept {
auto tmp = *this;
++ptr_;
return tmp;
}
auto operator--() & {
STATIC_ASSERT(always_false<Category>);
}
auto operator--(int) & {
STATIC_ASSERT(always_false<Category>);
}
friend void iter_swap(iterator const&, iterator const&) {
STATIC_ASSERT(always_false<Category>);
}
void operator<(iterator const&) const {
STATIC_ASSERT(always_false<Category>);
}
void operator>(iterator const&) const {
STATIC_ASSERT(always_false<Category>);
}
void operator<=(iterator const&) const {
STATIC_ASSERT(always_false<Category>);
}
void operator>=(iterator const&) const {
STATIC_ASSERT(always_false<Category>);
}
// input iterator operations:
constexpr void operator++(int) & noexcept requires std::is_same_v<Category, input> {
++ptr_;
}
[[nodiscard]] constexpr friend std::remove_cv_t<Element> iter_move(iterator const& i)
requires at_least<input> && std::constructible_from<std::remove_cv_t<Element>, Element> {
return std::move(*i.ptr_);
}
constexpr friend void iter_swap(iterator const& x, iterator const& y) requires at_least<input> {
ranges::iter_swap(x.ptr_, y.ptr_);
}
// sentinel operations (implied by forward iterator):
iterator(iterator const&) requires (to_bool(Eq)) = default;
iterator& operator=(iterator const&) requires (to_bool(Eq)) = default;
[[nodiscard]] constexpr boolish operator==(iterator const& that) const noexcept requires (to_bool(Eq)) {
return {ptr_ == that.ptr_};
}
[[nodiscard]] constexpr boolish operator!=(iterator const& that) const noexcept requires (to_bool(Eq)) {
return !(*this == that);
}
// bidi iterator operations:
constexpr iterator& operator--() & noexcept requires at_least<bidi> {
--ptr_;
return *this;
}
constexpr iterator operator--(int) & noexcept requires at_least<bidi> {
auto tmp = *this;
--ptr_;
return tmp;
}
// random-access iterator operations:
[[nodiscard]] constexpr boolish operator<(iterator const& that) const noexcept requires at_least<random> {
return {ptr_ < that.ptr_};
}
[[nodiscard]] constexpr boolish operator>(iterator const& that) const noexcept requires at_least<random> {
return that < *this;
}
[[nodiscard]] constexpr boolish operator<=(iterator const& that) const noexcept requires at_least<random> {
return !(that < *this);
}
[[nodiscard]] constexpr boolish operator>=(iterator const& that) const noexcept requires at_least<random> {
return !(*this < that);
}
[[nodiscard]] constexpr ReferenceType operator[](ptrdiff_t const n) const& noexcept requires at_least<random> {
return ReferenceType{ptr_[n]};
}
constexpr iterator& operator+=(ptrdiff_t const n) & noexcept requires at_least<random> {
ptr_ += n;
return *this;
}
constexpr iterator& operator-=(ptrdiff_t const n) & noexcept requires at_least<random> {
ptr_ -= n;
return *this;
}
[[nodiscard]] constexpr iterator operator+(ptrdiff_t const n) const noexcept requires at_least<random> {
return iterator{ptr_ + n};
}
[[nodiscard]] friend constexpr iterator operator+(ptrdiff_t const n, iterator const& i) noexcept
requires at_least<random> {
return i + n;
}
[[nodiscard]] constexpr iterator operator-(ptrdiff_t const n) const noexcept requires at_least<random> {
return iterator{ptr_ - n};
}
// contiguous iterator operations:
[[nodiscard]] constexpr Element* operator->() const noexcept requires at_least<contiguous> {
return ptr_;
}
// sized_sentinel_for operations:
[[nodiscard]] constexpr ptrdiff_t operator-(iterator const& that) const noexcept
requires (to_bool(Diff) && to_bool(Eq)) || at_least<random> {
return ptr_ - that.ptr_;
}
[[nodiscard]] constexpr ptrdiff_t operator-(sentinel<Element, Wrapped> const& s) const noexcept
requires (to_bool(Diff)) {
return ptr_ - s.peek();
}
[[nodiscard]] friend constexpr ptrdiff_t operator-(
sentinel<Element, Wrapped> const& s, iterator const& i) noexcept requires (to_bool(Diff)) {
return -(i - s);
}
// iterator unwrapping operations:
using _Prevent_inheriting_unwrap = iterator;
using unwrap = std::conditional_t<derived_from<Category, contiguous>, Element*,
iterator<Category, Element, Diff, Eq, Proxy, IsWrapped::no>>;
[[nodiscard]] constexpr auto _Unwrapped() const& noexcept requires (to_bool(Wrapped) && to_bool(Eq)) {
return unwrap{ptr_};
}
[[nodiscard]] constexpr auto _Unwrapped() && noexcept requires (to_bool(Wrapped)) {
return unwrap{exchange(ptr_, nullptr)};
}
static constexpr bool _Unwrap_when_unverified = true;
constexpr void _Seek_to(unwrap const& i) noexcept requires (to_bool(Wrapped) && to_bool(Eq)) {
if constexpr (at_least<contiguous>) {
ptr_ = i;
} else {
ptr_ = i.peek();
}
}
constexpr void _Seek_to(unwrap&& i) noexcept requires (to_bool(Wrapped)) {
if constexpr (at_least<contiguous>) {
ptr_ = i;
} else {
ptr_ = i.peek();
}
}
};
// clang-format on
} // namespace test
template <class Category, class Element, ::test::CanDifference Diff, ::test::CanCompare Eq, ::test::ProxyRef Proxy,
::test::IsWrapped Wrapped>
struct std::iterator_traits<::test::iterator<Category, Element, Diff, Eq, Proxy, Wrapped>> {
using iterator_concept = Category;
using iterator_category = conditional_t<derived_from<Category, forward_iterator_tag>, //
conditional_t<static_cast<bool>(Proxy), input_iterator_tag, Category>, //
conditional_t<static_cast<bool>(Eq), Category, void>>; // TRANSITION, LWG-3289
using value_type = remove_cv_t<Element>;
using difference_type = ptrdiff_t;
using pointer = conditional_t<derived_from<Category, contiguous_iterator_tag>, Element*, void>;
using reference = iter_reference_t<::test::iterator<Category, Element, Diff, Eq, Proxy, Wrapped>>;
};
template <class Element, ::test::CanDifference Diff, ::test::IsWrapped Wrapped>
struct std::pointer_traits<::test::iterator<std::contiguous_iterator_tag, Element, Diff, ::test::CanCompare::yes,
::test::ProxyRef::no, Wrapped>> {
using pointer = ::test::iterator<contiguous_iterator_tag, Element, Diff, ::test::CanCompare::yes,
::test::ProxyRef::no, Wrapped>;
using element_type = Element;
using difference_type = ptrdiff_t;
[[nodiscard]] static constexpr element_type* to_address(pointer const& x) noexcept {
return x.peek();
}
};
namespace test {
enum class Sized : bool { no, yes };
enum class Common : bool { no, yes };
// clang-format off
template <class Category, class Element,
// Implement member size? (NB: Not equivalent to "Is this a sized_range?")
Sized IsSized = Sized::no,
// iterator and sentinel model sized_sentinel_for (also iterator and iterator if Eq)
CanDifference Diff = CanDifference{derived_from<Category, random>},
// Model common_range?
Common IsCommon = Common::no,
// Iterator models sentinel_for with self
CanCompare Eq = CanCompare{derived_from<Category, fwd>},
// Use a ProxyRef reference type?
ProxyRef Proxy = ProxyRef{!derived_from<Category, contiguous>}>
requires (!to_bool(IsCommon) || to_bool(Eq))
&& (to_bool(Eq) || !derived_from<Category, fwd>)
&& (!to_bool(Proxy) || !derived_from<Category, contiguous>)
class range {
span<Element> elements_;
mutable bool begin_called_ = false;
public:
using I = iterator<Category, Element, Diff, Eq, Proxy, IsWrapped::yes>;
using S = conditional_t<to_bool(IsCommon), I, sentinel<Element, IsWrapped::yes>>;
range() = default;
constexpr explicit range(span<Element> elements) noexcept : elements_{elements} {}
range(range const&) = delete;
range& operator=(range const&) = delete;
[[nodiscard]] constexpr I begin() const noexcept {
if constexpr (!derived_from<Category, fwd>) {
assert(!exchange(begin_called_, true));
}
return I{elements_.data()};
}
[[nodiscard]] constexpr S end() const noexcept {
return S{elements_.data() + elements_.size()};
}
[[nodiscard]] constexpr ptrdiff_t size() const noexcept requires (to_bool(IsSized)) {
if constexpr (!derived_from<Category, fwd>) {
assert(!begin_called_);
}
return static_cast<ptrdiff_t>(elements_.size());
}
[[nodiscard]] constexpr Element* data() const noexcept requires derived_from<Category, contiguous> {
return elements_.data();
}
using UI = typename I::unwrap;
using US = conditional_t<to_bool(IsCommon), UI, sentinel<Element, IsWrapped::no>>;
[[nodiscard]] constexpr UI _Unchecked_begin() const noexcept {
if constexpr (!derived_from<Category, fwd>) {
assert(!exchange(begin_called_, true));
}
return UI{elements_.data()};
}
[[nodiscard]] constexpr US _Unchecked_end() const noexcept {
return US{elements_.data() + elements_.size()};
}
void operator&() const {
STATIC_ASSERT(always_false<Category>);
}
template <class T>
friend void operator,(range const&, T&&) {
STATIC_ASSERT(always_false<Category>);
}
};
// clang-format on
} // namespace test
template <class T>
class basic_borrowed_range : public test::range<test::input, T, test::Sized::no, test::CanDifference::no,
test::Common::no, test::CanCompare::no, test::ProxyRef::no> {
using test::range<test::input, T, test::Sized::no, test::CanDifference::no, test::Common::no, test::CanCompare::no,
test::ProxyRef::no>::range;
};
template <ranges::contiguous_range R>
basic_borrowed_range(R&) -> basic_borrowed_range<std::remove_reference_t<ranges::range_reference_t<R>>>;
template <class T>
inline constexpr bool ranges::enable_borrowed_range<::basic_borrowed_range<T>> = true;
template <int>
struct unique_tag {};
template <class I, int Tag = 0>
using ProjectionFor = unique_tag<Tag> (*)(std::iter_common_reference_t<I>);
template <class I>
using UnaryPredicateFor = boolish (*)(std::iter_common_reference_t<I>);
template <int Tag = 0>
using ProjectedUnaryPredicate = boolish (*)(unique_tag<Tag>);
template <class I2, int Tag1 = 0>
using HalfProjectedBinaryPredicateFor = boolish (*)(unique_tag<Tag1>, std::iter_common_reference_t<I2>);
template <int Tag1 = 0, int Tag2 = 0>
using ProjectedBinaryPredicate = boolish (*)(unique_tag<Tag1>, unique_tag<Tag2>);
template <class I1, class I2>
using BinaryPredicateFor = boolish (*)(std::iter_common_reference_t<I1>, std::iter_common_reference_t<I2>);
template <class Continuation, class Element>
struct with_output_iterators {
template <class... Args>
static constexpr void call() {
using namespace test;
using test::iterator;
// Diff and Eq are not significant for "lone" single-pass iterators, so we can ignore them here.
Continuation::template call<Args...,
iterator<output, Element, CanDifference::no, CanCompare::no, ProxyRef::no>>();
Continuation::template call<Args...,
iterator<output, Element, CanDifference::no, CanCompare::no, ProxyRef::yes>>();
// For forward and bidi, Eq is necessarily true but Diff and Proxy may vary.
Continuation::template call<Args...,
iterator<fwd, Element, CanDifference::no, CanCompare::yes, ProxyRef::no>>();
Continuation::template call<Args...,
iterator<fwd, Element, CanDifference::no, CanCompare::yes, ProxyRef::yes>>();
Continuation::template call<Args...,
iterator<fwd, Element, CanDifference::yes, CanCompare::yes, ProxyRef::no>>();
Continuation::template call<Args...,
iterator<fwd, Element, CanDifference::yes, CanCompare::yes, ProxyRef::yes>>();
Continuation::template call<Args...,
iterator<bidi, Element, CanDifference::no, CanCompare::yes, ProxyRef::no>>();
Continuation::template call<Args...,
iterator<bidi, Element, CanDifference::no, CanCompare::yes, ProxyRef::yes>>();
Continuation::template call<Args...,
iterator<bidi, Element, CanDifference::yes, CanCompare::yes, ProxyRef::no>>();
Continuation::template call<Args...,
iterator<bidi, Element, CanDifference::yes, CanCompare::yes, ProxyRef::yes>>();
// Random iterators are Diff and Eq - only Proxy varies.
Continuation::template call<Args...,
iterator<random, Element, CanDifference::yes, CanCompare::yes, ProxyRef::no>>();
Continuation::template call<Args...,
iterator<random, Element, CanDifference::yes, CanCompare::yes, ProxyRef::yes>>();
// Contiguous iterators are totally locked down.
Continuation::template call<Args..., iterator<contiguous, Element>>();
}
};
template <class Continuation, class Element>
struct with_writable_iterators {
template <class... Args>
static constexpr void call() {
using namespace test;
using test::iterator;
// Diff and Eq are not significant for "lone" single-pass iterators, so we can ignore them here.
Continuation::template call<Args...,
iterator<input, Element, CanDifference::no, CanCompare::no, ProxyRef::no>>();
Continuation::template call<Args...,
iterator<input, Element, CanDifference::no, CanCompare::no, ProxyRef::yes>>();
with_output_iterators<Continuation, Element>::template call<Args...>();
}
};
template <class Continuation, class Element>
struct with_contiguous_ranges {
template <class... Args>
static constexpr void call() {
using namespace test;
using test::range;
// Ditto always Eq; !IsSized && SizedSentinel is uninteresting (ranges::size still works), as is
// !IsSized && IsCommon. contiguous also implies !Proxy.
Continuation::template call<Args...,
range<contiguous, Element, Sized::no, CanDifference::no, Common::no, CanCompare::yes, ProxyRef::no>>();
Continuation::template call<Args...,
range<contiguous, Element, Sized::yes, CanDifference::no, Common::no, CanCompare::yes, ProxyRef::no>>();
Continuation::template call<Args...,
range<contiguous, Element, Sized::yes, CanDifference::no, Common::yes, CanCompare::yes, ProxyRef::no>>();
Continuation::template call<Args...,
range<contiguous, Element, Sized::yes, CanDifference::yes, Common::no, CanCompare::yes, ProxyRef::no>>();
Continuation::template call<Args...,
range<contiguous, Element, Sized::yes, CanDifference::yes, Common::yes, CanCompare::yes, ProxyRef::no>>();
}
};
template <class Continuation, class Element>
struct with_random_ranges {
template <class... Args>
static constexpr void call() {
using namespace test;
using test::range;
// Ditto always Eq; !IsSized && SizedSentinel is uninteresting (ranges::size works either way), as is
// !IsSized && IsCommon.
Continuation::template call<Args...,
range<random, Element, Sized::no, CanDifference::no, Common::no, CanCompare::yes, ProxyRef::no>>();
Continuation::template call<Args...,
range<random, Element, Sized::no, CanDifference::no, Common::no, CanCompare::yes, ProxyRef::yes>>();
Continuation::template call<Args...,
range<random, Element, Sized::yes, CanDifference::no, Common::no, CanCompare::yes, ProxyRef::no>>();
Continuation::template call<Args...,
range<random, Element, Sized::yes, CanDifference::no, Common::no, CanCompare::yes, ProxyRef::yes>>();
Continuation::template call<Args...,
range<random, Element, Sized::yes, CanDifference::no, Common::yes, CanCompare::yes, ProxyRef::no>>();
Continuation::template call<Args...,
range<random, Element, Sized::yes, CanDifference::no, Common::yes, CanCompare::yes, ProxyRef::yes>>();
Continuation::template call<Args...,
range<random, Element, Sized::yes, CanDifference::yes, Common::no, CanCompare::yes, ProxyRef::no>>();
Continuation::template call<Args...,
range<random, Element, Sized::yes, CanDifference::yes, Common::no, CanCompare::yes, ProxyRef::yes>>();
Continuation::template call<Args...,
range<random, Element, Sized::yes, CanDifference::yes, Common::yes, CanCompare::yes, ProxyRef::no>>();
Continuation::template call<Args...,
range<random, Element, Sized::yes, CanDifference::yes, Common::yes, CanCompare::yes, ProxyRef::yes>>();
with_contiguous_ranges<Continuation, Element>::template call<Args...>();
}
};
template <class Continuation, class Element>
struct with_bidirectional_ranges {
template <class... Args>
static constexpr void call() {
using namespace test;
using test::range;
// Ditto always Eq; !IsSized && Diff is uninteresting (ranges::size still works).
Continuation::template call<Args...,
range<bidi, Element, Sized::no, CanDifference::no, Common::no, CanCompare::yes, ProxyRef::no>>();
Continuation::template call<Args...,
range<bidi, Element, Sized::no, CanDifference::no, Common::no, CanCompare::yes, ProxyRef::yes>>();
Continuation::template call<Args...,
range<bidi, Element, Sized::no, CanDifference::no, Common::yes, CanCompare::yes, ProxyRef::no>>();
Continuation::template call<Args...,
range<bidi, Element, Sized::no, CanDifference::no, Common::yes, CanCompare::yes, ProxyRef::yes>>();
Continuation::template call<Args...,
range<bidi, Element, Sized::yes, CanDifference::no, Common::no, CanCompare::yes, ProxyRef::no>>();
Continuation::template call<Args...,
range<bidi, Element, Sized::yes, CanDifference::no, Common::no, CanCompare::yes, ProxyRef::yes>>();
Continuation::template call<Args...,
range<bidi, Element, Sized::yes, CanDifference::no, Common::yes, CanCompare::yes, ProxyRef::no>>();
Continuation::template call<Args...,
range<bidi, Element, Sized::yes, CanDifference::no, Common::yes, CanCompare::yes, ProxyRef::yes>>();
Continuation::template call<Args...,
range<bidi, Element, Sized::yes, CanDifference::yes, Common::no, CanCompare::yes, ProxyRef::no>>();
Continuation::template call<Args...,
range<bidi, Element, Sized::yes, CanDifference::yes, Common::no, CanCompare::yes, ProxyRef::yes>>();
Continuation::template call<Args...,
range<bidi, Element, Sized::yes, CanDifference::yes, Common::yes, CanCompare::yes, ProxyRef::no>>();
Continuation::template call<Args...,
range<bidi, Element, Sized::yes, CanDifference::yes, Common::yes, CanCompare::yes, ProxyRef::yes>>();
with_random_ranges<Continuation, Element>::template call<Args...>();
}
};
template <class Continuation, class Element>
struct with_forward_ranges {
template <class... Args>
static constexpr void call() {
using namespace test;
using test::range;
// forward always has Eq; !IsSized && Diff is uninteresting (sized_range is sized_range).
Continuation::template call<Args...,
range<fwd, Element, Sized::no, CanDifference::no, Common::no, CanCompare::yes, ProxyRef::no>>();
Continuation::template call<Args...,
range<fwd, Element, Sized::no, CanDifference::no, Common::no, CanCompare::yes, ProxyRef::yes>>();
Continuation::template call<Args...,
range<fwd, Element, Sized::no, CanDifference::no, Common::yes, CanCompare::yes, ProxyRef::no>>();
Continuation::template call<Args...,
range<fwd, Element, Sized::no, CanDifference::no, Common::yes, CanCompare::yes, ProxyRef::yes>>();
Continuation::template call<Args...,
range<fwd, Element, Sized::yes, CanDifference::no, Common::no, CanCompare::yes, ProxyRef::no>>();
Continuation::template call<Args...,
range<fwd, Element, Sized::yes, CanDifference::no, Common::no, CanCompare::yes, ProxyRef::yes>>();
Continuation::template call<Args...,
range<fwd, Element, Sized::yes, CanDifference::no, Common::yes, CanCompare::yes, ProxyRef::no>>();
Continuation::template call<Args...,
range<fwd, Element, Sized::yes, CanDifference::no, Common::yes, CanCompare::yes, ProxyRef::yes>>();
Continuation::template call<Args...,
range<fwd, Element, Sized::yes, CanDifference::yes, Common::no, CanCompare::yes, ProxyRef::no>>();
Continuation::template call<Args...,
range<fwd, Element, Sized::yes, CanDifference::yes, Common::no, CanCompare::yes, ProxyRef::yes>>();
Continuation::template call<Args...,
range<fwd, Element, Sized::yes, CanDifference::yes, Common::yes, CanCompare::yes, ProxyRef::no>>();
Continuation::template call<Args...,
range<fwd, Element, Sized::yes, CanDifference::yes, Common::yes, CanCompare::yes, ProxyRef::yes>>();
with_bidirectional_ranges<Continuation, Element>::template call<Args...>();
}
};
template <class Continuation, class Element>
struct with_input_ranges {
template <class... Args>
static constexpr void call() {
using namespace test;
using test::range;
// For all ranges, IsCommon implies Eq.
// For single-pass ranges, Eq is uninteresting without IsCommon (there's only one valid iterator
// value at a time, and no reason to compare it with itself for equality).
Continuation::template call<Args...,
range<input, Element, Sized::no, CanDifference::no, Common::no, CanCompare::no, ProxyRef::no>>();
Continuation::template call<Args...,
range<input, Element, Sized::no, CanDifference::no, Common::no, CanCompare::no, ProxyRef::yes>>();
Continuation::template call<Args...,
range<input, Element, Sized::no, CanDifference::no, Common::yes, CanCompare::yes, ProxyRef::no>>();
Continuation::template call<Args...,
range<input, Element, Sized::no, CanDifference::no, Common::yes, CanCompare::yes, ProxyRef::yes>>();
Continuation::template call<Args...,
range<input, Element, Sized::no, CanDifference::yes, Common::no, CanCompare::no, ProxyRef::no>>();
Continuation::template call<Args...,
range<input, Element, Sized::no, CanDifference::yes, Common::no, CanCompare::no, ProxyRef::yes>>();
Continuation::template call<Args...,
range<input, Element, Sized::no, CanDifference::yes, Common::yes, CanCompare::yes, ProxyRef::no>>();
Continuation::template call<Args...,
range<input, Element, Sized::no, CanDifference::yes, Common::yes, CanCompare::yes, ProxyRef::yes>>();
Continuation::template call<Args...,
range<input, Element, Sized::yes, CanDifference::no, Common::no, CanCompare::no, ProxyRef::no>>();
Continuation::template call<Args...,
range<input, Element, Sized::yes, CanDifference::no, Common::no, CanCompare::no, ProxyRef::yes>>();
Continuation::template call<Args...,
range<input, Element, Sized::yes, CanDifference::no, Common::yes, CanCompare::yes, ProxyRef::no>>();
Continuation::template call<Args...,
range<input, Element, Sized::yes, CanDifference::no, Common::yes, CanCompare::yes, ProxyRef::yes>>();
Continuation::template call<Args...,
range<input, Element, Sized::yes, CanDifference::yes, Common::no, CanCompare::no, ProxyRef::no>>();
Continuation::template call<Args...,
range<input, Element, Sized::yes, CanDifference::yes, Common::no, CanCompare::no, ProxyRef::yes>>();
Continuation::template call<Args...,
range<input, Element, Sized::yes, CanDifference::yes, Common::yes, CanCompare::yes, ProxyRef::no>>();
Continuation::template call<Args...,
range<input, Element, Sized::yes, CanDifference::yes, Common::yes, CanCompare::yes, ProxyRef::yes>>();
with_forward_ranges<Continuation, Element>::template call<Args...>();
}
};
template <class Continuation, class Element>
struct with_output_ranges {
template <class... Args>
static constexpr void call() {
using namespace test;
using test::range;
// For all ranges, IsCommon implies Eq.
// For single-pass ranges, Eq is uninteresting without IsCommon (there's only one valid iterator
// value at a time, and no reason to compare it with itself for equality).
Continuation::template call<Args...,
range<output, Element, Sized::no, CanDifference::no, Common::no, CanCompare::no, ProxyRef::no>>();
Continuation::template call<Args...,
range<output, Element, Sized::no, CanDifference::no, Common::no, CanCompare::no, ProxyRef::yes>>();
Continuation::template call<Args...,
range<output, Element, Sized::no, CanDifference::no, Common::yes, CanCompare::yes, ProxyRef::no>>();
Continuation::template call<Args...,
range<output, Element, Sized::no, CanDifference::no, Common::yes, CanCompare::yes, ProxyRef::yes>>();
Continuation::template call<Args...,
range<output, Element, Sized::no, CanDifference::yes, Common::no, CanCompare::no, ProxyRef::no>>();
Continuation::template call<Args...,
range<output, Element, Sized::no, CanDifference::yes, Common::no, CanCompare::no, ProxyRef::yes>>();
Continuation::template call<Args...,
range<output, Element, Sized::no, CanDifference::yes, Common::yes, CanCompare::yes, ProxyRef::no>>();
Continuation::template call<Args...,
range<output, Element, Sized::no, CanDifference::yes, Common::yes, CanCompare::yes, ProxyRef::yes>>();
Continuation::template call<Args...,
range<output, Element, Sized::yes, CanDifference::no, Common::no, CanCompare::no, ProxyRef::no>>();
Continuation::template call<Args...,
range<output, Element, Sized::yes, CanDifference::no, Common::no, CanCompare::no, ProxyRef::yes>>();
Continuation::template call<Args...,
range<output, Element, Sized::yes, CanDifference::no, Common::yes, CanCompare::yes, ProxyRef::no>>();
Continuation::template call<Args...,
range<output, Element, Sized::yes, CanDifference::no, Common::yes, CanCompare::yes, ProxyRef::yes>>();
Continuation::template call<Args...,
range<output, Element, Sized::yes, CanDifference::yes, Common::no, CanCompare::no, ProxyRef::no>>();
Continuation::template call<Args...,
range<output, Element, Sized::yes, CanDifference::yes, Common::no, CanCompare::no, ProxyRef::yes>>();
Continuation::template call<Args...,
range<output, Element, Sized::yes, CanDifference::yes, Common::yes, CanCompare::yes, ProxyRef::no>>();
Continuation::template call<Args...,
range<output, Element, Sized::yes, CanDifference::yes, Common::yes, CanCompare::yes, ProxyRef::yes>>();
with_forward_ranges<Continuation, Element>::template call<Args...>();
}
};
template <class Continuation, class Element>
struct with_input_iterators {
template <class... Args>
static constexpr void call() {
using namespace test;
using test::iterator;
// IsSized and Eq are not significant for "lone" single-pass iterators, so we can ignore them here.
Continuation::template call<Args...,
iterator<input, Element, CanDifference::no, CanCompare::no, ProxyRef::no>>();
Continuation::template call<Args...,
iterator<input, Element, CanDifference::no, CanCompare::no, ProxyRef::yes>>();
// For forward and bidi, Eq is necessarily true but IsSized and Proxy may vary.
Continuation::template call<Args...,
iterator<fwd, Element, CanDifference::no, CanCompare::yes, ProxyRef::no>>();
Continuation::template call<Args...,
iterator<fwd, Element, CanDifference::no, CanCompare::yes, ProxyRef::yes>>();
Continuation::template call<Args...,
iterator<fwd, Element, CanDifference::yes, CanCompare::yes, ProxyRef::no>>();
Continuation::template call<Args...,
iterator<fwd, Element, CanDifference::yes, CanCompare::yes, ProxyRef::yes>>();
Continuation::template call<Args...,
iterator<bidi, Element, CanDifference::no, CanCompare::yes, ProxyRef::no>>();
Continuation::template call<Args...,
iterator<bidi, Element, CanDifference::no, CanCompare::yes, ProxyRef::yes>>();
Continuation::template call<Args...,
iterator<bidi, Element, CanDifference::yes, CanCompare::yes, ProxyRef::no>>();
Continuation::template call<Args...,
iterator<bidi, Element, CanDifference::yes, CanCompare::yes, ProxyRef::yes>>();
// Random iterators are IsSized and Eq - only Proxy varies.
Continuation::template call<Args...,
iterator<random, Element, CanDifference::yes, CanCompare::yes, ProxyRef::no>>();
Continuation::template call<Args...,
iterator<random, Element, CanDifference::yes, CanCompare::yes, ProxyRef::yes>>();
// Contiguous iterators are totally locked down.
Continuation::template call<Args..., iterator<contiguous, Element>>();
}
};