-
Notifications
You must be signed in to change notification settings - Fork 9
/
cartesian_product.hpp
2027 lines (1727 loc) · 71.9 KB
/
cartesian_product.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#pragma once
//#define DISABLE_CART_PROD_IOTA_SPEC
/*
Adapted from TartanLlama/ranges: https://github.com/TartanLlama/ranges
Original version License CC0 1.0 Universal (see below)
Modified by Gonzalo Brito Gadeschi, NVIDIA corporation
Modifications under MIT license.
---
SPDX-FileCopyrightText: Copyright (c) 2022 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
SPDX-License-Identifier: MIT
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
---
Creative Commons Legal Code
CC0 1.0 Universal
CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE
LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN
ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS
INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES
REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS
PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM
THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED
HEREUNDER.
Statement of Purpose
The laws of most jurisdictions throughout the world automatically confer
exclusive Copyright and Related Rights (defined below) upon the creator
and subsequent owner(s) (each and all, an "owner") of an original work of
authorship and/or a database (each, a "Work").
Certain owners wish to permanently relinquish those rights to a Work for
the purpose of contributing to a commons of creative, cultural and
scientific works ("Commons") that the public can reliably and without fear
of later claims of infringement build upon, modify, incorporate in other
works, reuse and redistribute as freely as possible in any form whatsoever
and for any purposes, including without limitation commercial purposes.
These owners may contribute to the Commons to promote the ideal of a free
culture and the further production of creative, cultural and scientific
works, or to gain reputation or greater distribution for their Work in
part through the use and efforts of others.
For these and/or other purposes and motivations, and without any
expectation of additional consideration or compensation, the person
associating CC0 with a Work (the "Affirmer"), to the extent that he or she
is an owner of Copyright and Related Rights in the Work, voluntarily
elects to apply CC0 to the Work and publicly distribute the Work under its
terms, with knowledge of his or her Copyright and Related Rights in the
Work and the meaning and intended legal effect of CC0 on those rights.
1. Copyright and Related Rights. A Work made available under CC0 may be
protected by copyright and related or neighboring rights ("Copyright and
Related Rights"). Copyright and Related Rights include, but are not
limited to, the following:
i. the right to reproduce, adapt, distribute, perform, display,
communicate, and translate a Work;
ii. moral rights retained by the original author(s) and/or performer(s);
iii. publicity and privacy rights pertaining to a person's image or
likeness depicted in a Work;
iv. rights protecting against unfair competition in regards to a Work,
subject to the limitations in paragraph 4(a), below;
v. rights protecting the extraction, dissemination, use and reuse of data
in a Work;
vi. database rights (such as those arising under Directive 96/9/EC of the
European Parliament and of the Council of 11 March 1996 on the legal
protection of databases, and under any national implementation
thereof, including any amended or successor version of such
directive); and
vii. other similar, equivalent or corresponding rights throughout the
world based on applicable law or treaty, and any national
implementations thereof.
2. Waiver. To the greatest extent permitted by, but not in contravention
of, applicable law, Affirmer hereby overtly, fully, permanently,
irrevocably and unconditionally waives, abandons, and surrenders all of
Affirmer's Copyright and Related Rights and associated claims and causes
of action, whether now known or unknown (including existing as well as
future claims and causes of action), in the Work (i) in all territories
worldwide, (ii) for the maximum duration provided by applicable law or
treaty (including future time extensions), (iii) in any current or future
medium and for any number of copies, and (iv) for any purpose whatsoever,
including without limitation commercial, advertising or promotional
purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each
member of the public at large and to the detriment of Affirmer's heirs and
successors, fully intending that such Waiver shall not be subject to
revocation, rescission, cancellation, termination, or any other legal or
equitable action to disrupt the quiet enjoyment of the Work by the public
as contemplated by Affirmer's express Statement of Purpose.
3. Public License Fallback. Should any part of the Waiver for any reason
be judged legally invalid or ineffective under applicable law, then the
Waiver shall be preserved to the maximum extent permitted taking into
account Affirmer's express Statement of Purpose. In addition, to the
extent the Waiver is so judged Affirmer hereby grants to each affected
person a royalty-free, non transferable, non sublicensable, non exclusive,
irrevocable and unconditional license to exercise Affirmer's Copyright and
Related Rights in the Work (i) in all territories worldwide, (ii) for the
maximum duration provided by applicable law or treaty (including future
time extensions), (iii) in any current or future medium and for any number
of copies, and (iv) for any purpose whatsoever, including without
limitation commercial, advertising or promotional purposes (the
"License"). The License shall be deemed effective as of the date CC0 was
applied by Affirmer to the Work. Should any part of the License for any
reason be judged legally invalid or ineffective under applicable law, such
partial invalidity or ineffectiveness shall not invalidate the remainder
of the License, and in such case Affirmer hereby affirms that he or she
will not (i) exercise any of his or her remaining Copyright and Related
Rights in the Work or (ii) assert any associated claims and causes of
action with respect to the Work, in either case contrary to Affirmer's
express Statement of Purpose.
4. Limitations and Disclaimers.
a. No trademark or patent rights held by Affirmer are waived, abandoned,
surrendered, licensed or otherwise affected by this document.
b. Affirmer offers the Work as-is and makes no representations or
warranties of any kind concerning the Work, express, implied,
statutory or otherwise, including without limitation warranties of
title, merchantability, fitness for a particular purpose, non
infringement, or the absence of latent or other defects, accuracy, or
the present or absence of errors, whether or not discoverable, all to
the greatest extent permissible under applicable law.
c. Affirmer disclaims responsibility for clearing rights of other persons
that may apply to the Work or any use thereof, including without
limitation any person's Copyright and Related Rights in the Work.
Further, Affirmer disclaims responsibility for obtaining any necessary
consents, permissions or other rights required for any use of the
Work.
d. Affirmer understands and acknowledges that Creative Commons is not a
party to this document and has no duty or obligation with respect to
this CC0 or use of the Work.
*/
#include <algorithm>
#include <concepts>
#include <functional>
#include <iterator>
#include <ranges>
#include <tuple>
#include <type_traits>
#include <utility>
namespace tl {
namespace detail {
template <class I>
concept single_pass_iterator = std::input_or_output_iterator<I> && !std::forward_iterator<I>;
template <typename... V>
constexpr auto common_iterator_category() {
if constexpr ((std::ranges::random_access_range<V> && ...))
return std::random_access_iterator_tag{};
else if constexpr ((std::ranges::bidirectional_range<V> && ...))
return std::bidirectional_iterator_tag{};
else if constexpr ((std::ranges::forward_range<V> && ...))
return std::forward_iterator_tag{};
else if constexpr ((std::ranges::input_range<V> && ...))
return std::input_iterator_tag{};
else
return std::output_iterator_tag{};
}
}
template <class... V>
using common_iterator_category = decltype(detail::common_iterator_category<V...>());
template <class R>
concept simple_view = std::ranges::view<R> && std::ranges::range<const R> &&
std::same_as<std::ranges::iterator_t<R>, std::ranges::iterator_t<const R>> &&
std::same_as<std::ranges::sentinel_t<R>,
std::ranges::sentinel_t<const R>>;
struct as_sentinel_t {};
constexpr inline as_sentinel_t as_sentinel;
template <bool Const, class T>
using maybe_const = std::conditional_t<Const, const T, T>;
template <std::destructible T>
class basic_mixin : protected T {
public:
constexpr basic_mixin()
noexcept(std::is_nothrow_default_constructible<T>::value)
requires std::default_initializable<T> :
T() {}
constexpr basic_mixin(const T& t)
noexcept(std::is_nothrow_copy_constructible<T>::value)
requires std::copy_constructible<T> :
T(t) {}
constexpr basic_mixin(T&& t)
noexcept(std::is_nothrow_move_constructible<T>::value)
requires std::move_constructible<T> :
T(std::move(t)) {}
constexpr T& get() & noexcept { return *static_cast<T*>(this); }
constexpr const T& get() const& noexcept { return *static_cast<T const*>(this); }
constexpr T&& get() && noexcept { return std::move(*static_cast<T*>(this)); }
constexpr const T&& get() const&& noexcept { return std::move(*static_cast<T const*>(this)); }
};
namespace cursor {
namespace detail {
template <class C>
struct tags {
static constexpr auto single_pass() requires requires { { C::single_pass } -> std::convertible_to<bool>; } {
return C::single_pass;
}
static constexpr auto single_pass() { return false; }
static constexpr auto contiguous() requires requires { { C::contiguous } -> std::convertible_to<bool>; } {
return C::contiguous;
}
static constexpr auto contiguous() { return false; }
};
}
template <class C>
constexpr bool single_pass = detail::tags<C>::single_pass();
template <class C>
constexpr bool tagged_contiguous = detail::tags<C>::contiguous();
namespace detail {
template <class C>
struct deduced_mixin_t {
template <class T> static auto deduce(int)-> typename T::mixin;
template <class T> static auto deduce(...)->tl::basic_mixin<T>;
using type = decltype(deduce<C>(0));
};
}
template <class C>
using mixin_t = typename detail::deduced_mixin_t<C>::type;
template <class C>
requires
requires(const C& c) { c.read(); }
using reference_t = decltype(std::declval<const C&>().read());
namespace detail {
template <class C>
struct deduced_value_t {
template<class T> static auto deduce(int)-> typename T::value_type;
template<class T> static auto deduce(...)->std::decay_t<reference_t<T>>;
using type = decltype(deduce<C>(0));
};
}
template <class C>
requires std::same_as<typename detail::deduced_value_t<C>::type, std::decay_t<typename detail::deduced_value_t<C>::type>>
using value_type_t = typename detail::deduced_value_t<C>::type;
namespace detail {
template <class C>
struct deduced_difference_t {
template <class T> static auto deduce(int)-> typename T::difference_type;
template <class T>
static auto deduce(long)->decltype(std::declval<const T&>().distance_to(std::declval<const T&>()));
template <class T>
static auto deduce(...)->std::ptrdiff_t;
using type = decltype(deduce<C>(0));
};
}
template <class C>
using difference_type_t = typename detail::deduced_difference_t<C>::type;
template <class C>
concept cursor = std::semiregular<std::remove_cv_t<C>>
&& std::semiregular<mixin_t<std::remove_cv_t<C>>>
&& requires {typename difference_type_t<C>; };
template <class C>
concept readable = cursor<C> && requires(const C & c) {
c.read();
typename reference_t<C>;
typename value_type_t<C>;
};
template <class C>
concept arrow = readable<C>
&& requires(const C & c) { c.arrow(); };
template <class C, class T>
concept writable = cursor<C>
&& requires(C & c, T && t) { c.write(std::forward<T>(t)); };
template <class S, class C>
concept sentinel_for = cursor<C> && std::semiregular<S>
&& requires(const C & c, const S & s) { {c.equal(s)} -> std::same_as<bool>; };
template <class S, class C>
concept sized_sentinel_for = sentinel_for<S, C> &&
requires(const C & c, const S & s) {
{c.distance_to(s)} -> std::same_as<difference_type_t<C>>;
};
template <class C>
concept next = cursor<C> && requires(C & c) { c.next(); };
template <class C>
concept prev = cursor<C> && requires(C & c) { c.prev(); };
template <class C>
concept advance = cursor<C>
&& requires(C & c, difference_type_t<C> n) { c.advance(n); };
template <class C>
concept indirect_move = readable<C>
&& requires(const C & c) { c.indirect_move(); };
template <class C, class O>
concept indirect_swap = readable<C> && readable<O>
&& requires(const C & c, const O & o) {
c.indirect_swap(o);
o.indirect_swap(c);
};
template <class C>
concept input = readable<C> && next<C>;
template <class C>
concept forward = input<C> && sentinel_for<C, C> && !single_pass<C>;
template <class C>
concept bidirectional = forward<C> && prev<C>;
template <class C>
concept random_access = bidirectional<C> && advance<C> && sized_sentinel_for<C, C>;
template <class C>
concept contiguous = random_access<C> && tagged_contiguous<C> && std::is_reference_v<reference_t<C>>;
template <class C>
constexpr auto cpp20_iterator_category() {
if constexpr (contiguous<C>)
return std::contiguous_iterator_tag{};
else if constexpr (random_access<C>)
return std::random_access_iterator_tag{};
else if constexpr (bidirectional<C>)
return std::bidirectional_iterator_tag{};
else if constexpr (forward<C>)
return std::forward_iterator_tag{};
else
return std::input_iterator_tag{};
}
template <class C>
using cpp20_iterator_category_t = decltype(cpp20_iterator_category<C>());
//There were a few changes in requirements on iterators between C++17 and C++20
//See https://wg21.link/p2259 for discussion
//- C++17 didn't have contiguous iterators
//- C++17 input iterators required *it++ to be valid
//- C++17 forward iterators required the reference type to be exactly value_type&/value_type const& (i.e. not a proxy)
struct not_a_cpp17_iterator {};
template <class C>
concept reference_is_value_type_ref =
(std::same_as<reference_t<C>, value_type_t<C>&> || std::same_as<reference_t<C>, value_type_t<C> const&>);
template <class C>
concept can_create_postincrement_proxy =
(std::move_constructible<value_type_t<C>> && std::constructible_from<value_type_t<C>, reference_t<C>>);
template <class C>
constexpr auto cpp17_iterator_category() {
if constexpr (random_access<C>
#if !defined(__NVCOMPILER)
// YOLO: with nvc++ proxy iterators can be random access . . .
// BUG: Need to update Thrust to C++20 iterator categories
&& reference_is_value_type_ref<C>
#endif
)
return std::random_access_iterator_tag{};
else if constexpr (bidirectional<C> && reference_is_value_type_ref<C>)
return std::bidirectional_iterator_tag{};
else if constexpr (forward<C> && reference_is_value_type_ref<C>)
return std::forward_iterator_tag{};
else if constexpr (can_create_postincrement_proxy<C>)
return std::input_iterator_tag{};
else
return not_a_cpp17_iterator{};
}
template <class C>
using cpp17_iterator_category_t = decltype(cpp17_iterator_category<C>());
//iterator_concept and iterator_category are tricky; this abstracts them out.
//Since the rules for iterator categories changed between C++17 and C++20
//a C++20 iterator may have a weaker category in C++17,
//or it might not be a valid C++17 iterator at all.
//iterator_concept will be the C++20 iterator category.
//iterator_category will be the C++17 iterator category, or it will not exist
//in the case that the iterator is not a valid C++17 iterator.
template <cursor C, class category = cpp17_iterator_category_t<C>>
struct associated_types_category_base {
using iterator_category = category;
};
template <cursor C>
struct associated_types_category_base<C, not_a_cpp17_iterator> {};
template <cursor C>
struct associated_types : associated_types_category_base<C> {
using iterator_concept = cpp20_iterator_category_t<C>;
using value_type = cursor::value_type_t<C>;
using difference_type = cursor::difference_type_t<C>;
using reference = cursor::reference_t<C>;
};
namespace detail {
// We assume a cursor is writeable if it's either not readable
// or it is writeable with the same type it reads to
template <class C>
struct is_writable_cursor {
template <readable T>
requires requires (C c) {
c.write(c.read());
}
static auto deduce()->std::true_type;
template <readable T>
static auto deduce()->std::false_type;
template <class T>
static auto deduce()->std::true_type;
static constexpr bool value = decltype(deduce<C>())::value;
};
}
}
namespace detail {
template <class T>
struct post_increment_proxy {
private:
T cache_;
public:
template<typename U>
constexpr post_increment_proxy(U&& t)
: cache_(std::forward<U>(t))
{}
constexpr T const& operator*() const noexcept
{
return cache_;
}
};
}
template <cursor::input C>
class basic_iterator :
public cursor::mixin_t<C>
{
private:
using mixin = cursor::mixin_t<C>;
constexpr auto& cursor() noexcept { return this->mixin::get(); }
constexpr auto const& cursor() const noexcept { return this->mixin::get(); }
template <cursor::input>
friend class basic_iterator;
//TODO these need to change to support output iterators
using reference_t = decltype(std::declval<C>().read());
using const_reference_t = reference_t;
public:
using mixin::get;
using value_type = cursor::value_type_t<C>;
using difference_type = cursor::difference_type_t<C>;
using reference = cursor::reference_t<C>;
basic_iterator() = default;
using mixin::mixin;
constexpr explicit basic_iterator(C&& c)
noexcept(std::is_nothrow_constructible_v<mixin, C&&>) :
mixin(std::move(c)) {}
constexpr explicit basic_iterator(C const& c)
noexcept(std::is_nothrow_constructible_v<mixin, C const&>) :
mixin(c) {}
template <std::convertible_to<C> O>
constexpr basic_iterator(basic_iterator<O>&& that)
noexcept(std::is_nothrow_constructible<mixin, O&&>::value) :
mixin(that.cursor()) {}
template <std::convertible_to<C> O>
constexpr basic_iterator(const basic_iterator<O>& that)
noexcept(std::is_nothrow_constructible<mixin, const O&>::value) :
mixin(std::move(that.cursor())) {}
template <std::convertible_to<C> O>
constexpr basic_iterator& operator=(basic_iterator<O>&& that) &
noexcept(std::is_nothrow_assignable<C&, O&&>::value) {
cursor() = std::move(that.cursor());
return *this;
}
template <std::convertible_to<C> O>
constexpr basic_iterator& operator=(const basic_iterator<O>& that) &
noexcept(std::is_nothrow_assignable<C&, const O&>::value) {
cursor() = that.cursor();
return *this;
}
template <class T>
requires
(!std::same_as<std::decay_t<T>, basic_iterator> &&
!cursor::next<C>&&
cursor::writable<C, T>)
constexpr basic_iterator& operator=(T&& t) &
noexcept(noexcept(std::declval<C&>().write(static_cast<T&&>(t)))) {
cursor() = std::forward<T>(t);
return *this;
}
friend constexpr decltype(auto) iter_move(const basic_iterator& i)
#if !defined(__NVCOMPILER)
noexcept(noexcept(i.cursor().indirect_move()))
#endif
requires cursor::indirect_move<C> {
return i.cursor().indirect_move();
}
template <class O>
requires cursor::indirect_swap<C, O>
friend constexpr void iter_swap(
const basic_iterator& x, const basic_iterator<O>& y)
#if !defined(__NVCOMPILER)
noexcept(noexcept((void)x.indirect_swap(y)))
#endif
{
x.indirect_swap(y);
}
//Input iterator
constexpr decltype(auto) operator*() const
noexcept(noexcept(std::declval<const C&>().read()))
requires (cursor::readable<C> && !cursor::detail::is_writable_cursor<C>::value) {
return cursor().read();
}
//Output iterator
constexpr decltype(auto) operator*()
noexcept(noexcept(reference_t{ cursor() }))
requires (cursor::next<C>&& cursor::detail::is_writable_cursor<C>::value) {
return reference_t{ cursor() };
}
//Output iterator
constexpr decltype(auto) operator*() const
noexcept(noexcept(
const_reference_t{ cursor() }))
requires (cursor::next<C>&& cursor::detail::is_writable_cursor<C>::value) {
return const_reference_t{ cursor() };
}
constexpr basic_iterator& operator*() noexcept
requires (!cursor::next<C>) {
return *this;
}
// operator->: "Manual" deduction override,
constexpr decltype(auto) operator->() const
noexcept(noexcept(cursor().arrow()))
requires cursor::arrow<C> {
return cursor().arrow();
}
// operator->: Otherwise, if reference_t is an lvalue reference,
constexpr decltype(auto) operator->() const
noexcept(noexcept(*std::declval<const basic_iterator&>()))
requires (cursor::readable<C> && !cursor::arrow<C>)
&& std::is_lvalue_reference<const_reference_t>::value{
return std::addressof(**this);
}
// modifiers
constexpr basic_iterator& operator++() & noexcept {
return *this;
}
constexpr basic_iterator& operator++() &
noexcept(noexcept(std::declval<basic_iterator>().cursor().next()))
requires cursor::next<C> {
cursor().next();
return *this;
}
//C++17 required that *it++ was valid.
//For input iterators, we can't copy *this, so we need to create a proxy reference.
constexpr auto operator++(int) &
noexcept(noexcept(++std::declval<basic_iterator&>()) &&
std::is_nothrow_move_constructible_v<value_type>&&
std::is_nothrow_constructible_v<value_type, reference>)
requires (cursor::single_pass<C>&&
std::move_constructible<value_type>&&
std::constructible_from<value_type, reference>) {
detail::post_increment_proxy<value_type> p(**this);
++* this;
return p;
}
//If we can't create a proxy reference, it++ is going to return void
constexpr void operator++(int) &
noexcept(noexcept(++std::declval<basic_iterator&>()))
requires (cursor::single_pass<C> && !(std::move_constructible<value_type>&&
std::constructible_from<value_type, reference>)) {
(void)(++(*this));
}
//If C is a forward cursor then copying it is fine
constexpr basic_iterator operator++(int) &
noexcept(std::is_nothrow_copy_constructible_v<C>&&
std::is_nothrow_move_constructible_v<C> &&
noexcept(++std::declval<basic_iterator&>()))
requires (!cursor::single_pass<C>) {
auto temp = *this;
++* this;
return temp;
}
constexpr basic_iterator& operator--() &
noexcept(noexcept(cursor().prev()))
requires cursor::bidirectional<C> {
cursor().prev();
return *this;
}
//Postfix decrement doesn't have the same issue as postfix increment
//because bidirectional requires the cursor to be a forward cursor anyway
//so copying it is fine.
constexpr basic_iterator operator--(int) &
noexcept(std::is_nothrow_copy_constructible<basic_iterator>::value&&
std::is_nothrow_move_constructible<basic_iterator>::value &&
noexcept(--std::declval<basic_iterator&>()))
requires cursor::bidirectional<C> {
auto tmp = *this;
--* this;
return tmp;
}
constexpr basic_iterator& operator+=(difference_type n) &
noexcept(noexcept(cursor().advance(n)))
requires cursor::random_access<C> {
cursor().advance(n);
return *this;
}
constexpr basic_iterator& operator-=(difference_type n) &
noexcept(noexcept(cursor().advance(-n)))
requires cursor::random_access<C> {
cursor().advance(-n);
return *this;
}
constexpr decltype(auto) operator[](difference_type n) const
noexcept(noexcept(*(std::declval<basic_iterator&>() + n)))
requires cursor::random_access<C> {
return *(*this + n);
}
// non-template type-symmetric ops to enable implicit conversions
friend constexpr difference_type operator-(
const basic_iterator& x, const basic_iterator& y)
noexcept(noexcept(y.cursor().distance_to(x.cursor())))
requires cursor::sized_sentinel_for<C, C> {
return y.cursor().distance_to(x.cursor());
}
friend constexpr bool operator==(
const basic_iterator& x, const basic_iterator& y)
#if !defined(__NVCOMPILER)
noexcept(noexcept(x.cursor().equal(y.cursor())))
requires cursor::sentinel_for<C, C>
#endif
{
return x.cursor().equal(y.cursor());
}
friend constexpr bool operator!=(
const basic_iterator& x, const basic_iterator& y)
#if !defined(__NVCOMPILER)
noexcept(noexcept(!(x == y)))
requires cursor::sentinel_for<C, C>
#endif
{
return !(x == y);
}
friend constexpr bool operator<(
const basic_iterator& x, const basic_iterator& y)
#if !defined(__NVCOMPILER)
noexcept(noexcept(y - x))
#endif
requires cursor::sized_sentinel_for<C, C> {
return 0 < (y - x);
}
friend constexpr bool operator>(
const basic_iterator& x, const basic_iterator& y)
#if !defined(__NVCOMPILER)
noexcept(noexcept(y - x))
#endif
requires cursor::sized_sentinel_for<C, C> {
return 0 > (y - x);
}
friend constexpr bool operator<=(
const basic_iterator& x, const basic_iterator& y)
#if !defined(__NVCOMPILER)
noexcept(noexcept(y - x))
#endif
requires cursor::sized_sentinel_for<C, C> {
return 0 <= (y - x);
}
friend constexpr bool operator>=(
const basic_iterator& x, const basic_iterator& y)
#if !defined(__NVCOMPILER)
noexcept(noexcept(y - x))
#endif
requires cursor::sized_sentinel_for<C, C> {
return 0 >= (y - x);
}
};
namespace detail {
template <class C>
struct is_basic_iterator {
template <class T>
static auto deduce(basic_iterator<T> const&)->std::true_type;
template <class T>
static auto deduce(...)->std::false_type;
static constexpr inline bool value = decltype(deduce(std::declval<C>()))::value;
};
}
// basic_iterator nonmember functions
template <class C>
constexpr basic_iterator<C> operator+(
const basic_iterator<C>& i, cursor::difference_type_t<C> n)
noexcept(std::is_nothrow_copy_constructible<basic_iterator<C>>::value&&
std::is_nothrow_move_constructible<basic_iterator<C>>::value &&
noexcept(std::declval<basic_iterator<C>&>() += n))
requires cursor::random_access<C> {
auto tmp = i;
tmp += n;
return tmp;
}
template <class C>
constexpr basic_iterator<C> operator+(
cursor::difference_type_t<C> n, const basic_iterator<C>& i)
noexcept(noexcept(i + n))
requires cursor::random_access<C> {
return i + n;
}
template <class C>
constexpr basic_iterator<C> operator-(
const basic_iterator<C>& i, cursor::difference_type_t<C> n)
noexcept(noexcept(i + (-n)))
requires cursor::random_access<C> {
return i + (-n);
}
template <class C1, class C2>
requires cursor::sized_sentinel_for<C1, C2>
constexpr cursor::difference_type_t<C2> operator-(
const basic_iterator<C1>& lhs, const basic_iterator<C2>& rhs)
noexcept(noexcept(
rhs.get().distance_to(lhs.get()))) {
return rhs.get().distance_to(lhs.get());
}
template <class C, class S>
requires cursor::sized_sentinel_for<S, C>
constexpr cursor::difference_type_t<C> operator-(
const S& lhs, const basic_iterator<C>& rhs)
noexcept(noexcept(rhs.get().distance_to(lhs))) {
return rhs.get().distance_to(lhs);
}
template <class C, class S>
requires cursor::sized_sentinel_for<S, C>
constexpr cursor::difference_type_t<C> operator-(
const basic_iterator<C>& lhs, const S& rhs)
noexcept(noexcept(-(rhs - lhs))) {
return -(rhs - lhs);
}
template <class C1, class C2>
requires cursor::sentinel_for<C2, C1>
constexpr bool operator==(
const basic_iterator<C1>& lhs, const basic_iterator<C2>& rhs)
noexcept(noexcept(lhs.get().equal(rhs.get()))) {
return lhs.get().equal(rhs.get());
}
template <class C, class S>
requires cursor::sentinel_for<S, C>
constexpr bool operator==(
const basic_iterator<C>& lhs, const S& rhs)
noexcept(noexcept(lhs.get().equal(rhs))) {
return lhs.get().equal(rhs);
}
template <class C, class S>
requires cursor::sentinel_for<S, C>
constexpr bool operator==(
const S& lhs, const basic_iterator<C>& rhs)
noexcept(noexcept(rhs == lhs)) {
return rhs == lhs;
}
template <class C1, class C2>
requires cursor::sentinel_for<C2, C1>
constexpr bool operator!=(
const basic_iterator<C1>& lhs, const basic_iterator<C2>& rhs)
noexcept(noexcept(!(lhs == rhs))) {
return !(lhs == rhs);
}
template <class C, class S>
requires cursor::sentinel_for<S, C>
constexpr bool operator!=(
const basic_iterator<C>& lhs, const S& rhs)
noexcept(noexcept(!lhs.get().equal(rhs))) {
return !lhs.get().equal(rhs);
}
template <class C, class S>
requires cursor::sentinel_for<S, C>
constexpr bool operator!=(
const S& lhs, const basic_iterator<C>& rhs)
noexcept(noexcept(!rhs.get().equal(lhs))) {
return !rhs.get().equal(lhs);
}
template <class C1, class C2>
requires cursor::sized_sentinel_for<C1, C2>
constexpr bool operator<(
const basic_iterator<C1>& lhs, const basic_iterator<C2>& rhs)
noexcept(noexcept(lhs - rhs < 0)) {
return (lhs - rhs) < 0;
}
template <class C1, class C2>
requires cursor::sized_sentinel_for<C1, C2>
constexpr bool operator>(
const basic_iterator<C1>& lhs, const basic_iterator<C2>& rhs)
noexcept(noexcept((lhs - rhs) > 0)) {
return (lhs - rhs) > 0;
}
template <class C1, class C2>
requires cursor::sized_sentinel_for<C1, C2>
constexpr bool operator<=(
const basic_iterator<C1>& lhs, const basic_iterator<C2>& rhs)
noexcept(noexcept((lhs - rhs) <= 0)) {
return (lhs - rhs) <= 0;
}
template <class C1, class C2>
requires cursor::sized_sentinel_for<C1, C2>
constexpr bool operator>=(
const basic_iterator<C1>& lhs, const basic_iterator<C2>& rhs)
noexcept(noexcept((lhs - rhs) >= 0)) {
return (lhs - rhs) >= 0;
}
template <class V, bool Const>
class basic_sentinel {
using Base = std::conditional_t<Const, const V, V>;
public:
std::ranges::sentinel_t<Base> end_{};
basic_sentinel() = default;
constexpr explicit basic_sentinel(std::ranges::sentinel_t<Base> end)
: end_{ std::move(end) } {}
constexpr basic_sentinel(basic_sentinel<V, !Const> other) requires Const&& std::
convertible_to<std::ranges::sentinel_t<V>,
std::ranges::sentinel_t<Base>>
: end_{ std::move(other.end_) } {}
constexpr auto end() const {
return end_;
}
friend class basic_sentinel<V, !Const>;
};
//tl::compose composes f and g such that compose(f,g)(args...) is f(g(args...)), i.e. g is called first
template <class F, class G>
struct compose_fn {
[[no_unique_address]] F f;
[[no_unique_address]] G g;
template <class A, class B>
compose_fn(A&& a, B&& b) : f(std::forward<A>(a)), g(std::forward<B>(b)) {}
template <class A, class B, class ... Args>
static constexpr auto call(A&& a, B&& b, Args&&... args) {
if constexpr (std::is_void_v<std::invoke_result_t<G, Args...>>) {
std::invoke(std::forward<B>(b), std::forward<Args>(args)...);
return std::invoke(std::forward<A>(a));
}
else {
return std::invoke(std::forward<A>(a), std::invoke(std::forward<B>(b), std::forward<Args>(args)...));
}
}
template <class... Args>
constexpr auto operator()(Args&&... args) & {
return call(f, g, std::forward<Args>(args)...);
}
template <class... Args>
constexpr auto operator()(Args&&... args) const& {
return call(f, g, std::forward<Args>(args)...);
}
template <class... Args>
constexpr auto operator()(Args&&... args)&& {
return call(std::move(f), std::move(g), std::forward<Args>(args)...);
}
template <class... Args>
constexpr auto operator()(Args&&... args) const&& {
return call(std::move(f), std::move(g), std::forward<Args>(args)...);
}
};
template <class F, class G>
constexpr auto compose(F&& f, G&& g) {
return compose_fn<std::remove_cvref_t<F>, std::remove_cvref_t<G>>(std::forward<F>(f), std::forward<G>(g));
}
//tl::pipeable takes some invocable and enables:
//- Piping a single argument to it such that a | pipeable is the same as pipeable(a)
//- Piping it to another pipeable object, such that a | b is the same as tl::compose(b, a)
struct pipeable_base {};
template <class T>
concept is_pipeable = std::is_base_of_v<pipeable_base, std::remove_cvref_t<T>>;
template <class F>
struct pipeable_fn : pipeable_base {
[[no_unique_address]] F f_;
constexpr pipeable_fn(F f) : f_(std::move(f)) {}
template <class... Args>
constexpr auto operator()(Args&&... args) const requires std::invocable<F, Args...> {
return std::invoke(f_, std::forward<Args>(args)...);
}
};
template <class F>
constexpr auto pipeable(F f) {
return pipeable_fn{ std::move(f) };
}
template <class V, class Pipe>
constexpr auto operator|(V&& v, Pipe&& fn)
requires (!is_pipeable<V> && is_pipeable<Pipe> && std::invocable<Pipe, V>) {
return std::invoke(std::forward<Pipe>(fn).f_, std::forward<V>(v));
}
template <class Pipe1, class Pipe2>
constexpr auto operator|(Pipe1&& p1, Pipe2&& p2)
requires (is_pipeable<Pipe1>&& is_pipeable<Pipe2>) {
return pipeable(compose(std::forward<Pipe2>(p2).f_, std::forward<Pipe1>(p1).f_));
}
//tl::bind_back binds the last N arguments of f to the given ones, returning a new closure
template <class F, class... Args>
constexpr auto bind_back(F&& f, Args&&... args) {
return[f_ = std::forward<F>(f), ...args_ = std::forward<Args>(args)]
(auto&&... other_args)
requires std::invocable<F&, decltype(other_args)..., Args&...> {
return std::invoke(f_, std::forward<decltype(other_args)>(other_args)..., args_...);
};
}
}
namespace std {
template <class C>
struct iterator_traits<tl::basic_iterator<C>> : tl::cursor::associated_types<C> {};