-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap.h
More file actions
789 lines (674 loc) · 25.6 KB
/
map.h
File metadata and controls
789 lines (674 loc) · 25.6 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
/**
* implement a container like std::map
*/
#ifndef SJTU_MAP_HPP
#define SJTU_MAP_HPP
// only for std::less<T>
#include <functional>
#include <cstddef>
#include "exception.h"
#include "utility.h"
//实现了rbegin和rend 及其对应的++ --操作
//简单写了一下upper_bound和lower_bound 可能不太对
//1.定义一个 type_traits 模板类
template<typename T>
struct my_type_traits{
using iterator_assignable = typename T::iterator_assignable;
};
//2.定义两个类,表示迭代器是否可被赋值的特性(这一步也可以使用枚举变量来实现)
struct my_true_type{
static const bool isassignable=true;
};
struct my_false_type{
static const bool isassignable=false;
};
//3.分别在可被赋值的迭代器和不可被赋值的迭代器中定义 iterator_assignable 类型
template<
class Key,
class T,
class Compare = std::less<Key>
>
class map {
public:
/**
* the internal type of data.
* it should have a default constructor, a copy constructor.
* You can use std::map as value_type by typedef.
*/
typedef std::pair<const Key, T> value_type;
/**
* see BidirectionalIterator at CppReference for help.
*
* if there is anything wrong throw invalid_iterator.
* like it = map.begin(); --it;
* or it = map.end(); ++end();
*/
private:
struct node {
value_type data;
node *left = nullptr;
node *right = nullptr;
node *parent = nullptr; //利于实现iterator++ --
int height;
node(const value_type &data_, int h_, node *l_, node *r_, node *p_) : data(data_), height(h_), left(l_),
right(r_),
parent(p_) {}
node(int h_, node *l_, node *r_, node *p_) : height(h_), left(l_), right(r_), parent(p_) {}
node(const node &a) : data(a.data), left(a.left), right(a.right), parent(a.parent), height(a.height) {}
};
node *flag = nullptr; //用于insert 记录当前节点t=nullptr的父节点
node *root = nullptr;
size_t num = 0;
public:
class const_iterator;
class iterator {
public:
using iterator_assignable = my_true_type;
public:
node *pos;
const map<Key, T, Compare> *target;
iterator(node *pos_ = nullptr, const map<Key, T, Compare> *tar_ = nullptr,bool state_=true) : pos(pos_), target(tar_),state(state_){}
iterator(const iterator &other) {
pos = other.pos;
target = other.target;
state=other.state;
}
bool state =true;
/**
* ++iter
*/
iterator &operator++() { //关键点在于右子树是否为空
if (state) {
if (pos == nullptr) throw invalid_iterator();
if (pos->right) {
node *lef = pos->right; //非空 找右子树的最左节点
while (lef->left) lef = lef->left;
pos = lef;
} else {
node *par = pos->parent; //为空 找右子树不在当前树枝的父节点
node *cur = pos;
while (cur == par->right) {
cur = cur->parent;
par = par->parent;
if (par == nullptr) break;
}
pos = par;
}
return *this;
}
else return --(*this);
}
/**
* iter++
*/
iterator operator++(int) {
iterator tmp(*this);
++(*this);
return tmp;
}
/**
* --iter
*/
iterator least() {
node *cur = target->root;
while (cur != nullptr && cur->left) cur = cur->left;
return iterator(cur, target);
}
iterator &operator--() {
if(state) {
if (*this == least()) throw invalid_iterator();
if (pos == nullptr) {
pos = target->root;
while (pos->right) pos = pos->right;
} else if (pos->left) {
node *rig = pos->left;
while (rig->right) rig = rig->right;
pos = rig;
} else {
node *par = pos->parent;
node *cur = pos;
while (cur == par->left) {
cur = cur->parent;
par = par->parent;
if (par == nullptr) break;
}
pos = par;
}
return *this;
}
else return ++(*this);
}
/**
* iter--
*/
iterator operator--(int) {
iterator tmp(*this);
--(*this);
return tmp;
}
/**
* a operator to check whether two iterators are same (pointing to the same memory).
*/
value_type &operator*() const {
return pos->data;
}
bool operator==(const iterator &rhs) const {
return (pos == rhs.pos) && (target == rhs.target);
}
bool operator==(const const_iterator &rhs) const {
return (pos == rhs.pos) && (target == rhs.target);
}
/**
* some other operator for iterator.
*/
bool operator!=(const iterator &rhs) const {
return !operator==(rhs);
}
bool operator!=(const const_iterator &rhs) const {
return !operator==(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 {
value_type *tmp = &(pos->data);
return tmp;
}
};
class const_iterator {
public:
using iterator_assignable = my_false_type;
// it should have similar member method as iterator.
// and it should be able to construct from an iterator.
public:
const map<Key, T, Compare> *target;
const node *pos;
const_iterator(const node *pos_ = nullptr, const map<Key, T, Compare> *tar_ = nullptr) : pos(pos_),
target(tar_) {}
const_iterator(const const_iterator &other) {
target = other.target;
pos = other.pos;
}
const_iterator(const iterator &other) {
target = other.target;
pos = other.pos;
}
const_iterator &operator++() {
if (pos == nullptr) throw invalid_iterator();
if (pos->right) {
const node *lef = pos->right;
while (lef->left) lef = lef->left;
pos = lef;
} else {
const node *par = pos->parent;
const node *cur = pos;
while (cur == par->right) {
cur = cur->parent;
par = par->parent;
if (par == nullptr) break;
}
pos = par;
}
return *this;
}
const_iterator operator++(int) {
const_iterator tmp(*this);
++(*this);
return tmp;
}
const_iterator least() {
const node *cur = target->root;
while (cur != nullptr && cur->left != nullptr) cur = cur->left;
return const_iterator(cur, target);
}
const_iterator &operator--() {
if (*this == least()) throw invalid_iterator{};
if (pos == nullptr) {
pos = target->root;
while (pos->right) pos = pos->right;
} else if (pos->left) {
node *rig = pos->left;
while (rig->right) rig = rig->right;
pos = rig;
} else {
const node *par = pos->parent;
const node *cur = pos;
while (cur == par->left) {
cur = cur->parent;
par = par->parent;
if (par == nullptr) break;
}
pos = par;
}
return *this;
}
const_iterator operator--(int) {
const_iterator tmp(*this);
--(*this);
return tmp;
}
const value_type &operator*() const {
return pos->data;
}
bool operator==(const iterator &rhs) const {
return (pos == rhs.pos) && (target == rhs.target);
}
bool operator==(const const_iterator &rhs) const {
return (pos == rhs.pos) && (target == rhs.target);
}
bool operator!=(const iterator &rhs) const {
return !operator==(rhs);
}
bool operator!=(const const_iterator &rhs) const {
return !operator==(rhs);
}
const value_type *operator->() const noexcept {
return &(pos->data);
}
};
map() { root = nullptr; }
map(const map &other) { this->operator=(other); }
node *CopyTree(node *t) {
if (!t) return nullptr;
node *newL = nullptr;
node *newR = nullptr;
if (t->left) newL = CopyTree(t->left);
if (t->right) newR = CopyTree(t->right);
node *newTree = new node(t->data, t->height, newL, newR, nullptr);
if (newL != nullptr) newL->parent = newTree;
if (newR != nullptr) newR->parent = newTree;
return newTree;
}
map &operator=(const map &other) {
if (this == &other) return *this;
clear();
num = other.num;
root = CopyTree(other.root);
return *this;
}
~map() {
clear();
}
/**
* 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'
*/
//区别:用于不同的map
//带const用于const map 返回不可改变的值
//而普通的at可以用于正常的map 返回的值可以被改变
T &at(const Key &key) {
node *t = root;
while (t != nullptr && (Compare()(t->data.first, key) || Compare()(key, t->data.first))) {
if (Compare()(key, t->data.first)) t = t->left;
else t = t->right;
}
if (t == nullptr) throw index_out_of_bound{};
return t->data.second;
// return const_cast<T&>(const_cast<const map *>(this)->at(key));
}
const T &at(const Key &key) const {
node *t = root;
while (t != nullptr && (Compare()(t->data.first, key) || Compare()(key, t->data.first))) {
if (Compare()(key, t->data.first)) t = t->left;
else t = t->right;
}
if (t == nullptr) throw index_out_of_bound{};
return t->data.second;
}
/**
* 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) {
return insert({key, {}}).first->second;
// node *t = root;
// while (t != nullptr && (Compare()(t->data.first, key) || Compare()(key, t->data.first))) {
// if (Compare()(key, t->data.first)) t = t->left;
// else t = t->right;
// }
// if (t == nullptr) {
// //error:value_type a; 没有默认构造函数
// return insert({key, {}}).first->second;
// }
// return t->data.second;
}
/**
* behave like at() throw index_out_of_bound if such key does not exist.
*/
const T &operator[](const Key &key) const { //想调用at函数 但是不知道怎么显示调用const函数
node *t = root;
while (t != nullptr && (Compare()(t->data.first, key) || Compare()(key, t->data.first))) {
if (Compare()(key, t->data.first)) t = t->left;
else t = t->right;
}
if (t == nullptr) throw index_out_of_bound{};
return t->data.second;
}
/**
* return a iterator to the beginning
*/
iterator begin() {
node *l = root;
while (l && l->left) {
l = l->left;
}
return iterator(l, this);
}
const_iterator cbegin() const {
node *l = root;
while (l && l->left) {
l = l->left;
}
return const_iterator(l, this);
}
/**
* return a iterator to the end
* in fact, it returns past-the-end.
*/
iterator end() const {
return iterator(nullptr, this);
}
const_iterator cend() const {
return const_iterator(nullptr, this);
}
iterator rbegin() const {
node *r = root;
while (r && r->right) {
r = r->right;
}
return iterator(r, this,false);
}
iterator rend()const {
return iterator(nullptr,this,false);
}
/**
* checks whether the container is empty
* return true if empty, otherwise false.
*/
bool empty() const {
return num == 0;
}
/**
* returns the number of elements.
*/
size_t size() const {
return num;
}
/**
* clears the contents
*/
void clear1(node *&t) {
if (t == nullptr) return;
clear1(t->left);
clear1(t->right);
delete (t);
t = nullptr;
}
void clear() {
clear1(root);
num = 0;
flag = nullptr;
}
/**
* 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.
*/
int max(int a, int b) {
if (a > b) return a;
else return b;
}
int high(node *t) {
if (t == nullptr) return 0; //很重要!!!
else return t->height;
}
void LL(node *&t) {
node *t1 = t->left;
t->left = t1->right;
if (t1->right) t1->right->parent = t;
t1->parent = t->parent;
t->parent = t1;
t1->right = t;
t->height = max(high(t->left), high(t->right)) + 1;
t1->height = max(high(t1->left), high(t)) + 1;
if (t1->parent) {
if (t1->parent->left == t) t1->parent->left = t1;
else t1->parent->right = t1;
// printf("&left=%llx &t=%llx t=%llx\n", &t1->parent->left, &t, t);
}
t = t1;
}
void RR(node *&t) {
node *t1 = t->right;
t->right = t1->left;
if (t1->left) t1->left->parent = t;
t1->parent = t->parent;
t->parent = t1;
t1->left = t;
t->height = max(high(t->left), high(t->right)) + 1;
t1->height = max(high(t1->right), high(t)) + 1;
if (t1->parent) {
if (t1->parent->left == t) t1->parent->left = t1;
else t1->parent->right = t1;
}
t = t1;
}
void LR(node *&t) {
RR(t->left);
LL(t);
}
void RL(node *&t) {
LL(t->right);
RR(t);
}
std::pair<iterator, bool> insert1(const value_type &value, node *&t) {
if (t == nullptr) {
t = new node(value, 1, nullptr, nullptr, flag);
iterator m(t, this);
return std::pair<iterator, bool>(m, true);
} else if (Compare()(value.first, t->data.first)) {
flag = t;
std::pair<iterator, bool> tmp(insert1(value, t->left));
if (tmp.second && high(t->left) - high(t->right) == 2) {
if (Compare()(value.first, t->left->data.first)) LL(t);
else LR(t);
}
t->height = max(high(t->left), high(t->right)) + 1;
return tmp;
} else if (Compare()(t->data.first, value.first)) {
flag = t;
std::pair<iterator, bool> tmp(insert1(value, t->right));
if (tmp.second && high(t->right) - high(t->left) == 2) {
if (Compare()(t->right->data.first, value.first)) RR(t);
else RL(t);
}
t->height = max(high(t->left), high(t->right)) + 1;
return tmp;
} else { //对应节点已经存在
iterator m(t, this);
return std::pair<iterator, bool>(m, false);
}
}
std::pair<iterator, bool> insert(const value_type &value) {
flag = nullptr;
std::pair<iterator, bool> tmp(insert1(value, root));
if (tmp.second) num++;
return tmp;
}
/**
* erase the element at pos.
*
* throw if pos pointed to a bad element (pos == this->end() || pos points an element out of this)
*/
bool adjust(node *&t, int SubTree) {
if (SubTree) {
if (high(t->left) - high(t->right) == 1) return true;
if (high(t->right) == high(t->left)) {
--t->height;
return false;
}
if (high(t->left->right) > high(t->left->left)) {
LR(t);
return false;
}
LL(t);
if (high(t->right) == high(t->left)) return false;
else return true;
} else {
if (high(t->right) - high(t->left) == 1) return true;
if (high(t->right) == high(t->left)) {
--t->height;
return false;
}
if (high(t->right->left) > high(t->right->right)) {
RL(t);
return false;
}
RR(t);
if (high(t->right) == high(t->left)) return false;
else return true;
}
}
bool remove(const Key &x, node *&t) {
if (t == nullptr) return true;
if (Compare()(x, t->data.first)) {
if (remove(x, t->left)) return true;
return adjust(t, 0);
} else if (Compare()(t->data.first, x)) {
if (remove(x, t->right)) return true;
return adjust(t, 1);
} else {
if (t->left == nullptr || t->right == nullptr) {
node *oldnode = t;
t = (t->left != nullptr) ? t->left : t->right;
if (t != nullptr) t->parent = oldnode->parent;
delete (oldnode);
return false;
} else {
node *tmp = t->right;
while (tmp->left != nullptr) tmp = tmp->left;
int temp = tmp->height;
tmp->height = t->height;
t->height = temp;
//swap two node* 替身交换
if (tmp == t->right) { //特殊情况 单独处理
node *pa = t->parent;
node *son = t->left;
node *son1 = tmp->right;
t->parent = tmp;
t->left = nullptr;
t->right = son1;
tmp->parent = pa;
tmp->left = son;
tmp->right = t;
if (pa != nullptr) {
if (pa->left == t) pa->left = tmp;
else pa->right = tmp;
}
if (son != nullptr) son->parent = tmp;
if (son1 != nullptr) son1->parent = t;
t = tmp;
} else {
node *pa = tmp->parent;
node *son = tmp->right;
node *pa1 = t->parent;
node *son1 = t->left;
node *son2 = t->right;
tmp->parent = pa1;
tmp->left = son1;
tmp->right = son2;
t->left = nullptr;
t->right = son;
t->parent = pa;
if (son != nullptr) {
son->parent = t;
}
pa->left = t;
if (son1 != nullptr) son1->parent = tmp;
son2->parent = tmp;
if (pa1 != nullptr) {
if (t == pa1->left) pa1->left = tmp;
else pa1->right = tmp;
}
t = tmp;
}
//
if (remove(x, t->right)) return true;
return adjust(t, 1);
}
}
}
void erase(iterator pos_) {
node *tmp = pos_.pos;
//注意一些特殊情况:指针为空 找不到 指向的不是同一个vector
if (tmp == nullptr || !count(tmp->data.first) || pos_.target != this) throw invalid_iterator();
num--;
remove(tmp->data.first, root);
}
/**
* Returns the number of elements with key
* that compares equivalent to the specified argument,
* which is either 1 or 0
* since this container does not allow duplicates.
* The default method of check the equivalence is !(a < b || b > a)
*/
bool count1(const Key &key, node *t) const {
if (t == nullptr) return false;
if (Compare()(t->data.first, key)) return count1(key, t->right);
if (Compare()(key, t->data.first)) return count1(key, t->left);
return true;
}
size_t count(const Key &key) const {
if (count1(key, root)) return 1;
else return 0;
}
/**
* Finds an element with key equivalent to key.
* key value of the element to search for.
* Iterator to an element with key equivalent to key.
* If no such element is found, past-the-end (see end()) iterator is returned.
*/
iterator find1(const Key &key, node *t) {
if (t == nullptr) return end();
if (Compare()(key, t->data.first)) return find1(key, t->left);
else if (Compare()(t->data.first, key)) return find1(key, t->right);
else return iterator(t, this);
}
const_iterator find2(const Key &key, node *t) const {
if (t == nullptr) return end();
if (Compare()(key, t->data.first)) return find2(key, t->left);
else if (Compare()(t->data.first, key)) return find2(key, t->right);
else return const_iterator(t, this);
}
iterator find(const Key &key) {
return find1(key, root);
}
const_iterator find(const Key &key) const {
return find2(key, root);
}
iterator upper_bound (const Key& k){
iterator tmp= find(k);
++tmp;
return tmp;
}
const_iterator upper_bound (const Key& k) const{
const_iterator tmp=find(k);
++tmp;
return tmp;
}
iterator lower_bound (const Key& k){
iterator tmp=find(k);
++tmp;
return tmp;
}
const_iterator lower_bound (const Key& k) const{
const_iterator tmp= find(k);
++tmp;
return tmp;
}
};
#endif