-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlashRadixTree.hpp
More file actions
1216 lines (1057 loc) · 42.3 KB
/
FlashRadixTree.hpp
File metadata and controls
1216 lines (1057 loc) · 42.3 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
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
//
// FlashRadixTree.hpp
//
// Created by Matthew Varendorff on 26/2/2024.
//
// GNU GENERAL PUBLIC LICENSE
// Version 3, 29 June 2007
//
#ifndef FlashRadixTree_hpp
#define FlashRadixTree_hpp
#include <iostream>
#include <map>
#include <memory>
#include <string>
#include <stack>
#ifdef USE_SPLAY_TREE
#include "SplayTree.hpp"
#endif
#include <concepts>
#include <sstream>
#include <algorithm>
#include <optional>
template <typename T, typename Alloc, typename... Args>
std::unique_ptr<T, std::function<void(T*)>> make_unique_alloc(Alloc alloc, Args&&... args)
{
using AllocTraits = std::allocator_traits<Alloc>;
using pointer = typename AllocTraits::pointer;
// Allocate space for one object of type T
pointer ptr = AllocTraits::allocate(alloc, 1);
// Construct an object of type T with the provided arguments
AllocTraits::construct(alloc, ptr, std::forward<Args>(args)...);
// Create a custom deleter as a lambda function that captures the allocator by value
auto deleter = [alloc](T* ptr) mutable {
AllocTraits::destroy(alloc, ptr); // Call destructor
AllocTraits::deallocate(alloc, ptr, 1); // Deallocate memory
};
// Define the deleter type
using DeleterType = std::function<void(T*)>;
// Create and return a unique_ptr with the custom deleter
return std::unique_ptr<T, DeleterType>(ptr, deleter);
}
template<typename T>
concept IsBasicString = requires {
typename T::traits_type;
typename T::allocator_type;
// Check if T is an instantiation of std::basic_string
requires std::is_same_v<T, std::basic_string<typename T::value_type,
typename T::traits_type,
typename T::allocator_type>>;
};
template<typename T>
concept IsBasicStringView = requires {
typename T::traits_type;
// Check if T is an instantiation of std::basic_string_view
requires std::is_same_v<T, std::basic_string_view<typename T::value_type,
typename T::traits_type>>;
};
template<typename T>
concept BasicStringOrBasicStringView = IsBasicString<T> || IsBasicStringView<T>;
// Concept to check if a type supports streaming with '<<'
template<typename T>
concept Streaming = requires(std::ostream& os, const T& t) {
{ os << t } -> std::convertible_to<std::ostream&>;
};
enum class MatchMode {
Prefix,
Exact
};
template <BasicStringOrBasicStringView Key, Streaming Value, MatchMode MatchMode = MatchMode::Exact,
#ifdef USE_SPLAY_TREE
typename Allocator = std::allocator<std::unique_ptr<Value>>>
#else
typename Allocator = std::allocator<std::pair<const Key, std::unique_ptr<Value>>>>
#endif
class FlashRadixTree {
enum class Sentinal { END, REND, NONE };
private:
template<class Parent>
struct FlashRadixTreeNodeBase
{
FlashRadixTreeNodeBase() = default;
FlashRadixTreeNodeBase(bool isEndOfWord, Value&& value, const Key& prefix, Parent* parent)
: isEndOfWord(isEndOfWord), value(std::move(value)), prefix(prefix), parent(parent)
{};
FlashRadixTreeNodeBase(bool isEndOfWord, const Value& value, const Key& prefix, Parent* parent)
: isEndOfWord(isEndOfWord), value(value), prefix(prefix), parent(parent)
{};
//move and move assignment
FlashRadixTreeNodeBase(FlashRadixTreeNodeBase&& other) noexcept
{
isEndOfWord = other.isEndOfWord;
value = std::move(other.value);
prefix = std::move(other.prefix);
deleted = other.deleted;
next = other.next;
prev = other.prev;
other.next = nullptr;
other.prev = nullptr;
}
FlashRadixTreeNodeBase& operator=(FlashRadixTreeNodeBase&& other) noexcept
{
if(this != &other)
{
isEndOfWord = other.isEndOfWord;
value = std::move(other.value);
prefix = std::move(other.prefix);
deleted = other.deleted;
next = other.next;
prev = other.prev;
other.next = nullptr;
other.prev = nullptr;
}
return *this;
}
bool constexpr operator==(const FlashRadixTreeNodeBase& other) const
{
return (isEndOfWord == other.isEndOfWord)
&& (prefix == other.prefix)
&& (deleted == other.deleted);
}
bool constexpr operator!=(const FlashRadixTreeNodeBase& other) const
{
return !(*this == other);
}
Key getFullKey() const
{
if(fullKey.has_value())
return fullKey.value();
//traverse up levels and prefix the key
fullKey = prefix;
if(parent == nullptr) {
return fullKey.value();
}
//else traverse up the tree
for(auto p = parent; p != nullptr; p = p->parent) {
fullKey.value().insert(0, p->prefix);
}
return fullKey.value();
}
void setDeleted() noexcept
{
deleted = true;
}
void clear()
{
isEndOfWord = false;
value = Value();
prefix = Key();
deleted = false;
}
bool isEndOfWord = false;
Value value = Value();
Key prefix = Key();
bool deleted = false;
Parent* next = nullptr;
Parent* prev = nullptr;
Parent* parent = nullptr;
private:
mutable std::optional<std::string> fullKey;
};
class FlashRadixTreeNodeNoOwner;
class FlashRadixTreeNode : public FlashRadixTreeNodeBase<FlashRadixTreeNode> {
using BaseType = FlashRadixTreeNodeBase<FlashRadixTreeNode>;
public:
using TreeNodePtr = std::unique_ptr<FlashRadixTreeNode, std::function<void(FlashRadixTreeNode*)>>;
#ifdef USE_SPLAY_TREE
using Children = SplayTree<typename Key::value_type, TreeNodePtr, Allocator>;
Children children = Children(Allocator());
#else
using Children = std::map<typename Key::value_type, TreeNodePtr, std::less<typename Key::value_type>>;//, Allocator>;
Children children;
#endif
FlashRadixTreeNode(const Key& prefix, Value&& value, FlashRadixTreeNode* parent)
: BaseType(false, std::forward<Value>(value), prefix, parent)
{};
FlashRadixTreeNode(const Key& prefix, Value&& value, bool isEndOfWord, FlashRadixTreeNode* parent)
: BaseType(isEndOfWord, std::forward<Value>(value), prefix, parent)
{};
FlashRadixTreeNode() = default;
~FlashRadixTreeNode() = default;
FlashRadixTreeNode(const FlashRadixTreeNodeNoOwner& ) = delete;
FlashRadixTreeNode( FlashRadixTreeNode&& other) noexcept
: BaseType(std::move(other))
{
children = std::move(other.children);
}
//operators
FlashRadixTreeNode& operator=(const FlashRadixTreeNode& ) = delete;
FlashRadixTreeNode& operator=(FlashRadixTreeNode&& other) noexcept
{
if(this != &other)
{
BaseType::operator=(std::move(other));
children = std::move(other.children);
}
return *this;
}
void clear()
{
children.clear();
FlashRadixTreeNodeBase<FlashRadixTreeNode>::clear();
}
};
class FlashRadixTreeNodeNoOwner : public FlashRadixTreeNodeBase<FlashRadixTreeNode>
{
using BaseType = FlashRadixTreeNodeBase<FlashRadixTreeNode>;
public:
using TreeNodePtr = FlashRadixTreeNode*;
#ifdef USE_SPLAY_TREE
using Children = SplayTree<typename Key::value_type, TreeNodePtr, Allocator>;
Children children = Children(Allocator());
#else
using Children = std::map<typename Key::value_type, TreeNodePtr>;
Children children;
#endif
FlashRadixTreeNodeNoOwner(const Key& prefix, Value&& value, FlashRadixTreeNode* parent)
: BaseType(false, std::forward<Value>(value), prefix, parent)
{};
FlashRadixTreeNodeNoOwner(const Key& prefix, const Value& value, bool isEndOfWord, FlashRadixTreeNode* parent)
: BaseType(isEndOfWord, value, prefix, parent)
{};
FlashRadixTreeNodeNoOwner() = default;
~FlashRadixTreeNodeNoOwner() = default;
FlashRadixTreeNodeNoOwner(const FlashRadixTreeNodeNoOwner& other)
:BaseType(other)
{
children = other.children;
}
FlashRadixTreeNodeNoOwner( FlashRadixTreeNodeNoOwner&& other) noexcept
: BaseType(std::move(other))
{
children = std::move(other.children);
}
//operators
FlashRadixTreeNodeNoOwner& operator=(const FlashRadixTreeNodeNoOwner& other )
{
if(this != &other)
{
BaseType::operator=(other);
children = other.children;
}
return *this;
}
FlashRadixTreeNodeNoOwner& operator=(FlashRadixTreeNodeNoOwner&& other) noexcept
{
if(this != &other)
{
BaseType::operator=(std::move(other));
children = std::move(other.children);
}
return *this;
}
};
public:
using value_type = FlashRadixTreeNode;
using value_type_pointer = value_type*;
using const_value_type_pointer = const value_type*;
using value_type_reference = value_type&;
using const_value_type_reference = const value_type&;
using ValueType = Value;
using pair_type = std::pair<Key, Value>;
private:
enum class IteratorDirection { FORWARD, REVERSE};
enum class IteratorConstness { CONST, NON_CONST};
template<IteratorDirection Direction, IteratorConstness constness = IteratorConstness::NON_CONST>
class XFlashRadixTreeIterator
{
private:
using tree_pointer = std::conditional_t<constness == IteratorConstness::CONST, const FlashRadixTree*, FlashRadixTree*>;
using value_type_pointer = std::conditional_t<constness == IteratorConstness::CONST, const value_type*, value_type*>;
using value_type_reference = std::conditional_t<constness == IteratorConstness::CONST, const value_type&, value_type&>;
using value = std::conditional_t<constness == IteratorConstness::CONST, const XFlashRadixTreeIterator, XFlashRadixTreeIterator>;
using pointer = std::conditional_t<constness == IteratorConstness::CONST, const XFlashRadixTreeIterator*, XFlashRadixTreeIterator*>;
using reference = std::conditional_t<constness == IteratorConstness::CONST, const XFlashRadixTreeIterator&, XFlashRadixTreeIterator&>;
value_type_pointer _node;
tree_pointer _tree;
bool _end = true;
XFlashRadixTreeIterator(value_type_pointer node, tree_pointer tree)
: _node(node), _tree(tree), _end(node == nullptr || tree == nullptr)
{}
public:
XFlashRadixTreeIterator() noexcept = default;
~XFlashRadixTreeIterator() = default;
// Default copy constructor - used for same type
XFlashRadixTreeIterator(const XFlashRadixTreeIterator& other) noexcept = default;
XFlashRadixTreeIterator(XFlashRadixTreeIterator&& other) noexcept
{
_node = other._node;
_tree = other._tree;
_end = other._end;
other._node = nullptr;
other._tree = nullptr;
other._end = true;
}
// Default copy assignment operator - used for same type
XFlashRadixTreeIterator& operator=(const XFlashRadixTreeIterator& other) noexcept = default;
// Prevent cross-direction copying and assignment using a deleted function template
template<IteratorDirection OtherDirection>
XFlashRadixTreeIterator(const XFlashRadixTreeIterator<OtherDirection>&) = delete;
template<IteratorDirection OtherDirection>
XFlashRadixTreeIterator& operator=(const XFlashRadixTreeIterator<OtherDirection>&) = delete;
//converter functions to convert form forward to reverse and vice versa
//template<IteratorDirection OtherDirection>
auto get_other_direction() const noexcept {
if constexpr (Direction == IteratorDirection::FORWARD) {
return XFlashRadixTreeIterator<IteratorDirection::REVERSE>(_node, _tree);
} else {
return XFlashRadixTreeIterator<IteratorDirection::FORWARD>(_node, _tree);
}
}
XFlashRadixTreeIterator& operator++() noexcept
{
if constexpr (Direction == IteratorDirection::FORWARD) {
do{
_node = _node->next;
}while(_node != nullptr && (!_node->isEndOfWord || _node->deleted));
}
else {
do{
_node = _node->prev;
}while(_node != nullptr && (!_node->isEndOfWord || _node->deleted));
}
_end = _node == nullptr;
return *this;
}
XFlashRadixTreeIterator& operator--() noexcept
{
if constexpr (Direction == IteratorDirection::FORWARD) {
do{
_node = _node->prev;
}while(_node != nullptr && (!_node->isEndOfWord || _node->deleted));
}
else {
do{
_node = _node->next;
}while(_node != nullptr && (!_node->isEndOfWord || _node->deleted));
}
_end = _node == nullptr;
return *this;
}
XFlashRadixTreeIterator operator++(int) noexcept
{
XFlashRadixTreeIterator tmp = *this;
++(*this);
return tmp;
}
XFlashRadixTreeIterator operator--(int) noexcept
{
XFlashRadixTreeIterator tmp = *this;
--(*this);
return tmp;
}
bool operator==(const XFlashRadixTreeIterator& other) const
{
if(_tree != other._tree)
return false;
if(_end && other._end)
return true;
if(_node == nullptr && other._node == nullptr)
return true;
if(_node == nullptr || other._node == nullptr)
return false;
return *_node == *other._node;
}
bool operator!=(const XFlashRadixTreeIterator& other) const
{
return !(*this == other);
}
constexpr value_type_pointer operator->()
{
return _node;
}
constexpr value_type_reference operator*()
{
return *_node;
}
friend class FlashRadixTree;
};
public:
using iterator = XFlashRadixTreeIterator<IteratorDirection::FORWARD>;
using reverse_iterator = XFlashRadixTreeIterator<IteratorDirection::REVERSE>;
using const_iterator = XFlashRadixTreeIterator<IteratorDirection::FORWARD, IteratorConstness::CONST>;
using const_reverse_iterator = XFlashRadixTreeIterator<IteratorDirection::REVERSE, IteratorConstness::CONST>;
using size_type = size_t;
using TreeNodeAllocator = typename std::allocator_traits<Allocator>::template rebind_alloc<FlashRadixTreeNode>;
using UniquePtrAlloc = std::unique_ptr<FlashRadixTreeNode, std::function<void(FlashRadixTreeNode*)>>;
private:
TreeNodeAllocator _nodeAllocator;
UniquePtrAlloc _root;
FlashRadixTreeNode* _firstWord = nullptr;
const iterator _endIt = iterator(nullptr, this);
const const_iterator _cendIt = const_iterator(nullptr, this);
const reverse_iterator _rendIt = reverse_iterator(nullptr, this);
const const_reverse_iterator _crendIt = const_reverse_iterator(nullptr, this);
size_type _size = 0;
public:
FlashRadixTree(const Allocator& alloc = Allocator()) noexcept
: _nodeAllocator(alloc),
_root(make_unique_alloc<FlashRadixTreeNode>(_nodeAllocator))
{
}
~FlashRadixTree() {
clear();
}
FlashRadixTree(const FlashRadixTree& ) = delete;
FlashRadixTree(FlashRadixTree&& other) noexcept
: _nodeAllocator(std::move(other._nodeAllocator)), _root(std::move(other._root)), _firstWord(other._firstWord), _size(other._size) {
other._firstWord = nullptr;
other._size = 0;
}
FlashRadixTree& operator=(FlashRadixTree&& other) noexcept {
if(this != &other) {
clear();
_nodeAllocator = std::move(other._nodeAllocator);
_root = std::move(other._root);
_firstWord = other._firstWord;
_size = other._size;
other._firstWord = nullptr;
other._size = 0;
}
return *this;
}
constexpr const_iterator getRoot() const noexcept {
return const_iterator(_root.get(), this);
}
constexpr iterator getRoot() noexcept{
return iterator(_root.get(), this);
}
constexpr size_type size() const noexcept {
return _size;
}
iterator begin() noexcept {
const auto node = _getMinimum();
if(node == nullptr) {
return _endIt;
}
return iterator(node, this);
}
const_iterator begin() const noexcept {
const auto node = _getMinimum();
if(node == nullptr) {
return _endIt;
}
return const_iterator(node, this);
}
const_iterator cbegin() const noexcept {
return begin();
}
constexpr const iterator& end() noexcept {
return _endIt;
}
constexpr const const_iterator& end() const noexcept {
return _cendIt;
}
constexpr const const_iterator& cend() const noexcept {
return _cendIt;
}
reverse_iterator rbegin() noexcept {
const auto node = _getMaximum(_root.get());
if(node == nullptr) {
return _rendIt;
}
return reverse_iterator(node, this);
}
const_reverse_iterator rbegin() const noexcept {
const auto node = _getMaximum(_root.get());
if(node == nullptr) {
return _rendIt;
}
return const_reverse_iterator(node, this);
}
const_reverse_iterator crbegin() const noexcept {
return rbegin();
}
constexpr const reverse_iterator& rend() noexcept {
return _rendIt;
}
constexpr const const_reverse_iterator& rend() const noexcept {
return _crendIt;
}
constexpr const const_reverse_iterator& crend() const noexcept {
return _crendIt;
}
const_iterator find(const Key& key) const {
auto node = _find(key);
if(node == nullptr) {
return end();
}
return const_iterator(node, this);
}
iterator find(const Key& key) {
auto node = _find(key);
if(node == nullptr) {
return end();
}
return iterator(const_cast<value_type_pointer>(node), this);
}
bool contains(const Key& key) const {
return _find(key) != nullptr;
}
iterator operator[](const Key& key) { //untested
throw ("Not yet tested");
auto it = get(key);
if(it == nullptr) {
return insert(key, Value());
}
return it;
}
const_value_type_pointer get(const Key& key) const {
return _find(key);
}
value_type_pointer get(const Key& key) {
return _find(key);
}
iterator insert(const Key& key, Value&& value) {
auto node = _insert(key, std::move(value));
if(node == nullptr) {
return end();
}
return iterator(node, this);
}
std::pair<iterator, bool> insert( pair_type&& value) //untested
{
throw ("Not yet tested");
auto node = _insert(value.first, std::move(value.second));
if(node == nullptr) {
return {end(), false};
}
return {iterator(node, this), true};
}
template<class... Args>
std::pair<iterator, bool> emplace(Args&&... args) { //untested
throw ("Not yet tested");
return insert(pair_type(std::forward<Args>(args)...));
}
//_erase() requires the function append() which will not work on a string_view.
//in which case we use mark_erase()
bool erase(const Key& key)
{
if constexpr(IsBasicString<Key>)
return _erase(key);
else
return _mark_erase(key);
}
void print() const {
_printRecursively(' ', _root.get(), 0);
std::cout << std::endl;
}
constexpr bool empty() const noexcept{
return _size == 0;
}
//delete all items non recursively
void clear() {
if(_root != nullptr)
_root->clear();
_root = nullptr;
_size = 0;
_firstWord = nullptr;
}
private:
value_type_pointer _insert(const Key& key, Value&& value)
{
std::optional<FlashRadixTreeNodeNoOwner> rollback;
FlashRadixTreeNode* rollbackLocation = nullptr;
if (_root == nullptr) {
// If the root doesn't exist, create it.
_root = make_unique_alloc<FlashRadixTreeNode>(_nodeAllocator);
}
try
{
FlashRadixTreeNode* currentNode = _root.get();
FlashRadixTreeNode* inserted = nullptr;
Key remaining = key;
while (!remaining.empty()) {
const typename FlashRadixTreeNode::Children::iterator& it = currentNode->children.find(remaining[0]);
if( it != currentNode->children.end()) {
// Found a common prefix, split the edge if necessary
const typename Key::value_type edgeKey = it->first;
FlashRadixTreeNode* childNode = it->second.get();
const Key& edge = childNode->prefix; // Assuming first is the key in the SplayTree
// Determine the common prefix length
size_t commonPrefixLength = 0;
bool lineIsWholePrefix = false;
while (commonPrefixLength < remaining.size() && commonPrefixLength < edge.size()
&& remaining[commonPrefixLength] == edge[commonPrefixLength]) {
++commonPrefixLength;
lineIsWholePrefix = commonPrefixLength == remaining.size();
}
if (commonPrefixLength < edge.length()) {
// Split the edge
const Key& commonPrefix = edge.substr(0, commonPrefixLength);
const Key& suffixEdge = edge.substr(commonPrefixLength);
// Create a new node for the common prefix
auto newChild = make_unique_alloc<FlashRadixTreeNode>(_nodeAllocator, commonPrefix, std::move((lineIsWholePrefix ? std::forward<Value>(value) : Value())), lineIsWholePrefix, currentNode);
inserted = newChild.get();
//new node will take over location of old node in the list followed by existing split node
if(_firstWord == childNode)
_firstWord = inserted;
inserted->prev = childNode->prev;
childNode->prev = inserted;
inserted->next = childNode;
// The new node should adopt the existing child node
rollback = _copyFromOwnerNode(childNode);
rollbackLocation = childNode;
childNode->prefix = suffixEdge;
#if defined( USE_SPLAY_TREE)
newChild->children.insert(suffixEdge[0], std::move(it->second));
#else
newChild->children.emplace(suffixEdge[0], std::move(it->second));
#endif
childNode->parent = newChild.get();
// Insert the new child with the common prefix in the current node's children
auto itNewChild = currentNode->children.find(edgeKey);
itNewChild->second = std::move(newChild);
rollbackLocation = itNewChild->second.get();
currentNode = itNewChild->second.get();
} else {
// Entire edge is a common prefix, proceed with the child node
currentNode = childNode;
if(currentNode->isEndOfWord && currentNode->deleted)
{
//if the current node is deleted, then we can insert the value here
currentNode->value = std::move(value);
currentNode->deleted = false;
inserted = currentNode;
}
}
// Update the remaining part of the key to insert
remaining = remaining.substr(commonPrefixLength);
} else {
// No common prefix found, create a new edge for the remaining part of the key
auto newNode = make_unique_alloc<FlashRadixTreeNode>(_nodeAllocator, remaining, std::forward<Value>(value), currentNode);
const char newKey = remaining[0];
typename FlashRadixTreeNode::Children::iterator lowerBound;
#if defined( USE_SPLAY_TREE)
if(_firstWord)
lowerBound = currentNode->children.find_predecessor(newKey);
auto itNewNode = currentNode->children.insert(newKey, std::move(newNode));
inserted = itNewNode->second.get();
itNewNode->second->isEndOfWord = true;
#else
lowerBound = currentNode->children.lower_bound(newKey);
if(_firstWord && !currentNode->children.empty() && (lowerBound->first == newKey || lowerBound == currentNode->children.end()))
--lowerBound;
auto itNewNode = currentNode->children.emplace(newKey, std::move(newNode));
inserted = itNewNode.first->second.get();
itNewNode.first->second->isEndOfWord = true;
#endif
//update the firstword
if(_firstWord == nullptr)
_firstWord = inserted;
else if(lowerBound != currentNode->children.end())
{
auto* lowerNode = lowerBound->second.get();
if(!lowerNode->children.empty())
lowerNode = _getMaximum(lowerNode);
inserted->prev = lowerNode;
inserted->next = lowerNode->next;
lowerNode->next = inserted;
}
else if(currentNode->children.size() == 1) //no other children to link to so we link with the last node of the children above
{
auto* parent = currentNode->parent;
if(parent)
{
#if defined( USE_SPLAY_TREE)
auto* last = parent->children.getMaximum()->second.get();
#else
auto* last = parent->children.rbegin()->second.get();
#endif
last->next = inserted;
inserted->prev = last;
}
}
// As we've inserted the rest of the key, we're done
remaining = Key();
currentNode = inserted;
}
}
if(inserted == nullptr)
return nullptr;
else
{
++_size;
return inserted;
}
}
catch (const std::bad_alloc&)
{
if(rollback.has_value())
{
//override the rollback location with the rollback node
*rollbackLocation = _mergeFromNoOwnerNode(rollback.value(), rollbackLocation);
}
throw std::bad_alloc();
}
}
value_type_pointer _find(const Key& key) const
{
if constexpr( MatchMode == MatchMode::Exact)
return _findExact(key);
else
return _findPrefix(key);
}
value_type_pointer _findExact(const Key& key) const {
if (key.empty()) {
return nullptr; // An empty key cannot be found.
}
value_type_pointer currentNode = _root.get();
auto keyPrefix = key[0];
Key remaining = key;
size_t seen = 0;
while( currentNode != nullptr)
{
#if defined( USE_SPLAY_TREE)
const auto it = currentNode->children.get(keyPrefix);
if(it != nullptr)
#else
const auto it = currentNode->children.find(keyPrefix);
if(it != currentNode->children.end())
#endif
{
currentNode = it->second.get();
if(!remaining.starts_with(currentNode->prefix))
{
return nullptr;
}
else if(currentNode->isEndOfWord && remaining == currentNode->prefix)
{
if(currentNode->deleted)
return nullptr;
else
return currentNode;
}
else if( key.size() > (seen += currentNode->prefix.size()) )
{
//we look for children below looking at the next possible prefix
keyPrefix = key[seen];
remaining = key.substr(seen);
}
}
else
return nullptr; //assume doesn't exist
}
return nullptr;
}
value_type_pointer _findPrefix(const Key& key) const {
if (key.empty()) {
return nullptr; // An empty key cannot be found.
}
value_type_pointer currentNode = _root.get();
auto keyPrefix = key[0];
size_t seen = 0;
while( currentNode != nullptr)
{
#if defined( USE_SPLAY_TREE)
const auto it = currentNode->children.get(keyPrefix);
if(it != nullptr)
#else
const auto it = currentNode->children.find(keyPrefix);
if(it != currentNode->children.end())
#endif
{
currentNode = it->second.get();
const size_t remainingSize = key.size() - seen;
if(currentNode->isEndOfWord &&
(currentNode->children.empty() ||//if there are no children below this key we have our winner
currentNode->prefix.size() == remainingSize)) //if the prefix is the same size as the remaining key we can match on that also
{
if(currentNode->deleted)
return nullptr;
else
return currentNode;
}
else if( key.size() > (seen += currentNode->prefix.size()) )
{
//we look for children below looking at the next possible prefix
keyPrefix = key[seen];
}
}
else
return nullptr; //assume doesn't exist
}
return nullptr;
}
void _printRecursively(const typename Key::value_type& key, FlashRadixTreeNode* node, int level) const {
if (node == nullptr) {
return;
}
for (int i = 0; i < level; ++i) {
std::cout << " ";
}
std::cout << "K: " << key << " P: " << node->prefix << " (" << node->value << ")" << " is EOW " << (node->isEndOfWord ? "Yes" : "No") << std::endl;
#if defined( USE_SPLAY_TREE)
for(const auto& it : node->children)
{
_printRecursively(it.first, it.second.get(), level + 1);
}
#else
for (const auto& it : node->children) {
_printRecursively(it.first, it.second.get(), level + 1);
}
#endif
}
bool _mark_erase(const Key& key)
{
auto found = get(key);
if (found == nullptr) {
return false;
}
else
{
found->deleted = true;
return true;
}
}
bool _erase(const Key& key) {
if (key.empty()) {
return false; // Cannot erase an empty key
}
FlashRadixTreeNode* currentNode = _root.get();
FlashRadixTreeNode* parentNode = nullptr;
Key remainingKey = key;
// Step 1: Find the node
while (currentNode != nullptr && !remainingKey.empty()) {
parentNode = currentNode;
#if defined( USE_SPLAY_TREE)
auto it = currentNode->children.get(remainingKey[0]);
if (it == nullptr) {
#else
auto it = currentNode->children.find(remainingKey[0]);
if (it == currentNode->children.end()) {
#endif
return false; // Key not found
}
const Key& nodePrefix = it->second->prefix;
FlashRadixTreeNode* childNode = it->second.get();
if (remainingKey.starts_with(nodePrefix)) {
// Prefix matches, move to the next node
const auto nodePrefixSize = nodePrefix.size();
remainingKey = remainingKey.substr(nodePrefixSize);
currentNode = childNode;
} else {
// Prefix does not match the remaining key
return false; // Key not found
}
}
// If the end of the key has been reached and it's not marked as an end of a word, the key does not exist
if (!currentNode->isEndOfWord) {
return false;
}
// Step 2: Delete the node or unmark the end of the word
currentNode->isEndOfWord = false; // Unmark as the end of a word
// If the current node has has more than one child then we're done
if (currentNode->children.size() > 1) {
--_size;
return true;
}
// Step 3: Clean up the tree
if ((parentNode != nullptr) && (currentNode->children.size() <= 1) && !currentNode->isEndOfWord) {
// Remove the leaf node if it does not have any children
if(currentNode->children.empty())
{
//tidy up linked list of end of words
if(currentNode->prev)
currentNode->prev->next = currentNode->next;
if(currentNode->next)