-
Notifications
You must be signed in to change notification settings - Fork 18
/
Map.hpp
521 lines (426 loc) · 17.3 KB
/
Map.hpp
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
#pragma once
#include <cstddef>
#include <functional>
#include <memory>
#include <utility>
#include <stdexcept>
#include "_RbTree.hpp"
#include "_Common.hpp"
template <class _Compare, class _Value, class = void>
struct _RbTreeValueCompare {
protected:
[[no_unique_address]] _Compare _M_comp;
public:
_RbTreeValueCompare(_Compare __comp = _Compare()) noexcept
: _M_comp(__comp) {}
bool operator()(typename _Value::first_type const &__lhs,
_Value const &__rhs) const noexcept {
return this->_M_comp(__lhs, __rhs.first);
}
bool operator()(_Value const &__lhs,
typename _Value::first_type const &__rhs) const noexcept {
return this->_M_comp(__lhs.first, __rhs);
}
bool operator()(_Value const &__lhs, _Value const &__rhs) const noexcept {
return this->_M_comp(__lhs.first, __rhs.first);
}
struct _RbTreeIsMap;
};
template <class _Compare, class _Value>
struct _RbTreeValueCompare<
_Compare, _Value,
decltype((void)static_cast<typename _Compare::is_transparent *>(nullptr))> {
[[no_unique_address]] _Compare _M_comp;
_RbTreeValueCompare(_Compare __comp = _Compare()) noexcept
: _M_comp(__comp) {}
template <class _Lhs>
bool operator()(_Lhs &&__lhs, _Value const &__rhs) const noexcept {
return this->_M_comp(__lhs, __rhs.first);
}
template <class _Rhs>
bool operator()(_Value const &__lhs, _Rhs &&__rhs) const noexcept {
return this->_M_comp(__lhs.first, __rhs);
}
bool operator()(_Value const &__lhs, _Value const &__rhs) const noexcept {
return this->_M_comp(__lhs.first, __rhs.first);
}
using is_transparent = typename _Compare::is_transparent;
};
template <class _Key, class _Mapped, class _Compare = std::less<_Key>,
class _Alloc = std::allocator<std::pair<_Key const, _Mapped>>>
struct Map
: _RbTreeImpl<std::pair<_Key const, _Mapped>,
_RbTreeValueCompare<_Compare, std::pair<_Key const, _Mapped>>,
_Alloc> {
using key_type = _Key;
using mapped_type = _Mapped;
using value_type = std::pair<_Key const, _Mapped>;
using size_type = std::size_t;
using difference_type = std::ptrdiff_t;
private:
using _ValueComp = _RbTreeValueCompare<_Compare, value_type>;
public:
using typename _RbTreeImpl<value_type, _ValueComp, _Alloc>::iterator;
using typename _RbTreeImpl<value_type, _ValueComp, _Alloc>::const_iterator;
using typename _RbTreeImpl<value_type, _ValueComp, _Alloc>::node_type;
Map() = default;
explicit Map(_Compare __comp)
: _RbTreeImpl<value_type, _ValueComp, _Alloc>(__comp) {}
Map(std::initializer_list<value_type> __ilist) {
_M_single_insert(__ilist.begin(), __ilist.end());
}
explicit Map(std::initializer_list<value_type> __ilist, _Compare __comp)
: _RbTreeImpl<value_type, _ValueComp, _Alloc>(__comp) {
_M_single_insert(__ilist.begin(), __ilist.end());
}
template <_LIBPENGCXX_REQUIRES_ITERATOR_CATEGORY(std::input_iterator,
_InputIt)>
explicit Map(_InputIt __first, _InputIt __last) {
_M_single_insert(__first, __last);
}
template <_LIBPENGCXX_REQUIRES_ITERATOR_CATEGORY(std::input_iterator,
_InputIt)>
explicit Map(_InputIt __first, _InputIt __last, _Compare __comp)
: _RbTreeImpl<value_type, _ValueComp, _Alloc>(__comp) {
_M_single_insert(__first, __last);
}
Map(Map &&) = default;
Map &operator=(Map &&) = default;
Map(Map const &__that) : _RbTreeImpl<value_type, _ValueComp, _Alloc>() {
this->_M_single_insert(__that.begin(), __that.end());
}
Map &operator=(Map const &__that) {
if (&__that != this) {
this->assign(__that.begin(), __that.end());
}
return *this;
}
Map &operator=(std::initializer_list<value_type> __ilist) {
this->assign(__ilist);
return *this;
}
void assign(std::initializer_list<value_type> __ilist) {
this->clear();
this->_M_single_insert(__ilist.begin(), __ilist.end());
}
_Compare key_comp() const noexcept {
return this->_M_comp->_M_comp;
}
_ValueComp value_comp() const noexcept {
return this->_M_comp;
}
template <class _Kv,
_LIBPENGCXX_REQUIRES_TRANSPARENT_COMPARE(_ValueComp, _Kv, value_type)>
iterator find(_Kv &&__key) noexcept {
return this->_M_find(__key);
}
template <class _Kv,
_LIBPENGCXX_REQUIRES_TRANSPARENT_COMPARE(_ValueComp, _Kv, value_type)>
const_iterator find(_Kv &&__key) const noexcept {
return this->_M_find(__key);
}
iterator find(_Key const &__key) noexcept {
return this->_M_find(__key);
}
const_iterator find(_Key const &__key) const noexcept {
return this->_M_find(__key);
}
std::pair<iterator, bool> insert(value_type &&__value) {
return this->_M_single_emplace(std::move(__value));
}
std::pair<iterator, bool> insert(value_type const &__value) {
return this->_M_single_emplace(__value);
}
template <class _Kv,
_LIBPENGCXX_REQUIRES_TRANSPARENT_COMPARE(_ValueComp, _Kv, value_type)>
_Mapped const &at(_Kv const &__key) const {
const_iterator __it = this->_M_find(__key);
if (__it == this->end()) [[unlikely]] {
throw std::out_of_range("map::at");
}
return __it->second;
}
template <class _Kv,
_LIBPENGCXX_REQUIRES_TRANSPARENT_COMPARE(_ValueComp, _Kv, value_type)>
_Mapped &at(_Kv const &__key) {
iterator __it = this->_M_find(__key);
if (__it == this->end()) [[unlikely]] {
throw std::out_of_range("map::at");
}
return __it->second;
}
_Mapped const &at(_Key const &__key) const {
const_iterator __it = this->_M_find(__key);
if (__it == this->end()) [[unlikely]] {
throw std::out_of_range("map::at");
}
return __it->second;
}
_Mapped &at(_Key const &__key) {
iterator __it = this->_M_find(__key);
if (__it == this->end()) [[unlikely]] {
throw std::out_of_range("map::at");
}
return __it->second;
}
template <class _Kv,
_LIBPENGCXX_REQUIRES_TRANSPARENT_COMPARE(_ValueComp, _Kv, value_type)>
_Mapped &operator[](_Kv const &__key) {
iterator __it = this->_M_find(__key);
if (__it == this->end()) {
__it = this->_M_single_emplace(std::piecewise_construct,
std::forward_as_tuple(__key),
std::forward_as_tuple())
.first;
}
return __it->second;
}
_Mapped &operator[](_Key const &__key) {
iterator __it = this->_M_find(__key);
if (__it == this->end()) {
__it = this->_M_single_emplace(std::piecewise_construct,
std::forward_as_tuple(__key),
std::forward_as_tuple())
.first;
}
return __it->second;
}
template <class _Mp,
class = std::enable_if_t<std::is_convertible_v<_Mp, _Mapped>>>
std::pair<iterator, bool> insert_or_assign(_Key const &__key,
_Mp &&__mapped) {
std::pair<iterator, bool> __result = this->_M_single_emplace(
std::piecewise_construct, std::forward_as_tuple(__key),
std::forward_as_tuple(std::forward<_Mp>(__mapped)));
if (!__result.second) {
__result.first->second = std::forward<_Mp>(__mapped);
}
return __result;
}
template <class _Mp,
class = std::enable_if_t<std::is_convertible_v<_Mp, _Mapped>>>
std::pair<iterator, bool> insert_or_assign(_Key &&__key, _Mp &&__mapped) {
std::pair<iterator, bool> __result = this->_M_single_emplace(
std::piecewise_construct, std::forward_as_tuple(std::move(__key)),
std::forward_as_tuple(std::forward<_Mp>(__mapped)));
if (!__result.second) {
__result.first->second = std::forward<_Mp>(__mapped);
}
return __result;
}
template <class... Vs>
std::pair<iterator, bool> emplace(Vs &&...__value) {
return this->_M_single_emplace(std::forward<Vs>(__value)...);
}
template <class... _Ms>
std::pair<iterator, bool> try_emplace(_Key &&__key, _Ms &&...__mapped) {
return this->_M_single_emplace(
std::piecewise_construct, std::forward_as_tuple(std::move(__key)),
std::forward_as_tuple(std::forward<_Ms>(__mapped)...));
}
template <class... _Ms>
std::pair<iterator, bool> try_emplace(_Key const &__key,
_Ms &&...__mapped) {
return this->_M_single_emplace(
std::piecewise_construct, std::forward_as_tuple(__key),
std::forward_as_tuple(std::forward<_Ms>(__mapped)...));
}
template <_LIBPENGCXX_REQUIRES_ITERATOR_CATEGORY(std::input_iterator,
_InputIt)>
iterator insert(_InputIt __first, _InputIt __last) {
return this->_M_single_insert(__first, __last);
}
using _RbTreeImpl<value_type, _ValueComp, _Alloc>::assign;
template <_LIBPENGCXX_REQUIRES_ITERATOR_CATEGORY(std::input_iterator,
_InputIt)>
iterator assign(_InputIt __first, _InputIt __last) {
this->clear();
return this->_M_single_insert(__first, __last);
}
using _RbTreeImpl<value_type, _ValueComp, _Alloc>::erase;
template <class _Kv,
_LIBPENGCXX_REQUIRES_TRANSPARENT_COMPARE(_ValueComp, _Kv, value_type)>
size_t erase(_Kv &&__key) {
return this->_M_single_erase(__key);
}
size_t erase(_Key const &__key) {
return this->_M_single_erase(__key);
}
template <class _Kv,
_LIBPENGCXX_REQUIRES_TRANSPARENT_COMPARE(_ValueComp, _Kv, value_type)>
size_t count(_Kv &&__value) const noexcept {
return this->_M_contains(__value) ? 1 : 0;
}
size_t count(_Key const &__value) const noexcept {
return this->_M_contains(__value) ? 1 : 0;
}
template <class _Kv,
_LIBPENGCXX_REQUIRES_TRANSPARENT_COMPARE(_ValueComp, _Kv, value_type)>
bool contains(_Kv &&__value) const noexcept {
return this->_M_contains(__value);
}
bool contains(_Key const &__value) const noexcept {
return this->_M_contains(__value);
}
template <class _Kv,
_LIBPENGCXX_REQUIRES_TRANSPARENT_COMPARE(_ValueComp, _Kv, value_type)>
node_type extract(_Kv &&__key) {
iterator __it = this->_M_find(__key);
return __it != this->end() ? this->extract(__it) : node_type();
}
node_type extract(_Key const &__key) {
iterator __it = this->_M_find(__key);
return __it != this->end() ? this->extract(__it) : node_type();
}
};
template <class _Key, class _Mapped, class _Compare = std::less<_Key>,
class _Alloc = std::allocator<std::pair<_Key const, _Mapped>>>
struct MultiMap
: _RbTreeImpl<std::pair<_Key const, _Mapped>,
_RbTreeValueCompare<_Compare, std::pair<_Key const, _Mapped>>,
_Alloc> {
using key_type = _Key;
using mapped_type = _Mapped;
using value_type = std::pair<_Key const, _Mapped>;
using size_type = std::size_t;
using difference_type = std::ptrdiff_t;
private:
using _ValueComp = _RbTreeValueCompare<_Compare, value_type>;
public:
using typename _RbTreeImpl<value_type, _ValueComp, _Alloc>::iterator;
using typename _RbTreeImpl<value_type, _ValueComp, _Alloc>::const_iterator;
using typename _RbTreeImpl<value_type, _ValueComp, _Alloc>::node_type;
MultiMap() = default;
explicit MultiMap(_Compare __comp)
: _RbTreeImpl<value_type, _ValueComp, _Alloc>(__comp) {}
MultiMap(std::initializer_list<value_type> __ilist) {
_M_multi_insert(__ilist.begin(), __ilist.end());
}
explicit MultiMap(std::initializer_list<value_type> __ilist,
_Compare __comp)
: _RbTreeImpl<value_type, _ValueComp, _Alloc>(__comp) {
_M_multi_insert(__ilist.begin(), __ilist.end());
}
template <_LIBPENGCXX_REQUIRES_ITERATOR_CATEGORY(std::input_iterator,
_InputIt)>
explicit MultiMap(_InputIt __first, _InputIt __last) {
_M_multi_insert(__first, __last);
}
template <_LIBPENGCXX_REQUIRES_ITERATOR_CATEGORY(std::input_iterator,
_InputIt)>
explicit MultiMap(_InputIt __first, _InputIt __last, _Compare __comp)
: _RbTreeImpl<value_type, _ValueComp, _Alloc>(__comp) {
_M_multi_insert(__first, __last);
}
MultiMap(MultiMap &&) = default;
MultiMap &operator=(MultiMap &&) = default;
MultiMap(MultiMap const &__that)
: _RbTreeImpl<value_type, _ValueComp, _Alloc>() {
this->_M_multi_insert(__that.begin(), __that.end());
}
MultiMap &operator=(MultiMap const &__that) {
if (&__that != this) {
this->assign(__that.begin(), __that.end());
}
return *this;
}
MultiMap &operator=(std::initializer_list<value_type> __ilist) {
this->assign(__ilist);
return *this;
}
void assign(std::initializer_list<value_type> __ilist) {
this->clear();
this->_M_multi_insert(__ilist.begin(), __ilist.end());
}
_Compare key_comp() const noexcept {
return this->_M_comp->_M_comp;
}
_ValueComp value_comp() const noexcept {
return this->_M_comp;
}
template <class _Kv,
_LIBPENGCXX_REQUIRES_TRANSPARENT_COMPARE(_ValueComp, _Kv, value_type)>
iterator find(_Kv &&__key) noexcept {
return this->_M_find(__key);
}
template <class _Kv,
_LIBPENGCXX_REQUIRES_TRANSPARENT_COMPARE(_ValueComp, _Kv, value_type)>
const_iterator find(_Kv &&__key) const noexcept {
return this->_M_find(__key);
}
iterator find(_Key const &__key) noexcept {
return this->_M_find(__key);
}
const_iterator find(_Key const &__key) const noexcept {
return this->_M_find(__key);
}
std::pair<iterator, bool> insert(value_type &&__value) {
return this->_M_single_emplace(std::move(__value));
}
std::pair<iterator, bool> insert(value_type const &__value) {
return this->_M_single_emplace(__value);
}
template <class... _Ts>
std::pair<iterator, bool> emplace(_Ts &&...__value) {
return this->_M_single_emplace(std::forward<_Ts>(__value)...);
}
template <class... _Ts>
std::pair<iterator, bool> try_emplace(_Key &&__key, _Ts &&...__value) {
return this->_M_single_emplace(
std::piecewise_construct, std::forward_as_tuple(std::move(__key)),
std::forward_as_tuple(std::forward<_Ts>(__value)...));
}
template <class... _Ts>
std::pair<iterator, bool> try_emplace(_Key const &__key, _Ts &&...__value) {
return this->_M_single_emplace(
std::piecewise_construct, std::forward_as_tuple(__key),
std::forward_as_tuple(std::forward<_Ts>(__value)...));
}
template <_LIBPENGCXX_REQUIRES_ITERATOR_CATEGORY(std::input_iterator,
_InputIt)>
iterator insert(_InputIt __first, _InputIt __last) {
return this->_M_single_insert(__first, __last);
}
using _RbTreeImpl<value_type, _ValueComp, _Alloc>::assign;
template <_LIBPENGCXX_REQUIRES_ITERATOR_CATEGORY(std::input_iterator,
_InputIt)>
iterator assign(_InputIt __first, _InputIt __last) {
this->clear();
return this->_M_single_insert(__first, __last);
}
using _RbTreeImpl<value_type, _ValueComp, _Alloc>::erase;
template <class _Kv,
_LIBPENGCXX_REQUIRES_TRANSPARENT_COMPARE(_ValueComp, _Kv, value_type)>
size_t erase(_Kv &&__key) {
return this->_M_single_erase(__key);
}
size_t erase(_Key const &__key) {
return this->_M_single_erase(__key);
}
template <class _Kv,
_LIBPENGCXX_REQUIRES_TRANSPARENT_COMPARE(_ValueComp, _Kv, value_type)>
size_t count(_Kv &&__value) const noexcept {
return this->_M_multi_count(__value);
}
size_t count(_Key const &__value) const noexcept {
return this->_M_multi_count(__value);
}
template <class _Kv,
_LIBPENGCXX_REQUIRES_TRANSPARENT_COMPARE(_ValueComp, _Kv, value_type)>
bool contains(_Kv &&__value) const noexcept {
return this->_M_contains(__value);
}
bool contains(_Key const &__value) const noexcept {
return this->_M_contains(__value);
}
template <class _Kv,
_LIBPENGCXX_REQUIRES_TRANSPARENT_COMPARE(_ValueComp, _Kv, value_type)>
node_type extract(_Kv &&__key) {
iterator __it = this->_M_find(__key);
return __it != this->end() ? this->extract(__it) : node_type();
}
node_type extract(_Key const &__key) {
iterator __it = this->_M_find(__key);
return __it != this->end() ? this->extract(__it) : node_type();
}
};