-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap.hpp
More file actions
489 lines (407 loc) · 14.1 KB
/
map.hpp
File metadata and controls
489 lines (407 loc) · 14.1 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
#pragma once
#include "pair.h"
#include "Node.h"
#include "mapIterator.h"
#include "reverse_iterator.h"
#include "utils.h"
#include <functional>
#include <memory>
#include <cstddef>
#include <iostream>
#include <iomanip>
namespace ft {
template<
class Key,
class T,
class Compare = std::less<Key>,
class Allocator = std::allocator<ft::pair<const Key, T> >
>
class map {
public:
typedef map<Key, T, Compare, Allocator> self_type;
typedef Key key_type;
typedef T mapped_type;
typedef ft::pair<const key_type, mapped_type> value_type;
typedef Compare key_compare;
typedef Allocator allocator_type;
typedef typename allocator_type::reference reference;
typedef typename allocator_type::const_reference const_reference;
typedef typename allocator_type::pointer pointer;
typedef typename allocator_type::const_pointer const_pointer;
typedef mapIterator<value_type> iterator;
typedef mapIterator<const value_type> const_iterator;
typedef ft::reverse_iterator<iterator> reverse_iterator;
typedef ft::reverse_iterator<const_iterator> const_reverse_iterator;
typedef ptrdiff_t difference_type;
typedef size_t size_type;
typedef Node<value_type> node_type;
private:
key_compare _key_cmp;
struct ValComp {
ValComp(key_compare key_cmp) : _key_cmp(key_cmp) {}
bool operator()(const value_type &lhs, const value_type &rhs) const {
return _key_cmp(lhs.first, rhs.first);
}
private:
key_compare _key_cmp;
};
typedef ValComp value_compare;
value_compare _v_cmp_less;
allocator_type _alloc;
node_type *_root;
node_type *_Leaf;
node_type *_most_left;
node_type *_most_right;
typedef typename Allocator::template rebind<node_type>::other node_allocator;
node_allocator _node_alloc;
node_type *_alloc_node() {
node_type *allocated = _node_alloc.allocate(1);
_node_alloc.construct(allocated, node_type());
return allocated;
}
node_type *_alloc_node(const value_type &val, node_type *leaf, node_type *parent = NULL) {
node_type *allocated = _node_alloc.allocate(1);
value_type *p_val = _alloc.allocate(1);
_alloc.construct(p_val, value_type(val));
_node_alloc.construct(allocated, node_type(p_val, leaf, parent));
return allocated;
}
void _dealloc_node(node_type *node) {
if (node->data) {
_alloc.destroy(node->data);
_alloc.deallocate(node->data, 1);
}
_node_alloc.destroy(node);
_node_alloc.deallocate(node, 1);
}
bool _v_cmp(const value_type &lhs, const value_type &rhs) const {
return !(_v_cmp_less(lhs, rhs) || _v_cmp_less(rhs, lhs));
}
void _update_Leaf() {
_most_left = GetMin(_root);
_most_right = GetMax(_root);
_Leaf->parent = _most_right;
}
static value_type make_val(const key_type &k) {
return value_type(k, mapped_type());
}
ft::pair<node_type *, bool> _put(const value_type &val, node_type *&curr, node_type *parent = NULL) {
if (curr == NULL || curr->isNull()) {
curr = _alloc_node(val, _Leaf, parent);
return ft::make_pair(curr, true);
}
ft::pair<node_type *, bool> ret = ft::make_pair(curr, false);
if (rand() % (curr->size + 1) == 0)
ret = insert_root(val, curr, curr->parent);
else if (_v_cmp_less(curr->val(), val))
ret = _put(val, curr->right, curr);
else if (_v_cmp_less(val, curr->val()))
ret = _put(val, curr->left, curr);
curr->update_size();
return ret;
}
void rotate_right(node_type *p) {
node_type *q = p->left;
bool is_root = p == _root;
if (!q || q->isNull()) {
return;
}
if (p->parent && p->parent->notNull()) {
p->ParentBranch() = q;
}
q->parent = p->parent;
p->assign_node(LEFT_BRANCH, q->right);
q->assign_node(RIGHT_BRANCH, p);
if (is_root)
_root = q;
p->update_size();
q->update_size();
}
void rotate_left(node_type *p) {
node_type *q = p->right;
bool is_root = p == _root;
if (!q || q->isNull()) {
if (is_root)
_root = p;
return;
}
if (p->parent && p->parent->notNull()) {
p->ParentBranch() = q;
}
q->parent = p->parent;
p->assign_node(RIGHT_BRANCH, q->left);
q->assign_node(LEFT_BRANCH, p);
if (is_root)
_root = q;
p->update_size();
q->update_size();
}
node_type *join(node_type *left, node_type *right) // объединение двух деревьев
{
if (!left || left->isNull()) return right;
if (!right || right->isNull()) return left;
if (right->size > left->size) {
left->assign_node(RIGHT_BRANCH, join(left->right, right));
left->update_size();
return left;
} else {
right->assign_node(LEFT_BRANCH, join(left, right->left));
right->update_size();
return right;
}
}
ft::pair<node_type *, bool> insert_root(const value_type &val, node_type *&curr, node_type *parent = NULL) {
if (curr == NULL || curr->isNull()) {
curr = _alloc_node(val, _Leaf, parent);
return ft::make_pair(curr, true);
}
ft::pair<node_type *, bool> ret = ft::make_pair(curr, false);
if (_v_cmp_less(curr->val(), val)) {
ret = insert_root(val, curr->right, curr);
rotate_left(curr);
} else if (_v_cmp_less(val, curr->val())) {
ret = insert_root(val, curr->left, curr);
rotate_right(curr);
}
return ret;
}
node_type *_find_node(const value_type &val, node_type *start = NULL) const {
start = start ? start : _root;
if (start->isNull() || _v_cmp(start->val(), val))
return start;
if (_v_cmp_less(start->val(), val))
return _find_node(val, start->right);
else
return _find_node(val, start->left);
}
node_type *_lower_bound(const value_type &val, node_type *start = NULL) const {
if (start == NULL)
start = _root;
if (start->isNull() || _v_cmp(start->val(), val))
return start;
if (_v_cmp_less(start->val(), val)) {
if (start->right->isNull())
return increment(start);
return _lower_bound(val, start->right);
} else {
if (start->left->isNull())
return start;
return _lower_bound(val, start->left);
}
}
ft::pair<node_type *, node_type *> _equal_range(const value_type &val) const {
node_type *node = _lower_bound(val);
if (node->notNull() && _v_cmp(node->val(), val))
return ft::make_pair(node, increment(node));
return ft::make_pair(node, node);
};
size_t _delete_random_node(const value_type &val, node_type *&curr) {
if (curr == NULL || curr->isNull()) {
return 0;
}
size_t ret = 0;
node_type *copy_curr = curr;
node_type *parent_curr = curr->parent;
if (_v_cmp(curr->val(), val)) {
curr = join(curr->left, curr->right);
if (!curr || curr->notNull()) {
curr->parent = parent_curr;
curr->update_size();
}
_dealloc_node(copy_curr);
return 1;
}
if (_v_cmp_less(curr->val(), val)) {
ret = _delete_random_node(val, curr->right);
} else if (_v_cmp_less(val, curr->val()))
ret = _delete_random_node(val, curr->left);
curr->update_size();
return ret;
}
public:
/*
* Construct and destructors
*/
explicit map(const key_compare &comp = key_compare(),
const allocator_type &alloc = allocator_type())
: _key_cmp(comp), _v_cmp_less(comp), _alloc(alloc), _root(_alloc_node()), _Leaf(_root) {
_update_Leaf();
}
template<class InputIterator>
map(InputIterator
first, InputIterator
last,
const key_compare &comp = key_compare(),
const allocator_type &alloc = allocator_type(),
typename enable_if<!is_integral<InputIterator>::value>::type * = 0) :
_key_cmp(comp), _v_cmp_less(comp), _alloc(alloc), _root(_alloc_node()), _Leaf(_root) {
_update_Leaf();
this->insert(first, last);
}
map(
const map &other) :
_key_cmp(other._key_cmp), _v_cmp_less(_key_cmp), _alloc(other._alloc),
_root(_alloc_node()), _Leaf(_root) {
_update_Leaf();
*this = other;
}
map &operator=(const map &other) {
if (&other == this)
return *this;
this->clear();
this->insert(other.begin(), other.end());
return *this;
}
~map() {
this->clear();
_dealloc_node(_Leaf);
}
/*
* Iterator funcs
*/
iterator begin() {
return iterator(_most_left);
}
iterator end() {
return iterator(_Leaf);
}
const_iterator begin() const {
return const_iterator(_most_left);
}
const_iterator end() const {
return const_iterator(_Leaf);
}
reverse_iterator rbegin() {
return reverse_iterator(end());
}
reverse_iterator rend() {
return reverse_iterator(begin());
}
const_reverse_iterator rbegin() const {
return const_reverse_iterator(end());
}
const_reverse_iterator rend() const {
return const_reverse_iterator(begin());
}
/*
* Acsess
*/
mapped_type &operator[](const key_type &k) {
node_type *nodeType = insert(ft::make_pair(k, mapped_type())).first.GetNode();
return nodeType->ref_val().second;
}
/*
* Capacity
*/
size_type size() const {
return _root->size;
}
bool empty() const {
return !size();
}
size_type max_size() const {
return _alloc.max_size();
}
/*
* Modifiers
*/
void swap(map &x) {
std::swap(_alloc, x._alloc);
std::swap(_root, x._root);
std::swap(_Leaf, x._Leaf);
std::swap(_most_left, x._most_left);
std::swap(_most_right, x._most_right);
}
void clear() {
erase(begin(), end());
}
ft::pair<iterator, bool> insert(const value_type &val) {
ft::pair<iterator, bool> ret = _put(val, _root);
_update_Leaf();
return ret;
}
iterator insert(iterator hint, const value_type &value) {
(void) hint;
return iterator(insert(value).first);
}
template<class InputIt>
void insert(InputIt first, InputIt last) {
while (first != last)
insert(*(first++));
}
void erase(iterator position) {
_delete_random_node(*position, _root);
_update_Leaf();
}
size_type erase(const key_type &k) {
size_type ret = _delete_random_node(make_val(k), _root);
_update_Leaf();
return ret;
}
void erase(iterator first, iterator last) {
while (first != last) {
erase(first++);
}
}
key_compare key_comp() const {
return _key_cmp;
}
value_compare value_comp() const {
return _v_cmp_less;
}
/*
* Lookup
*/
size_type count(const key_type &key) const {
return find(key) != end();
}
iterator find(const key_type &key) {
node_type *node = _find_node(ft::make_pair(key, mapped_type()));
return iterator(node);
}
const_iterator find(const key_type &key) const {
node_type *node = _find_node(ft::make_pair(key, mapped_type()));
return const_iterator(node);
}
iterator lower_bound(const key_type &k) {
return iterator(_lower_bound(make_val(k)));
}
const_iterator lower_bound(const key_type &k) const {
return const_iterator(_lower_bound(make_val(k)));
}
iterator upper_bound(const key_type &k) {
return iterator(_equal_range(make_val(k)).second);
}
const_iterator upper_bound(const key_type &k) const {
return const_iterator(_equal_range(make_val(k)).second);
}
ft::pair<iterator, iterator> equal_range(const key_type &k) {
ft::pair<node_type *, node_type *> range = _equal_range(make_val(k));
return ft::make_pair(iterator(range.first), iterator(range.second));
}
ft::pair<const_iterator, const_iterator> equal_range(const key_type &k) const {
ft::pair<node_type *, node_type *> range = _equal_range(make_val(k));
return ft::make_pair(const_iterator(range.first), const_iterator(range.second));
}
friend bool operator==(const map &lhs, const map &rhs) {
if (lhs.size() != rhs.size())
return false;
return ft::equal(lhs.begin(), lhs.end(), rhs.begin());
}
friend bool operator!=(const map &lhs, const map &rhs) {
return !(rhs == lhs);
}
friend bool operator<(const map &lhs, const map &rhs) {
return ft::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
}
friend bool operator>(const map &lhs, const map &rhs) {
return rhs < lhs;
}
friend bool operator<=(const map &lhs, const map &rhs) {
return !(rhs < lhs);
}
friend bool operator>=(const map &lhs, const map &rhs) {
return !(lhs < rhs);
}
};
}