-
Notifications
You must be signed in to change notification settings - Fork 1
/
multiset.hpp
515 lines (425 loc) · 12 KB
/
multiset.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
#ifndef XSG_MULTIMAP_HPP
# define XSG_MULTIMAP_HPP
# pragma once
#include "utils.hpp"
#include "multimapiterator.hpp"
namespace xsg
{
template <typename Key, class Compare = std::compare_three_way>
class multiset
{
public:
struct node;
using key_type = Key;
using value_type = Key;
using difference_type = detail::difference_type;
using size_type = detail::size_type;
using reference = value_type&;
using const_reference = value_type const&;
using iterator = multimapiterator<node const>;
using reverse_iterator = std::reverse_iterator<iterator>;
using const_iterator = multimapiterator<node const>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
struct node
{
using value_type = multiset::value_type;
static constinit inline Compare const cmp;
std::uintptr_t l_, r_;
xl::list<value_type> v_;
explicit node(auto&& k)
noexcept(noexcept(v_.emplace_back(std::forward<decltype(k)>(k))))
{
v_.emplace_back(std::forward<decltype(k)>(k));
}
//
auto& key() const noexcept { return v_.front(); }
//
static auto emplace(auto& r, auto&& k)
noexcept(noexcept(new node(std::forward<decltype(k)>(k))))
requires(detail::Comparable<Compare, decltype(k), key_type>)
{
auto const create_node([&](node* const p)
noexcept(noexcept(new node(std::forward<decltype(k)>(k))))
{
auto const q(new node(std::forward<decltype(k)>(k)));
q->l_ = q->r_ = detail::conv(p);
return q;
}
);
if (r)
{
auto const [q, qp, s](detail::emplace(r, k, create_node));
if (!s) q->v_.emplace_back(std::forward<decltype(k)>(k));
return std::pair(q, qp);
}
else
{
return std::pair<node*, node*>(r = create_node({}), {});
}
}
static auto emplace(auto& r, auto&& ...a)
noexcept(noexcept(node::emplace(r,
key_type(std::forward<decltype(a)>(a)...))))
requires(std::is_constructible_v<key_type, decltype(a)...>)
{
return node::emplace(r, key_type(std::forward<decltype(a)>(a)...));
}
static iterator erase(auto& r0, const_iterator const i)
noexcept(noexcept(std::declval<node>().v_.erase(i.i()),
node::erase(r0, i.n(), i.p())))
{
if (auto const n(i.n()), p(i.p()); 1 == n->v_.size())
{
auto const [nn, np, s](node::erase(r0, n, p));
return {&r0, nn, np};
}
else if (auto const it(i.i()); std::next(it) == n->v_.end())
{
auto const ni(std::next(i));
n->v_.erase(it);
return {&r0, ni.n(), ni.p()};
}
else
{
return {&r0, n, p, n->v_.erase(it)};
}
}
static inline auto erase(auto& r0, auto const pp, decltype(pp) p,
decltype(pp) n, std::uintptr_t* const q)
noexcept(noexcept(delete r0))
{
auto const s(n->v_.size());
auto [nnn, nnp](detail::next_node(n, p));
// pp - p - n - lr
if (auto const l(detail::left_node(n, p)),
r(detail::right_node(n, p)); l && r)
{
if (detail::size(l, n) < detail::size(r, n)) // erase from right side?
{
auto const [fnn, fnp](detail::first_node(r, n));
if (fnn == nnn)
{
nnp = p;
}
if (q)
{
*q = detail::conv(fnn, pp);
}
else
{
r0 = fnn;
}
// convert and attach l to fnn
fnn->l_ = detail::conv(l, p);
{
auto const nfnn(detail::conv(n, fnn));
l->l_ ^= nfnn; l->r_ ^= nfnn;
}
if (r == fnn)
{
r->r_ ^= detail::conv(n, p);
}
else
{
// attach right node of fnn to parent left
{
auto const fnpp(detail::left_node(fnp, fnn));
auto const rn(detail::right_node(fnn, fnp));
fnp->l_ = detail::conv(rn, fnpp);
if (rn)
{
auto const fnnfnp(detail::conv(fnn, fnp));
rn->l_ ^= fnnfnp; rn->r_ ^= fnnfnp;
}
}
// convert and attach r to fnn
fnn->r_ = detail::conv(r, p);
{
auto const nfnn(detail::conv(n, fnn));
r->l_ ^= nfnn; r->r_ ^= nfnn;
}
}
}
else // erase from the left side
{
auto const [lnn, lnp](detail::last_node(l, n));
if (r == nnn)
{
nnp = lnn;
}
if (q)
{
*q = detail::conv(lnn, pp);
}
else
{
r0 = lnn;
}
// convert and attach r to lnn
lnn->r_ = detail::conv(r, p);
{
auto const nlnn(detail::conv(n, lnn));
r->l_ ^= nlnn; r->r_ ^= nlnn;
}
if (l == lnn)
{
l->l_ ^= detail::conv(n, p);
}
else
{
{
auto const lnpp(detail::right_node(lnp, lnn));
auto const ln(detail::left_node(lnn, lnp));
lnp->r_ = detail::conv(ln, lnpp);
if (ln)
{
auto const lnnlnp(detail::conv(lnn, lnp));
ln->l_ ^= lnnlnp; ln->r_ ^= lnnlnp;
}
}
// convert and attach l to lnn
lnn->l_ = detail::conv(l, p);
{
auto const nlnn(detail::conv(n, lnn));
l->l_ ^= nlnn; l->r_ ^= nlnn;
}
}
}
}
else
{
auto const lr(l ? l : r);
if (lr)
{
if (lr == nnn)
{
nnp = p;
}
auto const np(detail::conv(n, p));
lr->l_ ^= np; lr->r_ ^= np;
}
if (q)
{
*q = detail::conv(lr, pp);
}
else
{
r0 = lr;
}
}
delete n;
return std::tuple(nnn, nnp, s);
}
static auto erase(auto& r0, auto const& k)
noexcept(noexcept(erase(r0, r0, r0, r0, {})))
{
using pointer = std::remove_cvref_t<decltype(r0)>;
using node = std::remove_pointer_t<pointer>;
std::uintptr_t* q{};
for (pointer pp{}, p{}, n(r0); n;)
{
if (auto const c(node::cmp(k, n->key())); c < 0)
{
detail::assign(pp, p, n, q)(p, n, detail::left_node(n, p), &n->l_);
}
else if (c > 0)
{
detail::assign(pp, p, n, q)(p, n, detail::right_node(n, p), &n->r_);
}
else
{
return erase(r0, pp, p, n, q);
}
}
return std::tuple(pointer{}, pointer{}, size_type{});
}
static auto erase(auto& r0, auto const n, decltype(n) const p)
noexcept(noexcept(erase(r0, r0, r0, r0, {})))
{
using pointer = std::remove_cvref_t<decltype(r0)>;
using node = std::remove_pointer_t<pointer>;
pointer pp{};
std::uintptr_t* q{};
if (p)
{
node::cmp(n->key(), p->key()) < 0 ?
detail::assign(pp, q)(detail::left_node(p, n), &p->l_) :
detail::assign(pp, q)(detail::right_node(p, n), &p->r_);
}
return erase(r0, pp, p, n, q);
}
};
private:
using this_class = multiset;
node* root_{};
public:
multiset() = default;
multiset(multiset const& o)
noexcept(noexcept(*this = o))
requires(std::is_copy_constructible_v<value_type>)
{
*this = o;
}
multiset(multiset&& o)
noexcept(noexcept(*this = std::move(o)))
{
*this = std::move(o);
}
multiset(std::input_iterator auto const i, decltype(i) j)
noexcept(noexcept(insert(i, j)))
{
insert(i, j);
}
multiset(std::initializer_list<value_type> l)
noexcept(noexcept(multiset(l.begin(), l.end()))):
multiset(l.begin(), l.end())
{
}
~multiset() noexcept(noexcept(delete root_)) { detail::destroy(root_, {}); }
# include "common.hpp"
//
size_type size() const noexcept
{
static constinit auto const f(
[](auto&& f, auto const n, decltype(n) p) noexcept -> size_type
{
return n ?
n->v_.size() +
f(f, detail::left_node(n, p), n) +
f(f, detail::right_node(n, p), n) :
size_type{};
}
);
return f(f, root_, {});
}
//
template <int = 0>
size_type count(auto const& k) const noexcept
requires(detail::Comparable<Compare, decltype(k), key_type>)
{
for (decltype(root_) p{}, n(root_); n;)
{
if (auto const c(node::cmp(k, n->key())); c < 0)
{
detail::assign(n, p)(detail::left_node(n, p), n);
}
else if (c > 0)
{
detail::assign(n, p)(detail::right_node(n, p), n);
}
else
{
return n->v_.size();
}
}
return {};
}
auto count(key_type const k) const noexcept { return count<0>(k); }
//
iterator emplace(auto&& ...a)
noexcept(noexcept(node::emplace(root_, std::forward<decltype(a)>(a)...)))
{
return {
&root_,
node::emplace(root_, 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_, {}, 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_, {}, 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(node::erase(root_, k)))
requires(detail::Comparable<Compare, decltype(k), key_type> &&
!std::convertible_to<decltype(k), const_iterator>)
{
return std::get<2>(node::erase(root_, 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(node::erase(root_, i)))
{
return node::erase(root_, i);
}
//
iterator insert(value_type const& v)
noexcept(noexcept(node::emplace(root_, v)))
{
return {&root_, node::emplace(root_, v)};
}
iterator insert(value_type&& v)
noexcept(noexcept(node::emplace(root_, std::move(v))))
{
return {&root_, node::emplace(root_, std::move(v))};
}
void insert(std::input_iterator auto const i, decltype(i) j)
noexcept(noexcept(emplace(*i)))
{
std::for_each(
i,
j,
[&](auto&& v) noexcept(noexcept(emplace(std::forward<decltype(v)>(v))))
{
emplace(std::forward<decltype(v)>(v));
}
);
}
};
//////////////////////////////////////////////////////////////////////////////
template <int = 0, typename K, class C>
inline auto erase(multiset<K, 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, class C>
inline auto erase(multiset<K, C>& c, auto const& k)
noexcept(noexcept(c.erase(k)))
requires(detail::Comparable<C, decltype(k), K>)
{
return c.erase(k);
}
template <typename K, class C>
inline auto erase(multiset<K, C>& c, K const k)
noexcept(noexcept(erase<0>(c, k)))
{
return erase<0>(c, k);
}
template <typename K, class C>
inline auto erase_if(multiset<K, C>& c, auto pred)
noexcept(noexcept(pred(std::declval<K>()), c.erase(c.begin())))
{
typename std::remove_reference_t<decltype(c)>::size_type r{};
for (auto i(c.begin()); i.n(); pred(*i) ? ++r, i = c.erase(i) : ++i);
return r;
}
//////////////////////////////////////////////////////////////////////////////
template <typename K, class C>
inline void swap(multiset<K, C>& l, decltype(l) r) noexcept { l.swap(r); }
}
#endif // XSG_MULTISET_HPP