-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathlist
1940 lines (1633 loc) · 73.5 KB
/
list
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
// list standard header
// Copyright (c) Microsoft Corporation.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#ifndef _LIST_
#define _LIST_
#include <yvals_core.h>
#if _STL_COMPILER_PREPROCESSOR
#include <xmemory>
#if _HAS_CXX17
#include <xpolymorphic_allocator.h>
#endif // _HAS_CXX17
#pragma pack(push, _CRT_PACKING)
#pragma warning(push, _STL_WARNING_LEVEL)
#pragma warning(disable : _STL_DISABLED_WARNINGS)
_STL_DISABLE_CLANG_WARNINGS
#pragma push_macro("new")
#undef new
_STD_BEGIN
template <class _Mylist, class _Base = _Iterator_base0>
class _List_unchecked_const_iterator : public _Base {
public:
using iterator_category = bidirectional_iterator_tag;
using _Nodeptr = typename _Mylist::_Nodeptr;
using value_type = typename _Mylist::value_type;
using difference_type = typename _Mylist::difference_type;
using pointer = typename _Mylist::const_pointer;
using reference = const value_type&;
_List_unchecked_const_iterator() noexcept : _Ptr() {}
_List_unchecked_const_iterator(_Nodeptr _Pnode, const _Mylist* _Plist) noexcept : _Ptr(_Pnode) {
this->_Adopt(_Plist);
}
_NODISCARD reference operator*() const noexcept {
return _Ptr->_Myval;
}
_NODISCARD pointer operator->() const noexcept {
return pointer_traits<pointer>::pointer_to(**this);
}
_List_unchecked_const_iterator& operator++() noexcept {
_Ptr = _Ptr->_Next;
return *this;
}
_List_unchecked_const_iterator operator++(int) noexcept {
_List_unchecked_const_iterator _Tmp = *this;
_Ptr = _Ptr->_Next;
return _Tmp;
}
_List_unchecked_const_iterator& operator--() noexcept {
_Ptr = _Ptr->_Prev;
return *this;
}
_List_unchecked_const_iterator operator--(int) noexcept {
_List_unchecked_const_iterator _Tmp = *this;
_Ptr = _Ptr->_Prev;
return _Tmp;
}
_NODISCARD bool operator==(const _List_unchecked_const_iterator& _Right) const noexcept {
return _Ptr == _Right._Ptr;
}
#if !_HAS_CXX20
_NODISCARD bool operator!=(const _List_unchecked_const_iterator& _Right) const noexcept {
return !(*this == _Right);
}
#endif // !_HAS_CXX20
_Nodeptr _Ptr; // pointer to node
};
template <class _Mylist>
class _List_unchecked_iterator : public _List_unchecked_const_iterator<_Mylist> {
public:
using _Mybase = _List_unchecked_const_iterator<_Mylist>;
using iterator_category = bidirectional_iterator_tag;
using _Nodeptr = typename _Mylist::_Nodeptr;
using value_type = typename _Mylist::value_type;
using difference_type = typename _Mylist::difference_type;
using pointer = typename _Mylist::pointer;
using reference = value_type&;
using _Mybase::_Mybase;
_NODISCARD reference operator*() const noexcept {
return const_cast<reference>(_Mybase::operator*());
}
_NODISCARD pointer operator->() const noexcept {
return pointer_traits<pointer>::pointer_to(**this);
}
_List_unchecked_iterator& operator++() noexcept {
_Mybase::operator++();
return *this;
}
_List_unchecked_iterator operator++(int) noexcept {
_List_unchecked_iterator _Tmp = *this;
_Mybase::operator++();
return _Tmp;
}
_List_unchecked_iterator& operator--() noexcept {
_Mybase::operator--();
return *this;
}
_List_unchecked_iterator operator--(int) noexcept {
_List_unchecked_iterator _Tmp = *this;
_Mybase::operator--();
return _Tmp;
}
};
template <class _Mylist>
class _List_const_iterator : public _List_unchecked_const_iterator<_Mylist, _Iterator_base> {
public:
using _Mybase = _List_unchecked_const_iterator<_Mylist, _Iterator_base>;
using iterator_category = bidirectional_iterator_tag;
using _Nodeptr = typename _Mylist::_Nodeptr;
using value_type = typename _Mylist::value_type;
using difference_type = typename _Mylist::difference_type;
using pointer = typename _Mylist::const_pointer;
using reference = const value_type&;
using _Mybase::_Mybase;
_NODISCARD reference operator*() const noexcept {
#if _ITERATOR_DEBUG_LEVEL == 2
const auto _Mycont = static_cast<const _Mylist*>(this->_Getcont());
_STL_ASSERT(_Mycont, "cannot dereference value-initialized list iterator");
_STL_VERIFY(this->_Ptr != _Mycont->_Myhead, "cannot dereference end list iterator");
#endif // _ITERATOR_DEBUG_LEVEL == 2
return this->_Ptr->_Myval;
}
_NODISCARD pointer operator->() const noexcept {
return pointer_traits<pointer>::pointer_to(**this);
}
_List_const_iterator& operator++() noexcept {
#if _ITERATOR_DEBUG_LEVEL == 2
const auto _Mycont = static_cast<const _Mylist*>(this->_Getcont());
_STL_ASSERT(_Mycont, "cannot increment value-initialized list iterator");
_STL_VERIFY(this->_Ptr != _Mycont->_Myhead, "cannot increment end list iterator");
#endif // _ITERATOR_DEBUG_LEVEL == 2
this->_Ptr = this->_Ptr->_Next;
return *this;
}
_List_const_iterator operator++(int) noexcept {
_List_const_iterator _Tmp = *this;
++*this;
return _Tmp;
}
_List_const_iterator& operator--() noexcept {
const auto _New_ptr = this->_Ptr->_Prev;
#if _ITERATOR_DEBUG_LEVEL == 2
const auto _Mycont = static_cast<const _Mylist*>(this->_Getcont());
_STL_ASSERT(_Mycont, "cannot decrement value-initialized list iterator");
_STL_VERIFY(_New_ptr != _Mycont->_Myhead, "cannot decrement begin list iterator");
#endif // _ITERATOR_DEBUG_LEVEL == 2
this->_Ptr = _New_ptr;
return *this;
}
_List_const_iterator operator--(int) noexcept {
_List_const_iterator _Tmp = *this;
--*this;
return _Tmp;
}
_NODISCARD bool operator==(const _List_const_iterator& _Right) const noexcept {
#if _ITERATOR_DEBUG_LEVEL == 2
_STL_VERIFY(this->_Getcont() == _Right._Getcont(), "list iterators incompatible");
#endif // _ITERATOR_DEBUG_LEVEL == 2
return this->_Ptr == _Right._Ptr;
}
#if !_HAS_CXX20
_NODISCARD bool operator!=(const _List_const_iterator& _Right) const noexcept {
return !(*this == _Right);
}
#endif // !_HAS_CXX20
#if _ITERATOR_DEBUG_LEVEL == 2
friend void _Verify_range(const _List_const_iterator& _First, const _List_const_iterator& _Last) noexcept {
_STL_VERIFY(_First._Getcont() == _Last._Getcont(), "list iterators in range are from different containers");
}
#endif // _ITERATOR_DEBUG_LEVEL == 2
using _Prevent_inheriting_unwrap = _List_const_iterator;
_NODISCARD _List_unchecked_const_iterator<_Mylist> _Unwrapped() const noexcept {
return _List_unchecked_const_iterator<_Mylist>(this->_Ptr, static_cast<const _Mylist*>(this->_Getcont()));
}
void _Seek_to(const _List_unchecked_const_iterator<_Mylist> _It) noexcept {
this->_Ptr = _It._Ptr;
}
};
template <class _Mylist>
class _List_iterator : public _List_const_iterator<_Mylist> {
public:
using _Mybase = _List_const_iterator<_Mylist>;
using iterator_category = bidirectional_iterator_tag;
using _Nodeptr = typename _Mylist::_Nodeptr;
using value_type = typename _Mylist::value_type;
using difference_type = typename _Mylist::difference_type;
using pointer = typename _Mylist::pointer;
using reference = value_type&;
using _Mybase::_Mybase;
_NODISCARD reference operator*() const noexcept {
return const_cast<reference>(_Mybase::operator*());
}
_NODISCARD pointer operator->() const noexcept {
return pointer_traits<pointer>::pointer_to(**this);
}
_List_iterator& operator++() noexcept {
_Mybase::operator++();
return *this;
}
_List_iterator operator++(int) noexcept {
_List_iterator _Tmp = *this;
_Mybase::operator++();
return _Tmp;
}
_List_iterator& operator--() noexcept {
_Mybase::operator--();
return *this;
}
_List_iterator operator--(int) noexcept {
_List_iterator _Tmp = *this;
_Mybase::operator--();
return _Tmp;
}
using _Prevent_inheriting_unwrap = _List_iterator;
_NODISCARD _List_unchecked_iterator<_Mylist> _Unwrapped() const noexcept {
return _List_unchecked_iterator<_Mylist>(this->_Ptr, static_cast<const _Mylist*>(this->_Getcont()));
}
};
template <class _Value_type, class _Size_type, class _Difference_type, class _Pointer, class _Const_pointer,
class _Nodeptr_type>
struct _List_iter_types {
using value_type = _Value_type;
using size_type = _Size_type;
using difference_type = _Difference_type;
using pointer = _Pointer;
using const_pointer = _Const_pointer;
using _Nodeptr = _Nodeptr_type;
};
template <class _Value_type, class _Voidptr>
struct _List_node { // list node
using value_type = _Value_type;
using _Nodeptr = _Rebind_pointer_t<_Voidptr, _List_node>;
_Nodeptr _Next; // successor node, or first element if head
_Nodeptr _Prev; // predecessor node, or last element if head
_Value_type _Myval = // the stored value, unused if head
_Returns_exactly<_Value_type>(); // fake a viable constructor to workaround GH-2749
_List_node() = default;
_List_node(const _List_node&) = delete;
_List_node& operator=(const _List_node&) = delete;
template <class _Alnode>
static _Nodeptr _Buyheadnode(_Alnode& _Al) {
const auto _Result = _Al.allocate(1);
_Construct_in_place(_Result->_Next, _Result);
_Construct_in_place(_Result->_Prev, _Result);
return _Result;
}
template <class _Alnode>
static void _Freenode0(_Alnode& _Al, _Nodeptr _Ptr) noexcept {
// destroy pointer members in _Ptr and deallocate with _Al
static_assert(is_same_v<typename _Alnode::value_type, _List_node>, "Bad _Freenode0 call");
_Destroy_in_place(_Ptr->_Next);
_Destroy_in_place(_Ptr->_Prev);
allocator_traits<_Alnode>::deallocate(_Al, _Ptr, 1);
}
template <class _Alnode>
static void _Freenode(_Alnode& _Al, _Nodeptr _Ptr) noexcept { // destroy all members in _Ptr and deallocate with _Al
allocator_traits<_Alnode>::destroy(_Al, _STD addressof(_Ptr->_Myval));
_Freenode0(_Al, _Ptr);
}
template <class _Alnode>
static void _Free_non_head(
_Alnode& _Al, _Nodeptr _Head) noexcept { // free a list starting at _First and terminated at nullptr
_Head->_Prev->_Next = nullptr;
auto _Pnode = _Head->_Next;
for (_Nodeptr _Pnext; _Pnode; _Pnode = _Pnext) {
_Pnext = _Pnode->_Next;
_Freenode(_Al, _Pnode);
}
}
};
template <class _Ty>
struct _List_simple_types : _Simple_types<_Ty> {
using _Node = _List_node<_Ty, void*>;
using _Nodeptr = _Node*;
};
template <class _Val_types>
class _List_val : public _Container_base {
public:
using _Nodeptr = typename _Val_types::_Nodeptr;
using value_type = typename _Val_types::value_type;
using size_type = typename _Val_types::size_type;
using difference_type = typename _Val_types::difference_type;
using pointer = typename _Val_types::pointer;
using const_pointer = typename _Val_types::const_pointer;
using reference = value_type&;
using const_reference = const value_type&;
_List_val() noexcept : _Myhead(), _Mysize(0) {} // initialize data
void _Orphan_ptr2(_Nodeptr _Ptr) noexcept { // orphan iterators with specified node pointers
#if _ITERATOR_DEBUG_LEVEL == 2
_Lockit _Lock(_LOCK_DEBUG);
_Iterator_base12** _Pnext = &this->_Myproxy->_Myfirstiter;
const auto _Head = _Myhead;
while (*_Pnext) {
_Iterator_base12** _Pnextnext = &(*_Pnext)->_Mynextiter;
const auto _Pnextptr = static_cast<_List_const_iterator<_List_val>&>(**_Pnext)._Ptr;
if (_Pnextptr == _Head || _Pnextptr != _Ptr) {
// iterator is end() or doesn't point at the one we are orphaning, move on
_Pnext = _Pnextnext;
} else { // orphan the iterator
(*_Pnext)->_Myproxy = nullptr;
*_Pnext = *_Pnextnext;
}
}
#else // ^^^ _ITERATOR_DEBUG_LEVEL == 2 / _ITERATOR_DEBUG_LEVEL != 2 vvv
(void) _Ptr;
#endif // ^^^ _ITERATOR_DEBUG_LEVEL != 2 ^^^
}
void _Orphan_non_end() noexcept { // orphan iterators except end()
#if _ITERATOR_DEBUG_LEVEL == 2
_Lockit _Lock(_LOCK_DEBUG);
_Iterator_base12** _Pnext = &this->_Myproxy->_Myfirstiter;
const auto _Head = _Myhead;
while (*_Pnext) {
_Iterator_base12** _Pnextnext = &(*_Pnext)->_Mynextiter;
if (static_cast<_List_const_iterator<_List_val>&>(**_Pnext)._Ptr == _Head) { // iterator is end(), move on
_Pnext = _Pnextnext;
} else { // orphan the iterator
(*_Pnext)->_Myproxy = nullptr;
*_Pnext = *_Pnextnext;
}
}
#endif // _ITERATOR_DEBUG_LEVEL == 2
}
_Nodeptr _Unlinknode(_Nodeptr _Pnode) noexcept { // unlink node at _Where from the list
_Orphan_ptr2(_Pnode);
_Pnode->_Prev->_Next = _Pnode->_Next;
_Pnode->_Next->_Prev = _Pnode->_Prev;
--_Mysize;
return _Pnode;
}
#if _ITERATOR_DEBUG_LEVEL == 2
void _Adopt_unique(_List_val& _Other, _Nodeptr _Pnode) noexcept {
// adopt iterators pointing to the spliced node
_Lockit _Lock(_LOCK_DEBUG);
_Iterator_base12** _Pnext = &_Other._Myproxy->_Myfirstiter;
const auto _Myproxy = this->_Myproxy;
while (*_Pnext) {
auto& _Iter = static_cast<_List_const_iterator<_List_val>&>(**_Pnext);
if (_Iter._Ptr == _Pnode) { // adopt the iterator
*_Pnext = _Iter._Mynextiter;
_Iter._Myproxy = _Myproxy;
_Iter._Mynextiter = _Myproxy->_Myfirstiter;
_Myproxy->_Myfirstiter = _STD addressof(_Iter);
} else { // skip the iterator
_Pnext = &_Iter._Mynextiter;
}
}
}
void _Adopt_all(_List_val& _Other) noexcept {
// adopt all iterators (except _Other.end())
_Lockit _Lock(_LOCK_DEBUG);
_Iterator_base12** _Pnext = &_Other._Myproxy->_Myfirstiter;
const auto _Myproxy = this->_Myproxy;
const auto _Otherhead = _Other._Myhead;
while (*_Pnext) {
auto& _Iter = static_cast<_List_const_iterator<_List_val>&>(**_Pnext);
if (_Iter._Ptr != _Otherhead) { // adopt the iterator
*_Pnext = _Iter._Mynextiter;
_Iter._Myproxy = _Myproxy;
_Iter._Mynextiter = _Myproxy->_Myfirstiter;
_Myproxy->_Myfirstiter = _STD addressof(_Iter);
} else { // skip the iterator
_Pnext = &_Iter._Mynextiter;
}
}
}
void _Adopt_range(_List_val& _Other, const _Nodeptr _First, const _Nodeptr _Last) noexcept {
// adopt all iterators pointing to nodes in the "range" [_First, _Last) by marking nodes
_Lockit _Lock(_LOCK_DEBUG);
_Iterator_base12** _Pnext = &_Other._Myproxy->_Myfirstiter;
const auto _Myproxy = this->_Myproxy;
_Nodeptr _Oldprev = _First->_Prev;
for (_Nodeptr _Ptr = _First; _Ptr != _Last; _Ptr = _Ptr->_Next) { // mark _Prev pointers
_Ptr->_Prev = nullptr;
}
while (*_Pnext) { // check the iterator
auto& _Iter = static_cast<_List_const_iterator<_List_val>&>(**_Pnext);
if (_Iter._Ptr->_Prev) { // skip the iterator
_Pnext = &_Iter._Mynextiter;
} else { // adopt the iterator
*_Pnext = _Iter._Mynextiter;
_Iter._Myproxy = _Myproxy;
_Iter._Mynextiter = _Myproxy->_Myfirstiter;
_Myproxy->_Myfirstiter = _STD addressof(_Iter);
}
}
for (_Nodeptr _Ptr = _First; _Ptr != _Last; _Ptr = _Ptr->_Next) { // restore _Prev pointers
_Ptr->_Prev = _Oldprev;
_Oldprev = _Ptr;
}
}
#endif // _ITERATOR_DEBUG_LEVEL == 2
static _Nodeptr _Unchecked_splice(const _Nodeptr _Before, const _Nodeptr _First, const _Nodeptr _Last) noexcept {
// splice [_First, _Last) before _Before; returns _Last
_STL_INTERNAL_CHECK(_Before != _First && _Before != _Last && _First != _Last);
// 3 reads and 6 writes
// fixup the _Next values
const auto _First_prev = _First->_Prev;
_First_prev->_Next = _Last;
const auto _Last_prev = _Last->_Prev;
_Last_prev->_Next = _Before;
const auto _Before_prev = _Before->_Prev;
_Before_prev->_Next = _First;
// fixup the _Prev values
_Before->_Prev = _Last_prev;
_Last->_Prev = _First_prev;
_First->_Prev = _Before_prev;
return _Last;
}
static _Nodeptr _Unchecked_splice(const _Nodeptr _Before, const _Nodeptr _First) noexcept {
// splice [_First, _First->_Next) before _Before; returns _First->_Next
_STL_INTERNAL_CHECK(_Before != _First && _First->_Next != _Before);
// still 3 reads and 6 writes, but 1 less read if the caller was going to get _First->_Next
const auto _Last = _First->_Next;
// fixup the _Next values
const auto _First_prev = _First->_Prev;
_First_prev->_Next = _Last;
// const auto _Last_prev = _First;
_First->_Next = _Before;
const auto _Before_prev = _Before->_Prev;
_Before_prev->_Next = _First;
// fixup the _Prev values
_Before->_Prev = _First;
_Last->_Prev = _First_prev;
_First->_Prev = _Before_prev;
return _Last;
}
template <class _Pr2>
static _Nodeptr _Merge_same(_Nodeptr _First, _Nodeptr _Mid, const _Nodeptr _Last, _Pr2 _Pred) {
// Merge the sorted ranges [_First, _Mid) and [_Mid, _Last)
// Returns the new beginning of the range (which won't be _First if it was spliced elsewhere)
_STL_INTERNAL_CHECK(_First != _Mid && _Mid != _Last);
_Nodeptr _Newfirst;
if (_DEBUG_LT_PRED(_Pred, _Mid->_Myval, _First->_Myval)) {
// _Mid will be spliced to the front of the range
_Newfirst = _Mid;
} else {
// Establish _Pred(_Mid->_Myval, _First->_Myval) by skipping over elements from the first
// range already in position.
_Newfirst = _First;
do {
_First = _First->_Next;
if (_First == _Mid) {
return _Newfirst;
}
} while (!_DEBUG_LT_PRED(_Pred, _Mid->_Myval, _First->_Myval));
}
for (;;) { // process one run splice
auto _Run_start = _Mid;
do { // find the end of the "run" of elements we need to splice from the second range into the first
_Mid = _Mid->_Next;
} while (_Mid != _Last && _DEBUG_LT_PRED(_Pred, _Mid->_Myval, _First->_Myval));
// [_Run_start, _Mid) goes before _First->_Myval
_Unchecked_splice(_First, _Run_start, _Mid);
if (_Mid == _Last) {
return _Newfirst;
}
// Reestablish _Pred(_Mid->_Myval, _First->_Myval) by skipping over elements from the first
// range already in position.
do {
_First = _First->_Next;
if (_First == _Mid) {
return _Newfirst;
}
} while (!_DEBUG_LT_PRED(_Pred, _Mid->_Myval, _First->_Myval));
}
}
template <class _Pr2>
static _Nodeptr _Sort(_Nodeptr& _First, const size_type _Size, _Pr2 _Pred) {
// order [_First, _First + _Size), return _First + _Size
switch (_Size) {
case 0:
return _First;
case 1:
return _First->_Next;
default:
break;
}
auto _Mid = _Sort(_First, _Size / 2, _Pred);
const auto _Last = _Sort(_Mid, _Size - _Size / 2, _Pred);
_First = _Merge_same(_First, _Mid, _Last, _Pred);
return _Last;
}
_Nodeptr _Myhead; // pointer to head node
size_type _Mysize; // number of elements
};
template <class _Alnode>
struct _List_node_emplace_op2 : _Alloc_construct_ptr<_Alnode> {
using _Alnode_traits = allocator_traits<_Alnode>;
using pointer = typename _Alnode_traits::pointer;
template <class... _Valtys>
explicit _List_node_emplace_op2(_Alnode& _Al_, _Valtys&&... _Vals) : _Alloc_construct_ptr<_Alnode>(_Al_) {
this->_Allocate();
_Alnode_traits::construct(this->_Al, _STD addressof(this->_Ptr->_Myval), _STD forward<_Valtys>(_Vals)...);
}
~_List_node_emplace_op2() {
if (this->_Ptr != pointer{}) {
_Alnode_traits::destroy(this->_Al, _STD addressof(this->_Ptr->_Myval));
}
}
_List_node_emplace_op2(const _List_node_emplace_op2&) = delete;
_List_node_emplace_op2& operator=(const _List_node_emplace_op2&) = delete;
pointer _Transfer_before(const pointer _Insert_before) noexcept {
const pointer _Insert_after = _Insert_before->_Prev;
_Construct_in_place(this->_Ptr->_Next, _Insert_before);
_Construct_in_place(this->_Ptr->_Prev, _Insert_after);
const auto _Result = this->_Ptr;
this->_Ptr = pointer{};
_Insert_before->_Prev = _Result;
_Insert_after->_Next = _Result;
return _Result;
}
};
template <class _Alnode>
struct _List_node_insert_op2 {
// list insert operation which maintains exception safety
using _Alnode_traits = allocator_traits<_Alnode>;
using pointer = typename _Alnode_traits::pointer;
using size_type = typename _Alnode_traits::size_type;
using value_type = typename _Alnode_traits::value_type;
explicit _List_node_insert_op2(_Alnode& _Al_) : _Al(_Al_), _Added(0) {}
_List_node_insert_op2(const _List_node_insert_op2&) = delete;
_List_node_insert_op2& operator=(const _List_node_insert_op2&) = delete;
template <class... _CArgT>
void _Append_n(size_type _Count, const _CArgT&... _Carg) {
// Append _Count elements constructed from _Carg
if (_Count <= 0) {
return;
}
_Alloc_construct_ptr<_Alnode> _Newnode(_Al);
if (_Added == 0) {
_Newnode._Allocate(); // throws
_Alnode_traits::construct(_Al, _STD addressof(_Newnode._Ptr->_Myval), _Carg...); // throws
_Head = _Newnode._Ptr;
_Tail = _Newnode._Ptr;
++_Added;
--_Count;
}
for (; 0 < _Count; --_Count) {
_Newnode._Allocate(); // throws
_Alnode_traits::construct(_Al, _STD addressof(_Newnode._Ptr->_Myval), _Carg...); // throws
_Construct_in_place(_Tail->_Next, _Newnode._Ptr);
_Construct_in_place(_Newnode._Ptr->_Prev, _Tail);
_Tail = _Newnode._Ptr;
++_Added;
}
_Newnode._Ptr = pointer{};
}
template <class _InIt, class _Sentinel>
void _Append_range_unchecked(_InIt _First, const _Sentinel _Last) {
// Append the values in [_First, _Last)
if (_First == _Last) { // throws
return;
}
_Alloc_construct_ptr<_Alnode> _Newnode(_Al);
if (_Added == 0) {
_Newnode._Allocate(); // throws
_Alnode_traits::construct(_Al, _STD addressof(_Newnode._Ptr->_Myval), *_First); // throws
const auto _Newhead = _STD exchange(_Newnode._Ptr, pointer{});
_Head = _Newhead;
_Tail = _Newhead;
++_Added;
++_First; // throws
}
while (_First != _Last) { // throws
_Newnode._Allocate(); // throws
_Alnode_traits::construct(_Al, _STD addressof(_Newnode._Ptr->_Myval), *_First); // throws
_Construct_in_place(_Tail->_Next, _Newnode._Ptr);
_Construct_in_place(_Newnode._Ptr->_Prev, _Tail);
_Tail = _STD exchange(_Newnode._Ptr, pointer{});
++_Added;
++_First; // throws
}
}
template <class _Val_types>
pointer _Attach_before(_List_val<_Val_types>& _List_data, const pointer _Insert_before) noexcept {
// Attach the elements in *this before _Insert_before.
// If *this is empty, returns _Insert_before; otherwise returns a pointer to the first inserted list node.
// Resets *this to the default-initialized state.
const auto _Local_added = _Added;
if (_Local_added == 0) {
return _Insert_before;
}
const auto _Local_head = _Head;
const auto _Local_tail = _Tail;
const auto _Insert_after = _Insert_before->_Prev;
_Construct_in_place(_Local_head->_Prev, _Insert_after);
_Insert_after->_Next = _Local_head;
_Construct_in_place(_Local_tail->_Next, _Insert_before);
_Insert_before->_Prev = _Local_tail;
_List_data._Mysize += _Local_added;
_Added = 0;
return _Local_head;
}
template <class _Val_types>
void _Attach_at_end(_List_val<_Val_types>& _List_data) noexcept {
_Attach_before(_List_data, _List_data._Myhead);
}
template <class _Val_types>
void _Attach_head(_List_val<_Val_types>& _List_data) {
_Alloc_construct_ptr<_Alnode> _Newnode(_Al);
_Newnode._Allocate(); // throws
const auto _Local_added = _STD exchange(_Added, size_type{0});
if (_Local_added == 0) {
_Construct_in_place(_Newnode._Ptr->_Next, _Newnode._Ptr);
_Construct_in_place(_Newnode._Ptr->_Prev, _Newnode._Ptr);
} else {
const auto _Local_head = _Head;
const auto _Local_tail = _Tail;
_Construct_in_place(_Newnode._Ptr->_Next, _Local_head);
_Construct_in_place(_Newnode._Ptr->_Prev, _Local_tail);
_Construct_in_place(_Local_head->_Prev, _Newnode._Ptr);
_Construct_in_place(_Local_tail->_Next, _Newnode._Ptr);
}
_List_data._Mysize = _Local_added;
_List_data._Myhead = _Newnode._Release();
}
~_List_node_insert_op2() {
if (_Added == 0) {
return;
}
_Construct_in_place(_Head->_Prev, pointer{});
_Construct_in_place(_Tail->_Next, pointer{});
pointer _Subject = _Head;
while (_Subject) {
value_type::_Freenode(_Al, _STD exchange(_Subject, _Subject->_Next));
}
}
private:
_Alnode& _Al;
size_type _Added; // if 0, the values of _Head and _Tail are indeterminate
pointer _Tail{}; // points to the most recently appended element; it doesn't have _Next constructed
pointer _Head{}; // points to the first appended element; it doesn't have _Prev constructed
};
template <class _Traits>
class _Hash;
_EXPORT_STD template <class _Ty, class _Alloc = allocator<_Ty>>
class list { // bidirectional linked list
private:
template <class>
friend class _Hash;
template <class _Traits>
friend bool _Hash_equal(const _Hash<_Traits>&, const _Hash<_Traits>&);
using _Alty = _Rebind_alloc_t<_Alloc, _Ty>;
using _Alty_traits = allocator_traits<_Alty>;
using _Node = _List_node<_Ty, typename allocator_traits<_Alloc>::void_pointer>;
using _Alnode = _Rebind_alloc_t<_Alloc, _Node>;
using _Alnode_traits = allocator_traits<_Alnode>;
using _Nodeptr = typename _Alnode_traits::pointer;
using _Val_types = conditional_t<_Is_simple_alloc_v<_Alnode>, _List_simple_types<_Ty>,
_List_iter_types<_Ty, typename _Alty_traits::size_type, typename _Alty_traits::difference_type,
typename _Alty_traits::pointer, typename _Alty_traits::const_pointer, _Nodeptr>>;
using _Scary_val = _List_val<_Val_types>;
public:
static_assert(!_ENFORCE_MATCHING_ALLOCATORS || is_same_v<_Ty, typename _Alloc::value_type>,
_MISMATCHED_ALLOCATOR_MESSAGE("list<T, Allocator>", "T"));
static_assert(is_object_v<_Ty>, "The C++ Standard forbids containers of non-object types "
"because of [container.requirements].");
using value_type = _Ty;
using allocator_type = _Alloc;
using size_type = typename _Alty_traits::size_type;
using difference_type = typename _Alty_traits::difference_type;
using pointer = typename _Alty_traits::pointer;
using const_pointer = typename _Alty_traits::const_pointer;
using reference = value_type&;
using const_reference = const value_type&;
using iterator = _List_iterator<_Scary_val>;
using const_iterator = _List_const_iterator<_Scary_val>;
using _Unchecked_iterator = _List_unchecked_iterator<_Scary_val>;
using _Unchecked_const_iterator = _List_unchecked_const_iterator<_Scary_val>;
using reverse_iterator = _STD reverse_iterator<iterator>;
using const_reverse_iterator = _STD reverse_iterator<const_iterator>;
list() : _Mypair(_Zero_then_variadic_args_t{}) {
_Alloc_sentinel_and_proxy();
}
explicit list(const _Alloc& _Al) : _Mypair(_One_then_variadic_args_t{}, _Al) {
_Alloc_sentinel_and_proxy();
}
private:
template <class _Any_alloc>
explicit list(_Move_allocator_tag, _Any_alloc& _Al) : _Mypair(_One_then_variadic_args_t{}, _STD move(_Al)) {
_Alloc_sentinel_and_proxy();
}
void _Construct_n(_CRT_GUARDOVERFLOW size_type _Count) {
auto&& _Alproxy = _GET_PROXY_ALLOCATOR(_Alnode, _Getal());
_Container_proxy_ptr<_Alty> _Proxy(_Alproxy, _Mypair._Myval2);
_List_node_insert_op2<_Alnode> _Appended(_Getal());
_Appended._Append_n(_Count);
_Appended._Attach_head(_Mypair._Myval2);
_Proxy._Release();
}
public:
explicit list(_CRT_GUARDOVERFLOW size_type _Count)
: _Mypair(_Zero_then_variadic_args_t{}) { // construct list from _Count * _Ty()
_Construct_n(_Count);
}
explicit list(_CRT_GUARDOVERFLOW size_type _Count, const _Alloc& _Al)
: _Mypair(_One_then_variadic_args_t{}, _Al) { // construct list from _Count * _Ty(), with allocator
_Construct_n(_Count);
}
private:
void _Construct_n(_CRT_GUARDOVERFLOW size_type _Count, const _Ty& _Val) {
auto&& _Alproxy = _GET_PROXY_ALLOCATOR(_Alnode, _Getal());
_Container_proxy_ptr<_Alty> _Proxy(_Alproxy, _Mypair._Myval2);
_List_node_insert_op2<_Alnode> _Appended(_Getal());
_Appended._Append_n(_Count, _Val);
_Appended._Attach_head(_Mypair._Myval2);
_Proxy._Release();
}
public:
list(_CRT_GUARDOVERFLOW size_type _Count, const _Ty& _Val)
: _Mypair(_Zero_then_variadic_args_t{}) { // construct list from _Count * _Val
_Construct_n(_Count, _Val);
}
list(_CRT_GUARDOVERFLOW size_type _Count, const _Ty& _Val, const _Alloc& _Al)
: _Mypair(_One_then_variadic_args_t{}, _Al) { // construct list from _Count * _Val, allocator
_Construct_n(_Count, _Val);
}
private:
template <class _Iter, class _Sent>
void _Construct_range_unchecked(_Iter _First, const _Sent _Last) {
auto&& _Alproxy = _GET_PROXY_ALLOCATOR(_Alnode, _Getal());
_Container_proxy_ptr<_Alty> _Proxy(_Alproxy, _Mypair._Myval2);
_List_node_insert_op2<_Alnode> _Appended(_Getal());
_Appended._Append_range_unchecked(_STD move(_First), _Last);
_Appended._Attach_head(_Mypair._Myval2);
_Proxy._Release();
}
public:
list(const list& _Right)
: _Mypair(_One_then_variadic_args_t{}, _Alnode_traits::select_on_container_copy_construction(_Right._Getal())) {
_Construct_range_unchecked(_Right._Unchecked_begin(), _Right._Unchecked_end());
}
list(const list& _Right, const _Identity_t<_Alloc>& _Al) : _Mypair(_One_then_variadic_args_t{}, _Al) {
_Construct_range_unchecked(_Right._Unchecked_begin(), _Right._Unchecked_end());
}
template <class _Iter, enable_if_t<_Is_iterator_v<_Iter>, int> = 0>
list(_Iter _First, _Iter _Last) : _Mypair(_Zero_then_variadic_args_t{}) {
_Adl_verify_range(_First, _Last);
_Construct_range_unchecked(_Get_unwrapped(_First), _Get_unwrapped(_Last));
}
template <class _Iter, enable_if_t<_Is_iterator_v<_Iter>, int> = 0>
list(_Iter _First, _Iter _Last, const _Alloc& _Al) : _Mypair(_One_then_variadic_args_t{}, _Al) {
_Adl_verify_range(_First, _Last);
_Construct_range_unchecked(_Get_unwrapped(_First), _Get_unwrapped(_Last));
}
#if _HAS_CXX23 && defined(__cpp_lib_concepts) // TRANSITION, GH-395
template <_Container_compatible_range<_Ty> _Rng>
list(from_range_t, _Rng&& _Range) : _Mypair(_Zero_then_variadic_args_t{}) {
_Construct_range_unchecked(_RANGES _Ubegin(_Range), _RANGES _Uend(_Range));
}
template <_Container_compatible_range<_Ty> _Rng>
list(from_range_t, _Rng&& _Range, const _Alloc& _Al) : _Mypair(_One_then_variadic_args_t{}, _Al) {
_Construct_range_unchecked(_RANGES _Ubegin(_Range), _RANGES _Uend(_Range));
}
#endif // _HAS_CXX23 && defined(__cpp_lib_concepts)
list(list&& _Right) noexcept(false) : _Mypair(_One_then_variadic_args_t{}, _STD move(_Right._Getal())) {
_Alloc_sentinel_and_proxy();
_Swap_val(_Right);
}
list(list&& _Right, const _Identity_t<_Alloc>& _Al) : _Mypair(_One_then_variadic_args_t{}, _Al) {
if constexpr (!_Alnode_traits::is_always_equal::value) {
if (_Getal() != _Right._Getal()) {
_Construct_range_unchecked(_STD make_move_iterator(_Right._Unchecked_begin()),
_STD make_move_iterator(_Right._Unchecked_end()));
return;
}
}
_Alloc_sentinel_and_proxy();
_Swap_val(_Right);
}
list& operator=(list&& _Right) noexcept(
_Choose_pocma_v<_Alnode> == _Pocma_values::_Equal_allocators) /* strengthened */ {
if (this == _STD addressof(_Right)) {
return *this;
}
auto& _Al = _Getal();
auto& _Right_al = _Right._Getal();
constexpr auto _Pocma_val = _Choose_pocma_v<_Alnode>;
if constexpr (_Pocma_val == _Pocma_values::_Propagate_allocators) {
if (_Al != _Right_al) {
auto&& _Alproxy = _GET_PROXY_ALLOCATOR(_Alnode, _Al);
auto&& _Right_alproxy = _GET_PROXY_ALLOCATOR(_Alnode, _Right_al);
_Container_proxy_ptr<_Alty> _Proxy(_Right_alproxy, _Leave_proxy_unbound{});
auto& _My_data = _Mypair._Myval2;
auto& _Right_data = _Right._Mypair._Myval2;
const auto _Newhead = _STD exchange(_Right_data._Myhead, _Node::_Buyheadnode(_Right_al));
const auto _Newsize = _STD exchange(_Right_data._Mysize, size_type{0});
_Tidy();
_Pocma(_Al, _Right_al);
_My_data._Myhead = _Newhead;
_My_data._Mysize = _Newsize;
_Proxy._Bind(_Alproxy, _STD addressof(_My_data));
_My_data._Swap_proxy_and_iterators(_Right_data);
return *this;
}
} else if constexpr (_Pocma_val == _Pocma_values::_No_propagate_allocators) {
if (_Al != _Right_al) {
assign(_STD make_move_iterator(_Right._Unchecked_begin()),
_STD make_move_iterator(_Right._Unchecked_end()));
return *this;
}
}
clear();
_Pocma(_Al, _Right_al);
_Swap_val(_Right);
return *this;
}
private:
void _Swap_val(list& _Right) noexcept { // swap with _Right, same allocator
using _STD swap;
auto& _My_data = _Mypair._Myval2;
auto& _Right_data = _Right._Mypair._Myval2;
_My_data._Swap_proxy_and_iterators(_Right_data);
swap(_My_data._Myhead, _Right_data._Myhead); // intentional ADL
_STD swap(_My_data._Mysize, _Right_data._Mysize);
}
public:
void push_front(_Ty&& _Val) { // insert element at beginning
_Emplace(_Mypair._Myval2._Myhead->_Next, _STD move(_Val));
}
void push_back(_Ty&& _Val) { // insert element at end
_Emplace(_Mypair._Myval2._Myhead, _STD move(_Val));
}
iterator insert(const_iterator _Where, _Ty&& _Val) { // insert _Val at _Where
return emplace(_Where, _STD move(_Val));
}
template <class... _Valty>
decltype(auto) emplace_front(_Valty&&... _Val) { // insert element at beginning
reference _Result = _Emplace(_Mypair._Myval2._Myhead->_Next, _STD forward<_Valty>(_Val)...)->_Myval;
#if _HAS_CXX17
return _Result;
#else // ^^^ _HAS_CXX17 / !_HAS_CXX17 vvv
(void) _Result;
#endif // ^^^ !_HAS_CXX17 ^^^
}
template <class... _Valty>
decltype(auto) emplace_back(_Valty&&... _Val) { // insert element at end
reference _Result = _Emplace(_Mypair._Myval2._Myhead, _STD forward<_Valty>(_Val)...)->_Myval;
#if _HAS_CXX17
return _Result;
#else // ^^^ _HAS_CXX17 / !_HAS_CXX17 vvv
(void) _Result;
#endif // ^^^ !_HAS_CXX17 ^^^
}