forked from scylladb/scylladb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompact-radix-tree.hh
2089 lines (1733 loc) · 71.6 KB
/
compact-radix-tree.hh
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
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2021-present ScyllaDB
*/
/*
* SPDX-License-Identifier: LicenseRef-ScyllaDB-Source-Available-1.0
*/
#pragma once
#include <cassert>
#include <algorithm>
#include <bitset>
#include <fmt/core.h>
#include "utils/assert.hh"
#include "utils/allocation_strategy.hh"
#include "utils/array-search.hh"
#include <boost/intrusive/parent_from_member.hpp>
class size_calculator;
namespace compact_radix_tree {
template <typename T, typename Idx> class printer;
template <unsigned Size>
inline unsigned find_in_array(uint8_t val, const uint8_t* arr);
template <>
inline unsigned find_in_array<4>(uint8_t val, const uint8_t* arr) {
return utils::array_search_4_eq(val, arr);
}
template <>
inline unsigned find_in_array<8>(uint8_t val, const uint8_t* arr) {
return utils::array_search_8_eq(val, arr);
}
template <>
inline unsigned find_in_array<16>(uint8_t val, const uint8_t* arr) {
return utils::array_search_16_eq(val, arr);
}
template <>
inline unsigned find_in_array<32>(uint8_t val, const uint8_t* arr) {
return utils::array_search_32_eq(val, arr);
}
template <>
inline unsigned find_in_array<64>(uint8_t val, const uint8_t* arr) {
return utils::array_search_x32_eq(val, arr, 2);
}
// A union of any number of types.
template <typename... Ts>
struct variadic_union;
template <typename Tx>
struct variadic_union<Tx> {
union {
Tx _this;
};
variadic_union() noexcept {}
~variadic_union() {}
};
template <typename Tx, typename Ty, typename... Ts>
struct variadic_union<Tx, Ty, Ts...> {
union {
Tx _this;
variadic_union<Ty, Ts...> _other;
};
variadic_union() noexcept {}
~variadic_union() {}
};
/*
* Radix tree implementation for the key being an integer type.
* The search key is split into equal-size pieces to find the
* next node in each level. The pieces are defined compile-time
* so the tree is compile-time limited in ints depth.
*
* Uses 3 memory optimizations:
* - a node dynamically grows in size depending on the range of
* keys it carries
* - additionally, if the set of keys on a node is very sparse the
* node may become "indirect" thus keeping only the actual set
* of keys
* - if a node has 1 child it's removed from the tree and this
* loneley kid is attached directly to its (former) grandfather
*/
template <typename T, typename Index = unsigned int>
requires std::is_nothrow_move_constructible_v<T> && std::is_integral_v<Index>
class tree {
friend class ::size_calculator;
template <typename A, typename I> friend class printer;
class leaf_node;
class inner_node;
struct node_head;
class node_head_ptr;
public:
/*
* The search key in the tree is an integer, the whole
* logic below is optimized for that.
*/
using key_t = std::make_unsigned_t<Index>;
/*
* The lookup uses 7-bit pieces from the key to search on
* each level. Thus all levels but the last one keep pointers
* on lower levels, the last one is the leaf node that keeps
* values on board.
*
* The 8th bit in the node index byte is used to denote an
* unused index which is quite helpful.
*/
using node_index_t = uint8_t;
static constexpr unsigned radix_bits = 7;
static constexpr key_t radix_mask = (1 << radix_bits) - 1;
static constexpr unsigned leaf_depth = (8 * sizeof(key_t) + radix_bits - 1) / radix_bits - 1;
static constexpr unsigned node_index_limit = 1 << radix_bits;
static_assert(node_index_limit != 0);
static constexpr node_index_t unused_node_index = node_index_limit;
private:
/*
* Nodes can be of 2 kinds -- direct and indirect.
*
* Direct nodes are arrays of elements. Getting a value from
* this node is simple indexing. There are 2 of them -- static
* and dynamic. Static nodes have fixed size capable to keep all
* the possible indices, dynamic work like a vector growing in
* size. Former occupy more space, but work a bit faster because
* of * missing boundary checks.
*
* Indirect nodes keep map of indices on board and perform lookup
* rather than direct indexing to get a value. They also grow in
* size, but unlike dynamic direct nodes by converting between each
* other.
*
* When a node is tried to push a new index over its current
* capacity it grows into some other node, that can fit all its
* keys plus at least one.
*
* When a key is removed from an indirect node and it becomes
* less that some threshold, it's shrunk into smaller node.
*
* The nil is a placeholder for non-existing empty node.
*/
enum class layout : uint8_t { nil,
indirect_tiny, indirect_small, indirect_medium, indirect_large,
direct_dynamic, direct_static, };
/*
* When a node has only one child, the former is removed from
* the tree and its parent is set up to directly point to this
* only kid. The kid, in turn, carries a "prefix" on board
* denoting the index that might have been skipped by this cut.
*
* The lower 7 bits are the prefix length, the rest is the
* prefix itself.
*/
static constexpr key_t prefix_len_mask = radix_mask;
static constexpr key_t prefix_mask = ~prefix_len_mask;
static key_t make_prefix(key_t key, unsigned len) noexcept {
return (key & prefix_mask) + len;
}
/*
* Mask to check node's prefix (mis-)match
*/
static key_t prefix_mask_at(unsigned depth) noexcept {
return prefix_mask << (radix_bits * (leaf_depth - depth));
}
/*
* Finds the number of leading elements that coincide for two
* indices. Needed on insertion, when a short-cut node gets
* expanded back.
*/
static unsigned common_prefix_len(key_t k1, key_t k2) noexcept {
static constexpr unsigned trailing_bits = (8 * sizeof(key_t)) % radix_bits;
static constexpr unsigned round_up_delta = trailing_bits == 0 ? 0 : radix_bits - trailing_bits;
/*
* This won't work if k1 == k2 (clz is undefined for full
* zeroes value), but we don't get here in this case
*/
return (__builtin_clz(k1 ^ k2) + round_up_delta) / radix_bits;
}
/*
* Gets the depth's radix_bits-len index from the whole key, that's
* used in intra-node search.
*/
static node_index_t node_index(key_t key, unsigned depth) noexcept {
return (key >> (radix_bits * (leaf_depth - depth))) & radix_mask;
}
enum class erase_mode { real, cleanup, };
/*
* When removing an index from a node it may end-up in one of 4
* states:
*
* - empty -- the last index was removed, the parent node is
* welcome to drop the slot and mark it as unused
* (and maybe get shrunk/squashed after that)
* - squash -- only one index left, the parent node is welcome
* to remove this node and replace it with its only
* child (tuning it's prefix respectively)
* - shrink -- current layout contains few indices, so parent
* node should shrink the slot into smaller node
* - nothing - just nothing
*/
enum class erase_result { nothing, empty, shrink, squash, };
template <unsigned Threshold>
static erase_result after_drop(unsigned count) noexcept {
if (count == 0) {
return erase_result::empty;
}
if (count == 1) {
return erase_result::squash;
}
if constexpr (Threshold != 0) {
if (count <= Threshold) {
return erase_result::shrink;
}
}
return erase_result::nothing;
}
/*
* Lower-bound calls return back pointer on the value and the leaf
* node_head on which the value was found. The latter is needed
* for iterator's ++ optimization.
*/
struct lower_bound_res {
const T* elem;
const node_head* leaf;
key_t key;
lower_bound_res(const T* e, const node_head& l, key_t k) noexcept : elem(e), leaf(&l), key(k) {}
lower_bound_res() noexcept : elem(nullptr), leaf(nullptr), key(0) {}
};
/*
* Allocation returns a slot pointer and a boolean denoting
* if the allocation really took place (false if the slot
* is already occupied)
*/
using allocate_res = std::pair<T*, bool>;
using clone_res = std::pair<node_head*, std::exception_ptr>;
/*
* A header all nodes start with. The type of a node (inner/leaf)
* is evaluated (fingers-crossed) from the depth argument, so the
* header doesn't have this bit.
*/
struct node_head {
node_head_ptr* _backref;
// Prefix for squashed nodes
key_t _prefix;
const layout _base_layout;
// Number of keys on the node
uint8_t _size;
// How many slots are there. Used only by direct dynamic nodes
const uint8_t _capacity;
node_head() noexcept : _backref(nullptr), _prefix(0), _base_layout(layout::nil), _size(0), _capacity(0) {}
node_head(key_t prefix, layout lt, uint8_t capacity) noexcept
: _backref(nullptr)
, _prefix(prefix)
, _base_layout(lt)
, _size(0)
, _capacity(capacity) {}
node_head(node_head&& o) noexcept
: _backref(std::exchange(o._backref, nullptr))
, _prefix(o._prefix)
, _base_layout(o._base_layout)
, _size(std::exchange(o._size, 0))
, _capacity(o._capacity) {
if (_backref != nullptr) {
*_backref = this;
}
}
node_head(const node_head&) = delete;
~node_head() { SCYLLA_ASSERT(_size == 0); }
/*
* Helpers to cast header to the actual node class or to the
* node's base class (see below).
*/
template <typename NBT>
NBT& as_base() noexcept {
return *boost::intrusive::get_parent_from_member(this, &NBT::_head);
}
template <typename NBT>
const NBT& as_base() const noexcept {
return *boost::intrusive::get_parent_from_member(this, &NBT::_head);
}
template <typename NT>
typename NT::node_type& as_base_of() noexcept {
return as_base<typename NT::node_type>();
}
template <typename NT>
const typename NT::node_type& as_base_of() const noexcept {
return as_base<typename NT::node_type>();
}
template <typename NT>
NT& as_node() noexcept {
return *boost::intrusive::get_parent_from_member(&as_base_of<NT>(), &NT::_base);
}
template <typename NT>
const NT& as_node() const noexcept {
return *boost::intrusive::get_parent_from_member(&as_base_of<NT>(), &NT::_base);
}
// Construct a key from leaf node prefix and index
key_t key_of(node_index_t ni) const noexcept {
return (_prefix & prefix_mask) + ni;
}
// Prefix manipulations
unsigned prefix_len() const noexcept { return _prefix & prefix_len_mask; }
void trim_prefix(unsigned v) noexcept { _prefix -= v; }
void bump_prefix(unsigned v) noexcept { _prefix += v; }
bool check_prefix(key_t key, unsigned& depth) const noexcept {
unsigned real_depth = depth + prefix_len();
key_t mask = prefix_mask_at(real_depth);
if ((key & mask) != (_prefix & mask)) {
return false;
}
depth = real_depth;
return true;
}
/*
* A bunch of "polymorphic" API wrappers that selects leaf/inner
* node to call the method on.
*
* The node_base below provides the same set, but ploymorphs
* the calls into the actual node layout.
*/
/*
* Finds the element by the given key
*/
const T* get(key_t key, unsigned depth) const noexcept {
if (depth == leaf_depth) {
return as_base_of<leaf_node>().get(key, depth);
} else {
return as_base_of<inner_node>().get(key, depth);
}
}
/*
* Finds the element whose key is not greater than the given one
*/
lower_bound_res lower_bound(key_t key, unsigned depth) const noexcept {
unsigned real_depth = depth + prefix_len();
key_t mask = prefix_mask_at(real_depth);
if ((key & mask) > (_prefix & mask)) {
return lower_bound_res();
}
depth = real_depth;
if (depth == leaf_depth) {
return as_base_of<leaf_node>().lower_bound(key, depth);
} else {
return as_base_of<inner_node>().lower_bound(key, depth);
}
}
/*
* Allocates a new slot for the value. The caller is given the
* pointer to the slot and the sign if it's now busy or not,
* so that it can destruct it and construct a new element.
*/
allocate_res alloc(key_t key, unsigned depth) {
if (depth == leaf_depth) {
return as_base_of<leaf_node>().alloc(key, depth);
} else {
return as_base_of<inner_node>().alloc(key, depth);
}
}
/*
* Erase the element with the given key, if present.
*/
erase_result erase(key_t key, unsigned depth, erase_mode erm) noexcept {
if (depth == leaf_depth) {
return as_base_of<leaf_node>().erase(key, depth, erm);
} else {
return as_base_of<inner_node>().erase(key, depth, erm);
}
}
/*
* Weed walks the tree and removes the elements for which
* the filter() returns true.
*/
template <typename Fn>
erase_result weed(Fn&& filter, unsigned depth) {
if (depth == leaf_depth) {
return as_base_of<leaf_node>().weed(filter, depth);
} else {
return as_base_of<inner_node>().weed(filter, depth);
}
}
/*
* Grow the current node and return the new one
*/
node_head* grow(key_t key, unsigned depth) {
node_index_t ni = node_index(key, depth);
if (depth == leaf_depth) {
return as_base_of<leaf_node>().template grow<leaf_node>(ni);
} else {
return as_base_of<inner_node>().template grow<inner_node>(ni);
}
}
/*
* Shrink the current node and return the new one
*/
node_head* shrink(unsigned depth) {
if (depth == leaf_depth) {
return as_base_of<leaf_node>().template shrink<leaf_node>();
} else {
return as_base_of<inner_node>().template shrink<inner_node>();
}
}
/*
* Walk the tree without modifying it (however, the elements
* themselves can be modified)
*/
template <typename Visitor>
bool visit(Visitor&& v, unsigned depth) const {
bool ret = true;
depth += prefix_len();
if (v(*this, depth, true)) {
if (depth == leaf_depth) {
ret = as_base_of<leaf_node>().visit(v, depth);
} else {
ret = as_base_of<inner_node>().visit(v, depth);
}
v(*this, depth, false);
}
return ret;
}
template <typename Fn>
clone_res clone(Fn&& cloner, unsigned depth) const noexcept {
depth += prefix_len();
if (depth == leaf_depth) {
return as_base_of<leaf_node>().template clone<leaf_node, Fn>(cloner, depth);
} else {
return as_base_of<inner_node>().template clone<inner_node, Fn>(cloner, depth);
}
}
void free(unsigned depth) noexcept {
if (depth == leaf_depth) {
leaf_node::free(as_node<leaf_node>());
} else {
inner_node::free(as_node<inner_node>());
}
}
size_t node_size(unsigned depth) const noexcept {
if (depth == leaf_depth) {
return as_base_of<leaf_node>().node_size();
} else {
return as_base_of<inner_node>().node_size();
}
}
/*
* A leaf-node specific helper for iterator
*/
lower_bound_res lower_bound(key_t key) const noexcept {
return as_base_of<leaf_node>().lower_bound(key, leaf_depth);
}
/*
* And two inner-node specific calls for nodes
* squashing/expanding
*/
void set_lower(node_index_t ni, node_head* n) noexcept {
as_node<inner_node>().set_lower(ni, n);
}
node_head_ptr pop_lower() noexcept {
return as_node<inner_node>().pop_lower();
}
};
/*
* Pointer to node head. Inner nodes keep these, tree root pointer
* is the one as well.
*/
class node_head_ptr {
node_head* _v;
public:
node_head_ptr(node_head* v) noexcept : _v(v) {}
node_head_ptr(const node_head_ptr&) = delete;
node_head_ptr(node_head_ptr&& o) noexcept : _v(std::exchange(o._v, nullptr)) {
if (_v != nullptr) {
_v->_backref = this;
}
}
node_head& operator*() const noexcept { return *_v; }
node_head* operator->() const noexcept { return _v; }
node_head* raw() const noexcept { return _v; }
operator bool() const noexcept { return _v != nullptr; }
bool is(const node_head& n) const noexcept { return _v == &n; }
node_head_ptr& operator=(node_head* v) noexcept {
_v = v;
// Checking (_v != &nil_root) is not needed for correctness, since
// nil_root's _backref is never read anyway. But we do this check for
// performance reasons: since nil_root is shared between shards,
// writing to it would cause serious cache contention.
if (_v != nullptr && _v != &nil_root) {
_v->_backref = this;
}
return *this;
}
};
/*
* This helper wraps several layouts into one and precedes them with
* the header. It does nothing but provides a polymorphic calls to the
* lower/inner layouts depending on the head.base_layout value.
*/
template <typename Slot, typename... Layouts>
struct node_base {
node_head _head;
variadic_union<Layouts...> _layouts;
template <typename Tx>
static size_t node_size(layout lt, uint8_t capacity) noexcept {
return sizeof(node_head) + Tx::layout_size(capacity);
}
template <typename Tx, typename Ty, typename... Ts>
static size_t node_size(layout lt, uint8_t capacity) noexcept {
return lt == Tx::this_layout ? sizeof(node_head) + Tx::layout_size(capacity) : node_size<Ty, Ts...>(lt, capacity);
}
static size_t node_size(layout lt, uint8_t capacity) noexcept {
return node_size<Layouts...>(lt, capacity);
}
size_t node_size() const noexcept {
return node_size(_head._base_layout, _head._capacity);
}
// construct
template <typename Tx>
void construct(variadic_union<Tx>& cur) noexcept {
new (&cur._this) Tx(_head);
}
template <typename Tx, typename Ty, typename... Ts>
void construct(variadic_union<Tx, Ty, Ts...>& cur) noexcept {
if (_head._base_layout == Tx::this_layout) {
new (&cur._this) Tx(_head);
return;
}
construct<Ty, Ts...>(cur._other);
}
node_base(key_t prefix, layout lt, uint8_t capacity) noexcept
: _head(prefix, lt, capacity) {
construct<Layouts...>(_layouts);
}
node_base(const node_base&) = delete;
template <typename Tx>
void move_construct(variadic_union<Tx>& cur, variadic_union<Tx>&& o) noexcept {
new (&cur._this) Tx(std::move(o._this), _head);
}
template <typename Tx, typename Ty, typename... Ts>
void move_construct(variadic_union<Tx, Ty, Ts...>& cur, variadic_union<Tx, Ty, Ts...>&& o) noexcept {
if (_head._base_layout == Tx::this_layout) {
new (&cur._this) Tx(std::move(o._this), _head);
return;
}
move_construct<Ty, Ts...>(cur._other, std::move(o._other));
}
node_base(node_base&& o) noexcept
: _head(std::move(o._head)) {
move_construct<Layouts...>(_layouts, std::move(o._layouts));
}
~node_base() { }
// get value by key
template <typename Tx>
const T* get(const variadic_union<Tx>& cur, key_t key, unsigned depth) const noexcept {
if (_head._base_layout == Tx::this_layout) {
return cur._this.get(_head, key, depth);
}
return (const T*)nullptr;
}
template <typename Tx, typename Ty, typename... Ts>
const T* get(const variadic_union<Tx, Ty, Ts...>& cur, key_t key, unsigned depth) const noexcept {
if (_head._base_layout == Tx::this_layout) {
return cur._this.get(_head, key, depth);
}
return get<Ty, Ts...>(cur._other, key, depth);
}
const T* get(key_t key, unsigned depth) const noexcept {
return get<Layouts...>(_layouts, key, depth);
}
// finds a lowed-bound element
template <typename Tx>
lower_bound_res lower_bound(const variadic_union<Tx>& cur, key_t key, unsigned depth) const noexcept {
if (_head._base_layout == Tx::this_layout) {
return cur._this.lower_bound(_head, key, depth);
}
return lower_bound_res();
}
template <typename Tx, typename Ty, typename... Ts>
lower_bound_res lower_bound(const variadic_union<Tx, Ty, Ts...>& cur, key_t key, unsigned depth) const noexcept {
if (_head._base_layout == Tx::this_layout) {
return cur._this.lower_bound(_head, key, depth);
}
return lower_bound<Ty, Ts...>(cur._other, key, depth);
}
lower_bound_res lower_bound(key_t key, unsigned depth) const noexcept {
return lower_bound<Layouts...>(_layouts, key, depth);
}
// erase by key
template <typename Tx>
erase_result erase(variadic_union<Tx>& cur, key_t key, unsigned depth, erase_mode erm) noexcept {
return cur._this.erase(_head, key, depth, erm);
}
template <typename Tx, typename Ty, typename... Ts>
erase_result erase(variadic_union<Tx, Ty, Ts...>& cur, key_t key, unsigned depth, erase_mode erm) noexcept {
if (_head._base_layout == Tx::this_layout) {
return cur._this.erase(_head, key, depth, erm);
}
return erase<Ty, Ts...>(cur._other, key, depth, erm);
}
erase_result erase(key_t key, unsigned depth, erase_mode erm) noexcept {
return erase<Layouts...>(_layouts, key, depth, erm);
}
// weed values with filter
template <typename Fn, typename Tx>
erase_result weed(variadic_union<Tx>& cur, Fn&& filter, unsigned depth) {
return cur._this.weed(_head, filter, _head._prefix, depth);
}
template <typename Fn, typename Tx, typename Ty, typename... Ts>
erase_result weed(variadic_union<Tx, Ty, Ts...>& cur, Fn&& filter, unsigned depth) {
if (_head._base_layout == Tx::this_layout) {
return cur._this.weed(_head, filter, _head._prefix, depth);
}
return weed<Fn, Ty, Ts...>(cur._other, filter, depth);
}
template <typename Fn>
erase_result weed(Fn&& filter, unsigned depth) {
return weed<Fn, Layouts...>(_layouts, filter, depth);
}
// allocate new slot
template <typename Tx>
allocate_res alloc(variadic_union<Tx>& cur, key_t key, unsigned depth) {
return cur._this.alloc(_head, key, depth);
}
template <typename Tx, typename Ty, typename... Ts>
allocate_res alloc(variadic_union<Tx, Ty, Ts...>& cur, key_t key, unsigned depth) {
if (_head._base_layout == Tx::this_layout) {
return cur._this.alloc(_head, key, depth);
}
return alloc<Ty, Ts...>(cur._other, key, depth);
}
allocate_res alloc(key_t key, unsigned depth) {
return alloc<Layouts...>(_layouts, key, depth);
}
// append slot to node
template <typename Tx>
void append(variadic_union<Tx>& cur, node_index_t ni, Slot&& val) noexcept {
cur._this.append(_head, ni, std::move(val));
}
template <typename Tx, typename Ty, typename... Ts>
void append(variadic_union<Tx, Ty, Ts...>& cur, node_index_t ni, Slot&& val) noexcept {
if (_head._base_layout == Tx::this_layout) {
cur._this.append(_head, ni, std::move(val));
return;
}
append<Ty, Ts...>(cur._other, ni, std::move(val));
}
void append(node_index_t ni, Slot&& val) noexcept {
return append<Layouts...>(_layouts, ni, std::move(val));
}
// find and remove some element (usually the last one)
template <typename Tx>
Slot pop(variadic_union<Tx>& cur) noexcept {
return cur._this.pop(_head);
}
template <typename Tx, typename Ty, typename... Ts>
Slot pop(variadic_union<Tx, Ty, Ts...>& cur) noexcept {
if (_head._base_layout == Tx::this_layout) {
return cur._this.pop(_head);
}
return pop<Ty, Ts...>(cur._other);
}
Slot pop() noexcept {
return pop<Layouts...>(_layouts);
}
// visiting
template <typename Visitor, typename Tx>
bool visit(const variadic_union<Tx>& cur, Visitor&& v, unsigned depth) const {
return cur._this.visit(_head, v, depth);
}
template <typename Visitor, typename Tx, typename Ty, typename... Ts>
bool visit(const variadic_union<Tx, Ty, Ts...>& cur, Visitor&& v, unsigned depth) const {
if (_head._base_layout == Tx::this_layout) {
return cur._this.visit(_head, v, depth);
}
return visit<Visitor, Ty, Ts...>(cur._other, v, depth);
}
template <typename Visitor>
bool visit(Visitor&& v, unsigned depth) const {
return visit<Visitor, Layouts...>(_layouts, v, depth);
}
// cloning
template <typename NT, typename Fn, typename Tx>
clone_res clone(const variadic_union<Tx>& cur, Fn&& cloner, unsigned depth) const noexcept {
return cur._this.template clone<NT, Fn>(_head, cloner, depth);
}
template <typename NT, typename Fn, typename Tx, typename Ty, typename... Ts>
clone_res clone(const variadic_union<Tx, Ty, Ts...>& cur, Fn&& cloner, unsigned depth) const noexcept {
if (_head._base_layout == Tx::this_layout) {
return cur._this.template clone<NT, Fn>(_head, cloner, depth);
}
return clone<NT, Fn, Ty, Ts...>(cur._other, cloner, depth);
}
template <typename NT, typename Fn>
clone_res clone(Fn&& cloner, unsigned depth) const noexcept {
return clone<NT, Fn, Layouts...>(_layouts, cloner, depth);
}
// growing into larger layout
template <typename NT, typename Tx>
node_head* grow(variadic_union<Tx>& cur, node_index_t want_ni) {
if constexpr (Tx::growable) {
return cur._this.template grow<NT>(_head, want_ni);
}
std::abort();
}
template <typename NT, typename Tx, typename Ty, typename... Ts>
node_head* grow(variadic_union<Tx, Ty, Ts...>& cur, node_index_t want_ni) {
if constexpr (Tx::growable) {
if (_head._base_layout == Tx::this_layout) {
return cur._this.template grow<NT>(_head, want_ni);
}
}
return grow<NT, Ty, Ts...>(cur._other, want_ni);
}
template <typename NT>
node_head* grow(node_index_t want_ni) {
return grow<NT, Layouts...>(_layouts, want_ni);
}
// shrinking into smaller layout
template <typename NT, typename Tx>
node_head* shrink(variadic_union<Tx>& cur) {
if constexpr (Tx::shrinkable) {
return cur._this.template shrink<NT>(_head);
}
std::abort();
}
template <typename NT, typename Tx, typename Ty, typename... Ts>
node_head* shrink(variadic_union<Tx, Ty, Ts...>& cur) {
if constexpr (Tx::shrinkable) {
if (_head._base_layout == Tx::this_layout) {
return cur._this.template shrink<NT>(_head);
}
}
return shrink<NT, Ty, Ts...>(cur._other);
}
template <typename NT>
node_head* shrink() {
return shrink<NT, Layouts...>(_layouts);
}
};
/*
* Node layouts. Define the way indices and payloads are stored on the node
*/
/*
* Direct layout is just an array of data.
*
* It makes a difference between inner slots, that are pointers to other nodes,
* and leaf slots, which are of user type. The former can be nullptr denoting
* the missing slot, while the latter may not have this sign, so the layout
* uses a bitmask to check if a slot is occupiued or not.
*/
template <typename Slot, layout Layout, layout GrowInto, unsigned GrowThreshold, layout ShrinkInto, unsigned ShrinkThreshold>
struct direct_layout {
static constexpr bool shrinkable = ShrinkInto != layout::nil;
static constexpr bool growable = GrowInto != layout::nil;
static constexpr layout this_layout = Layout;
static bool check_capacity(const node_head& head, node_index_t ni) noexcept {
if constexpr (this_layout == layout::direct_static) {
return true;
} else {
return ni < head._capacity;
}
}
static unsigned capacity(const node_head& head) noexcept {
if constexpr (this_layout == layout::direct_static) {
return node_index_limit;
} else {
return head._capacity;
}
}
struct array_of_non_node_head_ptr {
/*
* This bismask is the maximum possible, while the array of slots
* is dynamic. This is to make sure all direct layouts have the
* slots at the same offset, so we may not introduce new layouts
* for it, and to avoid some capacity if-s in the code below
*/
std::bitset<node_index_limit> _present;
Slot _slots[0];
array_of_non_node_head_ptr(const node_head& head) noexcept {
_present.reset();
}
array_of_non_node_head_ptr(array_of_non_node_head_ptr&& o, const node_head& head) noexcept
: _present(std::move(o._present)) {
for (unsigned i = 0; i < capacity(head); i++) {
if (o.has(i)) {
new (&_slots[i]) Slot(std::move(o._slots[i]));
o._slots[i].~Slot();
}
}
}
array_of_non_node_head_ptr(const array_of_non_node_head_ptr&) = delete;
bool has(unsigned i) const noexcept { return _present.test(i); }
bool has(const node_head& h, unsigned i) const noexcept { return has(i); }
void add(node_head& head, unsigned i) noexcept { _present.set(i); }
void del(node_head& head, unsigned i) noexcept { _present.set(i, false); }
unsigned count(const node_head& head) const noexcept { return _present.count(); }
};
struct array_of_node_head_ptr {
Slot _slots[0];
array_of_node_head_ptr(const node_head& head) noexcept {
for (unsigned i = 0; i < capacity(head); i++) {
new (&_slots[i]) node_head_ptr(nullptr);
}
}
array_of_node_head_ptr(array_of_node_head_ptr&& o, const node_head& head) noexcept {
for (unsigned i = 0; i < capacity(head); i++) {
new (&_slots[i]) Slot(std::move(o._slots[i]));
o._slots[i].~Slot();
}
}
array_of_node_head_ptr(const array_of_node_head_ptr&) = delete;
bool has(unsigned i) const noexcept { return _slots[i]; }
bool has(const node_head& h, unsigned i) const noexcept { return check_capacity(h, i) && _slots[i]; }
void add(node_head& head, unsigned i) noexcept { head._size++; }
void del(node_head& head, unsigned i) noexcept { head._size--; }
unsigned count(const node_head& head) const noexcept { return head._size; }
};
using array_of_slot = std::conditional_t<std::is_same_v<Slot, node_head_ptr>, array_of_node_head_ptr, array_of_non_node_head_ptr>;
array_of_slot _data;
direct_layout(const node_head& head) noexcept : _data(head) {}
direct_layout(direct_layout&& o, const node_head& head) noexcept : _data(std::move(o._data), head) {}
direct_layout(const direct_layout&) = delete;
const T* get(const node_head& head, key_t key, unsigned depth) const noexcept {
node_index_t ni = node_index(key, depth);
if (!_data.has(head, ni)) {
return nullptr;
}
return get_at(_data._slots[ni], key, depth + 1);
}
Slot pop(node_head& head) noexcept {
for (unsigned i = 0; i < capacity(head); i++) {
if (_data.has(i)) {
Slot ret = std::move(_data._slots[i]);
_data.del(head, i);
_data._slots[i].~Slot();