-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist_array.hpp
More file actions
10978 lines (10068 loc) · 532 KB
/
list_array.hpp
File metadata and controls
10978 lines (10068 loc) · 532 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
#ifndef LIST_ARRAY
#define LIST_ARRAY
#include <algorithm>
#include <concepts>
#include <cstring>
#include <functional>
#include <iterator>
#include <memory>
#include <stdexcept>
#include <stdint.h>
#include <string.h>
#include <type_traits>
#include <utility>
/**
* @namespace __new_list_array_impl
* @brief Implementation details for a hybrid `list_array` container.
*
* This namespace contains the implementation of the `list_array` class, a container designed to
* offer the random access capabilities of an array and the insertion/deletion flexibility of a list.
* It is particularly well-suited for move-only types where copying is expensive.
*/
namespace _list_array_impl {
template <typename... Ts>
struct conditions_helper {};
template <typename T, typename _ = void>
struct is_container : std::false_type {};
template <typename T>
struct is_container<
T,
std::conditional_t<
false,
conditions_helper<
typename T::value_type,
typename T::size_type,
typename T::iterator,
typename T::const_iterator,
decltype(std::declval<T>().size()),
decltype(std::declval<T>().begin()),
decltype(std::declval<T>().end()),
decltype(std::declval<T>().cbegin()),
decltype(std::declval<T>().cend())>,
void>> : std::true_type {
using container_type = typename T::value_type;
using container = T;
};
template <typename T, typename _ = void>
struct can_direct_index : std::false_type {};
template <typename T>
struct can_direct_index<
T,
std::conditional_t<
false,
conditions_helper<decltype(std::declval<T>().data())>,
void>> : public std::true_type {};
template <typename FN, typename>
struct is_apply_invocable : std::false_type {};
template <typename FN, typename... T>
struct is_apply_invocable<FN, std::tuple<T...>> : std::bool_constant<std::is_invocable_v<FN, std::add_lvalue_reference_t<T>...> || std::is_invocable_v<FN, std::add_rvalue_reference_t<T>...>> {
};
template <typename FN, typename... T>
struct is_apply_invocable<FN, std::pair<T...>> : std::bool_constant<std::is_invocable_v<FN, std::add_lvalue_reference_t<T>...> || std::is_invocable_v<FN, std::add_rvalue_reference_t<T>...>> {
};
template <typename Ret, class FN, typename>
struct is_apply_invocable_r : std::false_type {};
template <typename Ret, typename FN, typename... T>
struct is_apply_invocable_r<Ret, FN, std::tuple<T...>> : std::bool_constant<std::is_invocable_r_v<Ret, FN, std::add_lvalue_reference_t<T>...> || std::is_invocable_r_v<Ret, FN, std::add_rvalue_reference_t<T>...>> {
};
template <typename Ret, typename FN, typename... T>
struct is_apply_invocable_r<Ret, FN, std::pair<T...>> : std::bool_constant<std::is_invocable_r_v<Ret, FN, std::add_lvalue_reference_t<T>...> || std::is_invocable_r_v<Ret, FN, std::add_rvalue_reference_t<T>...>> {
};
template <typename T>
static inline constexpr bool can_direct_index_v = can_direct_index<T>::value;
template <typename T>
static inline constexpr bool is_container_v = is_container<T>::value;
template <typename FN, typename Tupple>
static inline constexpr bool is_apply_invocable_v = is_apply_invocable<FN, Tupple>::value;
template <typename Ret, typename FN, typename Tupple>
static inline constexpr bool is_apply_invocable_r_v = is_apply_invocable_r<Ret, FN, Tupple>::value;
template <class T>
concept is_equality_comparable = requires(const T& it) { it == it; } || requires(const T it) { it == it; };
template <class T0, class T1>
concept is_equality_comparable_with = requires(const T0& it0, const T0& it1) { it0 == it1; };
/**
* @struct compressed_allocator
* @brief A space-optimized allocator that stores a value alongside its allocation state.
*
* This allocator is designed to be used when the allocator itself is small or empty, allowing
* it to store an additional value without incurring significant overhead.
*
* @tparam Alloc The underlying allocator type.
* @tparam T The type of the value to store alongside the allocator.
*/
template <class Alloc, class T, bool = std::is_empty_v<Alloc> && !std::is_final_v<Alloc>>
struct compressed_allocator final : private Alloc {
public:
T hold_value{};
template <class... Args>
constexpr compressed_allocator(const Alloc& allocator, Args&&... args)
: Alloc(allocator), hold_value(std::forward<Args>(args)...) {}
constexpr compressed_allocator(compressed_allocator<Alloc, T>&& move)
: Alloc(std::move(move)), hold_value(std::move(move.hold_value)) {}
constexpr compressed_allocator(const compressed_allocator<Alloc, T>& move)
: Alloc(move), hold_value(move.hold_value) {}
using value_type = typename Alloc::value_type;
using size_type = typename Alloc::size_type;
constexpr value_type* allocate(size_type n) {
return Alloc::allocate(n);
}
constexpr void deallocate(value_type* p, size_type n) {
Alloc::deallocate(p, n);
}
constexpr Alloc& get_allocator() {
return *this;
}
constexpr const Alloc& get_allocator() const {
return *this;
}
constexpr compressed_allocator<Alloc, T>& operator=(const compressed_allocator<Alloc, T>& allocator) {
if constexpr (std::allocator_traits<Alloc>::propagate_on_container_copy_assignment::value) {
Alloc::operator=(allocator);
}
hold_value = allocator.hold_value;
return *this;
}
constexpr compressed_allocator<Alloc, T>& operator=(compressed_allocator<Alloc, T>&& allocator) {
if constexpr (std::allocator_traits<Alloc>::propagate_on_container_move_assignment::value) {
Alloc::operator=(std::move(allocator));
}
hold_value = std::move(allocator.hold_value);
return *this;
}
};
/**
* @struct compressed_allocator
* @brief A space-optimized allocator that stores a value alongside its allocation state.
*
* This allocator is designed to be used when the allocator itself is small or empty, allowing
* it to store an additional value without incurring significant overhead.
*
* @tparam Alloc The underlying allocator type.
* @tparam T The type of the value to store alongside the allocator.
*/
template <class Alloc, class T>
struct compressed_allocator<Alloc, T, false> final {
Alloc allocator;
public:
T hold_value{};
template <class... Args>
constexpr compressed_allocator(const Alloc& allocator, Args&&... args)
: allocator(allocator), hold_value(std::forward<Args>(args)...) {}
constexpr compressed_allocator(compressed_allocator<Alloc, T>&& move)
: allocator(move), hold_value(std::move(move.hold_value)) {}
using value_type = typename Alloc::value_type;
using size_type = typename Alloc::size_type;
constexpr value_type* allocate(size_type n) {
return allocator.allocate(n);
}
constexpr void deallocate(value_type* p, size_type n) {
allocator.deallocate(p, n);
}
constexpr Alloc& get_allocator() {
return allocator;
}
constexpr const Alloc& get_allocator() const {
return allocator;
}
constexpr compressed_allocator<Alloc, T>& operator=(const compressed_allocator<Alloc, T>& allocator) {
if constexpr (std::allocator_traits<Alloc>::propagate_on_container_copy_assignment::value) {
this->allocator = allocator;
}
hold_value = allocator.hold_value;
return *this;
}
constexpr compressed_allocator<Alloc, T>& operator=(compressed_allocator<Alloc, T>&& allocator) {
if constexpr (std::allocator_traits<Alloc>::propagate_on_container_move_assignment::value) {
this->allocator = std::move(allocator);
}
hold_value = std::move(allocator.hold_value);
return *this;
}
};
/**
* @struct auto_deallocate
* @brief A RAII (Resource Acquisition Is Initialization) helper class that automatically deallocates
* memory allocated by an allocator when it goes out of scope.
*/
template <class T, class Allocator>
struct auto_deallocate {
T* data;
Allocator& allocator;
size_t size;
constexpr auto_deallocate(T* data, Allocator& allocator, size_t size)
: data(data), allocator(allocator), size(size) {}
~auto_deallocate() {
if (data)
allocator.deallocate(data, size);
}
constexpr void release() noexcept {
data = nullptr;
}
};
/**
* @class bit_array_helper
* @brief A helper class to efficiently manage a bit array, used for tracking selected elements during removals.
*/
class bit_array_helper {
uint8_t* data;
size_t _set_values = 0;
public:
constexpr bit_array_helper(size_t size) : data(new uint8_t[(size + 7) / 8]) {
size_t compressed_size = (size + 7) / 8;
for (size_t i = 0; i < compressed_size; i++)
data[i] = 0;
}
~bit_array_helper() {
delete[] data;
}
constexpr void set(size_t index, bool value) noexcept {
if (get(index) != value) {
if (value)
_set_values++;
else
_set_values--;
}
if (value)
data[index / 8] |= 1 << (index % 8);
else
data[index / 8] &= ~(1 << (index % 8));
}
constexpr bool get(size_t index) const noexcept {
return data[index / 8] & (1 << (index % 8));
}
constexpr size_t set_values() const noexcept {
return _set_values;
}
};
template <class T, class Allocator>
class custom_unique_ptr {
T* data;
Allocator& allocator;
void (*deleter)(T*, Allocator&);
public:
constexpr custom_unique_ptr(T* data, Allocator& allocator, void (*deleter)(T*, Allocator&))
: data(data), allocator(allocator), deleter(deleter) {}
~custom_unique_ptr() {
if (data)
deleter(data, allocator);
data = 0;
}
constexpr T* get() const noexcept {
return data;
}
constexpr T* release() noexcept {
T* temp = data;
data = nullptr;
return temp;
}
constexpr T* operator->() const noexcept {
return data;
}
constexpr T& operator*() const noexcept {
return *data;
}
};
/**
* @struct arr_block
* @brief A block of memory used to store elements within the `list_array`.
*
* Each `arr_block` holds a contiguous array of elements and pointers to the next
* and previous blocks in the list.
*
* @tparam T The type of the elements stored in the block.
*/
template <class T>
struct arr_block {
arr_block<T>* next;
arr_block<T>* prev;
T* data;
size_t data_size;
template <class Allocator>
static constexpr arr_block<T>* create_block(Allocator& allocator, size_t size) {
custom_unique_ptr block(new arr_block<T>, allocator, __destroy_block);
auto_deallocate<T, Allocator> hold(allocator.allocate(size), allocator, size);
block->data = hold.data;
block->data_size = size;
block->next = nullptr;
block->prev = nullptr;
hold.release();
return block.release();
}
template <class Allocator>
static constexpr void destroy_block(arr_block<T>* block, Allocator& allocator) {
if (block->data)
allocator.deallocate(block->data, block->data_size);
delete block;
}
template <class Allocator>
static constexpr auto create_safe_block(Allocator& allocator, size_t size) {
return custom_unique_ptr(create_block(allocator, size), allocator, __destroy_block);
}
private:
template <class Allocator>
static constexpr void __destroy_block(arr_block<T>* block, Allocator& allocator) {
if (block->data)
allocator.deallocate(block->data, block->data_size);
delete block;
}
};
/**
* @class arr_block_manager
* @brief Manages the allocation and deallocation of `arr_block` instances.
*
* @tparam T The type of the elements stored in the blocks.
* @tparam Allocator The allocator type used for memory management.
*/
template <class T, class Allocator>
class arr_block_manager {
arr_block<T>* first = nullptr;
arr_block<T>* last = nullptr;
Allocator& allocator;
public:
constexpr arr_block_manager(Allocator& allocator) noexcept
: allocator(allocator) {}
~arr_block_manager() {
clear();
}
constexpr void add_block(arr_block<T>* block) noexcept {
if (!first) {
first = last = block;
} else {
last->next = block;
block->prev = last;
last = block;
}
}
constexpr arr_block<T>* allocate_and_take_from(size_t block_size, auto& iter) {
auto block = arr_block<T>::create_safe_block(allocator, block_size);
size_t constructed_at = 0;
try {
for (size_t i = 0; i < block_size; i++, ++iter, ++constructed_at)
std::construct_at(block->data + i, std::move(*iter));
} catch (...) {
for (size_t j = 0; j < constructed_at; j++)
std::destroy_at(block->data + j);
throw;
}
return block.release();
}
constexpr void clear() {
while (first) {
auto temp = first;
first = first->next;
for (size_t k = 0; k < temp->data_size; k++) {
std::destroy_at(temp->data + k);
}
arr_block<T>::destroy_block(temp, allocator);
}
}
constexpr arr_block<T>* get_first() const noexcept {
return first;
}
constexpr arr_block<T>* get_last() const noexcept {
return last;
}
constexpr void release() noexcept {
first = last = nullptr;
}
};
/**
* @class list_array
* @brief A hybrid list-array container optimized for move-only types.
*
* This class provides the random access capabilities of an array combined with
* the insertion/deletion flexibility of a linked list. It's designed to be
* efficient for storing move-only types, as it minimizes unnecessary copying
* of elements.
*
* @tparam T The type of elements stored in the container.
* @tparam Allocator The allocator type used for memory management.
*
* @note This class is not thread-safe.
* @note Can operate on move-only elements.
* @note The iterator will become invalid after removing, taking, committing, decommitting, inserting, sorting, or any other operation that modifies the block size or removes the block from the array.
*/
template <class T, class Allocator>
class list_array {
public:
class const_iterator {
friend class list_array<T, Allocator>;
arr_block<T>* block;
size_t relative_index;
size_t absolute_index;
public:
using iterator_category = std::bidirectional_iterator_tag;
using value_type = T;
using difference_type = ptrdiff_t;
using pointer = const T*;
using reference = const T&;
constexpr const_iterator(arr_block<T>* block, size_t relative_index, size_t absolute_index) noexcept
: block(block), relative_index(relative_index), absolute_index(absolute_index) {}
constexpr const_iterator(const const_iterator& iterator) noexcept
: block(iterator.block), relative_index(iterator.relative_index), absolute_index(iterator.absolute_index) {}
constexpr const_iterator& operator=(const_iterator iterator) noexcept {
block = iterator.block;
relative_index = iterator.relative_index;
absolute_index = iterator.absolute_index;
return *this;
}
constexpr const_iterator& operator++() noexcept {
if (block) {
if (++relative_index >= block->data_size) {
block = block->next;
relative_index = 0;
}
}
absolute_index++;
return *this;
}
constexpr const_iterator& operator--() noexcept {
if (block) {
if (relative_index-- == 0) {
block = block->prev;
if (block)
relative_index = block->data_size - 1;
else
relative_index = 0;
}
}
absolute_index--;
return *this;
}
constexpr const_iterator& operator+=(size_t add) {
size_t remaining = add;
absolute_index += add;
while (remaining > 0) {
size_t space_left = block->data_size - relative_index;
if (space_left > remaining) {
relative_index += remaining;
return *this;
}
remaining -= space_left;
block = block->next;
relative_index = 0;
}
return *this;
}
constexpr const_iterator& operator-=(size_t sub) {
if (sub > absolute_index)
throw std::out_of_range("list_array::const_iterator::operator-=: sub out of range");
size_t remaining = sub;
absolute_index -= sub;
while (remaining > 0) {
if (relative_index >= remaining) {
relative_index -= remaining;
return *this;
}
remaining -= relative_index + 1;
block = block->prev;
relative_index = block->data_size - 1;
}
return *this;
}
constexpr const_iterator operator+(size_t add) const {
const_iterator copy = *this;
copy += add;
return copy;
}
constexpr const_iterator operator-(size_t sub) const {
const_iterator copy = *this;
copy -= sub;
return copy;
}
constexpr const_iterator operator++(int) noexcept {
const_iterator copy = *this;
++(*this);
return copy;
}
constexpr const_iterator operator--(int) noexcept {
const_iterator copy = *this;
--(*this);
return copy;
}
constexpr const T& operator*() const noexcept {
return block->data[relative_index];
}
constexpr const T* operator->() const noexcept {
return &block->data[relative_index];
}
constexpr bool operator==(const const_iterator& other) const noexcept {
return absolute_index == other.absolute_index;
}
constexpr bool operator!=(const const_iterator& other) const noexcept {
return absolute_index != other.absolute_index;
}
ptrdiff_t operator-(const const_iterator& other) {
return ptrdiff_t(absolute_index) - other.absolute_index;
}
};
class iterator {
friend class list_array<T, Allocator>;
arr_block<T>* block;
size_t relative_index;
size_t absolute_index;
template <bool copy_construct, bool make_move = false>
constexpr void _fast_load(T* arr, size_t arr_size) {
size_t j = relative_index;
arr_block<T>* block_tmp = block;
size_t block_size = block_tmp->data_size;
T* block_arr = block_tmp->data;
for (size_t i = 0; i < arr_size;) {
for (; i < arr_size && j < block_size; j++) {
if constexpr (make_move) {
if constexpr (copy_construct)
std::construct_at(arr + i++, std::move(block_arr[j]));
else
arr[i++] = std::move(block_arr[j]);
} else {
if constexpr (copy_construct)
std::construct_at(arr + i++, block_arr[j]);
else
arr[i++] = block_arr[j];
}
}
j = 0;
block_tmp = block_tmp->next;
if (!block_tmp)
return;
block_size = block_tmp->data_size;
block_arr = block_tmp->data;
}
}
public:
using iterator_category = std::bidirectional_iterator_tag;
using value_type = T;
using difference_type = ptrdiff_t;
using pointer = T*;
using reference = T&;
constexpr iterator(arr_block<T>* block, size_t relative_index, size_t absolute_index) noexcept
: block(block), relative_index(relative_index), absolute_index(absolute_index) {}
constexpr iterator(const iterator& iterator) noexcept
: block(iterator.block), relative_index(iterator.relative_index), absolute_index(iterator.absolute_index) {}
constexpr iterator& operator=(iterator iterator) noexcept {
block = iterator.block;
relative_index = iterator.relative_index;
absolute_index = iterator.absolute_index;
return *this;
}
constexpr iterator& operator++() noexcept {
if (block) {
if (++relative_index >= block->data_size) {
block = block->next;
relative_index = 0;
}
}
absolute_index++;
return *this;
}
constexpr iterator& operator--() noexcept {
if (block) {
if (relative_index-- == 0) {
block = block->prev;
if (block)
relative_index = block->data_size - 1;
else
relative_index = 0;
}
}
absolute_index--;
return *this;
}
constexpr iterator operator++(int) noexcept {
iterator copy = *this;
++(*this);
return copy;
}
constexpr iterator operator--(int) noexcept {
iterator copy = *this;
--(*this);
return copy;
}
constexpr iterator& operator+=(size_t add) {
size_t remaining = add;
absolute_index += add;
while (remaining > 0) {
size_t space_left = block->data_size - relative_index;
if (space_left > remaining) {
relative_index += remaining;
return *this;
}
remaining -= space_left;
block = block->next;
relative_index = 0;
}
return *this;
}
constexpr iterator& operator-=(size_t sub) {
if (sub > absolute_index)
throw std::out_of_range("list_array::iterator::operator-=: sub out of range");
size_t remaining = sub;
absolute_index -= sub;
while (remaining > 0) {
if (relative_index >= remaining) {
relative_index -= remaining;
return *this;
}
remaining -= relative_index + 1;
block = block->prev;
relative_index = block->data_size - 1;
}
return *this;
}
constexpr iterator operator+(size_t add) const {
iterator copy = *this;
copy += add;
return copy;
}
constexpr iterator operator-(size_t sub) const {
iterator copy = *this;
copy -= sub;
return copy;
}
constexpr T& operator*() {
return block->data[relative_index];
}
constexpr T* operator->() {
return &block->data[relative_index];
}
constexpr bool operator==(const iterator& other) const noexcept {
return absolute_index == other.absolute_index;
}
constexpr bool operator!=(const iterator& other) const noexcept {
return absolute_index != other.absolute_index;
}
constexpr operator const_iterator() const noexcept {
return const_iterator(block, relative_index, absolute_index);
}
ptrdiff_t operator-(const iterator& other) {
return ptrdiff_t(absolute_index) - other.absolute_index;
}
};
class reverse_iterator {
friend class list_array<T, Allocator>;
arr_block<T>* block;
size_t relative_index;
size_t absolute_index;
public:
using iterator_category = std::bidirectional_iterator_tag;
using value_type = T;
using difference_type = ptrdiff_t;
using pointer = T*;
using reference = T&;
constexpr reverse_iterator() {
block = nullptr;
relative_index = 0;
absolute_index = 0;
}
constexpr reverse_iterator& operator=(const iterator& setter) {
block = setter.block;
relative_index = setter.relative_index;
absolute_index = setter.absolute_index;
return *this;
}
constexpr reverse_iterator(const iterator& copy) {
*this = copy;
}
constexpr reverse_iterator& operator=(const reverse_iterator& setter) {
block = setter.block;
relative_index = setter.relative_index;
absolute_index = setter.absolute_index;
return *this;
}
constexpr reverse_iterator(const reverse_iterator& copy) {
*this = copy;
}
constexpr reverse_iterator(arr_block<T>* block_pos, size_t relative_pos, size_t absolute_pos) {
block = block_pos;
relative_index = relative_pos;
absolute_index = absolute_pos;
}
constexpr reverse_iterator& operator++() {
if (block) {
if (0 == --relative_index) {
block = block->prev;
relative_index = block ? block->data_size : 0;
}
--absolute_index;
}
return *this;
}
constexpr reverse_iterator operator++(int) {
reverse_iterator tmp = *this;
operator++();
return tmp;
}
constexpr reverse_iterator& operator--() {
if (block) {
if (block->data_size == ++relative_index) {
block = block->next;
relative_index = 0;
}
++absolute_index;
}
return *this;
}
constexpr reverse_iterator operator--(int) {
reverse_iterator tmp = *this;
operator--();
return tmp;
}
constexpr reverse_iterator& operator+=(size_t sub) {
if (sub > absolute_index)
throw std::out_of_range("list_array::reverse_iterator::operator+=: sub out of range");
size_t remaining = sub;
absolute_index -= sub;
while (remaining > 0) {
if (relative_index >= remaining) {
relative_index -= remaining;
return *this;
}
remaining -= relative_index + 1;
block = block->prev;
relative_index = block->data_size - 1;
}
return *this;
}
constexpr reverse_iterator& operator-=(size_t add) {
size_t remaining = add;
absolute_index += add;
while (remaining > 0) {
size_t space_left = block->data_size - relative_index;
if (space_left > remaining) {
relative_index += remaining;
return *this;
}
remaining -= space_left;
block = block->next;
relative_index = 0;
}
return *this;
}
constexpr reverse_iterator operator+(size_t add) const {
reverse_iterator copy = *this;
copy += add;
return copy;
}
constexpr reverse_iterator operator-(size_t sub) const {
reverse_iterator copy = *this;
copy -= sub;
return copy;
}
constexpr bool operator==(const reverse_iterator& comparer) const {
return absolute_index == comparer.absolute_index;
}
constexpr bool operator!=(const reverse_iterator& comparer) const {
return absolute_index != comparer.absolute_index;
}
constexpr T& operator*() {
return block->data[relative_index - 1];
}
constexpr const T& operator*() const {
return block->data[relative_index - 1];
}
constexpr T* operator->() {
return block->data + relative_index - 1;
}
constexpr ptrdiff_t operator-(const reverse_iterator& other) {
return ptrdiff_t(other.absolute_index) - absolute_index;
}
};
class const_reverse_iterator {
friend class list_array<T, Allocator>;
arr_block<T>* block;
size_t relative_index;
size_t absolute_index;
public:
using iterator_category = std::bidirectional_iterator_tag;
using value_type = T;
using difference_type = ptrdiff_t;
using pointer = const T*;
using reference = const T&;
constexpr const_reverse_iterator() {
block = nullptr;
relative_index = 0;
absolute_index = 0;
}
constexpr const_reverse_iterator& operator=(const iterator& setter) {
block = setter.block;
relative_index = setter.relative_index;
absolute_index = setter.absolute_index;
return *this;
}
constexpr const_reverse_iterator(const iterator& copy) {
*this = copy;
}
constexpr const_reverse_iterator& operator=(const const_iterator& setter) {
block = setter.block;
relative_index = setter.relative_index;
absolute_index = setter.absolute_index;
return *this;
}
constexpr const_reverse_iterator(const const_iterator& copy) {
*this = copy;
}
constexpr const_reverse_iterator& operator=(const reverse_iterator& setter) {
block = setter.block;
relative_index = setter.relative_index;
absolute_index = setter.absolute_index;
return *this;
}
constexpr const_reverse_iterator(const reverse_iterator& copy) {
*this = copy;
}
constexpr const_reverse_iterator& operator=(const const_reverse_iterator& setter) {
block = setter.block;
relative_index = setter.relative_index;
absolute_index = setter.absolute_index;
return *this;
}
constexpr const_reverse_iterator(const const_reverse_iterator& copy) {
*this = copy;
}
constexpr const_reverse_iterator(arr_block<T>* block_pos, size_t relative_pos, size_t absolute_pos) {
block = block_pos;
relative_index = relative_pos;
absolute_index = absolute_pos;
}
constexpr const_reverse_iterator& operator++() {
if (block) {
if (0 == --relative_index) {
block = block->prev;
relative_index = block ? block->data_size : 0;
}
--absolute_index;
}
return *this;
}
constexpr const_reverse_iterator operator++(int) {
const_reverse_iterator tmp = *this;
operator++();
return tmp;
}
constexpr reverse_iterator& operator--() {
if (block) {
if (block->data_size == ++relative_index) {
block = block->next;
relative_index = 0;
}
++absolute_index;
}
return *this;
}