-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathxtree
1986 lines (1674 loc) · 77.4 KB
/
xtree
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
// xtree internal header
// Copyright (c) Microsoft Corporation.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#pragma once
#ifndef _XTREE_
#define _XTREE_
#include <yvals_core.h>
#if _STL_COMPILER_PREPROCESSOR
#include <xmemory>
#if _HAS_CXX17
#include <xnode_handle.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 _Mytree, class _Base = _Iterator_base0>
class _Tree_unchecked_const_iterator : public _Base {
public:
using iterator_category = bidirectional_iterator_tag;
using _Nodeptr = typename _Mytree::_Nodeptr;
using value_type = typename _Mytree::value_type;
using difference_type = typename _Mytree::difference_type;
using pointer = typename _Mytree::const_pointer;
using reference = const value_type&;
_Tree_unchecked_const_iterator() noexcept : _Ptr() {}
_Tree_unchecked_const_iterator(_Nodeptr _Pnode, const _Mytree* _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);
}
_Tree_unchecked_const_iterator& operator++() noexcept {
if (_Ptr->_Right->_Isnil) { // climb looking for right subtree
_Nodeptr _Pnode;
while (!(_Pnode = _Ptr->_Parent)->_Isnil && _Ptr == _Pnode->_Right) {
_Ptr = _Pnode; // ==> parent while right subtree
}
_Ptr = _Pnode; // ==> parent (head if end())
} else {
_Ptr = _Mytree::_Min(_Ptr->_Right); // ==> smallest of right subtree
}
return *this;
}
_Tree_unchecked_const_iterator operator++(int) noexcept {
_Tree_unchecked_const_iterator _Tmp = *this;
++*this;
return _Tmp;
}
_Tree_unchecked_const_iterator& operator--() noexcept {
if (_Ptr->_Isnil) {
_Ptr = _Ptr->_Right; // end() ==> rightmost
} else if (_Ptr->_Left->_Isnil) { // climb looking for left subtree
_Nodeptr _Pnode;
while (!(_Pnode = _Ptr->_Parent)->_Isnil && _Ptr == _Pnode->_Left) {
_Ptr = _Pnode; // ==> parent while left subtree
}
if (!_Ptr->_Isnil) { // decrement non-begin()
_Ptr = _Pnode; // ==> parent if not head
}
} else {
_Ptr = _Mytree::_Max(_Ptr->_Left); // ==> largest of left subtree
}
return *this;
}
_Tree_unchecked_const_iterator operator--(int) noexcept {
_Tree_unchecked_const_iterator _Tmp = *this;
--*this;
return _Tmp;
}
_NODISCARD bool operator==(const _Tree_unchecked_const_iterator& _Right) const noexcept {
return _Ptr == _Right._Ptr;
}
#if !_HAS_CXX20
_NODISCARD bool operator!=(const _Tree_unchecked_const_iterator& _Right) const noexcept {
return !(*this == _Right);
}
#endif // !_HAS_CXX20
_NODISCARD bool operator==(_Default_sentinel) const noexcept {
return !!_Ptr->_Isnil; // TRANSITION, avoid warning C4800:
// "Implicit conversion from 'char' to bool. Possible information loss" (/Wall)
}
#if !_HAS_CXX20
_NODISCARD bool operator!=(_Default_sentinel) const noexcept {
return !_Ptr->_Isnil;
}
#endif // !_HAS_CXX20
_Nodeptr _Ptr; // pointer to node
};
template <class _Mytree>
class _Tree_unchecked_iterator : public _Tree_unchecked_const_iterator<_Mytree> {
public:
using _Mybase = _Tree_unchecked_const_iterator<_Mytree>;
using iterator_category = bidirectional_iterator_tag;
using _Nodeptr = typename _Mytree::_Nodeptr;
using value_type = typename _Mytree::value_type;
using difference_type = typename _Mytree::difference_type;
using pointer = typename _Mytree::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);
}
_Tree_unchecked_iterator& operator++() noexcept {
_Mybase::operator++();
return *this;
}
_Tree_unchecked_iterator operator++(int) noexcept {
_Tree_unchecked_iterator _Tmp = *this;
_Mybase::operator++();
return _Tmp;
}
_Tree_unchecked_iterator& operator--() noexcept {
_Mybase::operator--();
return *this;
}
_Tree_unchecked_iterator operator--(int) noexcept {
_Tree_unchecked_iterator _Tmp = *this;
_Mybase::operator--();
return _Tmp;
}
};
template <class _Mytree>
class _Tree_const_iterator : public _Tree_unchecked_const_iterator<_Mytree, _Iterator_base> {
public:
using _Mybase = _Tree_unchecked_const_iterator<_Mytree, _Iterator_base>;
using iterator_category = bidirectional_iterator_tag;
using _Nodeptr = typename _Mytree::_Nodeptr;
using value_type = typename _Mytree::value_type;
using difference_type = typename _Mytree::difference_type;
using pointer = typename _Mytree::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 _Mytree*>(this->_Getcont());
_STL_ASSERT(_Mycont, "cannot dereference value-initialized map/set iterator");
_STL_VERIFY(this->_Ptr != _Mycont->_Myhead, "cannot dereference end map/set iterator");
#endif // _ITERATOR_DEBUG_LEVEL == 2
return this->_Ptr->_Myval;
}
_NODISCARD pointer operator->() const noexcept {
return pointer_traits<pointer>::pointer_to(**this);
}
_Tree_const_iterator& operator++() noexcept {
#if _ITERATOR_DEBUG_LEVEL == 2
_STL_VERIFY(this->_Getcont(), "cannot increment value-initialized map/set iterator");
_STL_VERIFY(!this->_Ptr->_Isnil, "cannot increment end map/set iterator");
#endif // _ITERATOR_DEBUG_LEVEL == 2
_Mybase::operator++();
return *this;
}
_Tree_const_iterator operator++(int) noexcept {
_Tree_const_iterator _Tmp = *this;
++*this;
return _Tmp;
}
_Tree_const_iterator& operator--() noexcept {
#if _ITERATOR_DEBUG_LEVEL == 2
_STL_ASSERT(this->_Getcont(), "cannot decrement value-initialized map/set iterator");
_Nodeptr _Ptrsav = this->_Ptr;
_Mybase::operator--();
_STL_VERIFY(_Ptrsav != this->_Ptr, "cannot decrement begin map/set iterator");
#else // ^^^ _ITERATOR_DEBUG_LEVEL == 2 / _ITERATOR_DEBUG_LEVEL != 2 vvv
_Mybase::operator--();
#endif // _ITERATOR_DEBUG_LEVEL == 2
return *this;
}
_Tree_const_iterator operator--(int) noexcept {
_Tree_const_iterator _Tmp = *this;
--*this;
return _Tmp;
}
_NODISCARD bool operator==(const _Tree_const_iterator& _Right) const noexcept {
#if _ITERATOR_DEBUG_LEVEL == 2
_STL_VERIFY(this->_Getcont() == _Right._Getcont(), "map/set iterators incompatible");
#endif // _ITERATOR_DEBUG_LEVEL == 2
return this->_Ptr == _Right._Ptr;
}
#if !_HAS_CXX20
_NODISCARD bool operator!=(const _Tree_const_iterator& _Right) const noexcept {
return !(*this == _Right);
}
#endif // !_HAS_CXX20
#if _ITERATOR_DEBUG_LEVEL == 2
friend void _Verify_range(const _Tree_const_iterator& _First, const _Tree_const_iterator& _Last) noexcept {
_STL_VERIFY(_First._Getcont() == _Last._Getcont(), "map/set iterators in range are from different containers");
}
#endif // _ITERATOR_DEBUG_LEVEL == 2
using _Prevent_inheriting_unwrap = _Tree_const_iterator;
_NODISCARD _Tree_unchecked_const_iterator<_Mytree> _Unwrapped() const noexcept {
return _Tree_unchecked_const_iterator<_Mytree>(this->_Ptr, static_cast<const _Mytree*>(this->_Getcont()));
}
void _Seek_to(const _Tree_unchecked_const_iterator<_Mytree> _It) noexcept {
this->_Ptr = _It._Ptr;
}
};
template <class _Mytree>
class _Tree_iterator : public _Tree_const_iterator<_Mytree> {
public:
using _Mybase = _Tree_const_iterator<_Mytree>;
using iterator_category = bidirectional_iterator_tag;
using _Nodeptr = typename _Mytree::_Nodeptr;
using value_type = typename _Mytree::value_type;
using difference_type = typename _Mytree::difference_type;
using pointer = typename _Mytree::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);
}
_Tree_iterator& operator++() noexcept {
_Mybase::operator++();
return *this;
}
_Tree_iterator operator++(int) noexcept {
_Tree_iterator _Tmp = *this;
_Mybase::operator++();
return _Tmp;
}
_Tree_iterator& operator--() noexcept {
_Mybase::operator--();
return *this;
}
_Tree_iterator operator--(int) noexcept {
_Tree_iterator _Tmp = *this;
_Mybase::operator--();
return _Tmp;
}
using _Prevent_inheriting_unwrap = _Tree_iterator;
_NODISCARD _Tree_unchecked_iterator<_Mytree> _Unwrapped() const noexcept {
return _Tree_unchecked_iterator<_Mytree>(this->_Ptr, static_cast<const _Mytree*>(this->_Getcont()));
}
};
template <class _Value_type, class _Size_type, class _Difference_type, class _Pointer, class _Const_pointer,
class _Reference, class _Const_reference, class _Nodeptr_type>
struct _Tree_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 _Tree_node {
using _Nodeptr = _Rebind_pointer_t<_Voidptr, _Tree_node>;
using value_type = _Value_type;
_Nodeptr _Left; // left subtree, or smallest element if head
_Nodeptr _Parent; // parent, or root of tree if head
_Nodeptr _Right; // right subtree, or largest element if head
char _Color; // _Red or _Black, _Black if head
char _Isnil; // true only if head (also nil) node; TRANSITION, should be bool
value_type _Myval = // the stored value, unused if head
_Returns_exactly<value_type>(); // fake a viable constructor to workaround GH-2749
enum _Redbl { // colors for link to parent
_Red,
_Black
};
_Tree_node() = default;
_Tree_node(const _Tree_node&) = delete;
_Tree_node& operator=(const _Tree_node&) = delete;
template <class _Alloc>
static _Nodeptr _Buyheadnode(_Alloc& _Al) {
static_assert(is_same_v<typename _Alloc::value_type, _Tree_node>, "Bad _Buyheadnode call");
const auto _Pnode = _Al.allocate(1);
_Construct_in_place(_Pnode->_Left, _Pnode);
_Construct_in_place(_Pnode->_Parent, _Pnode);
_Construct_in_place(_Pnode->_Right, _Pnode);
_Pnode->_Color = _Black;
_Pnode->_Isnil = true;
return _Pnode;
}
template <class _Alloc, class... _Valty>
static _Nodeptr _Buynode(_Alloc& _Al, _Nodeptr _Myhead, _Valty&&... _Val) {
// allocate a node with defaults and set links and value
static_assert(is_same_v<typename _Alloc::value_type, _Tree_node>, "Bad _Buynode call");
_Alloc_construct_ptr<_Alloc> _Newnode(_Al);
_Newnode._Allocate();
allocator_traits<_Alloc>::construct(_Al, _STD addressof(_Newnode._Ptr->_Myval), _STD forward<_Valty>(_Val)...);
_Construct_in_place(_Newnode._Ptr->_Left, _Myhead);
_Construct_in_place(_Newnode._Ptr->_Parent, _Myhead);
_Construct_in_place(_Newnode._Ptr->_Right, _Myhead);
_Newnode._Ptr->_Color = _Red;
_Newnode._Ptr->_Isnil = false;
return _Newnode._Release();
}
template <class _Alloc>
static void _Freenode0(_Alloc& _Al, _Nodeptr _Ptr) noexcept {
static_assert(is_same_v<typename _Alloc::value_type, _Tree_node>, "Bad _Freenode0 call");
_Destroy_in_place(_Ptr->_Left);
_Destroy_in_place(_Ptr->_Parent);
_Destroy_in_place(_Ptr->_Right);
allocator_traits<_Alloc>::deallocate(_Al, _Ptr, 1);
}
template <class _Alloc>
static void _Freenode(_Alloc& _Al, _Nodeptr _Ptr) noexcept {
static_assert(is_same_v<typename _Alloc::value_type, _Tree_node>, "Bad _Freenode call");
allocator_traits<_Alloc>::destroy(_Al, _STD addressof(_Ptr->_Myval));
_Freenode0(_Al, _Ptr);
}
};
template <class _Ty>
struct _Tree_simple_types : _Simple_types<_Ty> {
using _Node = _Tree_node<_Ty, void*>;
using _Nodeptr = _Node*;
};
enum class _Tree_child {
_Right, // perf note: compare with _Right rather than _Left where possible for comparison with zero
_Left,
_Unused // indicates that tree child should never be used for insertion
};
template <class _Nodeptr>
struct _Tree_id {
_Nodeptr _Parent; // the leaf node under which a new node should be inserted
_Tree_child _Child;
};
template <class _Nodeptr>
struct _Tree_find_result {
_Tree_id<_Nodeptr> _Location;
_Nodeptr _Bound;
};
template <class _Nodeptr>
struct _Tree_find_hint_result {
_Tree_id<_Nodeptr> _Location;
bool _Duplicate;
};
[[noreturn]] inline void _Throw_tree_length_error() {
_Xlength_error("map/set too long");
}
template <class _Val_types>
class _Tree_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&;
using _Unchecked_const_iterator = _Tree_unchecked_const_iterator<_Tree_val>;
using const_iterator = _Tree_const_iterator<_Tree_val>;
_Tree_val() noexcept : _Myhead(), _Mysize(0) {}
enum _Redbl { // colors for link to parent
_Red,
_Black
};
static _Nodeptr _Max(_Nodeptr _Pnode) noexcept { // return rightmost node in subtree at _Pnode
while (!_Pnode->_Right->_Isnil) {
_Pnode = _Pnode->_Right;
}
return _Pnode;
}
static _Nodeptr _Min(_Nodeptr _Pnode) noexcept { // return leftmost node in subtree at _Pnode
while (!_Pnode->_Left->_Isnil) {
_Pnode = _Pnode->_Left;
}
return _Pnode;
}
void _Lrotate(_Nodeptr _Wherenode) noexcept { // promote right node to root of subtree
_Nodeptr _Pnode = _Wherenode->_Right;
_Wherenode->_Right = _Pnode->_Left;
if (!_Pnode->_Left->_Isnil) {
_Pnode->_Left->_Parent = _Wherenode;
}
_Pnode->_Parent = _Wherenode->_Parent;
if (_Wherenode == _Myhead->_Parent) {
_Myhead->_Parent = _Pnode;
} else if (_Wherenode == _Wherenode->_Parent->_Left) {
_Wherenode->_Parent->_Left = _Pnode;
} else {
_Wherenode->_Parent->_Right = _Pnode;
}
_Pnode->_Left = _Wherenode;
_Wherenode->_Parent = _Pnode;
}
void _Rrotate(_Nodeptr _Wherenode) noexcept { // promote left node to root of subtree
_Nodeptr _Pnode = _Wherenode->_Left;
_Wherenode->_Left = _Pnode->_Right;
if (!_Pnode->_Right->_Isnil) {
_Pnode->_Right->_Parent = _Wherenode;
}
_Pnode->_Parent = _Wherenode->_Parent;
if (_Wherenode == _Myhead->_Parent) {
_Myhead->_Parent = _Pnode;
} else if (_Wherenode == _Wherenode->_Parent->_Right) {
_Wherenode->_Parent->_Right = _Pnode;
} else {
_Wherenode->_Parent->_Left = _Pnode;
}
_Pnode->_Right = _Wherenode;
_Wherenode->_Parent = _Pnode;
}
_Nodeptr _Extract(_Unchecked_const_iterator _Where) noexcept {
_Nodeptr _Erasednode = _Where._Ptr; // node to erase
++_Where; // save successor iterator for return
_Nodeptr _Fixnode; // the node to recolor as needed
_Nodeptr _Fixnodeparent; // parent of _Fixnode (which may be nil)
_Nodeptr _Pnode = _Erasednode;
if (_Pnode->_Left->_Isnil) {
_Fixnode = _Pnode->_Right; // stitch up right subtree
} else if (_Pnode->_Right->_Isnil) {
_Fixnode = _Pnode->_Left; // stitch up left subtree
} else { // two subtrees, must lift successor node to replace erased
_Pnode = _Where._Ptr; // _Pnode is successor node
_Fixnode = _Pnode->_Right; // _Fixnode is only subtree
}
if (_Pnode == _Erasednode) { // at most one subtree, relink it
_Fixnodeparent = _Erasednode->_Parent;
if (!_Fixnode->_Isnil) {
_Fixnode->_Parent = _Fixnodeparent; // link up
}
if (_Myhead->_Parent == _Erasednode) {
_Myhead->_Parent = _Fixnode; // link down from root
} else if (_Fixnodeparent->_Left == _Erasednode) {
_Fixnodeparent->_Left = _Fixnode; // link down to left
} else {
_Fixnodeparent->_Right = _Fixnode; // link down to right
}
if (_Myhead->_Left == _Erasednode) {
_Myhead->_Left = _Fixnode->_Isnil ? _Fixnodeparent // smallest is parent of erased node
: _Min(_Fixnode); // smallest in relinked subtree
}
if (_Myhead->_Right == _Erasednode) {
_Myhead->_Right = _Fixnode->_Isnil ? _Fixnodeparent // largest is parent of erased node
: _Max(_Fixnode); // largest in relinked subtree
}
} else { // erased has two subtrees, _Pnode is successor to erased
_Erasednode->_Left->_Parent = _Pnode; // link left up
_Pnode->_Left = _Erasednode->_Left; // link successor down
if (_Pnode == _Erasednode->_Right) {
_Fixnodeparent = _Pnode; // successor is next to erased
} else { // successor further down, link in place of erased
_Fixnodeparent = _Pnode->_Parent; // parent is successor's
if (!_Fixnode->_Isnil) {
_Fixnode->_Parent = _Fixnodeparent; // link fix up
}
_Fixnodeparent->_Left = _Fixnode; // link fix down
_Pnode->_Right = _Erasednode->_Right; // link next down
_Erasednode->_Right->_Parent = _Pnode; // right up
}
if (_Myhead->_Parent == _Erasednode) {
_Myhead->_Parent = _Pnode; // link down from root
} else if (_Erasednode->_Parent->_Left == _Erasednode) {
_Erasednode->_Parent->_Left = _Pnode; // link down to left
} else {
_Erasednode->_Parent->_Right = _Pnode; // link down to right
}
_Pnode->_Parent = _Erasednode->_Parent; // link successor up
_STD swap(_Pnode->_Color, _Erasednode->_Color); // recolor it
}
if (_Erasednode->_Color == _Black) { // erasing black link, must recolor/rebalance tree
for (; _Fixnode != _Myhead->_Parent && _Fixnode->_Color == _Black; _Fixnodeparent = _Fixnode->_Parent) {
if (_Fixnode == _Fixnodeparent->_Left) { // fixup left subtree
_Pnode = _Fixnodeparent->_Right;
if (_Pnode->_Color == _Red) { // rotate red up from right subtree
_Pnode->_Color = _Black;
_Fixnodeparent->_Color = _Red;
_Lrotate(_Fixnodeparent);
_Pnode = _Fixnodeparent->_Right;
}
if (_Pnode->_Isnil) {
_Fixnode = _Fixnodeparent; // shouldn't happen
} else if (_Pnode->_Left->_Color == _Black
&& _Pnode->_Right->_Color == _Black) { // redden right subtree with black children
_Pnode->_Color = _Red;
_Fixnode = _Fixnodeparent;
} else { // must rearrange right subtree
if (_Pnode->_Right->_Color == _Black) { // rotate red up from left sub-subtree
_Pnode->_Left->_Color = _Black;
_Pnode->_Color = _Red;
_Rrotate(_Pnode);
_Pnode = _Fixnodeparent->_Right;
}
_Pnode->_Color = _Fixnodeparent->_Color;
_Fixnodeparent->_Color = _Black;
_Pnode->_Right->_Color = _Black;
_Lrotate(_Fixnodeparent);
break; // tree now recolored/rebalanced
}
} else { // fixup right subtree
_Pnode = _Fixnodeparent->_Left;
if (_Pnode->_Color == _Red) { // rotate red up from left subtree
_Pnode->_Color = _Black;
_Fixnodeparent->_Color = _Red;
_Rrotate(_Fixnodeparent);
_Pnode = _Fixnodeparent->_Left;
}
if (_Pnode->_Isnil) {
_Fixnode = _Fixnodeparent; // shouldn't happen
} else if (_Pnode->_Right->_Color == _Black
&& _Pnode->_Left->_Color == _Black) { // redden left subtree with black children
_Pnode->_Color = _Red;
_Fixnode = _Fixnodeparent;
} else { // must rearrange left subtree
if (_Pnode->_Left->_Color == _Black) { // rotate red up from right sub-subtree
_Pnode->_Right->_Color = _Black;
_Pnode->_Color = _Red;
_Lrotate(_Pnode);
_Pnode = _Fixnodeparent->_Left;
}
_Pnode->_Color = _Fixnodeparent->_Color;
_Fixnodeparent->_Color = _Black;
_Pnode->_Left->_Color = _Black;
_Rrotate(_Fixnodeparent);
break; // tree now recolored/rebalanced
}
}
}
_Fixnode->_Color = _Black; // stopping node is black
}
if (0 < _Mysize) {
--_Mysize;
}
return _Erasednode;
}
_Nodeptr _Insert_node(const _Tree_id<_Nodeptr> _Loc, const _Nodeptr _Newnode) noexcept {
++_Mysize;
const auto _Head = _Myhead;
_Newnode->_Parent = _Loc._Parent;
if (_Loc._Parent == _Head) { // first node in tree, just set head values
_Head->_Left = _Newnode;
_Head->_Parent = _Newnode;
_Head->_Right = _Newnode;
_Newnode->_Color = _Black; // the root is black
return _Newnode;
}
_STL_INTERNAL_CHECK(_Loc._Child != _Tree_child::_Unused);
if (_Loc._Child == _Tree_child::_Right) { // add to right of _Loc._Parent
_STL_INTERNAL_CHECK(_Loc._Parent->_Right->_Isnil);
_Loc._Parent->_Right = _Newnode;
if (_Loc._Parent == _Head->_Right) { // remember rightmost node
_Head->_Right = _Newnode;
}
} else { // add to left of _Loc._Parent
_STL_INTERNAL_CHECK(_Loc._Parent->_Left->_Isnil);
_Loc._Parent->_Left = _Newnode;
if (_Loc._Parent == _Head->_Left) { // remember leftmost node
_Head->_Left = _Newnode;
}
}
for (_Nodeptr _Pnode = _Newnode; _Pnode->_Parent->_Color == _Red;) {
if (_Pnode->_Parent == _Pnode->_Parent->_Parent->_Left) { // fixup red-red in left subtree
const auto _Parent_sibling = _Pnode->_Parent->_Parent->_Right;
if (_Parent_sibling->_Color == _Red) { // parent's sibling has two red children, blacken both
_Pnode->_Parent->_Color = _Black;
_Parent_sibling->_Color = _Black;
_Pnode->_Parent->_Parent->_Color = _Red;
_Pnode = _Pnode->_Parent->_Parent;
} else { // parent's sibling has red and black children
if (_Pnode == _Pnode->_Parent->_Right) { // rotate right child to left
_Pnode = _Pnode->_Parent;
_Lrotate(_Pnode);
}
_Pnode->_Parent->_Color = _Black; // propagate red up
_Pnode->_Parent->_Parent->_Color = _Red;
_Rrotate(_Pnode->_Parent->_Parent);
}
} else { // fixup red-red in right subtree
const auto _Parent_sibling = _Pnode->_Parent->_Parent->_Left;
if (_Parent_sibling->_Color == _Red) { // parent's sibling has two red children, blacken both
_Pnode->_Parent->_Color = _Black;
_Parent_sibling->_Color = _Black;
_Pnode->_Parent->_Parent->_Color = _Red;
_Pnode = _Pnode->_Parent->_Parent;
} else { // parent's sibling has red and black children
if (_Pnode == _Pnode->_Parent->_Left) { // rotate left child to right
_Pnode = _Pnode->_Parent;
_Rrotate(_Pnode);
}
_Pnode->_Parent->_Color = _Black; // propagate red up
_Pnode->_Parent->_Parent->_Color = _Red;
_Lrotate(_Pnode->_Parent->_Parent);
}
}
}
_Head->_Parent->_Color = _Black; // root is always black
return _Newnode;
}
void _Orphan_ptr(const _Nodeptr _Ptr) noexcept {
#if _ITERATOR_DEBUG_LEVEL == 2
_Lockit _Lock(_LOCK_DEBUG);
_Iterator_base12** _Pnext = &this->_Myproxy->_Myfirstiter;
while (*_Pnext) {
const auto _Pnextptr = static_cast<const_iterator&>(**_Pnext)._Ptr;
if (_Pnextptr == _Myhead || (_Ptr != nullptr && _Pnextptr != _Ptr)) {
_Pnext = &(*_Pnext)->_Mynextiter;
} else { // orphan the iterator
(*_Pnext)->_Myproxy = nullptr;
*_Pnext = (*_Pnext)->_Mynextiter;
}
}
#else // ^^^ _ITERATOR_DEBUG_LEVEL == 2 / _ITERATOR_DEBUG_LEVEL != 2 vvv
(void) _Ptr;
#endif // _ITERATOR_DEBUG_LEVEL == 2
}
template <class _Alnode>
void _Erase_tree_and_orphan(_Alnode& _Al, _Nodeptr _Rootnode) noexcept {
while (!_Rootnode->_Isnil) { // free subtrees, then node
_Erase_tree_and_orphan(_Al, _Rootnode->_Right);
auto _To_delete = _STD exchange(_Rootnode, _Rootnode->_Left);
_Orphan_ptr(_To_delete);
_Alnode::value_type::_Freenode(_Al, _To_delete);
}
}
template <class _Alnode>
void _Erase_tree(_Alnode& _Al, _Nodeptr _Rootnode) noexcept {
while (!_Rootnode->_Isnil) { // free subtrees, then node
_Erase_tree(_Al, _Rootnode->_Right);
_Alnode::value_type::_Freenode(_Al, _STD exchange(_Rootnode, _Rootnode->_Left));
}
}
template <class _Alnode>
void _Erase_head(_Alnode& _Al) noexcept {
this->_Orphan_all();
_Erase_tree(_Al, _Myhead->_Parent);
_Alnode::value_type::_Freenode0(_Al, _Myhead);
}
_Nodeptr _Myhead; // pointer to head node
size_type _Mysize; // number of elements
};
template <class _Alnode, class _Scary_val>
struct _Tree_head_scoped_ptr { // temporary storage for allocated node pointers to ensure exception safety
_Alnode& _Al;
_Scary_val* _Mycont;
_Tree_head_scoped_ptr(_Alnode& _Al_, _Scary_val& _Mycont_) : _Al(_Al_), _Mycont(_STD addressof(_Mycont_)) {
_Mycont->_Myhead = _Alnode::value_type::_Buyheadnode(_Al);
}
void _Release() noexcept {
_Mycont = nullptr;
}
~_Tree_head_scoped_ptr() {
if (_Mycont) {
_Mycont->_Erase_head(_Al);
}
}
};
template <class _Alnode>
struct _Tree_temp_node_alloc : _Alloc_construct_ptr<_Alnode> {
// EH helper for _Tree_temp_node
explicit _Tree_temp_node_alloc(_Alnode& _Al_) : _Alloc_construct_ptr<_Alnode>(_Al_) {
_Alloc_construct_ptr<_Alnode>::_Allocate();
}
_Tree_temp_node_alloc(const _Tree_temp_node_alloc&) = delete;
_Tree_temp_node_alloc& operator=(const _Tree_temp_node_alloc&) = delete;
};
template <class _Alnode>
struct _Tree_temp_node : _Tree_temp_node_alloc<_Alnode> {
// temporarily stores a constructed tree node
using _Alnode_traits = allocator_traits<_Alnode>;
using _Nodeptr = typename _Alnode_traits::pointer;
enum _Redbl { // colors for link to parent
_Red,
_Black
};
template <class... _Valtys>
explicit _Tree_temp_node(_Alnode& _Al_, _Nodeptr _Myhead, _Valtys&&... _Vals)
: _Tree_temp_node_alloc<_Alnode>(_Al_) {
_Alnode_traits::construct(this->_Al, _STD addressof(this->_Ptr->_Myval), _STD forward<_Valtys>(_Vals)...);
_Construct_in_place(this->_Ptr->_Left, _Myhead);
_Construct_in_place(this->_Ptr->_Parent, _Myhead);
_Construct_in_place(this->_Ptr->_Right, _Myhead);
this->_Ptr->_Color = _Red;
this->_Ptr->_Isnil = false;
}
_Tree_temp_node(const _Tree_temp_node&) = delete;
_Tree_temp_node& operator=(const _Tree_temp_node&) = delete;
~_Tree_temp_node() {
if (this->_Ptr) {
_Destroy_in_place(this->_Ptr->_Left);
_Destroy_in_place(this->_Ptr->_Parent);
_Destroy_in_place(this->_Ptr->_Right);
_Alnode_traits::destroy(this->_Al, _STD addressof(this->_Ptr->_Myval));
}
}
};
template <class _Traits>
class _Tree { // ordered red-black tree for map/multimap/set/multiset
public:
using key_type = typename _Traits::key_type;
using value_type = typename _Traits::value_type;
using allocator_type = typename _Traits::allocator_type;
protected:
using _Alty = _Rebind_alloc_t<allocator_type, value_type>;
using _Alty_traits = allocator_traits<_Alty>;
using _Node = _Tree_node<value_type, typename _Alty_traits::void_pointer>;
using _Alnode = _Rebind_alloc_t<allocator_type, _Node>;
using _Alnode_traits = allocator_traits<_Alnode>;
using _Nodeptr = typename _Alnode_traits::pointer;
using _Scary_val = _Tree_val<conditional_t<_Is_simple_alloc_v<_Alnode>, _Tree_simple_types<value_type>,
_Tree_iter_types<value_type, typename _Alty_traits::size_type, typename _Alty_traits::difference_type,
typename _Alty_traits::pointer, typename _Alty_traits::const_pointer, value_type&, const value_type&,
_Nodeptr>>>;
static constexpr bool _Multi = _Traits::_Multi;
static constexpr bool _Is_set = is_same_v<key_type, value_type>;
enum _Redbl { // colors for link to parent
_Red,
_Black
};
public:
using value_compare = typename _Traits::value_compare;
using key_compare = typename _Traits::key_compare;
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 = conditional_t<_Is_set, _Tree_const_iterator<_Scary_val>, _Tree_iterator<_Scary_val>>;
using const_iterator = _Tree_const_iterator<_Scary_val>;
using _Unchecked_iterator =
conditional_t<_Is_set, _Tree_unchecked_const_iterator<_Scary_val>, _Tree_unchecked_iterator<_Scary_val>>;
using _Unchecked_const_iterator = _Tree_unchecked_const_iterator<_Scary_val>;
using reverse_iterator = _STD reverse_iterator<iterator>;
using const_reverse_iterator = _STD reverse_iterator<const_iterator>;
enum class _Strategy : bool {
_Copy,
_Move,
};
_Tree(const key_compare& _Parg) : _Mypair(_One_then_variadic_args_t{}, _Parg, _Zero_then_variadic_args_t{}) {
_Alloc_sentinel_and_proxy();
}
_Tree(const key_compare& _Parg, const allocator_type& _Al)
: _Mypair(_One_then_variadic_args_t{}, _Parg, _One_then_variadic_args_t{}, _Al) {
_Alloc_sentinel_and_proxy();
}
template <class _Any_alloc>
_Tree(const _Tree& _Right, _Any_alloc&& _Al)
: _Mypair(_One_then_variadic_args_t{}, _Right.key_comp(), _One_then_variadic_args_t{},
_STD forward<_Any_alloc>(_Al)) {
auto&& _Alproxy = _GET_PROXY_ALLOCATOR(_Alnode, _Getal());
const auto _Scary = _Get_scary();
_Container_proxy_ptr<_Alty> _Proxy(_Alproxy, *_Scary);
_Tree_head_scoped_ptr<_Alnode, _Scary_val> _Sentinel(_Getal(), *_Scary);
_Copy<_Strategy::_Copy>(_Right);
_Sentinel._Release();
_Proxy._Release();
}
_Tree(_Tree&& _Right)
: _Mypair(_One_then_variadic_args_t{}, _Right.key_comp(), // intentionally copy comparator, see LWG-2227
_One_then_variadic_args_t{}, _STD move(_Right._Getal())) {
_Alloc_sentinel_and_proxy();
_Swap_val_excluding_comp(_Right);
}
private:
void _Different_allocator_move_construct(_Tree&& _Right) {
// TRANSITION, VSO-761321 (inline into only caller when that is fixed)
auto&& _Alproxy = _GET_PROXY_ALLOCATOR(_Alnode, _Getal());
const auto _Scary = _Get_scary();
_Container_proxy_ptr<_Alty> _Proxy(_Alproxy, *_Scary);
_Tree_head_scoped_ptr<_Alnode, _Scary_val> _Sentinel(_Getal(), *_Scary);
_Copy<_Strategy::_Move>(_Right);
_Sentinel._Release();
_Proxy._Release();
}
public:
_Tree(_Tree&& _Right, const allocator_type& _Al)
: _Mypair(_One_then_variadic_args_t{}, _Right.key_comp(), // intentionally copy comparator, see LWG-2227
_One_then_variadic_args_t{}, _Al) {
if constexpr (!_Alnode_traits::is_always_equal::value) {
if (_Getal() != _Right._Getal()) {
_Different_allocator_move_construct(_STD move(_Right));
return;
}
}
_Alloc_sentinel_and_proxy();
_Swap_val_excluding_comp(_Right);
}
_Tree& operator=(_Tree&& _Right) noexcept(
_Choose_pocma_v<_Alnode> == _Pocma_values::_Equal_allocators && is_nothrow_move_assignable_v<key_compare>) {
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) {
clear();
_Getcomp() = _Right._Getcomp(); // intentionally copy comparator, see LWG-2227
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{});
const auto _Scary = _Get_scary();
const auto _Right_scary = _Right._Get_scary();
const auto _Newhead = _STD exchange(_Right_scary->_Myhead, _Node::_Buyheadnode(_Right_al));
const auto _Newsize = _STD exchange(_Right_scary->_Mysize, size_type{0});
_Scary->_Erase_head(_Al);
_Pocma(_Al, _Right_al);
_Scary->_Myhead = _Newhead;
_Scary->_Mysize = _Newsize;
_Proxy._Bind(_Alproxy, _Scary);
_Scary->_Swap_proxy_and_iterators(*_Right_scary);
return *this;
}
} else if constexpr (_Pocma_val == _Pocma_values::_No_propagate_allocators) {
if (_Al != _Right_al) {
clear();
_Getcomp() = _Right._Getcomp(); // intentionally copy comparator, see LWG-2227
_Copy<_Strategy::_Move>(_Right);
return *this;
}
}
clear();
_Getcomp() = _Right._Getcomp(); // intentionally copy comparator, see LWG-2227
_Pocma(_Al, _Right_al);
_Swap_val_excluding_comp(_Right);
return *this;
}
private:
void _Swap_val_excluding_comp(_Tree& _Right) { // swap contents (except comparator) with _Right, equal allocators
using _STD swap;
const auto _Scary = _Get_scary();
const auto _Right_scary = _Right._Get_scary();
_Scary->_Swap_proxy_and_iterators(*_Right_scary);
swap(_Scary->_Myhead, _Right_scary->_Myhead); // intentional ADL
_STD swap(_Scary->_Mysize, _Right_scary->_Mysize);
}
protected:
template <class... _Valtys>
pair<_Nodeptr, bool> _Emplace(_Valtys&&... _Vals) {
using _In_place_key_extractor = typename _Traits::template _In_place_key_extractor<_Remove_cvref_t<_Valtys>...>;
const auto _Scary = _Get_scary();
_Tree_find_result<_Nodeptr> _Loc;
_Nodeptr _Inserted;
if constexpr (!_Multi && _In_place_key_extractor::_Extractable) {