-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathList.h
1462 lines (1277 loc) · 49.4 KB
/
List.h
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
#include <memory>
#include <iterator>
#include <algorithm>
#include <limits>
#include <bit>
#include <compare>
#include "utils/macros.h"
#include "debug.h"
// utils::List<T> is doubly-linked list like std::list<T>.
//
// Like std::list, the iterator is just a wrapper around a Node* (sizeof(iterator) == sizeof(void*)).
//
// utils::List has the following extra features:
//
// * iterator is derived from const_iterator; making the conversion from iterator to const_iterator a non-op
// and allowing a static_cast in the opposite direction.
// * const_iterator (and therefore iterator) have the member functions:
// bool is_begin();
// bool is_end();
// which return if the current iterator is pointing to the begin() or end() of a list, without the
// need to have access to the list itself!
// For example, instead of requiring `iter != my_list.end()` it is possible to do `!iter.is_end()`
// (which is just a single bitwise AND).
// * the value_type gives access to the previous and next elements, also without the need to have access
// to the underlying list.
// For example,
// list_type::value_type first_element = my_list.front();
// list_type::iterator second_element_iter = first_element.next(); // Doesn't need my_list.
// if (!second_element.is_end())
// {
// list_type::value_type second_element = *second_element_iter;
//
// The latter feature means, however, that value_type != T.
// This list has:
//
// template<typename T>
// class List {
// public:
// class value_type : protected Node {
// T data_;
//
// where Node is defined as
//
// struct Node {
// Node* prev_;
// Node* next_;
// };
//
// Note that value_type can be constructed like T and automatically converts to T; so it can be treated
// as-if it is of type T - it is just larger.
//
// A std::list uses the same amount of memory, it just defines value_type to be T:
//
// template<typename T>
// class list {
// public:
// using value_type = T;
// private:
// class Data : public Node {
// T value_;
#define DEBUG_MERGE_SORT 0
#if DEBUG_MERGE_SORT
#include "utils/print_using.h"
#endif
namespace utils {
template<typename T>
class ListBase
{
public:
using size_type = std::size_t;
using difference_type = std::ptrdiff_t;
public:
class const_iterator;
class iterator;
struct Node
{
Node* prev_;
Node* next_;
bool is_tainted() const { return reinterpret_cast<uintptr_t>(prev_) & 1; }
uintptr_t get_taint() const { return reinterpret_cast<uintptr_t>(prev_) & 1; }
};
protected:
Node base_node_{taint(&base_node_), &base_node_}; // The base node of the list. Contains pointers to the first and last node.
size_type size_{0}; // A count of the number of elements currently in the list.
protected:
static Node* taint(Node* ptr) { return reinterpret_cast<Node*>(reinterpret_cast<uintptr_t>(ptr) | 1); }
static Node* untaint(Node* ptr) { return reinterpret_cast<Node*>(reinterpret_cast<uintptr_t>(ptr) & ~uintptr_t{1}); }
[[gnu::always_inline]] static Node* extract_taint(Node* ptr, uintptr_t& taint_out)
{
uintptr_t integral = reinterpret_cast<uintptr_t>(ptr);
taint_out = integral & 1;
return reinterpret_cast<Node*>(integral & ~uintptr_t{1});
}
// Assign the result to the argument that extract_taint was called with, passing the return of that as second parameter.
[[gnu::always_inline]] static Node* assign_with_taint(Node* new_ptr, uintptr_t extracted_taint)
{
return reinterpret_cast<Node*>(reinterpret_cast<uintptr_t>(new_ptr) | extracted_taint);
}
public:
class value_type : protected Node
{
// Need access to Node.
friend class const_iterator;
friend class iterator;
friend class ListBase;
private:
T data_;
public:
template<typename... Args>
value_type(Args&&... args) : data_(std::forward<Args>(args)...) { }
inline iterator prev();
inline const_iterator prev() const;
inline iterator next();
inline const_iterator next() const;
operator T&() { return data_; }
operator T const&() const { return data_; }
};
using reference = value_type&;
using const_reference = value_type const&;
// Iterator types.
class const_iterator
{
public:
// Iterator type aliases.
using iterator_category = std::bidirectional_iterator_tag;
using value_type = ListBase::value_type;
using difference_type = ListBase::difference_type;
using pointer = value_type const*;
using reference = value_type const&;
private:
friend class iterator;
friend class ListBase;
Node* ptr_;
public:
const_iterator() = default;
explicit const_iterator(Node* ptr) : ptr_(ptr) { }
// Construct an `end()` iterator.
const_iterator(ListBase const& list) : ptr_(const_cast<Node*>(&list.base_node_)) { }
bool is_end() const { return ptr_->is_tainted(); }
bool is_begin() const { return !is_end() && ptr_->prev_->is_tainted(); }
pointer operator->() const { ASSERT(!is_end()); return static_cast<pointer>(ptr_); }
reference operator*() const { ASSERT(!is_end()); return static_cast<reference>(*ptr_); }
const_iterator& operator++()
{
ptr_ = ptr_->next_;
return *this;
}
const_iterator operator++(int)
{
const_iterator tmp = *this;
++(*this);
return tmp;
}
const_iterator& operator--()
{
ptr_ = ListBase<T>::untaint(ptr_->prev_);
return *this;
}
const_iterator operator--(int)
{
const_iterator tmp = *this;
--(*this);
return tmp;
}
friend bool operator==(const_iterator const& rhs, const_iterator const& lhs) { return rhs.ptr_ == lhs.ptr_; }
friend bool operator!=(const_iterator const& rhs, const_iterator const& lhs) { return rhs.ptr_ != lhs.ptr_; }
};
class iterator : public const_iterator
{
using base = const_iterator;
public:
using pointer = value_type*;
using reference = value_type&;
public:
using const_iterator::const_iterator;
pointer operator->() const { return static_cast<pointer>(base::ptr_); }
reference operator*() const { return static_cast<reference>(*base::ptr_); }
iterator& operator++() { return static_cast<iterator&>(++static_cast<const_iterator&>(*this)); }
iterator operator++(int) { iterator result{*this}; operator++(); return result; }
iterator& operator--() { return static_cast<iterator&>(--static_cast<const_iterator&>(*this)); }
iterator operator--(int) { iterator result{*this}; operator--(); return result; }
};
using reverse_iterator = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
protected:
// Cast back and forth between value_type* and Node*.
static Node* node_cast(value_type* value) { return static_cast<Node*>(value); }
static value_type* value_type_cast(Node* node) { return static_cast<value_type*>(node); }
// Return the number of elements in the range [first, last).
static size_type count(const_iterator first, const_iterator last)
{
size_type result = 0;
for (Node* node = first.ptr_; node != last.ptr_; node = node->next_)
++result;
return result;
}
// Return true if pos == end().
bool is_end(const_iterator pos) const { return pos.ptr_ == &base_node_; }
// Add new_node to the end. new_node must have been allocated with List<T, Allocator>::create_node.
void push_back_node(Node* new_node)
{
new_node->next_ = &base_node_;
new_node->prev_ = untaint(base_node_.prev_);
new_node->prev_->next_ = new_node;
base_node_.prev_ = taint(new_node);
++size_;
}
// Append new_node after `after`.
// This function may be called repeatedly where each subsequent call must use the previous new_node as after, appending a whole chain.
// After the last call, one must call link(last_new_node, next_existing_node), where next_existing_node is not allowed to be end,
// otherwise call link(last_new_node, possibly_end, extracted_taint).
// size_ is not updated and must be updated separately.
[[gnu::always_inline]] void link_node(Node* after, Node* new_node)
{
after->next_ = new_node;
new_node->prev_ = after; // new_node != end, therefore prev_ is not tainted.
}
// Same but with optional taint. In this case new_node is allowed to be end (provided the correct taint is supplied).
// size_ is not updated and must be updated separately.
[[gnu::always_inline]] void link_node(Node* after, Node* new_node, uintptr_t extracted_taint)
{
after->next_ = new_node;
new_node->prev_ = assign_with_taint(after, extracted_taint);
}
// Same, but insert pre-allocated new_node before pos.
// pos is not allowed to be end(), use push_back_node in that case.
void insert_node(const_iterator pos, Node* new_node)
{
link_node(pos.ptr_->prev_, new_node); // Connect pos.ptr_->prev_ ⇄ new_node
link_node(new_node, pos.ptr_); // Connect new_node ⇄ pos
++size_;
}
// Does NOT update size_!
// The taint of base_node_.prev_ must be fixed if the list is empty after this call.
Node* extract_front()
{
// From:
//
// | base_node_ | begin | second_element |
// | |
// ____________________v_________________v________________
// | next_ -->| next_ -->| |
// | |<-- prev_ |<-- prev_ |
//
// To:
//
// | base_node_ | second_element |
// |
// ____________________v________________
// | next_ -->| |
// | |<-- prev_ |
//
Node* begin = base_node_.next_;
base_node_.next_ = begin->next_;
base_node_.next_->prev_ = &base_node_;
return begin;
}
Node* extract_back()
{
// From:
//
// | second_last | last | base_node_ |
// | |
// __v_________________v__________________________________
// | next_ -->| next_ -->| |
// | |<-- prev_ |<-- prev_ |
//
// To:
//
// | second_last | base_node_ |
// |
// __v__________________________________
// | next_ -->| |
// | |<-- prev_ |
//
Node* last = untaint(base_node_.prev_);
last->prev_->next_ = &base_node_;
base_node_.prev_ = taint(last->prev_);
return last;
}
Node* extract_node_before(const_iterator position)
{
// position is not allowed to be end(), use extract_back in that case.
// position is also not allowed to be begin()+1, use extract_front in that case.
// From:
//
// | pos_minus_two | extracted_node | pos |
// | | |
// __v_________________v_________________v________________
// | next_ -->| next_ -->| |
// | |<-- prev_ |<-- prev_ |
//
// To:
//
// | pos_minus_two | pos |
// | |
// __v_________________v________________
// | next_ -->| |
// | |<-- prev_ |
//
Node* pos = position.ptr_;
Node* extracted_node = pos->prev_;
link_node(extracted_node->prev_, pos); // Connect pos_minus_two ⇄ pos.
return extracted_node;
}
// Remove the range [first, last) from the list, returns the last element removed.
// The range is not allowed to be empty.
Node* extract_range(const_iterator first, const_iterator last)
{
// From:
// last
// | first_minus_one | first | | last_minus_one | possibly_end |
// | | | |
// __v_________________v_____________________........______v_________________v________________
// | next_ -->| next_ -->| -->| next_ -->| |
// | |<-- prev_ |<-- |<-- prev_ |<-- prev_ |
//
// To:
// last
// | first_minus_one | possibly_end |
// | |
// __v_________________v________________
// | next_ -->| |
// | |<-- prev_ |
//
// | first | | last_minus_one |
// | |
// __v_____________________........______v________________
// | next_ -->| -->| next_ -->|
// |<-- prev_ |<-- |<-- prev_ |
//
// Where last_minus_one is returned.
//
Node* possibly_end = last.ptr_; // last can be end().
uintptr_t extracted_taint;
Node* last_minus_one = extract_taint(possibly_end->prev_, extracted_taint);
Node* first_minus_one = first.ptr_->prev_; // This is not tainted because the range is not empty and thus first can not be end().
link_node(first_minus_one, possibly_end, extracted_taint);
return last_minus_one;
}
// Find the element following the first count elements.
// Only call this when size_ > count (i.e. size_ > 0 in all cases).
const_iterator element_after(size_type count)
{
Node* result;
if (count < size_ / 2)
{
// Point to the first element in the list.
result = base_node_.next_;
// Skip count elements.
for (int i = 0; i < count; ++i)
result = result->next_;
}
else
{
// Point to the last element in the list.
result = untaint(base_node_.prev_);
// Skip (size_ - 1) - count elements.
//
// For example,
// A B C D E size_ = 5
// ^
// |
// result
// A B C D E count = 3
// ^
// |
// result
// result had to be decremented size_ - 1 - count = 1 times.
for (int i = size_ - 1; i > count; --i)
result = result->prev_;
}
return const_iterator{result};
}
public:
// Capacity
[[nodiscard]] bool empty() const noexcept { return size_ == 0; }
size_type size() const noexcept { return size_; }
size_type max_size() const noexcept { return std::numeric_limits<difference_type>::max(); }
void pop_front()
{
extract_front();
if (AI_UNLIKELY(--size_ == 0))
base_node_.prev_ = taint(&base_node_);
}
void pop_back()
{
extract_back();
--size_;
}
reference front() { return *static_cast<value_type*>(base_node_.next_); }
const_reference front() const { *static_cast<value_type const*>(base_node_.next_); }
reference back() { return *static_cast<value_type*>(untaint(base_node_.prev_)); }
const_reference back() const { return *static_cast<value_type const*>(untaint(base_node_.prev_)); }
// Iterator functions
iterator begin() noexcept { return iterator{base_node_.next_}; }
const_iterator begin() const noexcept { return const_iterator{base_node_.next_}; }
const_iterator cbegin() const noexcept { return const_iterator{base_node_.next_}; }
iterator end() noexcept { return {*this}; }
const_iterator end() const noexcept { return {*this}; }
const_iterator cend() const noexcept { return {*this}; }
reverse_iterator rbegin() noexcept { return reverse_iterator{end()}; }
const_reverse_iterator rbegin() const noexcept { return const_reverse_iterator{end()}; }
const_reverse_iterator crbegin() const noexcept { return const_reverse_iterator{cend()}; }
reverse_iterator rend() noexcept { return reverse_iterator{begin()}; }
const_reverse_iterator rend() const noexcept { return const_reverse_iterator{begin()}; }
const_reverse_iterator crend() const noexcept { return const_reverse_iterator{cbegin()}; }
// Insert [first, last) from other before pos. The number of elements in the range must be passed in range_size.
void splice(const_iterator pos, ListBase& other, const_iterator first, const_iterator last, size_type const range_size)
{
if (AI_UNLIKELY(range_size == 0))
return;
// Extract the elements of other.
Node* last_node = other.extract_range(first, last); // last_node will point to the element *before* last.
other.size_ -= range_size;
// From:
//
// pos
// | pos_minus_one | possibly_end |
// | |
// __v_________________v________________
// next_ -->|
// |<-- prev_
//
// To:
// pos
// | pos_minus_one | first_node | | last_node | possibly_end |
// | | | |
// __v_________________v_____________________........______v_________________v________________
// | next_ -->| next_ -->| -->| next_ -->| |
// | |<-- prev_ |<-- |<-- prev_ |<-- prev_ |
Node* first_node = first.ptr_;
Node* possibly_end = pos.ptr_; // pos can be end().
uintptr_t extracted_taint;
Node* pos_minus_one = extract_taint(possibly_end->prev_, extracted_taint);
link_node(pos_minus_one, first_node);
link_node(last_node, possibly_end, extracted_taint);
size_ += range_size;
}
void splice(const_iterator pos, ListBase& other);
void splice(const_iterator pos, ListBase&& other) { splice(pos, other); }
void splice(const_iterator pos, ListBase& other, const_iterator it) { splice(pos, other, it, std::next(it), 1); }
void splice(const_iterator pos, ListBase&& other, const_iterator it) { splice(pos, other, it); }
void splice(const_iterator pos, ListBase& other, const_iterator first, const_iterator last) { splice(pos, other, first, last, count(first, last)); }
void splice(const_iterator pos, ListBase&& other, const_iterator first, const_iterator last) { splice(pos, other, first, last); }
template<typename Compare>
void merge(ListBase& other, Compare comp);
void merge(ListBase& other) { merge(other, std::less<T>()); }
void merge(ListBase&& other) { merge(other); }
template<typename Compare>
void merge(ListBase&& other, Compare comp) { merge(other, comp); }
void reverse() noexcept;
template<typename Compare>
requires std::predicate<Compare&, T const&, T const&>
void sort(Compare comp);
void sort() { sort(std::less<>{}); }
#if DEBUG_MERGE_SORT
struct WrapIterator { iterator it_; };
static void dump_single_linked_list_on(std::ostream& os, WrapIterator wrap);
#endif
};
template<typename T, typename Allocator = std::allocator<typename ListBase<T>::value_type>>
class List : public ListBase<T>
{
public:
using allocator_type = Allocator;
using value_type = ListBase<T>::value_type;
using size_type = ListBase<T>::size_type;
using difference_type = ListBase<T>::difference_type;
using reference = ListBase<T>::reference;
using const_reference = ListBase<T>::const_reference;
using pointer = std::allocator_traits<Allocator>::pointer;
using const_pointer = std::allocator_traits<Allocator>::const_pointer;
using iterator = ListBase<T>::iterator;
using const_iterator = ListBase<T>::const_iterator;
using reverse_iterator = ListBase<T>::reverse_iterator;
using const_reverse_iterator = ListBase<T>::const_reverse_iterator;
using node_type = ListBase<T>::Node;
private:
Allocator alloc_{}; // The allocator of this list.
private:
template<typename... Args>
node_type* create_node(Args&&... args);
public:
// Constructors.
List() = default;
explicit List(Allocator const& alloc) : alloc_(alloc) { }
List(List const& other);
List(List const& other, Allocator const& alloc);
List(List&& other);
List(List&& other, Allocator const& alloc);
List(size_type count, T const& value, Allocator const& alloc = Allocator());
explicit List(size_type count, Allocator const& alloc = Allocator());
template<typename InputIt>
requires (std::convertible_to<std::iter_reference_t<InputIt>, T>)
List(InputIt first, InputIt last, Allocator const& alloc = Allocator());
List(std::initializer_list<T> init, Allocator const& alloc = Allocator()) : List(init.begin(), init.end(), alloc) { }
// Assignment operators.
List& operator=(List const& other) { assign(other.begin(), other.end()); return *this; }
List& operator=(List&& other) noexcept { clear(); this->splice(this->end(), other); return *this; }
// Destructor
~List() { clear(); }
allocator_type get_allocator() const { return alloc_; }
// Modifiers
void clear() noexcept;
template<typename... Args>
iterator emplace(const_iterator pos, Args&&... args);
template<typename... Args>
reference emplace_back(Args&&... args);
template<typename... Args>
reference emplace_front(Args&&... args) { return *emplace(const_iterator{this->base_node_.next_}, std::forward<Args>(args)...); }
// Insertion
iterator insert(const_iterator pos, T const& value) { return emplace(pos, value); }
iterator insert(const_iterator pos, T&& value) { return emplace(pos, std::move(value)); }
iterator insert(const_iterator pos, size_type count, T const& value);
template<typename InputIt>
requires (std::convertible_to<std::iter_reference_t<InputIt>, T>)
iterator insert(const_iterator pos, InputIt first, InputIt last);
iterator insert(const_iterator pos, std::initializer_list<T> ilist) { return insert(pos, ilist.begin(), ilist.end()); }
void assign(size_type count, T const& value) { clear(); insert(this->end(), count, value); }
template<typename InputIt>
requires (std::convertible_to<std::iter_reference_t<InputIt>, T>)
void assign(InputIt first, InputIt last) { clear(); insert(this->end(), first, last); }
void assign(std::initializer_list<T> ilist) { clear(); insert(ilist); }
iterator erase(const_iterator position);
iterator erase(const_iterator first, const_iterator last);
void push_back(T const& value) { emplace_back(value); }
void push_back(T&& value) { emplace_back(std::move(value)); }
void push_front(T const& value) { emplace_front(value); }
void push_front(T&& value) { emplace_front(std::move(value)); }
void resize(size_type count);
void resize(size_type count, T const& value);
void swap(List& other) noexcept(std::allocator_traits<Allocator>::is_always_equal::value);
template<typename UnaryPredicate>
requires std::predicate<UnaryPredicate&, T const&>
size_type remove_if(UnaryPredicate p);
size_type remove(T const& value) { return remove_if([&value](T const& element) { return element == value; }); }
template<typename BinaryPredicate>
requires std::predicate<BinaryPredicate&, T const&, T const&>
size_type unique(BinaryPredicate p);
size_type unique() { return unique(std::equal_to<>()); }
};
template<typename T>
ListBase<T>::iterator ListBase<T>::value_type::prev()
{
return typename ListBase<T>::iterator{untaint(this->prev_)};
}
template<typename T>
ListBase<T>::const_iterator ListBase<T>::value_type::prev() const
{
return typename ListBase<T>::const_iterator{untaint(this->prev_)};
}
template<typename T>
ListBase<T>::iterator ListBase<T>::value_type::next()
{
return typename ListBase<T>::iterator{this->next_};
}
template<typename T>
ListBase<T>::const_iterator ListBase<T>::value_type::next() const
{
return typename ListBase<T>::const_iterator{this->next_};
}
template<typename T>
void ListBase<T>::splice(const_iterator pos, ListBase& other)
{
if (AI_UNLIKELY(other.empty()))
return;
// Extract the elements of other.
Node base_node = other.base_node_;
// Update size_.
size_ += other.size_;
// Empty other.
other.base_node_.next_ = &other.base_node_;
other.base_node_.prev_ = ListBase<T>::taint(&other.base_node_);
other.size_ = 0;
// Insert all elements from other before pos.
// From:
//
// | base_node_ | other_begin | | other_last | base_node
// | | |
// ____________________v_____________________........______v_______________|__________________
// | next_ -->| next_ -->| -->| next_ --->&other.base_node_
// | &other.base_node<--- prev_ |<-- |<-- prev_ |<-- prev_ (tainted)
//
// pos
// | pos_minus_one | possibly_end |
// | |
// __v_________________v________________
// | next_ -->| |
// | |<-- prev_ |
//
// To:
// pos
// | pos_minus_one | other_begin | | other_last | possibly_end |
// | | | |
// __v_________________v_____________________........______v_________________v________________
// | next_ -->| next_ -->| -->| next_ -->| |
// | |<-- prev_ |<-- |<-- prev_ |<-- prev_ |
//
Node* possibly_end = pos.ptr_; // pos can be end().
uintptr_t extracted_taint;
Node* pos_minus_one = extract_taint(possibly_end->prev_, extracted_taint);
Node* other_begin = base_node.next_; // This is not &other.base_node_ because other is not empty.
Node* other_last = untaint(base_node.prev_); // Idem.
link_node(pos_minus_one, other_begin); // Connect pos_minus_one ⇄ other_begin.
link_node(other_last, possibly_end, extracted_taint); // Connect other_last ⇄ possibly_end.
}
template<typename T>
template<typename Compare>
void ListBase<T>::merge(ListBase& other, Compare comp)
{
if (AI_UNLIKELY(this == &other))
return;
iterator tail = begin();
iterator const end = this->end();
iterator const other_end = other.end();
while (!other.empty())
{
// Move tail forward till the first element larger than other.front().
iterator other_front = other.begin();
while (tail != end && !comp(*other_front, *tail)) // while (*tail <= *other_front)
++tail;
// Now *tail > *other_front.
// Move splice_end forward till the first element larger or equal than *tail.
iterator splice_end = other_front;
size_type range_size = 1;
while (++splice_end != other_end && (tail == end || comp(*splice_end, *tail))) // while (*splice_end < *tail)
++range_size;
// Now *splice_end >= *tail.
// Insert all elements from other that are less than *tail before tail.
splice(tail, other, other_front, splice_end, range_size);
}
}
template<typename T>
void ListBase<T>::reverse() noexcept
{
for (Node* current = base_node_.next_; current != &base_node_; current = current->prev_)
std::swap(current->next_, current->prev_);
Node* begin = base_node_.next_;
base_node_.next_ = untaint(base_node_.prev_);
base_node_.prev_ = taint(begin);
}
#if DEBUG_MERGE_SORT
//static
template<typename T>
void ListBase<T>::dump_single_linked_list_on(std::ostream& os, typename ListBase<T>::WrapIterator wrap)
{
iterator it = wrap.it_;
os << '{';
char const* separator = "";
for (Node* node = it.ptr_; node; node = (--it).ptr_)
{
os << separator << *it;
separator = ", ";
}
os << '}';
}
#define MergeDout(cntrl, data) \
LibcwDout(LIBCWD_DEBUGCHANNELS, ::libcwd::libcw_do, cntrl, data)
#else // DEBUG_MERGE_SORT
#define MergeDout(cntrl, data) do { } while(0)
#endif // DEBUG_MERGE_SORT
template<typename T>
template<typename Compare>
requires std::predicate<Compare&, T const&, T const&>
void ListBase<T>::sort(Compare comp)
{
// A merge sort implementation by Carlo Wood.
// A single merge step exists of having a situation like the following:
//
// P
// ⇓
// X → l0 → l1 → Y } next_ pointers
// ↓ ↓ ⎫
// a k ⎪
// ↓ ↓ ⎪
// ⋮ ⋮ ⎬ prev_ pointers
// ↓ ↓ ⎪
// j z ⎪
// ↓ ↓ ⎭
// null null
//
// where P points to an element (X) whose next_ pointer points to l0, the first
// element of an ascending, sorted, singly-linked list, that uses the prev_ pointer
// to point to the next (smaller) element (a) and terminates with a node (j) that has
// prev_ == nullptr.
//
// The next_ pointer of l0 must point to another node (l1) which in turn is the
// first element of an ascending, sorted, singly-linked list, that uses the prev_
// pointer to point to the next (smaller) element (k) and also terminates with a
// node (z) that has prev_ == nullptr.
//
// The step exists of merging l0 and l1 into a single, ascending, sorted,
// singly-linked list (lₙ - which is the larger of l0 and l1) and update P,
// the next_ pointer of X and lₙ.
//
// Hence, after the merge step we should end up with:
//
// P
// ⇓
// X → lₙ → Y
// ↓
// ⋮
// ↓
// null
//
// where lₙ = *l1 < *l0 ? l0 : l1; so that we'll get l1 if l0 and l1 are equal (to preserve the same order).
//
// Hence, P must initially point to the node_base_.
// The algoritm then repeats
//
// X->next_ = P = merge(l0, l1); // Where `merge` merges l0 and l1 and return lₙ.
// P->next_ = l1->next_; // Make the top element (lₙ above) point to Y.
//
// until P->next_ points to base_node_ (Y is base_node_), or P->next_->next_ points to
// base_node_, at which point we have to reset P to point to the base_node_ again.
//
// This terminating condition at the *beginning* of a new step is therefore:
//
// P
// ⇓
// X → B
//
// or
//
// P
// ⇓
// X → l0 → B
// ↓
// ⋮
// ↓
// null
//
//
// In other words, either l0 or l1 are equal to B.
//
// Note that if the list is fully sorted then X is base_node_ too, hence
// at the start of the next step both X and l1 are the base_node_.
// B points to the base_node_.
iterator B{*this};
ASSERT(B.is_end());
// We must begin by bringing the nodes of the list into the above described state.
// It would be sufficient to set all prev_ pointers to null, but since that means
// we need to run over all elements, we might as well combine that with the first
// pass.
//
// That is, for every element pair, we want to go from:
//
// | previous node | first | second | next Node |
// _________________________________________________________________________
// | next_ -->| next_ -->| next_ -->| |
// |<-- prev_ |<-- prev_ |<-- prev_ | |
//
// if second < first, to
//
// | previous node | first | | next Node |
// _____________________________________ ___________________
// | next_ -->| next_ ==========(1)=======>| |
// |<-- prev_ | prev_ | | |
// ‖(2)
// v
// | second |
// __________________
// | |
// | prev_ |
// ‖(3)
// v
// nullptr
//
// otherwise (first <= second) to
//
// | previous node | | second | next Node |
// __________________| _____________________________________
// | next_ ==========(4)=======>| next_ -->| |
// |<-- prev_ | | prev_ | |
// |
// v
// | first |
// ___________________
// | |
// | prev_ |
// ‖(5)
// v
// nullptr
//
// If there are an odd number of nodes, then the last one must have its prev_ set to nullptr.
//
iterator previous_node = B;
iterator first{previous_node.ptr_->next_};
iterator second{first.ptr_->next_};
// If the list contains less than two elements, do nothing.
if (AI_UNLIKELY(second == B))
return;
do
{
iterator next{second.ptr_->next_};
if (comp(*second, *first))
{
first->next_ = next.ptr_; // (1)
first->prev_ = second.ptr_; // (2)
second->prev_ = nullptr; // (3)
previous_node = first;
}
else
{
previous_node->next_ = second.ptr_; // (4)
first->prev_ = nullptr; // (5)
previous_node = second;
}
first.ptr_ = previous_node.ptr_->next_;
second.ptr_ = first.ptr_->next_;
}
while (first != B && second != B);
if (first != B)
first.ptr_->prev_ = nullptr;
// We had at least two elements, so we have at least one singly-linked list now.
// If there is only one such list, then the list has two elements and we have the following situation:
//
// B → l0 → B
// ↓
// S
// ↓
// null
//
int remaining_lists = 1;
iterator l0 = std::next(B);
iterator l1 = std::next(l0);
for (Node* node = l1.ptr_; node != B.ptr_; node = node->next_)
if (++remaining_lists == 3) // Three or more lists is treated the same.
break;
// If the list only had two elements handle that now.
if (remaining_lists == 1)
{
// We need to convert the above into:
// B ⇄ S ⇄ l0 ⇄ B
Node* S = l0->prev_;
S->prev_ = B.ptr_;
B.ptr_->prev_ = taint(l0.ptr_);
B->next_ = S;
S->next_ = l0.ptr_;
return;
}
std::array<iterator, 2> iters = {{l0, l1}};
bool done = remaining_lists == 2;
while (!done)
{
iterator P = B; // Reset P to the beginning.
for (;;)
{
// l0, l1 and iters were already initialized: before entering the while loop and/or at the bottom of the while or for loop respectively.
iterator Y = std::next(l1);
//---------------------------------------------------------------
// Merge the singly-linked lists l0 and l1, which are already sorted in ascending order.
//
// Only the prev_ pointer is used and the last element of both lists must have prev_ == nullptr.
// Afterwards P is set to point to the largest (first) element (which will be either l0 or l1).
//
int initial_largest;
// Test terminating condition.
//
// There are exactly two lists left:
//
// P
// ⇓
// B → l0 → l1 → B
// ↓ ↓
// ⋮ ⋮
// ↓ ↓
// null null
//
// In otherwords, Y == B, but so does P.