-
Notifications
You must be signed in to change notification settings - Fork 1
/
map.hpp
434 lines (370 loc) · 10.2 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
#ifndef XSG_MAP_HPP
# define XSG_MAP_HPP
# pragma once
#include "utils.hpp"
#include "mapiterator.hpp"
namespace xsg
{
template <typename Key, typename Value,
class Compare = std::compare_three_way>
class map
{
public:
struct node;
using key_type = Key;
using mapped_type = Value;
using value_type = std::pair<Key const, Value>;
using difference_type = detail::difference_type;
using size_type = detail::size_type;
using reference = value_type&;
using const_reference = value_type const&;
using iterator = mapiterator<node>;
using reverse_iterator = std::reverse_iterator<iterator>;
using const_iterator = mapiterator<node const>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
struct node
{
using value_type = map::value_type;
static constinit inline Compare const cmp;
std::uintptr_t l_, r_;
value_type kv_;
explicit node(auto&& k, auto&& ...a)
noexcept(noexcept(
value_type(
std::piecewise_construct_t{},
std::forward_as_tuple(std::forward<decltype(k)>(k)),
std::forward_as_tuple(std::forward<decltype(a)>(a)...)
)
)
):
kv_(
std::piecewise_construct_t{},
std::forward_as_tuple(std::forward<decltype(k)>(k)),
std::forward_as_tuple(std::forward<decltype(a)>(a)...)
)
{
}
//
auto& key() const noexcept { return std::get<0>(kv_); }
//
static auto emplace(auto& r, auto&& k, auto&& ...a)
noexcept(noexcept(new node(std::forward<decltype(k)>(k),
std::forward<decltype(a)>(a)...)))
requires(detail::Comparable<Compare, decltype(k), key_type>)
{
auto const create_node([&](node* const p)
noexcept(noexcept(new node(std::forward<decltype(k)>(k),
std::forward<decltype(a)>(a)...)))
{
auto const q(new node(std::forward<decltype(k)>(k),
std::forward<decltype(a)>(a)...));
q->l_ = q->r_ = detail::conv(p);
return q;
}
);
return r ? detail::emplace(r, k, create_node) :
std::tuple<node*, node*, bool>(r = create_node({}), {}, true);
}
};
private:
using this_class = map;
node* root_{};
public:
map() = default;
map(map const& o)
noexcept(noexcept(map(o.begin(), o.end())))
requires(std::is_copy_constructible_v<value_type>):
map(o.begin(), o.end())
{
}
map(map&& o)
noexcept(noexcept(*this = std::move(o)))
{
*this = std::move(o);
}
map(std::input_iterator auto const i, decltype(i) j)
noexcept(noexcept(insert(i, j)))
{
insert(i, j);
}
map(std::initializer_list<value_type> l)
noexcept(noexcept(map(l.begin(), l.end()))):
map(l.begin(), l.end())
{
}
~map() noexcept(noexcept(detail::destroy(root_, {})))
{
detail::destroy(root_, {});
}
# include "common.hpp"
//
auto size() const noexcept { return detail::size(root_, {}); }
//
template <int = 0>
auto& operator[](auto&& k)
noexcept(noexcept(node::emplace(root_, std::forward<decltype(k)>(k))))
requires(detail::Comparable<Compare, decltype(k), key_type>)
{
return std::get<1>(std::get<0>(
node::emplace(root_, std::forward<decltype(k)>(k)))->kv_);
}
auto& operator[](key_type k)
noexcept(noexcept(operator[]<0>(std::move(k))))
{
return operator[]<0>(std::move(k));
}
template <int = 0>
auto& operator[](auto const& k) const
noexcept(noexcept(at(k)))
requires(detail::Comparable<Compare, decltype(k), key_type>)
{
return at(k);
}
auto& operator[](key_type const k) const
noexcept(noexcept(operator[]<0>(k)))
{
return operator[]<0>(k);
}
template <int = 0>
auto& at(auto&& k) noexcept
requires(detail::Comparable<Compare, decltype(k), key_type>)
{
return std::get<1>(detail::find(root_, k)->kv_);
}
auto& at(key_type k) noexcept { return at<0>(std::move(k)); }
template <int = 0>
auto const& at(auto&& k) const noexcept
requires(detail::Comparable<Compare, decltype(k), key_type>)
{
return std::get<1>(detail::find(root_, std::forward<decltype(k)>(k))->kv_);
}
auto& at(key_type k) const noexcept { return at<0>(std::move(k)); }
//
template <int = 0>
size_type count(auto const& k) const noexcept
requires(detail::Comparable<Compare, decltype(k), key_type>)
{
return bool(detail::find(root_, {}, k));
}
auto count(key_type const k) const noexcept { return count<0>(k); }
//
template <int = 0>
auto emplace(auto&& k, auto&& ...a)
noexcept(noexcept(
node::emplace(
root_,
std::forward<decltype(k)>(k),
std::forward<decltype(a)>(a)...
)
)
)
requires(detail::Comparable<Compare, decltype(k), key_type>)
{
auto const [n, p, s](
node::emplace(
root_,
std::forward<decltype(k)>(k),
std::forward<decltype(a)>(a)...
)
);
return std::pair(iterator(&root_, n, p), s);
}
auto emplace(key_type k, auto&& ...a)
noexcept(noexcept(
emplace<0>(std::move(k), std::forward<decltype(a)>(a)...)
)
)
requires(detail::Comparable<Compare, decltype(k), key_type>)
{
return emplace<0>(std::move(k), std::forward<decltype(a)>(a)...);
}
//
template <int = 0>
auto equal_range(auto&& k) noexcept
requires(detail::Comparable<Compare, decltype(k), key_type>)
{
auto const [nl, g](
detail::equal_range(root_, {}, std::forward<decltype(k)>(k))
);
return std::pair(iterator(&root_, nl), iterator(&root_, g));
}
auto equal_range(key_type k) noexcept
{
return equal_range<0>(std::move(k));
}
template <int = 0>
auto equal_range(auto&& k) const noexcept
requires(detail::Comparable<Compare, decltype(k), key_type>)
{
auto const [nl, g](
detail::equal_range(root_, {}, std::forward<decltype(k)>(k))
);
return std::pair(const_iterator(&root_, nl), const_iterator(&root_, g));
}
auto equal_range(key_type k) const noexcept
{
return equal_range<0>(std::move(k));
}
//
template <int = 0>
size_type erase(auto&& k)
noexcept(noexcept(detail::erase(root_, k)))
requires(detail::Comparable<Compare, decltype(k), key_type> &&
!std::convertible_to<decltype(k), const_iterator>)
{
return bool(
std::get<0>(detail::erase(root_, std::forward<decltype(k)>(k)))
);
}
auto erase(key_type k) noexcept(noexcept(erase<0>(std::move(k))))
{
return erase<0>(std::move(k));
}
iterator erase(const_iterator const i)
noexcept(noexcept(
detail::erase(
root_,
const_cast<node*>(i.n_),
const_cast<node*>(i.p_)
)
)
)
{
return {
&root_,
detail::erase(
root_,
const_cast<node*>(i.n_),
const_cast<node*>(i.p_)
)
};
}
//
template <int = 0>
auto insert(auto&& v)
noexcept(noexcept(
node::emplace(
root_,
std::get<0>(std::forward<decltype(v)>(v)),
std::get<1>(std::forward<decltype(v)>(v))
)
)
)
requires(
detail::Comparable<
Compare,
decltype(std::get<0>(std::forward<decltype(v)>(v))),
key_type
>
)
{
auto const [n, p, s](
node::emplace(
root_,
std::get<0>(std::forward<decltype(v)>(v)),
std::get<1>(std::forward<decltype(v)>(v))
)
);
return std::pair(iterator(&root_, n, p), s);
}
auto insert(value_type const v)
noexcept(noexcept(insert<0>(v)))
{
return insert<0>(v);
}
void insert(std::input_iterator auto const i, decltype(i) j)
noexcept(noexcept(emplace(std::get<0>(*i), std::get<1>(*i))))
{
std::for_each(
i,
j,
[&](auto&& v)
noexcept(noexcept(
emplace(
std::get<0>(std::forward<decltype(v)>(v)),
std::get<1>(std::forward<decltype(v)>(v))
)
)
)
{
emplace(
std::get<0>(std::forward<decltype(v)>(v)),
std::get<1>(std::forward<decltype(v)>(v))
);
}
);
}
//
template <int = 0>
auto insert_or_assign(auto&& k, auto&& ...a)
noexcept(noexcept(
node::emplace(
root_,
std::forward<decltype(k)>(k),
std::forward<decltype(a)>(a)...
)
)
)
requires(detail::Comparable<Compare, decltype(k), key_type>)
{
auto const [n, p, s](
node::emplace(
root_,
std::forward<decltype(k)>(k),
std::forward<decltype(a)>(a)...
)
);
if (!s)
{
if constexpr(sizeof...(a))
{
std::get<1>(n->kv_) = mapped_type(std::forward<decltype(a)>(a)...);
}
else
{
std::get<1>(n->kv_) = (std::forward<decltype(a)>(a), ...);
}
}
return std::pair(iterator(&root_, n, p), s);
}
auto insert_or_assign(key_type k, auto&& ...a)
noexcept(noexcept(insert_or_assign<0>(std::move(k),
std::forward<decltype(a)>(a)...)))
{
return insert_or_assign<0>(std::move(k), std::forward<decltype(a)>(a)...);
}
};
//////////////////////////////////////////////////////////////////////////////
template <int = 0, typename K, typename V, class C>
inline auto erase(map<K, V, C>& c, auto const& k)
noexcept(noexcept(c.erase(K(k))))
requires(!detail::Comparable<C, decltype(k), K>)
{
return c.erase(K(k));
}
template <int = 0, typename K, typename V, class C>
inline auto erase(map<K, V, C>& c, auto const& k)
noexcept(noexcept(c.erase(k)))
requires(detail::Comparable<C, decltype(k), K>)
{
return c.erase(k);
}
template <typename K, typename V, class C>
inline auto erase(map<K, V, C>& c, K const k)
noexcept(noexcept(erase<0>(c, k)))
{
return erase<0>(c, k);
}
template <typename K, typename V, class C>
inline auto erase_if(map<K, V, C>& c, auto pred)
noexcept(noexcept(pred(std::declval<K const&>()), c.erase(c.begin())))
{
typename std::remove_reference_t<decltype(c)>::size_type r{};
for (auto i(c.begin()); i; pred(*i) ? ++r, i = c.erase(i) : ++i);
return r;
}
//////////////////////////////////////////////////////////////////////////////
template <typename K, typename V, class C>
inline void swap(map<K, V, C>& l, decltype(l) r) noexcept { l.swap(r); }
}
#endif // XSG_MAP_HPP