-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdeque.hpp
More file actions
3305 lines (2983 loc) · 109 KB
/
Copy pathdeque.hpp
File metadata and controls
3305 lines (2983 loc) · 109 KB
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 2025-2026 YexuanXiao
// Distributed under the MIT License.
// https://github.com/YexuanXiao/deque
#if !defined(BIZWEN_DEQUE_HPP)
#define BIZWEN_DEQUE_HPP
#pragma push_macro("BIZWEN_EXPORT")
#undef BIZWEN_EXPORT
#if !defined(BIZWEN_MODULE)
#define BIZWEN_EXPORT
// assert
#include <cassert>
// ptrdiff_t/size_t
#include <cstddef>
// ranges::copy/copy_back_ward/rotate/move/move_backward/remove/remove_if
#include <algorithm>
// strong_ordering/lexicographical_compare/lexicographical_compare_three_way
#include <compare>
// iterator concepts/reverse_iterator/sentinel/iterator tag
#include <iterator>
// construct_at/destroy_at/uninitialized algorithms
#include <memory>
// add_pointer/remove_pointer/remove_const/is_const/is_object
#include <type_traits>
// ranges::view_interface/subrange/sized_range/from_range_t/begin/end/swap/size/empty/views::all
#include <ranges>
// span
#include <span>
// move/forward
#include <utility>
// initializer_list
#include <initializer_list>
// __cpp_lib_containers_ranges/__cpp_lib_ranges_repeat
#include <version>
// polymorphic_allocator
#include <memory_resource>
#if defined(__cpp_exceptions)
// out_of_range
#include <stdexcept>
#else
// terminate
#include <exception>
#endif
#else
#define BIZWEN_EXPORT export
#endif
// 代码规范:
// 使用等号初始化
// 内部函数可以使用auto返回值
// 函数参数和常量加const
// 非API不使用size_t之外的整数
// const写右侧
// 不分配内存/构造/移动对象一律noexcept
// STL-vNext BEGIN
namespace bizwen
{
BIZWEN_EXPORT template <typename T, typename Alloc>
class deque;
namespace deque_detail
{
template <typename U, typename Alloc>
inline constexpr void destroy_range(Alloc &a, U first, U last)
{
static_assert(::std::is_same_v<::std::iter_value_t<U>, typename ::std::allocator_traits<Alloc>::value_type>);
for (; first != last; ++first)
{
::std::allocator_traits<Alloc>::destroy(a, ::std::to_address(first));
}
}
template <typename U, typename Alloc>
class throw_guard
{
U first_;
U ¤t_;
Alloc &a_;
bool flag = true;
public:
constexpr throw_guard(U &result, Alloc &al) noexcept : first_(result), current_(result), a_(al)
{
}
constexpr ~throw_guard()
{
if (flag)
{
destroy_range(a_, first_, current_);
}
}
constexpr void release() noexcept
{
flag = false;
}
};
template <typename U, typename V, typename W, typename X, typename Alloc>
inline constexpr void uninitialized_copy(Alloc &a, U first, V last, W first2, X last2)
{
throw_guard guard{first2, a};
for (; first != last && first2 != last2; ++first, (void)++first2)
{
::std::allocator_traits<Alloc>::construct(a, ::std::to_address(first2), *first);
}
guard.release();
}
template <typename U, typename V, typename T, typename Alloc>
inline constexpr void uninitialized_fill(Alloc &a, U first, V last, const T &value)
{
throw_guard guard{first, a};
for (; first != last; ++first)
{
::std::allocator_traits<Alloc>::construct(a, ::std::to_address(first), value);
}
guard.release();
}
template <typename U, typename V, typename W, typename X, typename Alloc>
inline constexpr void uninitialized_move(Alloc &a, U first, V last, W first2, X last2)
{
throw_guard guard{first2, a};
for (; first != last && first2 != last2; ++first, (void)++first2)
{
::std::allocator_traits<Alloc>::construct(a, ::std::to_address(first2), ::std::move(*first));
}
guard.release();
}
template <typename U, typename V, typename Alloc>
inline constexpr void uninitialized_value_construct(Alloc &a, U first, V last)
{
throw_guard guard{first, a};
for (; first != last; ++first)
{
::std::allocator_traits<Alloc>::construct(a, ::std::to_address(first));
}
guard.release();
}
template <typename T>
struct adl_firewall_impl_
{
// adl_firewall的关联类型包括adl_firewall_impl_<T>但不包括T
struct adl_firewall_
{
using type = T;
};
};
template <typename T>
using add_adl_firewall_t = adl_firewall_impl_<T>::adl_firewall_;
template <typename Firewall>
using remove_adl_firewall_t = Firewall::type;
template <typename A>
concept mini_alloc = requires(A &a) {
typename A::value_type;
a.allocate(::std::size_t(0));
};
// 用于从参数包中获得前两个对象(只有两个)的引用的辅助函数
template <typename U, typename V>
inline constexpr auto get_iter_pair(U &first, V &last) noexcept
{
struct iter_ref_pair
{
decltype(first) &src_begin;
decltype(last) &src_end;
};
return iter_ref_pair{first, last};
}
template <typename T>
inline constexpr auto to_address(T const t) noexcept
{
return ::std::to_address(t);
}
inline constexpr auto to_address(::std::nullptr_t) noexcept
{
return nullptr;
}
#if defined(BIZWEN_DEQUE_BLOCK_ELEMENTS)
template <typename T>
inline constexpr ::std::size_t block_elements_v = BIZWEN_DEQUE_BLOCK_ELEMENTS;
#else
template <typename T>
inline constexpr ::std::size_t block_elements_v = ::std::size_t(16) > ::std::size_t(4096) / sizeof(T)
? ::std::size_t(16)
: ::std::size_t(4096) / sizeof(T);
#endif
// 构造函数和赋值用,计算如何分配和构造
template <typename T>
inline constexpr auto calc_cap(::std::size_t const size) noexcept
{
auto const block_elems = block_elements_v<T>;
struct cap_t
{
::std::size_t block_size; // 需要分配多少block
::std::size_t full_blocks; // 分配了几个完整block
::std::size_t rem_elems; // 剩下的不完整block有多少元素
};
return cap_t{(size + block_elems - ::std::size_t(1)) / block_elems, size / block_elems, size % block_elems};
}
// 该函数计算位置,参数front_size是起始位置和块首的距离,pos是目标位置
// 对于负数pos返回负数位置
template <typename T>
inline constexpr auto calc_pos(::std::ptrdiff_t const front_size, ::std::ptrdiff_t const pos) noexcept
{
::std::ptrdiff_t const block_elems = block_elements_v<T>;
struct pos_t
{
::std::ptrdiff_t block_step; // 移动块的步数
::std::ptrdiff_t elem_step; // 移动元素的步数(相对于块首)
};
if (pos >= ::std::ptrdiff_t(0))
{
auto const new_pos = pos + front_size;
return pos_t{new_pos / block_elems, new_pos % block_elems};
}
else
{
auto const new_pos = pos + front_size - block_elems + ::std::ptrdiff_t(1);
return pos_t{new_pos / block_elems, new_pos % block_elems - ::std::ptrdiff_t(1) + block_elems};
}
}
template <typename T>
inline constexpr auto calc_pos(::std::size_t const front_size, ::std::size_t const pos) noexcept
{
::std::size_t const block_elems = block_elements_v<T>;
struct pos_t
{
::std::size_t block_step; // 移动块的步数
::std::size_t elem_step; // 移动元素的步数(相对于块首)
};
auto const new_pos = pos + front_size;
return pos_t{new_pos / block_elems, new_pos % block_elems};
}
template <typename T, typename Block, typename DiffType>
class buckets_type;
template <typename FirewallT, typename FirewallBlock, typename DiffType>
class bucket_iterator
{
using T = deque_detail::remove_adl_firewall_t<FirewallT>;
using Block = deque_detail::remove_adl_firewall_t<FirewallBlock>;
static_assert(::std::is_object_v<T> && ::std::is_object_v<Block> && ::std::is_signed_v<DiffType>);
using RConstT = ::std::remove_const_t<T>;
friend buckets_type<deque_detail::add_adl_firewall_t<RConstT>, FirewallBlock, DiffType>;
friend buckets_type<deque_detail::add_adl_firewall_t<T>, FirewallBlock, DiffType>;
friend bucket_iterator<deque_detail::add_adl_firewall_t<T const>, FirewallBlock, DiffType>;
Block *block_elem_begin_{};
Block *block_elem_end_{};
Block *block_elem_curr_{};
RConstT *elem_begin_begin_{};
RConstT *elem_begin_end_{};
RConstT *elem_end_begin_{};
RConstT *elem_end_end_{};
RConstT *elem_curr_begin_{};
RConstT *elem_curr_end_{};
constexpr bucket_iterator(Block *const block_elem_begin, Block *const block_elem_end, Block *const block_elem_curr,
RConstT *const elem_begin_begin, RConstT *const elem_begin_end,
RConstT *const elem_end_begin, RConstT *const elem_end_end,
RConstT *const elem_curr_begin, RConstT *const elem_curr_end) noexcept
: block_elem_begin_(block_elem_begin), block_elem_end_(block_elem_end), block_elem_curr_(block_elem_curr),
elem_begin_begin_(elem_begin_begin), elem_begin_end_(elem_begin_end), elem_end_begin_(elem_end_begin),
elem_end_end_(elem_end_end), elem_curr_begin_(elem_curr_begin), elem_curr_end_(elem_curr_end)
{
}
constexpr bucket_iterator<deque_detail::add_adl_firewall_t<RConstT>, FirewallBlock, DiffType> remove_const_()
const noexcept
requires(::std::is_const_v<T>)
{
return {block_elem_begin_, block_elem_end_, block_elem_curr_, elem_begin_begin_, elem_begin_end_,
elem_end_begin_, elem_end_end_, elem_curr_begin_, elem_curr_end_};
}
constexpr bucket_iterator &plus_and_assign_(::std::ptrdiff_t const pos) noexcept
{
block_elem_curr_ += pos;
if (block_elem_curr_ + ::std::size_t(1) == block_elem_end_)
{
elem_curr_begin_ = elem_end_begin_;
elem_curr_end_ = elem_end_end_;
}
else if (block_elem_curr_ == block_elem_begin_)
{
elem_curr_begin_ = elem_begin_begin_;
elem_curr_end_ = elem_begin_end_;
}
else
{
elem_curr_begin_ = *block_elem_begin_;
elem_curr_end_ = elem_begin_begin_ + block_elements_v<T>;
}
assert(block_elem_curr_ < block_elem_end_ && block_elem_curr_ >= block_elem_begin_);
return *this;
}
public:
using difference_type = DiffType;
using value_type = ::std::span<T>;
using pointer = value_type *;
using reference = value_type &;
using iterator_category = ::std::random_access_iterator_tag;
constexpr bucket_iterator() noexcept = default;
constexpr bucket_iterator(bucket_iterator const &other) noexcept = default;
constexpr bucket_iterator &operator=(bucket_iterator const &other) noexcept = default;
constexpr ~bucket_iterator() = default;
constexpr bucket_iterator &operator++() noexcept
{
++block_elem_curr_;
if (block_elem_curr_ + ::std::size_t(1) == block_elem_end_)
{
elem_curr_begin_ = elem_end_begin_;
elem_curr_end_ = elem_end_end_;
}
else
{
elem_curr_begin_ = *block_elem_begin_;
elem_curr_end_ = elem_begin_begin_ + block_elements_v<T>;
}
assert(block_elem_curr_ < block_elem_end_);
return *this;
}
constexpr bucket_iterator operator++(int) noexcept
{
#if defined(__cpp_auto_cast)
return ++auto{*this};
#else
auto temp = *this;
++temp;
return temp;
#endif
}
constexpr bucket_iterator &operator--() noexcept
{
--block_elem_curr_;
if (block_elem_curr_ == block_elem_begin_)
{
elem_curr_begin_ = elem_begin_begin_;
elem_curr_end_ = elem_begin_end_;
}
else
{
elem_begin_begin_ = *(block_elem_begin_ - ::std::size_t(1));
elem_begin_end_ = elem_begin_begin_ + block_elements_v<T>;
}
assert(block_elem_curr_ >= block_elem_begin_);
return *this;
}
constexpr bucket_iterator operator--(int) noexcept
{
#if defined(__cpp_auto_cast)
return --auto{*this};
#else
auto temp = *this;
--temp;
return temp;
#endif
}
constexpr bool operator==(bucket_iterator const &other) const noexcept
{
return block_elem_curr_ == other.block_elem_curr_;
}
constexpr ::std::strong_ordering operator<=>(bucket_iterator const &other) const noexcept
{
return block_elem_curr_ <=> other.block_elem_curr_;
}
constexpr difference_type operator-(bucket_iterator const &other) const noexcept
{
return block_elem_curr_ - other.block_elem_curr_;
}
constexpr ::std::span<T> operator[](difference_type const pos)
{
#if defined(__cpp_auto_cast)
return *(auto{*this} += pos);
#else
auto temp = *this;
temp += pos;
return *temp;
#endif
}
constexpr ::std::span<T> operator[](difference_type const pos) const noexcept
{
#if defined(__cpp_auto_cast)
return *(auto{*this} += pos);
#else
auto temp = *this;
temp += pos;
return *temp;
#endif
}
constexpr value_type operator*() noexcept
{
return {elem_curr_begin_, elem_curr_end_};
}
constexpr value_type operator*() const noexcept
{
return {elem_curr_begin_, elem_curr_end_};
}
constexpr bucket_iterator &operator+=(difference_type const pos) noexcept
{
return plus_and_assign_(pos);
}
constexpr bucket_iterator &operator-=(difference_type const pos) noexcept
{
return plus_and_assign_(-pos);
}
friend constexpr bucket_iterator operator+(bucket_iterator const &it, difference_type const pos) noexcept
{
#if defined(__cpp_auto_cast)
return auto{it}.plus_and_assign_(pos);
#else
auto temp = it;
temp.plus_and_assign_(pos);
return temp;
#endif
}
friend constexpr bucket_iterator operator+(difference_type const pos, bucket_iterator const &it) noexcept
{
return it + pos;
}
friend constexpr bucket_iterator operator-(difference_type const pos, bucket_iterator const &it) noexcept
{
return it + (-pos);
}
friend constexpr bucket_iterator operator-(bucket_iterator const &it, difference_type pos) noexcept
{
return it + (-pos);
}
constexpr operator bucket_iterator<deque_detail::add_adl_firewall_t<T const>,
deque_detail::add_adl_firewall_t<Block>, DiffType>() const noexcept
requires(!::std::is_const_v<T>)
{
return {block_elem_begin_, block_elem_end_, block_elem_curr_, elem_begin_begin_, elem_begin_end_,
elem_end_begin_, elem_end_end_, elem_curr_begin_, elem_curr_end_};
}
};
#if !defined(NDEBUG)
static_assert(::std::random_access_iterator<bucket_iterator<
deque_detail::add_adl_firewall_t<int>, deque_detail::add_adl_firewall_t<int *>, ::std::ptrdiff_t>>);
static_assert(
::std::random_access_iterator<bucket_iterator<deque_detail::add_adl_firewall_t<const int>,
deque_detail::add_adl_firewall_t<int *>, ::std::ptrdiff_t>>);
#endif
template <typename T, typename Block, typename DiffType>
class deque_iterator;
template <typename FirewallT, typename FirewallBlock, typename DiffType>
class buckets_type : public ::std::ranges::view_interface<buckets_type<FirewallT, FirewallBlock, DiffType>>
{
using T = deque_detail::remove_adl_firewall_t<FirewallT>;
using Block = deque_detail::remove_adl_firewall_t<FirewallBlock>;
static_assert(::std::is_object_v<T> && ::std::is_object_v<Block> && ::std::is_signed_v<DiffType>);
using RConstT = ::std::remove_const_t<T>;
template <typename U, typename Alloc>
friend class bizwen::deque;
friend buckets_type<deque_detail::add_adl_firewall_t<RConstT>, FirewallBlock, DiffType>;
friend deque_iterator<deque_detail::add_adl_firewall_t<T const>, FirewallBlock, DiffType>;
friend deque_iterator<deque_detail::add_adl_firewall_t<RConstT>, FirewallBlock, DiffType>;
Block *block_elem_begin_{};
Block *block_elem_end_{};
RConstT *elem_begin_begin_{};
RConstT *elem_begin_end_{};
RConstT *elem_end_begin_{};
RConstT *elem_end_end_{};
template <typename U, typename V>
constexpr buckets_type(U const block_elem_begin, U const block_elem_end, V const elem_begin_begin,
V const elem_begin_end, V const elem_end_begin, V const elem_end_end) noexcept
: block_elem_begin_(::std::to_address(block_elem_begin)), block_elem_end_(::std::to_address(block_elem_end)),
elem_begin_begin_(::std::to_address(elem_begin_begin)), elem_begin_end_(::std::to_address(elem_begin_end)),
elem_end_begin_(::std::to_address(elem_end_begin)), elem_end_end_(::std::to_address(elem_end_end))
{
}
constexpr ::std::span<T> at_impl(::std::size_t const pos) const noexcept
{
assert(block_elem_begin_ + pos <= block_elem_end_);
if (pos == ::std::size_t(0))
{
return {elem_begin_begin_, elem_begin_end_};
}
else if (block_elem_begin_ + pos + ::std::size_t(1) == block_elem_end_)
{
return {elem_end_begin_, elem_end_end_};
}
else
{
auto const begin = *(block_elem_begin_ + pos);
return {begin, begin + block_elements_v<T>};
}
}
public:
using value_type = ::std::span<T>;
using pointer = value_type *;
using reference = value_type &;
using const_pointer = value_type const *;
using const_reference = value_type const &;
using size_type = ::std::make_unsigned_t<DiffType>;
using difference_type = DiffType;
using iterator =
bucket_iterator<deque_detail::add_adl_firewall_t<T>, deque_detail::add_adl_firewall_t<Block>, DiffType>;
using const_iterator =
bucket_iterator<deque_detail::add_adl_firewall_t<T const>, deque_detail::add_adl_firewall_t<Block>, DiffType>;
using reverse_iterator = ::std::reverse_iterator<iterator>;
using const_reverse_iterator = ::std::reverse_iterator<const_iterator>;
constexpr buckets_type() = default;
constexpr ~buckets_type() = default;
constexpr buckets_type(buckets_type const &) = default;
constexpr buckets_type &operator=(buckets_type const &) = default;
constexpr size_type size() const noexcept
{
return static_cast<size_type>(block_elem_end_ - block_elem_begin_);
}
// empty and operator bool provided by view_interface
// Since bucket_iterator is not a continuous iterator,
// view_interface does not provide the member function data
// constexpr void data() const noexcept = delete;
constexpr ::std::span<T> front() const noexcept
{
return {elem_begin_begin_, elem_begin_end_};
}
constexpr ::std::span<T const> front() noexcept
requires(!::std::is_const_v<T>)
{
return {elem_begin_begin_, elem_begin_end_};
}
constexpr ::std::span<T> back() const noexcept
{
return {elem_end_begin_, elem_end_end_};
}
constexpr ::std::span<T const> back() noexcept
requires(!::std::is_const_v<T>)
{
return {elem_end_begin_, elem_end_end_};
}
constexpr ::std::span<T> at(size_type const pos) noexcept
{
return at_impl(pos);
}
constexpr ::std::span<const T> at(size_type const pos) const noexcept
requires(!::std::is_const_v<T>)
{
auto const s = at_impl(pos);
return {s.data(), s.size()};
}
constexpr const_iterator begin() const noexcept
{
return {block_elem_begin_, block_elem_end_, block_elem_begin_, elem_begin_begin_, elem_begin_end_,
elem_end_begin_, elem_end_end_, elem_begin_begin_, elem_begin_end_};
}
constexpr const_iterator end() const noexcept
{
if (block_elem_begin_ == block_elem_end_)
{
return {block_elem_begin_, block_elem_end_, block_elem_end_, elem_begin_begin_, elem_begin_end_,
elem_end_begin_, elem_end_end_, elem_end_begin_, elem_end_end_};
}
else
{
return {block_elem_begin_, block_elem_end_, block_elem_end_ - ::std::size_t(1),
elem_begin_begin_, elem_begin_end_, elem_end_begin_,
elem_end_end_, elem_end_begin_, elem_end_end_};
}
}
constexpr iterator begin() noexcept
{
return static_cast<buckets_type const &>(*this).begin().remove_const_();
}
constexpr iterator end() noexcept
{
return static_cast<buckets_type const &>(*this).end().remove_const_();
}
constexpr const_iterator cbegin() const noexcept
{
return begin();
}
constexpr const_iterator cend() const noexcept
{
return end();
}
constexpr auto rbegin() noexcept
{
return reverse_iterator{end()};
}
constexpr auto rend() noexcept
{
return reverse_iterator{begin()};
}
constexpr auto rbegin() const noexcept
{
return const_reverse_iterator{end()};
}
constexpr auto rend() const noexcept
{
return const_reverse_iterator{begin()};
}
constexpr auto crbegin() const noexcept
{
return const_reverse_iterator{end()};
}
constexpr auto crend() const noexcept
{
return const_reverse_iterator{begin()};
}
constexpr operator buckets_type<deque_detail::add_adl_firewall_t<T const>, deque_detail::add_adl_firewall_t<Block>,
DiffType>() const noexcept
requires(!::std::is_const_v<T>)
{
return {block_elem_begin_, block_elem_end_, elem_begin_begin_, elem_begin_end_, elem_end_begin_, elem_end_end_};
}
};
template <typename FirewallT, typename FirewallBlock, typename DiffType>
class deque_iterator
{
using T = deque_detail::remove_adl_firewall_t<FirewallT>;
using Block = deque_detail::remove_adl_firewall_t<FirewallBlock>;
static_assert(::std::is_object_v<T> && ::std::is_object_v<Block> && ::std::is_signed_v<DiffType>);
using RConstT = ::std::remove_const_t<T>;
template <typename U, typename Alloc>
friend class bizwen::deque;
friend deque_iterator<deque_detail::add_adl_firewall_t<T const>, FirewallBlock, DiffType>;
friend deque_iterator<deque_detail::add_adl_firewall_t<RConstT>, FirewallBlock, DiffType>;
Block *block_elem_curr_{};
Block *block_elem_end_{};
RConstT *elem_begin_{};
RConstT *elem_curr_{};
#if !defined(NDEBUG)
buckets_type<deque_detail::add_adl_firewall_t<T const>, deque_detail::add_adl_firewall_t<Block>, DiffType>
buckets_{};
constexpr bool verify() const noexcept
{
assert(block_elem_curr_ >= buckets_.block_elem_begin_);
assert(block_elem_end_ == buckets_.block_elem_end_);
if (block_elem_curr_ != nullptr)
{
assert(block_elem_curr_ < block_elem_end_);
assert(elem_begin_ == ::std::to_address(*block_elem_curr_));
}
assert(elem_curr_ >= elem_begin_);
assert(elem_curr_ <= elem_begin_ + block_elements_v<T>);
if (block_elem_curr_ == buckets_.block_elem_end_ - ::std::size_t(1))
{
assert(elem_curr_ <= buckets_.elem_end_end_);
}
if (block_elem_curr_ == buckets_.block_elem_begin_)
{
assert(elem_curr_ >= buckets_.elem_begin_begin_);
}
return true;
}
constexpr deque_iterator(
Block *block_curr, Block *block_end, RConstT *const begin, RConstT *const pos,
buckets_type<deque_detail::add_adl_firewall_t<T const>, deque_detail::add_adl_firewall_t<Block>, DiffType>
buckets) noexcept
: block_elem_curr_(block_curr), block_elem_end_(block_end), elem_begin_(deque_detail::to_address(begin)),
elem_curr_(deque_detail::to_address(pos)), buckets_(buckets)
{
}
#else
constexpr deque_iterator(Block *block_curr, Block *block_end, RConstT *const begin, RConstT *const pos) noexcept
: block_elem_curr_(block_curr), block_elem_end_(block_end), elem_begin_(deque_detail::to_address(begin)),
elem_curr_(deque_detail::to_address(pos))
{
}
#endif
constexpr deque_iterator<deque_detail::add_adl_firewall_t<RConstT>, deque_detail::add_adl_firewall_t<Block>,
DiffType> remove_const_() const noexcept
requires(::std::is_const_v<T>)
{
#if !defined(NDEBUG)
return {block_elem_curr_, block_elem_end_, elem_begin_, elem_curr_, buckets_};
#else
return {block_elem_curr_, block_elem_end_, elem_begin_, elem_curr_};
#endif
}
constexpr T &at_impl_(::std::ptrdiff_t const pos) const noexcept
{
assert(verify());
auto const res = deque_detail::calc_pos<T>(elem_curr_ - elem_begin_, pos);
auto const target_block = block_elem_curr_ + res.block_step;
assert(target_block < block_elem_end_);
return *((*target_block) + res.elem_step);
}
constexpr deque_iterator &plus_and_assign_(::std::ptrdiff_t const pos) noexcept
{
assert(verify());
if (pos != ::std::ptrdiff_t(0))
{
auto const res = deque_detail::calc_pos<T>(elem_curr_ - elem_begin_, pos);
auto const target_block = block_elem_curr_ + res.block_step;
if (target_block < block_elem_end_)
{
block_elem_curr_ = target_block;
elem_begin_ = ::std::to_address(*target_block);
elem_curr_ = elem_begin_ + res.elem_step;
}
else
{
assert(target_block == block_elem_end_);
assert(res.elem_step == ::std::size_t(0));
block_elem_curr_ = target_block - ::std::size_t(1);
elem_begin_ = ::std::to_address(*(target_block - ::std::size_t(1)));
elem_curr_ = elem_begin_ + deque_detail::block_elements_v<T>;
}
}
assert(verify());
return *this;
}
public:
using difference_type = DiffType;
using value_type = RConstT;
using pointer = T *;
using reference = T &;
using iterator_category = ::std::random_access_iterator_tag;
constexpr deque_iterator() noexcept = default;
constexpr deque_iterator(deque_iterator const &other) noexcept = default;
constexpr deque_iterator &operator=(deque_iterator const &other) noexcept = default;
constexpr ~deque_iterator() = default;
constexpr bool operator==(deque_iterator const &other) const noexcept
{
return elem_curr_ == other.elem_curr_;
}
constexpr ::std::strong_ordering operator<=>(deque_iterator const &other) const noexcept
{
assert(block_elem_end_ == other.block_elem_end_);
if (block_elem_curr_ < other.block_elem_curr_)
return ::std::strong_ordering::less;
if (block_elem_curr_ > other.block_elem_curr_)
return ::std::strong_ordering::greater;
if (elem_curr_ < other.elem_curr_)
return ::std::strong_ordering::less;
if (elem_curr_ > other.elem_curr_)
return ::std::strong_ordering::greater;
return ::std::strong_ordering::equal;
}
constexpr T *operator->() noexcept
{
assert(elem_curr_ != elem_begin_ + deque_detail::block_elements_v<T>);
return elem_curr_;
}
constexpr T *operator->() const noexcept
{
assert(elem_curr_ != elem_begin_ + deque_detail::block_elements_v<T>);
return elem_curr_;
}
constexpr T &operator*() noexcept
{
return *this->operator->();
}
constexpr T &operator*() const noexcept
{
return *this->operator->();
}
constexpr deque_iterator &operator++() noexcept
{
assert(verify());
// 空deque的迭代器不能自增,不需要考虑
assert(elem_curr_ != elem_begin_ + deque_detail::block_elements_v<T>);
++elem_curr_;
if (elem_curr_ == elem_begin_ + deque_detail::block_elements_v<T>)
{
if (block_elem_curr_ + ::std::size_t(1) != block_elem_end_)
{
++block_elem_curr_;
elem_begin_ = ::std::to_address(*block_elem_curr_);
elem_curr_ = elem_begin_;
}
}
assert(verify());
return *this;
}
constexpr deque_iterator operator++(int) noexcept
{
#if defined(__cpp_auto_cast)
return ++auto{*this};
#else
auto temp = *this;
++temp;
return temp;
#endif
}
constexpr deque_iterator &operator--() noexcept
{
assert(verify());
if (elem_curr_ == elem_begin_)
{
--block_elem_curr_;
elem_begin_ = ::std::to_address(*block_elem_curr_);
elem_curr_ = elem_begin_ + deque_detail::block_elements_v<T>;
}
--elem_curr_;
assert(verify());
return *this;
}
constexpr deque_iterator operator--(int) noexcept
{
#if defined(__cpp_auto_cast)
return --auto{*this};
#else
auto temp = *this;
--temp;
return temp;
#endif
}
constexpr T &operator[](difference_type const pos) noexcept
{
return at_impl_(pos);
}
constexpr T &operator[](difference_type const pos) const noexcept
{
return at_impl_(pos);
}
friend constexpr difference_type operator-(deque_iterator const &lhs, deque_iterator const &rhs) noexcept
{
assert(lhs.block_elem_end_ == rhs.block_elem_end_);
auto const block_size = lhs.block_elem_curr_ - rhs.block_elem_curr_;
return static_cast<difference_type>(block_size * static_cast<difference_type>(block_elements_v<T>) +
lhs.elem_curr_ - lhs.elem_begin_ - (rhs.elem_curr_ - rhs.elem_begin_));
}
constexpr deque_iterator &operator+=(difference_type const pos) noexcept
{
return plus_and_assign_(pos);
}
friend constexpr deque_iterator operator+(deque_iterator const &it, difference_type const pos) noexcept
{
#if defined(__cpp_auto_cast)
return auto{it}.plus_and_assign_(pos);
#else
auto temp = it;
temp.plus_and_assign_(pos);
return temp;
#endif
}
friend constexpr deque_iterator operator+(difference_type const pos, deque_iterator const &it) noexcept
{
return it + pos;
}
constexpr deque_iterator &operator-=(difference_type const pos) noexcept
{
return plus_and_assign_(-pos);
}
friend constexpr deque_iterator operator-(deque_iterator const &it, difference_type const pos) noexcept
{
return it + (-pos);
}
friend constexpr deque_iterator operator-(difference_type const pos, deque_iterator const &it) noexcept
{
return it + (-pos);
}
constexpr operator deque_iterator<deque_detail::add_adl_firewall_t<T const>,
deque_detail::add_adl_firewall_t<Block>, DiffType>() const noexcept
requires(!::std::is_const_v<T>)
{
#if !defined(NDEBUG)
return {block_elem_curr_, block_elem_end_, elem_begin_, elem_curr_, buckets_};
#else
return {block_elem_curr_, block_elem_end_, elem_begin_, elem_curr_};
#endif
}
};
#if !defined(__cpp_lib_ranges_repeat) || 1
template <typename T>
class repeat_iterator
{
::std::ptrdiff_t pos_{};
T const *value_ptr_{};
public:
using iterator_category = ::std::random_access_iterator_tag;
using value_type = T;
using difference_type = ::std::ptrdiff_t;
using pointer = T const *;
using reference = T const &;
constexpr repeat_iterator() = default;