-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap.h
More file actions
1432 lines (1352 loc) · 53.3 KB
/
map.h
File metadata and controls
1432 lines (1352 loc) · 53.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Created by 陆鹏睿 on 2023/5/29.
//
#ifndef STRAIN_MAP_H
#define STRAIN_MAP_H
/**
* implement a container like std::map
*/
#ifndef SJTU_MAP_HPP
#define SJTU_MAP_HPP
// only for std::less<T>
#include <functional>
#include <cstddef>
#include "utility.h"
#include "exceptions.h"
//#include "type_traits.h"
namespace sjtu {
// 1. 定义一个 type_traits 模板类
template<typename T>
struct my_type_traits{
using iterator_assignable = typename T::iterator_assignable;
};
// 2. 定义两个类,表示迭代器是否可被赋值的特性
struct my_true_type{
using iterator_assignable = std::true_type;
};
struct my_false_type{
using iterator_assignable = std::false_type;
};
template<
class Key,
class T,
class Compare = std::less<Key>
> class map {
int sizeofmap=0;
enum colour{black,red};
public:
/**
* the internal type of data.
* it should have a default constructor, a copy constructor.
* You can use sjtu::map as value_type by typedef.
*/
typedef pair<const Key, T> value_type;
private:struct rbtreenode{
value_type value;
rbtreenode *left,*right,*father;
colour col;
rbtreenode():left(nullptr),right(nullptr),father(nullptr){}
rbtreenode(const rbtreenode&other):value(other.value),left(nullptr),right(nullptr),father(nullptr){
}
rbtreenode(value_type p):value(p),left(nullptr),right(nullptr),father(nullptr){
col=red;
}
~rbtreenode(){
// value.first.~Key();
// value.second.~T();
}
};
public:rbtreenode *head= nullptr;
rbtreenode *root= (rbtreenode*)malloc(sizeof(rbtreenode));
private:
rbtreenode* nodecopy(rbtreenode *p){
if(p==NULL)return NULL;
else{
rbtreenode* left = nodecopy(p->left); //递归拷贝当前结点的左孩子结点
rbtreenode* right= nodecopy(p->right); //递归拷贝当前结点的右孩子结点
rbtreenode* q = new rbtreenode(p->value);
q->col=p->col;
q->left = left;
q->right = right;
if(NULL != left)
left->father = q; //若当前结点不是叶子节点,则将左孩子的父节点指针指向当前结点
if(NULL != right)
right->father = q; //若当前结点不是叶子节点,则将右孩子的父节点指针指向当前结点
return q;
}
}
void Rightspan(rbtreenode *parent) {
if(parent==head){
parent->father=parent->left;
head=parent->left;
parent->left->father= nullptr;
parent->left= nullptr;
rbtreenode *r=head->right;
head->right=parent;
if(r) { parent->left = r; r->father=parent;}
}
else{
if(parent->father->left==parent){
parent->father->left=parent->left;
parent->left->father=parent->father;
}
else{
parent->father->right=parent->left;
parent->left->father=parent->father;
}
rbtreenode *tmp=parent->left;
parent->left= nullptr;
parent->father=tmp;
if(tmp->right){
parent->left=tmp->right;
tmp->right->father=parent;
}
tmp->right=parent;
}
}
void Leftspan(rbtreenode *parent) {
if(parent==head){
parent->father=parent->right;
head=parent->right;
parent->right->father= nullptr;
parent->right= nullptr;
rbtreenode *l=head->left;
head->left=parent;
if(l) { parent->right =l; l->father=parent;}
}
else{
if(parent->father->left==parent){
parent->father->left=parent->right;
parent->right->father=parent->father;
}
else{
parent->father->right=parent->right;
parent->right->father=parent->father;
}
rbtreenode *tmp=parent->right;
parent->right= nullptr;
parent->father=tmp;
if(tmp->left){
parent->right=tmp->left;
tmp->left->father=parent;
}
tmp->left=parent;
}
}
void LR(rbtreenode *parent){
Leftspan(parent->left);
Rightspan(parent);
}
void RL(rbtreenode *parent){
Rightspan(parent->right);
Leftspan(parent);
}
void deleteall(rbtreenode *node){
if(node== nullptr)return;
if(node->right)deleteall(node->right);
if(node->left)deleteall(node->left);
delete node;
}
public:
/**
* see BidirectionalIterator at CppReference for help.
*
* if there is anything wrong throw invalid_iterator.
* like it = map.begin(); --it;
* or it = map.end(); ++end();
*/
class const_iterator;
class iterator {
public: using iterator_assignable = my_true_type::iterator_assignable;
private:
// int iffind=0;
// rbtreenode* midOrder(const Key &key, rbtreenode *node){
//
// rbtreenode *finding;
//
// if(!iffind&&node->left)finding=midOrder(key,node->left);
// Compare cmp;
// if((!iffind)&&cmp(key,node->value.first)){
// iffind++;
// finding=node;
// }
// if(!iffind&&node->right)finding=midOrder(key,node->right);
// return finding;
// }
// rbtreenode* smallmidOrder(const Key &key, rbtreenode *node){
//
// rbtreenode *finding;
// if(!iffind&&node->right)finding=smallmidOrder(key,node->right);
//
// Compare cmp;
// if((!iffind)&&cmp(node->value.first,key)){
// iffind++;
// finding=node;
// }
// if(!iffind&&node->left)finding=smallmidOrder(key,node->left);
// return finding;
// }
/**
* TODO add data members
* just add whatever you want.
*/
public:
rbtreenode *iter;
rbtreenode *iterhead;
rbtreenode *iterroot;
bool isend= false;
iterator() {
iter= nullptr;
iterhead= nullptr;
iterroot= nullptr;
// TODO
}
iterator(const iterator &other) {
// TODO
iter=other.iter;
iterhead=other.iterhead;
iterroot=other.iterroot;
isend=other.isend;
}
/**
* TODO iter++
*/
iterator operator++(int) {
//iter++
iterator tmp(*this);
if(isend)throw index_out_of_bound();
if(iter->right){
iter=iter->right;
while(iter->left)iter=iter->left;
}
else{
iterator newiter(*this);
while(newiter.iter->father&&newiter.iter->father->right==newiter.iter)newiter.iter=newiter.iter->father;
if(!newiter.iter->father)isend=1;
else{
iter=newiter.iter->father;
}
}
return tmp;
}
/**
* TODO ++iter
*/
iterator & operator++() {
if(isend)throw index_out_of_bound();
if(iter->right){
iter=iter->right;
while(iter->left)iter=iter->left;
}
else{
iterator newiter(*this);
while(newiter.iter->father&&newiter.iter->father->right==newiter.iter)newiter.iter=newiter.iter->father;
if(!newiter.iter->father)isend=1;
else{
iter=newiter.iter->father;
}
}
return *this;
}
/**
* TODO iter--
*/
iterator operator--(int) {
iterator newiterr(*this);
if(isend) {if(iterhead== nullptr)throw index_out_of_bound();
isend = 0;
iter=iterhead;
while(iter->right)iter=iter->right;
return newiterr;
}
if(iter->left){
iter=iter->left;
while(iter->right)iter=iter->right;
isend=0;
}
else{
while(iter->father&&iter->father->left==iter)iter=iter->father;
if(!iter->father)throw index_out_of_bound();
else{
isend=0;
iter=iter->father;
}
};
return newiterr;
}
/**
* TODO --iter
*/
iterator & operator--() {
if(isend) {if(iterhead== nullptr)throw index_out_of_bound();
isend = 0;
iter=iterhead;
while(iter->right)iter=iter->right;
return *this;
}
if(iter->left){
iter=iter->left;
while(iter->right)iter=iter->right;
isend=0;
}
else{
while(iter->father&&iter->father->left==iter)iter=iter->father;
if(!iter->father)throw index_out_of_bound();
else{
isend=0;
iter=iter->father;
}
};
return *this;
}
/**
* a operator to check whether two iterators are same (pointing to the same memory).
*/
value_type & operator*() const {
return iter->value;
}
bool operator==(const iterator &rhs) const {
if(iterroot!=rhs.iterroot)return false;
if(isend!=rhs.isend)return false;
if(isend==1&&rhs.isend==1)return true;
if(iter==rhs.iter)return true;
return false;
}
bool operator==(const const_iterator &rhs) const {
if(iterroot!=rhs.iterroot)return false;
if(isend!=rhs.isend)return false;
if(isend==1&&rhs.isend==1)return true;
if(iter==rhs.iter)return true;
return false;
}
/**
* some other operator for iterator.
*/
bool operator!=(const iterator &rhs) const {
return !(*this==rhs);
}
bool operator!=(const const_iterator &rhs) const {
return !(*this==rhs);
}
/**
* for the support of it->first.
* See <http://kelvinh.github.io/blog/2013/11/20/overloading-of-member-access-operator-dash-greater-than-symbol-in-cpp/> for help.
*/
value_type* operator->() const noexcept {
return &(iter->value);
}
};
class const_iterator {
public:using iterator_assignable = my_false_type::iterator_assignable;
// it should has similar member method as iterator.
// and it should be able to construct from an iterator.
private:
int iffind=0;
// rbtreenode* midOrder(const Key &key, rbtreenode *node){
//
// rbtreenode *finding;
//
// if(!iffind&&node->left)finding=midOrder(key,node->left);
// Compare cmp;
// if((!iffind)&&cmp(key,node->value.first)){
// iffind++;
// finding=node;
// }
// if(!iffind&&node->right)finding=midOrder(key,node->right);
// return finding;
// }
// rbtreenode* smallmidOrder(const Key &key, rbtreenode *node){
//
// rbtreenode *finding;
// if(!iffind&&node->right)finding=smallmidOrder(key,node->right);
//
// Compare cmp;
// if((!iffind)&&cmp(node->value.first,key)){
// iffind++;
// finding=node;
// }
// if(!iffind&&node->left)finding=smallmidOrder(key,node->left);
// return finding;
// }
// data members.
public:
rbtreenode *iter;
rbtreenode *iterhead;
rbtreenode *iterroot;
bool isend= false;
const_iterator() {
// TODO
iter= nullptr;
iterhead= nullptr;
iterroot= nullptr;
}
const_iterator(const const_iterator &other) {
iter=other.iter;
iterhead=other.iterhead;
isend=other.isend;
}
const_iterator(const iterator &other) {
iter=other.iter;
isend=other.isend;
}
// And other methods in iterator.
// And other methods in iterator.
const_iterator operator++(int) {
const_iterator tmp=*this;
if(isend)throw index_out_of_bound();
iffind=0;
if(iter->right){
iter=iter->right;
while(iter->left)iter=iter->left;
}
else{
const_iterator newiter(*this);
while(newiter.iter->father&&newiter.iter->father->right==newiter.iter)newiter.iter=newiter.iter->father;
if(!newiter.iter->father)isend=1;
else{
iter=newiter.iter->father;
}
}
return tmp;
}
/**
* TODO ++iter
*/
const_iterator & operator++() {
if(isend)throw index_out_of_bound();
iffind=0;
if(iter->right){
iter=iter->right;
while(iter->left)iter=iter->left;
}
else{
const_iterator newiter(*this);
while(newiter.iter->father&&newiter.iter->father->right==newiter.iter)newiter.iter=newiter.iter->father;
if(!newiter.iter->father)isend=1;
else{
iter=newiter.iter->father;
}
}
return *this;
}
/**
* TODO iter--
*/
const_iterator operator--(int) {
const_iterator newiter(*this);
if(isend) {if(iterhead== nullptr)throw index_out_of_bound();
isend = 0;
iter=iterhead;
while(iter->right)iter=iter->right;
return newiter;
}
if(iter->left){
iter=iter->left;
while(iter->right)iter=iter->right;
isend=0;
}
else{
while(iter->father&&iter->father->left==iter)iter=iter->father;
if(!iter->father)throw index_out_of_bound();
else{
isend=0;
iter=iter->father;
}
};
return newiter;
}
/**
* TODO --iter
*/
const_iterator& operator--() {
if(isend) {if(iterhead== nullptr)throw index_out_of_bound();
isend = 0;
iter=iterhead;
while(iter->right)iter=iter->right;
return *this;
}
if(iter->left){
iter=iter->left;
while(iter->right)iter=iter->right;
isend=0;
}
else{
while(iter->father&&iter->father->left==iter)iter=iter->father;
if(!iter->father)throw index_out_of_bound();
else{
isend=0;
iter=iter->father;
}
};
return *this;
}
/**
* a operator to check whether two iterators are same (pointing to the same memory).
*/
const value_type & operator*() const {
if(isend)throw runtime_error();
return iter->value;
}
bool operator==(const iterator &rhs) const {
if(iterroot!=rhs.iterroot)return false;
if(isend!=rhs.isend)return false;
if(isend==1&&rhs.isend==1)return true;
if(iter==rhs.iter)return true;
return false;
}
bool operator==(const const_iterator &rhs) const {
if(iterroot!=rhs.iterroot)return false;
if(isend!=rhs.isend)return false;
if(isend==1&&rhs.isend==1)return true;
if(iter==rhs.iter)return true;
return false;
}
/**
* some other operator for iterator.
*/
bool operator!=(const iterator &rhs) const {
return !(*this==rhs);
}
bool operator!=(const const_iterator &rhs) const {
return !(*this==rhs);
}
/**
* for the support of it->first.
* See <http://kelvinh.github.io/blog/2013/11/20/overloading-of-member-access-operator-dash-greater-than-symbol-in-cpp/> for help.
*/
const value_type* operator->() const noexcept {
if(isend)throw runtime_error();
return &(iter->value);
}
// And other methods in iterator.
};
/**
* TODO two constructors
*/
map() {
sizeofmap=0;
}
map(const map &other) {
head= nodecopy(other.head);
sizeofmap=other.sizeofmap;
}
/**
* TODO assignment operator
*/
map & operator=(const map &other) {
if(other.head==head)return *this;
clear();
head= nodecopy(other.head);
sizeofmap=other.sizeofmap;
return *this;
}
/**
* TODO Destructors
*/
~map() {
deleteall(head);
free(root);
}
/**
* TODO
* access specified element with bounds checking
* Returns a reference to the mapped value of the element with key equivalent to key.
* If no such element exists, an exception of type `index_out_of_bound'
*/
T & at(const Key &key) {
if(find(key)==end())throw index_out_of_bound();
return (*this)[key];
}
const T & at(const Key &key) const {
if(find(key)==cend())throw index_out_of_bound();
return (*this)[key];
}
/**
* TODO
* access specified element
* Returns a reference to the value that is mapped to a key equivalent to key,
* performing an insertion if such key does not already exist.
*/
T & operator[](const Key &key) {
if(head== nullptr){
head=new rbtreenode (pair<Key,T>(key,{}));
head->col=black;
sizeofmap++;
return head->value.second;
}
auto t= insert(pair<Key,T>(key,{})).first;
return t.iter->value.second;
// rbtreenode *findp=head;
// iterator iter;
// Compare cmp;
// if(head== nullptr){
// head=new rbtreenode (pair<Key,T>(key,{}));
// head->col=black;
// sizeofmap++;
// return head->value.second;
// }
// while(1){
// if(!cmp(key,findp->value.first)&&!cmp(findp->value.first,key)){
// iter.iter=findp;
// return findp->value.second;
// }
//
// if(cmp(key,findp->value.first)){
// if(findp->left){
// findp=findp->left;
// }
// else{
// iter=insert(pair<Key,T>(key,{})).first;
// return iter.iter->value.second;
// }
// }
// else{
// if(findp->right){
// findp=findp->right;
// }
// else{
// iter=insert(pair<Key,T>(key,{})).first;
// return iter.iter->value.second;
// }
// }
// }
}
/**
* behave like at() throw index_out_of_bound if such key does not exist.
*/
const T & operator[](const Key &key) const {
rbtreenode *findp=head;
iterator iter;
Compare cmp;
if(head== nullptr){
throw index_out_of_bound();
}
while(1){
if(!cmp(key,findp->value.first)&&!cmp(findp->value.first,key)){
iter.iter=findp;
return findp->value.second;
}
if(cmp(key,findp->value.first)){
if(findp->left){
findp=findp->left;
}
else{
throw index_out_of_bound();
return iter.iter->value.second;
}
}
else{
if(findp->right){
findp=findp->right;
}
else{throw index_out_of_bound();
return iter.iter->value.second;
}
}
}
}
/**
* return a iterator to the beginning
*/
iterator begin() {
iterator tmp;tmp.iterroot=root;
rbtreenode *searchless=head;
if(head== nullptr){
tmp.isend=1;
return tmp;
}
while(searchless->left){
searchless=searchless->left;
}
tmp.iter=searchless;
tmp.iterhead=head;
return tmp;
}
const_iterator cbegin() const {
const_iterator tmp;tmp.iterroot=root;
rbtreenode *searchless=head;
if(head== nullptr){
tmp.isend=1;
return tmp;
}
while(searchless->left){
searchless=searchless->left;
}
tmp.iter=searchless;
tmp.iterhead=head;
return tmp;
}
/**
* return a iterator to the end
* in fact, it returns past-the-end.
*/
iterator end() {
iterator iter;iter.iterroot=root;iter.iterhead=head;
iter.isend=1;
return iter;
}
const_iterator cend() const {
const_iterator iter;iter.iterroot=root;
iter.iterhead=head;
iter.isend=1;
return iter;
}
/**
* checks whether the container is empty
* return true if empty, otherwise false.
*/
bool empty() const {
if(head)return false;
return true;
}
/**
* returns the number of elements.
*/
size_t size() const {return sizeofmap;}
/**
* clears the contents
*/
void clear() {
deleteall(head);
head= nullptr;
sizeofmap=0;
}
/**
* insert an element.
* return a pair, the first of the pair is
* the iterator to the new element (or the element that prevented the insertion),
* the second one is true if insert successfully, or false.
*/
pair<iterator, bool> insert(const value_type &value) {
if(head== nullptr){
sizeofmap++;
head=new rbtreenode (value);
iterator itertmp;
itertmp.iter=head;
itertmp.iterroot=head;
head->col=black;
return pair<iterator,bool>(itertmp,true);
}
// if(head== nullptr){
// head=new rbtreenode (value);
// head->col=black;
// sizeofmap++;
// iterator itertmp;
// itertmp.iter=head;
// itertmp.iterhead=head;
// return pair<iterator,bool>(itertmp,true);
// }
iterator itertmp=begin();
rbtreenode *insert=head,*parent= nullptr;
Compare cmp;
while(insert) {
parent=insert;
if (!cmp(insert->value.first, value.first) && !cmp(value.first, insert->value.first)) {
itertmp.iter = insert;
return pair<iterator, bool>(itertmp, false);
} else {
if (cmp(insert->value.first, value.first)) {
insert = insert->right;
} else {
insert = insert->left;
}
}
}
insert=new rbtreenode(value);
rbtreenode *tmp=insert;
if(cmp(value.first,parent->value.first)){
parent->left=tmp;
tmp->father=parent;
}
else{
parent->right=tmp;
tmp->father=parent;
}
rbtreenode *grapa=parent;
rbtreenode *uncle=parent;
while(parent&&parent->col==red){
grapa=parent->father;
if(parent==grapa->left){
uncle=grapa->right;
if(uncle== nullptr){
if(insert==parent->left) {
Rightspan(grapa);
grapa->col = red;
parent->col = black;
insert->col = red;
insert = parent;
break;
}
else{
LR(grapa);
grapa->col = red;
parent->col = red;
insert->col = black;
break;
}
}
else if(uncle->col==red){
parent->col=black;
grapa->col=red;
uncle->col=black;
insert=grapa;
}
else {
if(insert==parent->left) {
Rightspan(grapa);
grapa->col = red;
parent->col = black;
insert = parent;
break;
}
else{
LR(grapa);
grapa->col = red;
insert->col = black;
break;
}
}
}
else{
uncle=grapa->left;
if(uncle== nullptr){
if(insert==parent->right) {
Leftspan(grapa);
grapa->col = red;
parent->col = black;
insert->col = red;
break;
}
else{
RL(grapa);
grapa->col = red;
parent->col =red ;
insert->col =black;
break;
}
}
else if(uncle->col==red){
parent->col=black;
grapa->col=red;
uncle->col=black;
insert=grapa;
}
else {
if(insert==parent->right) {
Leftspan(grapa);
grapa->col = red;
parent->col = black;
insert = parent;
break;
}
else{
RL(grapa);
grapa->col = red;insert->col = black;break;
}
}
}
parent=insert->father;
}
head->col=black;
sizeofmap++;
itertmp.iter=tmp;
return pair<iterator,bool>(itertmp,true);
}
/**
* erase the element at pos.
*
* throw if pos pointed to a bad element (pos == this->end() || pos points an element out of this)
*/
void erase(iterator pos) {
if(pos.iterroot!=root){
throw index_out_of_bound();
}
if(pos.isend) {
throw index_out_of_bound();
}
pos.iterhead=head;
sizeofmap--;
rbtreenode* delPos = nullptr; rbtreenode* delfather = nullptr;
rbtreenode * cur=pos.iter;
rbtreenode *parent= cur->father;
if(cur==head){
if(head->left== nullptr&&head->right== nullptr) { delete head;head= nullptr;
return;}
else{
if(head->left== nullptr){
head=head->right;
delete head->father;
head->father= nullptr;
head->col=black;
return;
}
else if(head->right==nullptr){
head=head->left;
delete head->father;
head->father= nullptr;
head->col=black;
return;
}
}
}
if (cur->left == nullptr)
{
delfather = parent;
delPos = cur;
}
else if (cur->right == nullptr)
{
delfather =parent;
delPos = cur;
}
else
{
//两节点不为空
rbtreenode* minParent = cur;
rbtreenode* minRight = cur->right;
while (minRight->left)
{
minRight = minRight->left;
}
minParent = minRight->father;
rbtreenode* d=minRight;
if(cur==head)head=minRight;
d=new rbtreenode(cur->value);
d->father= nullptr;
d->left= nullptr;
d->right= nullptr;
d->col=minRight->col;
if(minRight->father) {
d->father = minRight->father;
if (d->father->left == minRight)d->father->left = d;
else d->father->right = d;
}
if(minRight->left){
d->left=minRight->left;
minRight->left->father=d;
}if(minRight->right){
d->right=minRight->right;
minRight->right->father=d;
}
minRight->father= nullptr;
minRight->left= nullptr;
minRight->right= nullptr;
minRight->col=cur->col;
if(cur->father){
minRight->father=cur->father;
if(cur->father->left==cur)cur->father->left=minRight;
else{
cur->father->right=minRight;
}
}
if(cur->right){
minRight->right=cur->right;
minRight->right->father=minRight;
}
if(cur->left){
minRight->left=cur->left;
minRight->left->father=minRight;
}