-
Notifications
You must be signed in to change notification settings - Fork 113
/
htrie_hash.h
2160 lines (1808 loc) · 70.4 KB
/
htrie_hash.h
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
/**
* MIT License
*
* Copyright (c) 2017 Thibaut Goetghebuer-Planchon <tessil@gmx.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef TSL_HTRIE_HASH_H
#define TSL_HTRIE_HASH_H
#include <algorithm>
#include <array>
#include <cmath>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <iterator>
#include <limits>
#include <memory>
#include <stdexcept>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>
#include "array-hash/array_map.h"
#include "array-hash/array_set.h"
/*
* __has_include is a bit useless
* (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=79433), check also __cplusplus
* version.
*/
#ifdef __has_include
#if __has_include(<string_view>) && (__cplusplus >= 201703L || _MSVC_LANG >= 201703L)
#define TSL_HT_HAS_STRING_VIEW
#endif
#endif
#ifdef TSL_HT_HAS_STRING_VIEW
#include <string_view>
#endif
#ifdef TSL_DEBUG
#define tsl_ht_assert(expr) assert(expr)
#else
#define tsl_ht_assert(expr) (static_cast<void>(0))
#endif
namespace tsl {
namespace detail_htrie_hash {
template <typename T, typename = void>
struct is_iterator : std::false_type {};
template <typename T>
struct is_iterator<T, typename std::enable_if<!std::is_same<
typename std::iterator_traits<T>::iterator_category,
void>::value>::type> : std::true_type {};
template <typename T, typename... U>
struct is_related : std::false_type {};
template <typename T, typename U>
struct is_related<T, U>
: std::is_same<typename std::remove_cv<
typename std::remove_reference<T>::type>::type,
typename std::remove_cv<
typename std::remove_reference<U>::type>::type> {};
template <typename T, typename U>
static T numeric_cast(U value,
const char* error_message = "numeric_cast() failed.") {
T ret = static_cast<T>(value);
if (static_cast<U>(ret) != value) {
throw std::runtime_error(error_message);
}
const bool is_same_signedness =
(std::is_unsigned<T>::value && std::is_unsigned<U>::value) ||
(std::is_signed<T>::value && std::is_signed<U>::value);
if (!is_same_signedness && (ret < T{}) != (value < U{})) {
throw std::runtime_error(error_message);
}
return ret;
}
template <class T>
struct value_node {
/*
* Avoid conflict with copy constructor 'value_node(const value_node&)'. If we
* call the copy constructor with a mutable reference
* 'value_node(value_node&)', we don't want the forward constructor to be
* called.
*/
template <class... Args, typename std::enable_if<!is_related<
value_node, Args...>::value>::type* = nullptr>
value_node(Args&&... args) : m_value(std::forward<Args>(args)...) {}
T m_value;
};
template <>
struct value_node<void> {};
template <class CharT, bool HasPrefix>
struct prefix_filter {};
template <class CharT>
struct prefix_filter<CharT, true> {
prefix_filter() = default;
prefix_filter(std::basic_string<CharT> prefix_filter)
: m_prefix_filter(std::move(prefix_filter)) {}
std::basic_string<CharT> m_prefix_filter;
};
/**
* T should be void if there is no value associated to a key (in a set for
* example).
*/
template <class CharT, class T, class Hash, class KeySizeT>
class htrie_hash {
private:
template <typename U>
using has_value =
typename std::integral_constant<bool, !std::is_same<U, void>::value>;
static_assert(std::is_same<CharT, char>::value,
"char is the only supported CharT type for now.");
static const std::size_t ALPHABET_SIZE =
std::numeric_limits<typename std::make_unsigned<CharT>::type>::max() + 1;
public:
template <bool IsConst, bool IsPrefixIterator>
class htrie_hash_iterator;
using char_type = CharT;
using key_size_type = KeySizeT;
using size_type = std::size_t;
using hasher = Hash;
using iterator = htrie_hash_iterator<false, false>;
using const_iterator = htrie_hash_iterator<true, false>;
using prefix_iterator = htrie_hash_iterator<false, true>;
using const_prefix_iterator = htrie_hash_iterator<true, true>;
private:
using array_hash_type = typename std::conditional<
has_value<T>::value,
tsl::array_map<CharT, T, Hash, tsl::ah::str_equal<CharT>, false, KeySizeT,
std::uint16_t, tsl::ah::power_of_two_growth_policy<4>>,
tsl::array_set<CharT, Hash, tsl::ah::str_equal<CharT>, false, KeySizeT,
std::uint16_t,
tsl::ah::power_of_two_growth_policy<4>>>::type;
private:
/*
* The tree is mainly composed of two nodes types: trie_node and hash_node
* which both have anode as base class. Each child is either a hash_node or a
* trie_node.
*
* A hash_node is always a leaf node, it doesn't have any child.
*
* Example:
* | ... | a |.. ..................... | f | ... | trie_node_1
* \ \
* hash_node_1 |array_hash = {"dd"}| |...| a | ... | trie_node_2
* /
* |array_hash = {"ble", "bric", "lse"}| hash_node_2
*
*
* Each trie_node may also have a value node, which contains a value T, if the
* trie_node marks the end of a string value.
*
* A trie node should at least have one child or a value node. There can't be
* a trie node without any child and no value node.
*/
using value_node = tsl::detail_htrie_hash::value_node<T>;
class trie_node;
class hash_node;
// TODO better encapsulate operations modifying the tree.
class anode {
friend class trie_node;
public:
/*
* TODO Avoid the virtual to economize 8 bytes. We could use a custom
* deleter in the std::unique_ptr<anode> we use (as we know if an anode is a
* trie_node or hash_node).
*/
virtual ~anode() = default;
bool is_trie_node() const noexcept {
return m_node_type == node_type::TRIE_NODE;
}
bool is_hash_node() const noexcept {
return m_node_type == node_type::HASH_NODE;
}
trie_node& as_trie_node() noexcept {
tsl_ht_assert(is_trie_node());
return static_cast<trie_node&>(*this);
}
hash_node& as_hash_node() noexcept {
tsl_ht_assert(is_hash_node());
return static_cast<hash_node&>(*this);
}
const trie_node& as_trie_node() const noexcept {
tsl_ht_assert(is_trie_node());
return static_cast<const trie_node&>(*this);
}
const hash_node& as_hash_node() const noexcept {
tsl_ht_assert(is_hash_node());
return static_cast<const hash_node&>(*this);
}
/**
* @see m_child_of_char
*/
CharT child_of_char() const noexcept {
tsl_ht_assert(parent() != nullptr);
return m_child_of_char;
}
/**
* Return nullptr if none.
*/
trie_node* parent() noexcept { return m_parent_node; }
const trie_node* parent() const noexcept { return m_parent_node; }
protected:
enum class node_type : unsigned char { HASH_NODE, TRIE_NODE };
anode(node_type node_type_)
: m_node_type(node_type_), m_child_of_char(0), m_parent_node(nullptr) {}
anode(node_type node_type_, CharT child_of_char)
: m_node_type(node_type_),
m_child_of_char(child_of_char),
m_parent_node(nullptr) {}
protected:
node_type m_node_type;
/**
* If the node has a parent, then it's a descendant of some char.
*
* Example:
* | ... | a | b | ... | trie_node_1
* \
* |...| a | ... | trie_node_2
* /
* |array_hash| hash_node_1
*
* trie_node_2 is a child of trie_node_1 through 'b', it will have 'b' as
* m_child_of_char. hash_node_1 is a child of trie_node_2 through 'a', it
* will have 'a' as m_child_of_char.
*
* trie_node_1 has no parent, its m_child_of_char is undefined.
*/
CharT m_child_of_char;
trie_node* m_parent_node;
};
// Give the position in trie_node::m_children corresponding to the character c
static std::size_t as_position(CharT c) noexcept {
return static_cast<std::size_t>(
static_cast<typename std::make_unsigned<CharT>::type>(c));
}
class trie_node : public anode {
public:
trie_node()
: anode(anode::node_type::TRIE_NODE),
m_value_node(nullptr),
m_children() {}
trie_node(const trie_node& other)
: anode(anode::node_type::TRIE_NODE, other.m_child_of_char),
m_value_node(nullptr),
m_children() {
if (other.m_value_node != nullptr) {
m_value_node = make_unique<value_node>(*other.m_value_node);
}
// TODO avoid recursion
for (std::size_t ichild = 0; ichild < other.m_children.size(); ichild++) {
if (other.m_children[ichild] != nullptr) {
if (other.m_children[ichild]->is_hash_node()) {
m_children[ichild] = make_unique<hash_node>(
other.m_children[ichild]->as_hash_node());
} else {
m_children[ichild] = make_unique<trie_node>(
other.m_children[ichild]->as_trie_node());
}
m_children[ichild]->m_parent_node = this;
}
}
}
trie_node(trie_node&& other) = delete;
trie_node& operator=(const trie_node& other) = delete;
trie_node& operator=(trie_node&& other) = delete;
/**
* Return nullptr if none.
*/
anode* first_child() noexcept {
return const_cast<anode*>(
static_cast<const trie_node*>(this)->first_child());
}
const anode* first_child() const noexcept {
for (std::size_t ichild = 0; ichild < m_children.size(); ichild++) {
if (m_children[ichild] != nullptr) {
return m_children[ichild].get();
}
}
return nullptr;
}
/**
* Get the next_child that come after current_child. Return nullptr if no
* next child.
*/
anode* next_child(const anode& current_child) noexcept {
return const_cast<anode*>(
static_cast<const trie_node*>(this)->next_child(current_child));
}
const anode* next_child(const anode& current_child) const noexcept {
tsl_ht_assert(current_child.parent() == this);
for (std::size_t ichild = as_position(current_child.child_of_char()) + 1;
ichild < m_children.size(); ichild++) {
if (m_children[ichild] != nullptr) {
return m_children[ichild].get();
}
}
return nullptr;
}
/**
* Return the first left-descendant trie node with an m_value_node. If none
* return the most left trie node.
*/
trie_node& most_left_descendant_value_trie_node() noexcept {
return const_cast<trie_node&>(
static_cast<const trie_node*>(this)
->most_left_descendant_value_trie_node());
}
const trie_node& most_left_descendant_value_trie_node() const noexcept {
const trie_node* current_node = this;
while (true) {
if (current_node->m_value_node != nullptr) {
return *current_node;
}
const anode* first_child = current_node->first_child();
tsl_ht_assert(first_child !=
nullptr); // a trie_node must either have a value_node or
// at least one child.
if (first_child->is_hash_node()) {
return *current_node;
}
current_node = &first_child->as_trie_node();
}
}
size_type nb_children() const noexcept {
return std::count_if(
m_children.cbegin(), m_children.cend(),
[](const std::unique_ptr<anode>& n) { return n != nullptr; });
}
bool empty() const noexcept {
return std::all_of(
m_children.cbegin(), m_children.cend(),
[](const std::unique_ptr<anode>& n) { return n == nullptr; });
}
std::unique_ptr<anode>& child(CharT for_char) noexcept {
return m_children[as_position(for_char)];
}
const std::unique_ptr<anode>& child(CharT for_char) const noexcept {
return m_children[as_position(for_char)];
}
typename std::array<std::unique_ptr<anode>, ALPHABET_SIZE>::iterator
begin() noexcept {
return m_children.begin();
}
typename std::array<std::unique_ptr<anode>, ALPHABET_SIZE>::iterator
end() noexcept {
return m_children.end();
}
void set_child(CharT for_char, std::unique_ptr<anode> child) noexcept {
if (child != nullptr) {
child->m_child_of_char = for_char;
child->m_parent_node = this;
}
m_children[as_position(for_char)] = std::move(child);
}
std::unique_ptr<value_node>& val_node() noexcept { return m_value_node; }
const std::unique_ptr<value_node>& val_node() const noexcept {
return m_value_node;
}
private:
// TODO Avoid storing a value_node when has_value<T>::value is false
std::unique_ptr<value_node> m_value_node;
/**
* Each character CharT corresponds to one position in the array. To convert
* a character to a position use the as_position method.
*
* TODO Try to reduce the size of m_children with a hash map, linear/binary
* search on array, ...
* TODO Store number of non-null values in m_children. Check if we can store
* this value in the alignment space as we don't want the node to get bigger
* (empty() and nb_children() are rarely used so it is not an important
* variable).
*/
std::array<std::unique_ptr<anode>, ALPHABET_SIZE> m_children;
};
class hash_node : public anode {
public:
hash_node(const Hash& hash, float max_load_factor)
: hash_node(HASH_NODE_DEFAULT_INIT_BUCKETS_COUNT, hash,
max_load_factor) {}
hash_node(size_type bucket_count, const Hash& hash, float max_load_factor)
: anode(anode::node_type::HASH_NODE), m_array_hash(bucket_count, hash) {
m_array_hash.max_load_factor(max_load_factor);
}
hash_node(array_hash_type&& array_hash) noexcept(
std::is_nothrow_move_constructible<array_hash_type>::value)
: anode(anode::node_type::HASH_NODE),
m_array_hash(std::move(array_hash)) {}
hash_node(const hash_node& other) = default;
hash_node(hash_node&& other) = delete;
hash_node& operator=(const hash_node& other) = delete;
hash_node& operator=(hash_node&& other) = delete;
array_hash_type& array_hash() noexcept { return m_array_hash; }
const array_hash_type& array_hash() const noexcept { return m_array_hash; }
private:
array_hash_type m_array_hash;
};
public:
template <bool IsConst, bool IsPrefixIterator>
class htrie_hash_iterator : private prefix_filter<CharT, IsPrefixIterator> {
friend class htrie_hash;
private:
using anode_type =
typename std::conditional<IsConst, const anode, anode>::type;
using trie_node_type =
typename std::conditional<IsConst, const trie_node, trie_node>::type;
using hash_node_type =
typename std::conditional<IsConst, const hash_node, hash_node>::type;
using array_hash_iterator_type =
typename std::conditional<IsConst,
typename array_hash_type::const_iterator,
typename array_hash_type::iterator>::type;
public:
using iterator_category = std::forward_iterator_tag;
using value_type =
typename std::conditional<has_value<T>::value, T, void>::type;
using difference_type = std::ptrdiff_t;
using reference = typename std::conditional<
has_value<T>::value,
typename std::conditional<
IsConst, typename std::add_lvalue_reference<const T>::type,
typename std::add_lvalue_reference<T>::type>::type,
void>::type;
using pointer = typename std::conditional<
has_value<T>::value,
typename std::conditional<IsConst, const T*, T*>::type, void>::type;
private:
/**
* Start reading from start_hash_node->array_hash().begin().
*/
htrie_hash_iterator(hash_node_type& start_hash_node) noexcept
: htrie_hash_iterator(start_hash_node,
start_hash_node.array_hash().begin()) {}
/**
* Start reading from iterator begin in start_hash_node->array_hash().
*/
htrie_hash_iterator(hash_node_type& start_hash_node,
array_hash_iterator_type begin) noexcept
: m_current_trie_node(start_hash_node.parent()),
m_current_hash_node(&start_hash_node),
m_array_hash_iterator(begin),
m_array_hash_end_iterator(start_hash_node.array_hash().end()),
m_read_trie_node_value(false) {
tsl_ht_assert(!m_current_hash_node->array_hash().empty());
}
/**
* Start reading from the value in start_trie_node.
* start_trie_node->val_node() should be non-null.
*/
htrie_hash_iterator(trie_node_type& start_trie_node) noexcept
: m_current_trie_node(&start_trie_node),
m_current_hash_node(nullptr),
m_read_trie_node_value(true) {
tsl_ht_assert(m_current_trie_node->val_node() != nullptr);
}
template <bool TIsPrefixIterator = IsPrefixIterator,
typename std::enable_if<!TIsPrefixIterator>::type* = nullptr>
htrie_hash_iterator(trie_node_type* tnode, hash_node_type* hnode,
array_hash_iterator_type begin,
array_hash_iterator_type end,
bool read_trie_node_value) noexcept
: m_current_trie_node(tnode),
m_current_hash_node(hnode),
m_array_hash_iterator(begin),
m_array_hash_end_iterator(end),
m_read_trie_node_value(read_trie_node_value) {}
template <bool TIsPrefixIterator = IsPrefixIterator,
typename std::enable_if<TIsPrefixIterator>::type* = nullptr>
htrie_hash_iterator(trie_node_type* tnode, hash_node_type* hnode,
array_hash_iterator_type begin,
array_hash_iterator_type end, bool read_trie_node_value,
std::basic_string<CharT> prefix_filter_) noexcept
: prefix_filter<CharT, TIsPrefixIterator>(std::move(prefix_filter_)),
m_current_trie_node(tnode),
m_current_hash_node(hnode),
m_array_hash_iterator(begin),
m_array_hash_end_iterator(end),
m_read_trie_node_value(read_trie_node_value) {}
public:
htrie_hash_iterator() noexcept {}
// Copy constructor from iterator to const_iterator.
template <bool TIsConst = IsConst,
bool TIsPrefixIterator = IsPrefixIterator,
typename std::enable_if<TIsConst && !TIsPrefixIterator>::type* =
nullptr>
htrie_hash_iterator(
const htrie_hash_iterator<!TIsConst, TIsPrefixIterator>& other) noexcept
: m_current_trie_node(other.m_current_trie_node),
m_current_hash_node(other.m_current_hash_node),
m_array_hash_iterator(other.m_array_hash_iterator),
m_array_hash_end_iterator(other.m_array_hash_end_iterator),
m_read_trie_node_value(other.m_read_trie_node_value) {}
// Copy constructor from iterator to const_iterator.
template <
bool TIsConst = IsConst, bool TIsPrefixIterator = IsPrefixIterator,
typename std::enable_if<TIsConst && TIsPrefixIterator>::type* = nullptr>
htrie_hash_iterator(
const htrie_hash_iterator<!TIsConst, TIsPrefixIterator>& other) noexcept
: prefix_filter<CharT, TIsPrefixIterator>(other.m_prefix_filter),
m_current_trie_node(other.m_current_trie_node),
m_current_hash_node(other.m_current_hash_node),
m_array_hash_iterator(other.m_array_hash_iterator),
m_array_hash_end_iterator(other.m_array_hash_end_iterator),
m_read_trie_node_value(other.m_read_trie_node_value) {}
htrie_hash_iterator(const htrie_hash_iterator& other) = default;
htrie_hash_iterator(htrie_hash_iterator&& other) = default;
htrie_hash_iterator& operator=(const htrie_hash_iterator& other) = default;
htrie_hash_iterator& operator=(htrie_hash_iterator&& other) = default;
void key(std::basic_string<CharT>& key_buffer_out) const {
key_buffer_out.clear();
trie_node_type* tnode = m_current_trie_node;
while (tnode != nullptr && tnode->parent() != nullptr) {
key_buffer_out.push_back(tnode->child_of_char());
tnode = tnode->parent();
}
std::reverse(key_buffer_out.begin(), key_buffer_out.end());
if (!m_read_trie_node_value) {
tsl_ht_assert(m_current_hash_node != nullptr);
if (m_current_hash_node->parent() != nullptr) {
key_buffer_out.push_back(m_current_hash_node->child_of_char());
}
key_buffer_out.append(m_array_hash_iterator.key(),
m_array_hash_iterator.key_size());
}
}
std::basic_string<CharT> key() const {
std::basic_string<CharT> key_buffer;
key(key_buffer);
return key_buffer;
}
template <class U = T,
typename std::enable_if<has_value<U>::value>::type* = nullptr>
reference value() const {
if (this->m_read_trie_node_value) {
tsl_ht_assert(this->m_current_trie_node != nullptr);
tsl_ht_assert(this->m_current_trie_node->val_node() != nullptr);
return this->m_current_trie_node->val_node()->m_value;
} else {
return this->m_array_hash_iterator.value();
}
}
template <class U = T,
typename std::enable_if<has_value<U>::value>::type* = nullptr>
reference operator*() const {
return value();
}
template <class U = T,
typename std::enable_if<has_value<U>::value>::type* = nullptr>
pointer operator->() const {
return std::addressof(value());
}
htrie_hash_iterator& operator++() {
if (m_read_trie_node_value) {
tsl_ht_assert(m_current_trie_node != nullptr);
m_read_trie_node_value = false;
anode_type* child = m_current_trie_node->first_child();
if (child != nullptr) {
set_most_left_descendant_as_next_node(*child);
} else if (m_current_trie_node->parent() != nullptr) {
trie_node_type* current_node_child = m_current_trie_node;
m_current_trie_node = m_current_trie_node->parent();
set_next_node_ascending(*current_node_child);
} else {
set_as_end_iterator();
}
} else {
++m_array_hash_iterator;
if (m_array_hash_iterator != m_array_hash_end_iterator) {
filter_prefix();
}
// End of the road, set the iterator as an end node.
else if (m_current_trie_node == nullptr) {
set_as_end_iterator();
} else {
tsl_ht_assert(m_current_hash_node != nullptr);
set_next_node_ascending(*m_current_hash_node);
}
}
return *this;
}
htrie_hash_iterator operator++(int) {
htrie_hash_iterator tmp(*this);
++*this;
return tmp;
}
friend bool operator==(const htrie_hash_iterator& lhs,
const htrie_hash_iterator& rhs) {
if (lhs.m_current_trie_node != rhs.m_current_trie_node ||
lhs.m_read_trie_node_value != rhs.m_read_trie_node_value) {
return false;
} else if (lhs.m_read_trie_node_value) {
return true;
} else {
if (lhs.m_current_hash_node != rhs.m_current_hash_node) {
return false;
} else if (lhs.m_current_hash_node == nullptr) {
return true;
} else {
return lhs.m_array_hash_iterator == rhs.m_array_hash_iterator &&
lhs.m_array_hash_end_iterator == rhs.m_array_hash_end_iterator;
}
}
}
friend bool operator!=(const htrie_hash_iterator& lhs,
const htrie_hash_iterator& rhs) {
return !(lhs == rhs);
}
private:
void hash_node_prefix(std::basic_string<CharT>& key_buffer_out) const {
tsl_ht_assert(!m_read_trie_node_value);
key_buffer_out.clear();
trie_node_type* tnode = m_current_trie_node;
while (tnode != nullptr && tnode->parent() != nullptr) {
key_buffer_out.push_back(tnode->child_of_char());
tnode = tnode->parent();
}
std::reverse(key_buffer_out.begin(), key_buffer_out.end());
tsl_ht_assert(m_current_hash_node != nullptr);
if (m_current_hash_node->parent() != nullptr) {
key_buffer_out.push_back(m_current_hash_node->child_of_char());
}
}
template <bool TIsPrefixIterator = IsPrefixIterator,
typename std::enable_if<!TIsPrefixIterator>::type* = nullptr>
void filter_prefix() {}
template <bool TIsPrefixIterator = IsPrefixIterator,
typename std::enable_if<TIsPrefixIterator>::type* = nullptr>
void filter_prefix() {
tsl_ht_assert(m_array_hash_iterator != m_array_hash_end_iterator);
tsl_ht_assert(!m_read_trie_node_value && m_current_hash_node != nullptr);
if (this->m_prefix_filter.empty()) {
return;
}
while ((this->m_prefix_filter.size() > m_array_hash_iterator.key_size() ||
this->m_prefix_filter.compare(
0, this->m_prefix_filter.size(), m_array_hash_iterator.key(),
this->m_prefix_filter.size()) != 0)) {
++m_array_hash_iterator;
if (m_array_hash_iterator == m_array_hash_end_iterator) {
if (m_current_trie_node == nullptr) {
set_as_end_iterator();
} else {
tsl_ht_assert(m_current_hash_node != nullptr);
set_next_node_ascending(*m_current_hash_node);
}
return;
}
}
}
/**
* Go back up in the tree to get the current_trie_node_child sibling.
* If none, try to go back up more in the tree to check the siblings of the
* ancestors.
*/
void set_next_node_ascending(anode_type& current_trie_node_child) {
tsl_ht_assert(m_current_trie_node != nullptr);
tsl_ht_assert(current_trie_node_child.parent() == m_current_trie_node);
anode_type* next_node =
m_current_trie_node->next_child(current_trie_node_child);
while (next_node == nullptr && m_current_trie_node->parent() != nullptr) {
anode_type* current_child = m_current_trie_node;
m_current_trie_node = m_current_trie_node->parent();
next_node = m_current_trie_node->next_child(*current_child);
}
// End of the road, set the iterator as an end node.
if (next_node == nullptr) {
set_as_end_iterator();
} else {
set_most_left_descendant_as_next_node(*next_node);
}
}
void set_most_left_descendant_as_next_node(anode_type& search_start) {
if (search_start.is_hash_node()) {
set_current_hash_node(search_start.as_hash_node());
} else {
m_current_trie_node =
&search_start.as_trie_node().most_left_descendant_value_trie_node();
if (m_current_trie_node->val_node() != nullptr) {
m_read_trie_node_value = true;
} else {
anode_type* first_child = m_current_trie_node->first_child();
// a trie_node must either have a value_node or at least one child.
tsl_ht_assert(first_child != nullptr);
set_current_hash_node(first_child->as_hash_node());
}
}
}
void set_current_hash_node(hash_node_type& hnode) {
tsl_ht_assert(!hnode.array_hash().empty());
m_current_hash_node = &hnode;
m_array_hash_iterator = m_current_hash_node->array_hash().begin();
m_array_hash_end_iterator = m_current_hash_node->array_hash().end();
}
void set_as_end_iterator() {
m_current_trie_node = nullptr;
m_current_hash_node = nullptr;
m_read_trie_node_value = false;
}
void skip_hash_node() {
tsl_ht_assert(!m_read_trie_node_value && m_current_hash_node != nullptr);
if (m_current_trie_node == nullptr) {
set_as_end_iterator();
} else {
tsl_ht_assert(m_current_hash_node != nullptr);
set_next_node_ascending(*m_current_hash_node);
}
}
private:
trie_node_type* m_current_trie_node;
hash_node_type* m_current_hash_node;
array_hash_iterator_type m_array_hash_iterator;
array_hash_iterator_type m_array_hash_end_iterator;
bool m_read_trie_node_value;
};
public:
htrie_hash(const Hash& hash, float max_load_factor, size_type burst_threshold)
: m_root(nullptr),
m_nb_elements(0),
m_hash(hash),
m_max_load_factor(max_load_factor) {
this->burst_threshold(burst_threshold);
}
htrie_hash(const htrie_hash& other)
: m_root(nullptr),
m_nb_elements(other.m_nb_elements),
m_hash(other.m_hash),
m_max_load_factor(other.m_max_load_factor),
m_burst_threshold(other.m_burst_threshold) {
if (other.m_root != nullptr) {
if (other.m_root->is_hash_node()) {
m_root = make_unique<hash_node>(other.m_root->as_hash_node());
} else {
m_root = make_unique<trie_node>(other.m_root->as_trie_node());
}
}
}
htrie_hash(htrie_hash&& other) noexcept(
std::is_nothrow_move_constructible<Hash>::value)
: m_root(std::move(other.m_root)),
m_nb_elements(other.m_nb_elements),
m_hash(std::move(other.m_hash)),
m_max_load_factor(other.m_max_load_factor),
m_burst_threshold(other.m_burst_threshold) {
other.clear();
}
htrie_hash& operator=(const htrie_hash& other) {
if (&other != this) {
std::unique_ptr<anode> new_root = nullptr;
if (other.m_root != nullptr) {
if (other.m_root->is_hash_node()) {
new_root = make_unique<hash_node>(other.m_root->as_hash_node());
} else {
new_root = make_unique<trie_node>(other.m_root->as_trie_node());
}
}
m_hash = other.m_hash;
m_root = std::move(new_root);
m_nb_elements = other.m_nb_elements;
m_max_load_factor = other.m_max_load_factor;
m_burst_threshold = other.m_burst_threshold;
}
return *this;
}
htrie_hash& operator=(htrie_hash&& other) {
other.swap(*this);
other.clear();
return *this;
}
/*
* Iterators
*/
iterator begin() noexcept { return mutable_iterator(cbegin()); }
const_iterator begin() const noexcept { return cbegin(); }
const_iterator cbegin() const noexcept {
if (empty()) {
return cend();
}
return cbegin<const_iterator>(*m_root);
}
iterator end() noexcept {
iterator it;
it.set_as_end_iterator();
return it;
}
const_iterator end() const noexcept { return cend(); }
const_iterator cend() const noexcept {
const_iterator it;
it.set_as_end_iterator();
return it;
}
/*
* Capacity
*/
bool empty() const noexcept { return m_nb_elements == 0; }
size_type size() const noexcept { return m_nb_elements; }
size_type max_size() const noexcept {
return std::numeric_limits<size_type>::max();
}
size_type max_key_size() const noexcept {
return array_hash_type::MAX_KEY_SIZE;
}
void shrink_to_fit() {
auto first = begin();
auto last = end();
while (first != last) {
if (first.m_read_trie_node_value) {
++first;
} else {
/*
* shrink_to_fit on array_hash will invalidate the iterators of
* array_hash. Save pointer to array_hash, skip the array_hash_node and
* then call shrink_to_fit on the saved pointer.
*/
hash_node* hnode = first.m_current_hash_node;
first.skip_hash_node();
tsl_ht_assert(hnode != nullptr);
hnode->array_hash().shrink_to_fit();
}
}
}
/*
* Modifiers
*/
void clear() noexcept {
m_root.reset(nullptr);