-
Notifications
You must be signed in to change notification settings - Fork 0
/
embedded_multimap.h
1034 lines (936 loc) · 31.3 KB
/
embedded_multimap.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
/* THOR - THOR Template Library
* Joshua M. Kriegshauser
*
* embedded_multimap.h
*
* This file defines a multimap (red-black tree) variant that uses a link node embedded in the
* stored class (Value)
*/
#ifndef THOR_TREE_H
#define THOR_TREE_H
#pragma once
#ifndef THOR_PAIR_H
#include "pair.h"
#endif
#ifndef THOR_ITERATOR_H
#include "iterator.h"
#endif
#ifndef THOR_ALGORITHM_H
#include "algorithm.h"
#endif
#ifndef THOR_MEMORY_H
#include "memory.h"
#endif
namespace thor
{
//
// Prototypes
//
template<class Key, class Value> class embedded_multimap_link;
template<class Key, class Value, embedded_multimap_link<Key, Value> Value::*LINK, class Compare> class embedded_multimap;
//
// embedded_multimap_link
//
template<class Key, class Value> class embedded_multimap_link
{
THOR_DECLARE_NOCOPY(embedded_multimap_link);
enum { alignment = memory::align_selector<Key>::alignment };
public:
typedef Key key_type;
typedef Value value_type;
typedef Value* pointer;
embedded_multimap_link()
: color_(uninit)
#ifdef THOR_DEBUG
, keydebug_(key())
#endif
{
THOR_COMPILETIME_ASSERT(THOR_OFFSET_OF(embedded_multimap_link, parent_) == 0, InvalidAssumption);
clear(false);
}
~embedded_multimap_link() { verify_free(); }
bool is_contained() const { return color_ != uninit; }
// Key retrieval
const key_type& key() const { return *(key_type*)memory::align_forward<alignment>(keybuf_); }
#if 0
protected:
typedef embedded_multimap_link<key_type, value_type> selftype;
template <class key_type, class value_type, selftype value_type::*LINK, class Compare> friend class embedded_multimap;
#endif
enum node_color
{
uninit = -1,
red = 0,
black = 1
};
value_type* parent_;
value_type* left_;
value_type* right_;
node_color color_;
void verify_free() const
{
verify_owner(0);
}
void clear(bool destroyKey = true)
{
if (color_ != uninit && destroyKey)
{
const_cast<key_type&>(key()).~key_type();
}
parent_ = left_ = right_ = 0;
color_ = uninit;
set_owner(0);
THOR_DEBUG_INIT_MEM(keybuf_, sizeof(keybuf_), 0xcc);
}
// The key is implemented as a buffer so that it is not constructed until necessary
byte keybuf_[sizeof(key_type) + alignment];
#ifdef THOR_DEBUG
const key_type& keydebug_;
void* owner_;
void set_owner(void* o) { owner_ = o; }
void verify_owner(void* o) const
{
THOR_DEBUG_ASSERT(owner_ == o);
THOR_DEBUG_ASSERT(is_contained() == (owner_ != 0));
}
#else
void set_owner(void*) {}
void verify_owner(void*) const {}
#endif
};
template <class Key, class Value, embedded_multimap_link<Key, Value> Value::*LINK, class Compare = less<Key> >
class embedded_multimap
{
THOR_DECLARE_NOCOPY(embedded_multimap);
public:
typedef Key key_type;
typedef Value value_type;
typedef Compare key_compare;
typedef value_type* pointer;
typedef const value_type* const_pointer;
typedef value_type& reference;
typedef const value_type& const_reference;
typedef thor_size_type size_type;
typedef thor_diff_type difference_type;
typedef embedded_multimap_link<key_type, value_type> link_type;
// iterator definitions
struct iterator_base : public iterator_type<bidirectional_iterator_tag, value_type>
{
pointer node_;
#ifdef THOR_DEBUG
const embedded_multimap* owner_;
iterator_base(value_type* n, const embedded_multimap* o) : node_(n), owner_(o) {}
#else
iterator_base(value_type* n, const embedded_multimap*) : node_(n) {}
#endif
void verify_not_end() const { THOR_DEBUG_ASSERT(owner_->end().node_ != node_); }
void decr()
{
link_type& link = node_->*LINK;
if (link.color_ == link_type::red && (link.parent_->*LINK).parent_ == node_)
{
node_ = link.right_;
}
else if (link.left_ != 0)
{
value_type* y = link.left_;
while ((y->*LINK).right_ != 0)
{
y = (y->*LINK).right_;
}
node_ = y;
}
else
{
value_type* y = link.parent_;
while (node_ == (y->*LINK).left_)
{
node_ = y;
y = (y->*LINK).parent_;
}
node_ = y;
}
}
void incr()
{
link_type& link = node_->*LINK;
verify_not_end();
if (link.right_ != 0)
{
node_ = link.right_;
while ((node_->*LINK).left_ != 0)
{
node_ = (node_->*LINK).left_;
}
}
else
{
value_type* y = link.parent_;
while (node_ == (y->*LINK).right_)
{
node_ = y;
y = (y->*LINK).parent_;
}
if ((node_->*LINK).right_ != y)
{
node_ = y;
}
}
}
bool operator == (const iterator_base& i) const { THOR_DEBUG_ASSERT(owner_ == i.owner_); return node_ == i.node_; }
bool operator != (const iterator_base& i) const { THOR_DEBUG_ASSERT(owner_ == i.owner_); return node_ != i.node_; }
};
template<class Traits> class fwd_iterator : public iterator_base
{
public:
typedef typename Traits::pointer pointer;
typedef typename Traits::reference reference;
typedef fwd_iterator<nonconst_traits<value_type> > nonconst_iterator;
typedef fwd_iterator<Traits> selftype;
fwd_iterator(value_type* n = 0, const embedded_multimap* o = 0) : iterator_base(n, o) {}
fwd_iterator(const nonconst_iterator& i) : iterator_base(i) {}
selftype& operator = (const nonconst_iterator& i) { iterator_base::operator = (i); return *this; }
reference operator * () const { verify_not_end(); return *node_; }
pointer operator -> () const { verify_not_end(); return &(operator*()); }
selftype& operator -- () /* --iterator */ { decr(); return *this; }
selftype operator -- (int) /* iterator-- */ { selftype n(*this); decr(); return n; }
selftype& operator ++ () /* ++iterator */ { incr(); return *this; }
selftype operator ++ (int) /* iterator++ */ { selftype n(*this); incr(); return n; }
const key_type& key() const { verify_not_end(); return (node_->*LINK).key(); }
};
template<class Traits> class rev_iterator : public iterator_base
{
public:
typedef typename Traits::pointer pointer;
typedef typename Traits::reference reference;
typedef rev_iterator<nonconst_traits<value_type> > nonconst_iterator;
typedef rev_iterator<Traits> selftype;
rev_iterator(value_type* n = 0, const embedded_multimap* o = 0) : iterator_base(n, o) {}
rev_iterator(const nonconst_iterator& i) : iterator_base(i) {}
selftype& operator = (const nonconst_iterator& i) { iterator_base::operator = (i); return *this; }
reference operator * () const { verify_not_end(); return *node_; }
pointer operator -> () const { verify_not_end(); return &(operator*()); }
selftype& operator -- () /* --iterator */ { incr(); return *this; }
selftype operator -- (int) /* iterator-- */ { selftype n(*this); incr(); return n; }
selftype& operator ++ () /* ++iterator */ { decr(); return *this; }
selftype operator ++ (int) /* iterator++ */ { selftype n(*this); decr(); return n; }
const key_type& key() const { verify_not_end(); return (node_->*LINK).key(); }
};
typedef fwd_iterator<nonconst_traits<value_type> > iterator;
typedef fwd_iterator<const_traits<value_type> > const_iterator;
typedef rev_iterator<nonconst_traits<value_type> > reverse_iterator;
typedef rev_iterator<const_traits<value_type> > const_reverse_iterator;
// constructors
embedded_multimap() :
m_root(terminator())
{}
embedded_multimap(const key_compare& k) :
m_root(terminator(), k)
{}
~embedded_multimap()
{
// Should be empty at destruction time since we don't own the elements
THOR_DEBUG_ASSERT(empty());
remove_all();
}
// Size
bool empty() const { return size() == 0; }
size_type size() const { return m_root.size_; }
size_type max_size() const { return size_type(-1); }
const key_compare& key_comp() const { return m_root; }
// Iteration
iterator begin() { return iterator(m_root.left_, this); }
iterator end() { return iterator(terminator(), this); }
const_iterator begin() const { return const_iterator(m_root.left_, this); }
const_iterator end() const { return const_iterator(terminator(), this); }
const_iterator cbegin() const { return const_iterator(m_root.left_, this); }
const_iterator cend() const { return const_iterator(terminator(), this); }
reverse_iterator rbegin() { return reverse_iterator(m_root.right_, this); }
reverse_iterator rend() { return reverse_iterator(terminator(), this); }
const_reverse_iterator rbegin() const { return const_reverse_iterator(m_root.right_, this); }
const_reverse_iterator rend() const { return const_reverse_iterator(terminator(), this); }
const_reverse_iterator crbegin() const { return const_reverse_iterator(m_root.right_, this); }
const_reverse_iterator crend() const { return const_reverse_iterator(terminator(), this); }
// Searching
iterator lower_bound(const key_type& k)
{
return iterator(lower_bound_internal(k), this);
}
const_iterator lower_bound(const key_type& k) const
{
return const_iterator(lower_bound_internal(k), this);
}
iterator upper_bound(const key_type& k)
{
return iterator(upper_bound_internal(k), this);
}
const_iterator upper_bound(const key_type& k) const
{
return const_iterator(upper_bound_internal(k), this);
}
pair<iterator,iterator> equal_range(const key_type& k)
{
return pair<iterator,iterator>(lower_bound(k),upper_bound(k));
}
pair<const_iterator,const_iterator> equal_range(const key_type& k) const
{
return pair<const_iterator,const_iterator>(lower_bound(k),upper_bound(k));
}
void swap(embedded_multimap& rhs)
{
thor::swap(m_root, rhs.m_root);
// Fixup terminators if in use
if (rhs.m_root.left_ == terminator())
{
THOR_ASSERT(rhs.m_root.right_ == terminator());
rhs.m_root.left_ = rhs.m_root.right_ = rhs.terminator();
}
else
{
THOR_ASSERT(rhs.m_root.parent_ != 0 && link(rhs.m_root.parent_).parent_ == terminator());
link(rhs.m_root.parent_).parent_ = rhs.terminator();
}
if (m_root.left_ == rhs.terminator())
{
THOR_ASSERT(m_root.right_ == rhs.terminator());
m_root.left_ = m_root.right_ = terminator();
}
else
{
THOR_ASSERT(m_root.parent_ != 0 && link(m_root.parent_).parent_ == rhs.terminator());
link(m_root.parent_).parent_ = terminator();
}
internal_set_owner(m_root.parent_);
rhs.internal_set_owner(rhs.m_root.parent_);
}
iterator insert(const key_type& k, pointer p)
{
THOR_DEBUG_ASSERT(p != 0);
THOR_DEBUG_ASSERT(!link(p).is_contained());
internal_insert(k, p);
return iterator(p, this);
}
pointer remove(iterator pos)
{
verify_iterator(pos);
pos.verify_not_end();
return internal_remove(pos.node_);
}
pointer remove(pointer pos)
{
THOR_DEBUG_ASSERT(pos != 0);
THOR_ASSERT(pos != terminator());
THOR_ASSERT(link(pos).is_contained());
link(pos).verify_owner(this);
return internal_remove(pos);
}
template<class Pred> void remove(pointer pos, Pred pred)
{
pred(remove(pos));
}
void remove_delete(iterator pos)
{
delete remove(pos);
}
void remove_delete(pointer pos)
{
delete remove(pos);
}
size_type remove(const key_type& x)
{
pair<iterator,iterator> p = equal_range(x);
return remove(p.first, p.second);
}
size_type remove(iterator first, iterator last)
{
// verify that the iterators belong to this container
verify_iterator(first);
verify_iterator(last);
if (first == begin() && last == end())
{
size_type n = size();
remove_all();
return n;
}
else
{
size_type n = 0;
while (first != last)
{
remove(first++); // must postincrement
++n;
}
return n;
}
}
size_type remove(const key_type* first, const key_type* last)
{
size_type n = 0;
while (first != last)
{
n += remove(*first++);
}
return n;
}
size_type remove_delete(const key_type& x)
{
pair<iterator,iterator> p = equal_range(x);
return remove_delete(p.first, p.second);
}
size_type remove_delete(iterator first, iterator last)
{
// verify that the iterators belong to this container
verify_iterator(first);
verify_iterator(last);
if (first == begin() && last == end())
{
size_type n = size();
delete_all();
return n;
}
else
{
size_type n = 0;
while (first != last)
{
remove_delete(first++); // must postincrement
++n;
}
return n;
}
}
size_type remove_delete(const key_type* first, const key_type* last)
{
size_type n = 0;
while (first != last)
{
n += remove_delete(*first++);
}
return n;
}
void remove_all()
{
internal_remove_all(m_root.parent_);
m_root.left_ = m_root.right_ = terminator();
m_root.parent_ = 0;
m_root.size_ = 0;
}
void delete_all()
{
internal_delete_all(m_root.parent_);
m_root.left_ = m_root.right_ = terminator();
m_root.parent_ = 0;
m_root.size_ = 0;
}
size_type count(const key_type& k) const
{
pair<const_iterator,const_iterator> p(equal_range(k));
return (size_type)distance(p.first, p.second);
}
template<class K> iterator find(const K& k) { return iterator(internal_find(k), this); }
template<class K> const_iterator find(const K& k) const { return const_iterator(internal_find(k), this); }
private:
// Use empty member optimization since key_compare is likely going to be an
// empty class.
struct root_node : public key_compare
{
// The first three members of this structure must match embedded_multimap_link
pointer parent_;
pointer left_;
pointer right_;
size_type size_;
root_node(pointer term) :
key_compare(),
parent_(0),
left_(term),
right_(term),
size_(0)
{}
root_node(pointer term, const key_compare& k) :
key_compare(k),
parent_(0)
left_(term),
right_(term),
size_(0)
{}
root_node& operator = (const key_compare& kc)
{
key_compare::operator = (kc);
return *this;
}
};
root_node m_root;
pointer terminator() const { return (pointer)((byte*)&m_root.parent_ - THOR_OFFSET_OF(value_type, *LINK)); }
void verify_iterator(const iterator_base& i) const { THOR_UNUSED(i); THOR_ASSERT(i.owner_ == this); }
key_compare& key_comp()
{
return static_cast<key_compare&>(m_root);
}
static link_type& link(pointer p)
{
return p->*LINK;
}
static const link_type& link(const_pointer p)
{
return p->*LINK;
}
// utility functions
static pointer minimum(pointer x)
{
while (link(x).left_ != 0)
{
x = link(x).left_;
}
return x;
}
static pointer maximum(pointer x)
{
while (link(x).right_ != 0)
{
x = link(x).right_;
}
return x;
}
// Does not construct the value_type
void internal_insert(const key_type& key, pointer which)
{
// Construct a copy of the key
link_type& l = link(which);
l.verify_free();
l.color_ = link_type::red;
l.left_ = l.right_ = 0;
new (&const_cast<key_type&>(l.key())) key_type(key);
pointer y = terminator();
pointer x = m_root.parent_;
while (x != 0)
{
y = x;
x = key_comp()(key, link(x).key()) ? link(x).left_ : link(x).right_;
}
if (y == terminator() ||
x != 0 || // If x != 0, the remainder succeeds to true
key_comp()(key, link(y).key()))
{
link(y).left_ = which;
if (y == terminator())
{
m_root.parent_ = which;
m_root.right_ = which;
}
else if (y == m_root.left_)
{
m_root.left_ = which; // maintain leftmost pointing to min node
}
}
else
{
link(y).right_ = which;
if (y == m_root.right_)
{
m_root.right_ = which; // maintain rightmost pointing to max node
}
}
l.parent_ = y;
l.set_owner(this);
rebalance(which, m_root.parent_);
++m_root.size_;
}
void rebalance(pointer x, pointer& root)
{
link(x).color_ = link_type::red;
while (x != root && link(link(x).parent_).color_ == link_type::red)
{
if (link(x).parent_ == link(link(link(x).parent_).parent_).left_)
{
pointer y = link(link(link(x).parent_).parent_).right_;
if (y && link(y).color_ == link_type::red)
{
link(link(x).parent_).color_ = link_type::black;
link(y).color_ = link_type::black;
link(link(link(x).parent_).parent_).color_ = link_type::red;
x = link(link(x).parent_).parent_;
}
else
{
if (x == link(link(x).parent_).right_)
{
x = link(x).parent_;
rotate_left(x, root);
}
link(link(x).parent_).color_ = link_type::black;
link(link(link(x).parent_).parent_).color_ = link_type::red;
rotate_right(link(link(x).parent_).parent_, root);
}
}
else
{
pointer y = link(link(link(x).parent_).parent_).left_;
if (y && link(y).color_ == link_type::red)
{
link(link(x).parent_).color_ = link_type::black;
link(y).color_ = link_type::black;
link(link(link(x).parent_).parent_).color_ = link_type::red;
x = link(link(x).parent_).parent_;
}
else
{
if (x == link(link(x).parent_).left_)
{
x = link(x).parent_;
rotate_right(x, root);
}
link(link(x).parent_).color_ = link_type::black;
link(link(link(x).parent_).parent_).color_ = link_type::red;
rotate_left(link(link(x).parent_).parent_, root);
}
}
}
link(root).color_ = link_type::black;
}
pointer internal_remove(pointer pos)
{
THOR_DEBUG_ASSERT(pos != terminator());
pos = rebalance_for_remove(pos, m_root.parent_, m_root.left_, m_root.right_);
THOR_DEBUG_ASSERT(m_root.size_ != 0);
--m_root.size_;
link(pos).clear(true);
return pos;
}
pointer rebalance_for_remove(pointer z,
pointer& root,
pointer& leftmost,
pointer& rightmost)
{
pointer y = z;
pointer x = 0;
pointer x_parent = 0;
if (link(y).left_ == 0) // z has at most one non-null child. y == z.
{
x = link(y).right_; // x might be null.
}
else if (link(y).right_ == 0) // z has exactly one non-null child. y == z.
{
x = link(y).left_; // x is not null.
}
else // z has two non-null children. Set y to
{
y = link(y).right_; // z's successor. x might be null.
while (link(y).left_ != 0)
{
y = link(y).left_;
}
x = link(y).right_;
}
if (y != z) // relink y in place of z. y is z's successor
{
link(link(z).left_).parent_ = y;
link(y).left_ = link(z).left_;
if (y != link(z).right_)
{
x_parent = link(y).parent_;
if (x != 0)
{
link(x).parent_ = link(y).parent_;
}
link(link(y).parent_).left_ = x; // y must be a child of left
link(y).right_ = link(z).right_;
link(link(z).right_).parent_ = y;
}
else
{
x_parent = y;
}
if (root == z)
{
root = y;
}
else if (link(link(z).parent_).left_ == z)
{
link(link(z).parent_).left_ = y;
}
else
{
link(link(z).parent_).right_ = y;
}
link(y).parent_ = link(z).parent_;
thor::swap(link(y).color_, link(z).color_);
y = z;
// y now points to node to be actually deleted
}
else // y == z
{
x_parent = link(y).parent_;
if (x)
{
link(x).parent_ = link(y).parent_;
}
if (root == z)
{
root = x;
}
else if (link(link(z).parent_).left_ == z)
{
link(link(z).parent_).left_ = x;
}
else
{
link(link(z).parent_).right_ = x;
}
if (leftmost == z)
{
if (link(z).right_ == 0) // z->left must be null also
{
leftmost = link(z).parent_;
}
// makes leftmost == header if z == root
else
{
leftmost = minimum(x);
}
}
if (rightmost == z)
{
if (link(z).left_ == 0) // z->right must be null also
{
rightmost = link(z).parent_;
}
// makes rightmost == header if z == root
else // x == z->left
{
rightmost = maximum(x);
}
}
}
if (link(y).color_ != link_type::red)
{
while (x != root && (x == 0 || link(x).color_ == link_type::black))
{
if (x == link(x_parent).left_)
{
pointer w = link(x_parent).right_;
if (link(w).color_ == link_type::red)
{
link(w).color_ = link_type::black;
link(x_parent).color_ = link_type::red;
rotate_left(x_parent, root);
w = link(x_parent).right_;
}
if ((link(w).left_ == 0 ||
link(link(w).left_).color_ == link_type::black) && (link(w).right_ == 0 ||
link(link(w).right_).color_ == link_type::black))
{
link(w).color_ = link_type::red;
x = x_parent;
x_parent = link(x_parent).parent_;
}
else
{
if (link(w).right_ == 0 ||
link(link(w).right_).color_ == link_type::black)
{
if (link(w).left_)
{
link(link(w).left_).color_ = link_type::black;
}
link(w).color_ = link_type::red;
rotate_right(w, root);
w = link(x_parent).right_;
}
link(w).color_ = link(x_parent).color_;
link(x_parent).color_ = link_type::black;
if (link(w).right_)
{
link(link(w).right_).color_ = link_type::black;
}
rotate_left(x_parent, root);
break;
}
}
else // same as above, with right <-> left.
{
pointer w = link(x_parent).left_;
if (link(w).color_ == link_type::red)
{
link(w).color_ = link_type::black;
link(x_parent).color_ = link_type::red;
rotate_right(x_parent, root);
w = link(x_parent).left_;
}
if ((link(w).right_ == 0 ||
link(link(w).right_).color_ == link_type::black) && (link(w).left_ == 0 ||
link(link(w).left_).color_ == link_type::black))
{
link(w).color_ = link_type::red;
x = x_parent;
x_parent = link(x_parent).parent_;
}
else
{
if (link(w).left_ == 0 ||
link(link(w).left_).color_ == link_type::black)
{
if (link(w).right_)
{
link(link(w).right_).color_ = link_type::black;
}
link(w).color_ = link_type::red;
rotate_left(w, root);
w = link(x_parent).left_;
}
link(w).color_ = link(x_parent).color_;
link(x_parent).color_ = link_type::black;
if (link(w).left_)
{
link(link(w).left_).color_ = link_type::black;
}
rotate_right(x_parent, root);
break;
}
}
}
if (x)
{
link(x).color_ = link_type::black;
}
}
return y;
}
void rotate_left(pointer x, pointer& root)
{
pointer y = link(x).right_;
link(x).right_ = link(y).left_;
if (link(y).left_ != 0)
{
link(link(y).left_).parent_ = x;
}
link(y).parent_ = link(x).parent_;
if (x == root)
{
root = y;
}
else if (x == link(link(x).parent_).left_)
{
link(link(x).parent_).left_ = y;
}
else
{
link(link(x).parent_).right_ = y;
}
link(y).left_ = x;
link(x).parent_ = y;
}
void rotate_right(pointer x, pointer& root)
{
pointer y = link(x).left_;
link(x).left_ = link(y).right_;
if (link(y).right_ != 0)
{
link(link(y).right_).parent_ = x;
}
link(y).parent_ = link(x).parent_;
if (x == root)
{
root = y;
}
else if (x == link(link(x).parent_).right_)
{
link(link(x).parent_).right_ = y;
}
else
{
link(link(x).parent_).left_ = y;
}
link(y).right_ = x;
link(x).parent_ = y;
}
template <class K> pointer internal_find(const K& k) const
{
pointer y = terminator(); // Last node which is not less than k.
pointer x = m_root.parent_; // Current node.
while (x != 0)
{
if (!key_comp()(link(x).key(), k))
{
y = x, x = link(x).left_;
}
else
{
x = link(x).right_;
}
}
if (y == terminator() || key_comp()(k, link(y).key()))
{
y = terminator();
}
return y;
}
pointer lower_bound_internal(const key_type& k) const
{
pointer y = terminator(); // Last node which is not less than k.
pointer x = m_root.parent_; // Current node.
while (x != 0)
{
if (!key_comp()(link(x).key(), k))
{
y = x, x = link(x).left_;
}
else
{
x = link(x).right_;
}
}
return y;
}
pointer upper_bound_internal(const key_type& k) const
{
pointer y = terminator(); // Last node which is greater than k.
pointer x = m_root.parent_; // Current node.
while (x != 0)
{
if (key_comp()(k, link(x).key()))
{
y = x, x = link(x).left_;
}
else
{
x = link(x).right_;
}
}
return y;
}
void internal_remove_all(pointer p)
{
while (p)
{
internal_remove_all(link(p).right_);
pointer next = link(p).left_;
link(p).clear(true);