-
Notifications
You must be signed in to change notification settings - Fork 0
/
trie.cpp
1152 lines (1034 loc) · 34.7 KB
/
trie.cpp
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
// By zhenglingyun ([email protected]), 2014-07-20. All rights reserved.
// Alghouth this is written in C++, I prefer C implementation because C is tiny,
// clear, and easy to debug. I use C++ mechanism only when it is hard to be
// implemented in C. So, this code is easy to be changed to C implementation.
#include "trie.h"
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <stdexcept>
#include <vector>
// INC_BASE_WHEN_COLLISION is used to indicate increasing base value when two
// nodes collision happened, it will be faster (about 2x faster in my test).
// But it will probably use a little more memory I guess.
#define INC_BASE_WHEN_COLLISION
// START_BASE_OPTIMIZATION is used to optimize finding base value.
// 10x faster then no any optimization but 2x memory occupation in my test.
#define START_BASE_OPTIMIZATION
// BTW, how to find base value is significant in Double-Array Trie algorithm.
#define EXPECT(c, v) __builtin_expect(c, v)
// If __builtin_expect() is not supported by your compiler:
//#define EXPECT(c, v) (c)
#define PRE_ALLOCED_WORDS 64 // used to store words prefix returned to user call
#if PRE_ALLOCED_WORDS <= 0
#error PRE_ALLOCED_WORDS should be greater then 0
#endif
#define ROOT 1
#if ROOT <= 0
#error ROOT should be greater then 0
#endif
#define BASE (ROOT + 1)
#if BASE <= ROOT
#error BASE should be greater then ROOT
#endif
#define NEXT_INDEX(base, word) ((base) + (word))
#define NEXT_NODE(base, word) (nodes[NEXT_INDEX(base, word)])
#define INVARIANT_VIOLATION \
"Invariant violation: every words should be distinguishable."
using namespace std;
struct TrieNode {
Index base;
Index prev;
};
struct _Tail {
Word *words;
Index n_words;
Index data;
Index used_by;
};
class _Trie {
struct TrieNode *nodes;
Index n_alloced_nodes;
#ifdef START_BASE_OPTIMIZATION
Index next_unused_node_idx;
#endif
struct _Tail *tails;
Index n_alloced_tails;
Index next_unused_tail_idx;
void expand_nodes(Index next);
#ifdef START_BASE_OPTIMIZATION
void inc_next_unused_node_idx(Index idx);
void dec_next_unused_node_idx(Index idx);
#endif
Index get_next_unused_tail_idx(void);
void set_next_unused_tail_idx(Index idx);
void fill_tail(Index tail_idx, const Word words[], Index n_words,
Index data, Index tn_idx);
void adjust(Index tn_idx, const vector<Word>& subs);
void adjust(Index tn_idx, const vector<Word>& subs, const Word& word);
void move(Index tn_idx, const vector<Word>& subs, Index offset);
void collect_sub_nodes(Index tn_idx, vector<Word>& subs) const;
void erase_all_subs(Index tn_idx);
void collect_all_subs(Index tn_idx, vector<Index>& results) const;
void collect_all_subs(Index tn_idx, struct Tail *result,
Index n_alloced_words, vector<Tail>& results) const;
bool search(const Word words[], Index n_words,
struct _Tail **tailp, Index *data, Index *unmatch) const;
bool search(const Word words[], Index n_words, struct _Tail **tailp) const;
public:
_Trie(void);
~_Trie(void);
void insert(const Word words[], Index n_words, Index data);
void erase(const Word words[], Index n_words);
bool search(const Word words[], Index n_words,
Index *data, Index *unmatch) const;
void prefix(const Word words[], Index n_words, vector<Tail>& results) const;
void prefix(const Word words[], Index n_words, vector<Index>& results) const;
bool segment_max_match(const Word words[], Index n_words, Word end_word,
Index *data, Index *unmatch) const;
bool segment_min_match(const Word words[], Index n_words, Word end_word,
Index *data, Index *unmatch) const;
};
_Trie::_Trie(void)
{
n_alloced_nodes = ROOT + 1;
nodes = (struct TrieNode *)calloc(n_alloced_nodes, sizeof *nodes);
if (!nodes) {
throw bad_alloc();
}
nodes[ROOT].base = BASE;
#ifdef START_BASE_OPTIMIZATION
// +1 is not needed but I want to leave the first Word space for the first
// Word. (nodes[ROOT].base == BASE forever)
next_unused_node_idx = BASE + (Word)(~0ULL) + 1;
#endif
n_alloced_tails = 1;
tails = (struct _Tail *)calloc(n_alloced_tails, sizeof (*tails));
if (!tails) {
free(nodes);
throw bad_alloc();
}
next_unused_tail_idx = 0;
}
_Trie::~_Trie(void)
{
free(nodes);
for (Index i = 0; i < n_alloced_tails; i++) {
free(tails[i].words);
}
free(tails);
}
void _Trie::expand_nodes(Index next)
{
assert(next >= n_alloced_nodes);
Index old_n_alloced_nodes = n_alloced_nodes;
struct TrieNode *old_nodes = nodes;
n_alloced_nodes = next >= (n_alloced_nodes << 1) ? next + 1
: (n_alloced_nodes << 1);
nodes = (struct TrieNode *)realloc(nodes, n_alloced_nodes * sizeof *nodes);
if (!nodes) {
n_alloced_nodes = old_n_alloced_nodes;
nodes = old_nodes;
throw bad_alloc();
}
memset(nodes + old_n_alloced_nodes, 0,
(n_alloced_nodes - old_n_alloced_nodes) * sizeof *nodes);
}
#ifdef START_BASE_OPTIMIZATION
void _Trie::inc_next_unused_node_idx(Index idx)
{
if (idx == next_unused_node_idx) {
while (++next_unused_node_idx < n_alloced_nodes) {
if (!nodes[next_unused_node_idx].prev) {
break;
}
}
}
}
void _Trie::dec_next_unused_node_idx(Index idx)
{
if (idx > BASE + (Word)(~0ULL) && idx < next_unused_node_idx) {
next_unused_node_idx = idx;
}
}
#endif
Index _Trie::get_next_unused_tail_idx(void)
{
assert(next_unused_tail_idx <= n_alloced_tails);
if (next_unused_tail_idx == n_alloced_tails) {
Index old_n_alloced_tails = n_alloced_tails;
struct _Tail *old_tails = tails;
assert(n_alloced_tails);
n_alloced_tails = n_alloced_tails << 1;
tails = (struct _Tail *)realloc(tails, n_alloced_tails * sizeof *tails);
if (!tails) {
n_alloced_tails = old_n_alloced_tails;
tails = old_tails;
throw bad_alloc();
}
memset(tails + old_n_alloced_tails, 0,
(n_alloced_tails - old_n_alloced_tails) * sizeof *tails);
return next_unused_tail_idx++;
}
Index result = next_unused_tail_idx;
while (++next_unused_tail_idx < n_alloced_tails) {
if (!tails[next_unused_tail_idx].used_by) {
break;
}
}
return result;
}
void _Trie::set_next_unused_tail_idx(Index idx)
{
if (idx < next_unused_tail_idx) {
next_unused_tail_idx = idx;
}
}
void _Trie::fill_tail(Index tail_idx, const Word words[], Index n_words,
Index data, Index tn_idx)
{
struct _Tail *tail = tails + tail_idx;
assert(!tail->words);
tail->n_words = n_words;
if (n_words) {
tail->words = (Word *)malloc(n_words * sizeof *tail->words);
if (!tail->words) {
throw bad_alloc();
}
memcpy(tail->words, words, n_words * sizeof *tail->words);
} else {
tail->words = NULL;
}
tail->data = data;
tail->used_by = tn_idx;
}
void _Trie::insert(const Word words[], Index n_words, Index data)
{
struct _Tail *tail;
if (search(words, n_words, &tail)) {
tail->data = data;
return;
}
Index tn_idx = ROOT;
for (Index i = 0; i < n_words; i++) {
Index base = nodes[tn_idx].base;
Index next;
if (base >= BASE) {
next = NEXT_INDEX(base, words[i]);
if (EXPECT(next >= n_alloced_nodes, 0)) {
expand_nodes(next);
} else {
Index next_prev = nodes[next].prev;
if (next_prev == tn_idx) {
tn_idx = next;
continue;
}
if (next_prev) {
assert(next_prev >= ROOT);
// next node collision
vector<Word> sub1, sub2;
collect_sub_nodes(next_prev, sub1);
collect_sub_nodes(tn_idx, sub2);
if (sub1.size() <= sub2.size()) {
bool tn_idx_changed = false;
// If tn_idx is a sub-node of next_prev,
// tn_idx will be changed after adjusting sub1.
if (nodes[tn_idx].prev == next_prev) {
tn_idx_changed = true;
}
adjust(nodes[next].prev, sub1);
if (tn_idx_changed) {
assert(i);
tn_idx = NEXT_INDEX(nodes[next_prev].base,
words[i - 1]);
}
} else {
adjust(tn_idx, sub2, words[i]);
// nodes[tn_idx].base is changed after adjusting sub2
next = NEXT_INDEX(nodes[tn_idx].base, words[i]);
if (EXPECT(next >= n_alloced_nodes, 0)) {
expand_nodes(next);
}
}
}
}
assert(!nodes[next].prev);
nodes[next].prev = tn_idx;
Index tail_idx = get_next_unused_tail_idx();
fill_tail(tail_idx, words + i + 1, n_words - i - 1, data, next);
nodes[next].base = -tail_idx;
#ifdef START_BASE_OPTIMIZATION
inc_next_unused_node_idx(next);
#endif
return;
} else {
assert(base <= 0);
assert(-base < n_alloced_tails);
struct _Tail *tail = tails - base;
Index j = 0;
while (j < tail->n_words && i < n_words) {
if (tail->words[j] != words[i]) {
break;
}
for (
#ifdef START_BASE_OPTIMIZATION
next = next_unused_node_idx;
#else
next = NEXT_INDEX(BASE, words[i]);
#endif
EXPECT(next < n_alloced_nodes, 1);
next++) {
if (!nodes[next].prev) {
break;
}
}
if (EXPECT(next >= n_alloced_nodes, 0)) {
expand_nodes(next);
}
nodes[tn_idx].base = next - words[i];
assert(!nodes[next].prev);
nodes[next].prev = tn_idx;
tn_idx = next;
#ifdef START_BASE_OPTIMIZATION
inc_next_unused_node_idx(next);
#endif
j++;
i++;
}
if (j == tail->n_words || i == n_words) {
assert(!(j == tail->n_words && i == n_words));
// Recovery.
nodes[tn_idx].base = (Index)-(tail - tails);
tail->used_by = tn_idx;
tail->n_words -= j;
if (tail->n_words) {
memmove(tail->words, tail->words + j,
tail->n_words * sizeof *tail->words);
tail->words = (Word *)realloc(tail->words,
tail->n_words * sizeof *tail->words);
// We consider this realloc() should always succeed.
assert(tail->words);
} else {
free(tail->words);
tail->words = NULL;
}
throw invalid_argument(INVARIANT_VIOLATION);
}
for (
#ifdef START_BASE_OPTIMIZATION
base = next_unused_node_idx -
(tail->words[j] < words[i] ? tail->words[j] : words[i]);
#else
base = BASE;
#endif
(EXPECT(NEXT_INDEX(base, tail->words[j]) < n_alloced_nodes, 1) &&
NEXT_NODE(base, tail->words[j]).prev) ||
(EXPECT(NEXT_INDEX(base, words[i]) < n_alloced_nodes, 1) &&
NEXT_NODE(base, words[i]).prev);
base++);
if (EXPECT(NEXT_INDEX(base, tail->words[j]) >= n_alloced_nodes, 0)) {
expand_nodes(NEXT_INDEX(base, tail->words[j]));
}
if (EXPECT(NEXT_INDEX(base, words[i]) >= n_alloced_nodes, 0)) {
expand_nodes(NEXT_INDEX(base, words[i]));
}
nodes[tn_idx].base = base;
next = NEXT_INDEX(base, tail->words[j]);
assert(!nodes[next].prev);
nodes[next].prev = tn_idx;
nodes[next].base = (Index)-(tail - tails);
tail->used_by = next;
tail->n_words -= j + 1;
if (tail->n_words) {
memmove(tail->words, tail->words + j + 1,
tail->n_words * sizeof *tail->words);
tail->words = (Word *)realloc(tail->words,
tail->n_words * sizeof *tail->words);
// We consider this realloc() should always succeed.
assert(tail->words);
} else {
free(tail->words);
tail->words = NULL;
}
#ifdef START_BASE_OPTIMIZATION
inc_next_unused_node_idx(next);
#endif
next = NEXT_INDEX(base, words[i]);
assert(!nodes[next].prev);
nodes[next].prev = tn_idx;
Index tail_idx = get_next_unused_tail_idx();
nodes[next].base = -tail_idx;
fill_tail(tail_idx, words + i + 1, n_words - i - 1, data, next);
#ifdef START_BASE_OPTIMIZATION
inc_next_unused_node_idx(next);
#endif
return;
}
}
throw invalid_argument(INVARIANT_VIOLATION);
}
void _Trie::adjust(Index tn_idx, const vector<Word>& subs)
{
Index subs_size = (Index)subs.size();
assert(subs_size);
#ifdef INC_BASE_WHEN_COLLISION
Index base = nodes[tn_idx].base + 1;
#elif defined(START_BASE_OPTIMIZATION)
Index base = next_unused_node_idx;
#else
Index base = BASE;
#endif
for (Index i = 0; i < subs_size;) {
Index next = NEXT_INDEX(base, subs[i]);
if (EXPECT(next < n_alloced_nodes, 1) && nodes[next].prev) {
base++;
i = 0;
} else {
i++;
}
}
move(tn_idx, subs, base - nodes[tn_idx].base);
}
void _Trie::adjust(Index tn_idx, const vector<Word>& subs, const Word& word)
{
#ifdef INC_BASE_WHEN_COLLISION
Index base = nodes[tn_idx].base + 1;
#elif defined(START_BASE_OPTIMIZATION)
Index base = next_unused_node_idx;
#else
Index base = BASE;
#endif
while (1) {
Index next = NEXT_INDEX(base, word);
while (EXPECT(next < n_alloced_nodes, 1) && nodes[next].prev) {
base++;
next++;
}
Index i;
Index subs_size = (Index)subs.size();
for (i = 0; i < subs_size; i++) {
next = NEXT_INDEX(base, subs[i]);
if (EXPECT(next < n_alloced_nodes, 1) && nodes[next].prev) {
base++;
break;
}
}
if (i == subs_size) {
break;
}
}
move(tn_idx, subs, base - nodes[tn_idx].base);
}
void _Trie::move(Index tn_idx, const vector<Word>& subs, Index offset)
{
assert(offset);
Index subs_size = (Index)subs.size();
for (Index i = 0; i < subs_size; i++) {
Index base = NEXT_NODE(nodes[tn_idx].base, subs[i]).base;
vector<Word> sub_subs;
collect_sub_nodes(NEXT_INDEX(nodes[tn_idx].base, subs[i]), sub_subs);
Index sub_subs_size = (Index)sub_subs.size();
for (Index j = 0; j < sub_subs_size; j++) {
nodes[base + sub_subs[j]].prev += offset;
}
Index next = NEXT_INDEX(nodes[tn_idx].base + offset, subs[i]);
if (EXPECT(next >= n_alloced_nodes, 0)) {
expand_nodes(next);
}
nodes[next] = nodes[next - offset];
if (nodes[next].base <= 0) {
assert(-nodes[next].base < n_alloced_tails);
assert(tails[-nodes[next].base].used_by == next - offset);
tails[-nodes[next].base].used_by = next;
}
nodes[next - offset].base = 0;
nodes[next - offset].prev = 0;
#ifdef START_BASE_OPTIMIZATION
dec_next_unused_node_idx(next - offset);
inc_next_unused_node_idx(next);
#endif
}
nodes[tn_idx].base += offset;
}
void _Trie::collect_sub_nodes(Index tn_idx, vector<Word>& subs) const
{
if (nodes[tn_idx].base >= BASE) {
for (Index i = 0; i <= (Word)(~0ULL); i++) {
Index next = NEXT_INDEX(nodes[tn_idx].base, i);
if (EXPECT(next < n_alloced_nodes, 1) && nodes[next].prev == tn_idx) {
subs.push_back((Word)i);
}
}
}
}
void _Trie::erase(const Word words[], Index n_words)
{
Index tn_idx = ROOT;
for (Index i = 0; i < n_words; i++) {
Index base = nodes[tn_idx].base;
if (base >= BASE) {
Index next = NEXT_INDEX(base, words[i]);
if (EXPECT(next >= n_alloced_nodes, 0) || nodes[next].prev != tn_idx) {
return;
}
tn_idx = next;
} else {
assert(base <= 0);
assert(-base < n_alloced_tails);
struct _Tail *tail = tails - base;
Index j = 0;
while (j < tail->n_words && i < n_words) {
if (tail->words[j] != words[i]) {
break;
}
j++;
i++;
}
if (i < n_words) {
return;
}
free(tail->words);
memset(tail, 0, sizeof *tail);
nodes[tn_idx].base = 0;
nodes[tn_idx].prev = 0;
set_next_unused_tail_idx(-base);
#ifdef START_BASE_OPTIMIZATION
dec_next_unused_node_idx(tn_idx);
#endif
return;
}
}
Index base = nodes[tn_idx].base;
if (base <= 0) {
assert(-base < n_alloced_tails);
struct _Tail *tail = tails - base;
free(tail->words);
memset(tail, 0, sizeof *tail);
nodes[tn_idx].base = 0;
nodes[tn_idx].prev = 0;
set_next_unused_tail_idx(-base);
#ifdef START_BASE_OPTIMIZATION
dec_next_unused_node_idx(tn_idx);
#endif
return;
}
erase_all_subs(tn_idx);
if (tn_idx != ROOT) {
nodes[tn_idx].base = 0;
nodes[tn_idx].prev = 0;
#ifdef START_BASE_OPTIMIZATION
dec_next_unused_node_idx(tn_idx);
#endif
}
}
void _Trie::erase_all_subs(Index tn_idx)
{
vector<Word> subs;
collect_sub_nodes(tn_idx, subs);
Index subs_size = (Index)subs.size();
for (Index i = 0; i < subs_size; i++) {
Index next = NEXT_INDEX(nodes[tn_idx].base, subs[i]);
Index base = nodes[next].base;
if (base >= BASE) {
erase_all_subs(next);
} else {
assert(base <= 0);
assert(-base < n_alloced_tails);
struct _Tail *tail = tails - base;
free(tail->words);
memset(tail, 0, sizeof *tail);
set_next_unused_tail_idx(-base);
}
nodes[next].base = 0;
nodes[next].prev = 0;
#ifdef START_BASE_OPTIMIZATION
dec_next_unused_node_idx(next);
#endif
}
}
bool _Trie::search(const Word words[], Index n_words,
struct _Tail **tailp) const
{
return search(words, n_words, tailp, NULL, NULL);
}
bool _Trie::search(const Word words[], Index n_words,
Index *data, Index *unmatch) const
{
return search(words, n_words, NULL, data, unmatch);
}
bool _Trie::search(const Word words[], Index n_words,
struct _Tail **tailp, Index *data, Index *unmatch) const
{
Index tn_idx = ROOT;
for (Index i = 0; i < n_words; i++) {
Index base = nodes[tn_idx].base;
if (base >= BASE) {
Index next = NEXT_INDEX(base, words[i]);
if (EXPECT(next >= n_alloced_nodes, 0) || nodes[next].prev != tn_idx) {
if (unmatch) {
*unmatch = i;
}
return false;
}
tn_idx = next;
} else {
assert(base <= 0);
assert(-base < n_alloced_tails);
struct _Tail *tail = tails - base;
Index j = 0;
while (j < tail->n_words && i < n_words) {
if (tail->words[j] != words[i]) {
break;
}
j++;
i++;
}
if (j == tail->n_words && i == n_words) {
if (tailp) {
*tailp = tail;
}
if (data) {
*data = tail->data;
}
return true;
}
if (unmatch) {
*unmatch = i;
}
return false;
}
}
Index base = nodes[tn_idx].base;
if (base >= BASE) {
if (unmatch) {
*unmatch = n_words;
}
return false;
} else {
assert(base <= 0);
assert(-base < n_alloced_tails);
struct _Tail *tail = tails - base;
if (tail->n_words) {
if (unmatch) {
*unmatch = n_words;
}
return false;
} else {
if (tailp) {
*tailp = tail;
}
if (data) {
*data = tail->data;
}
return true;
}
}
}
bool _Trie::segment_max_match(const Word words[], Index n_words, Word end_word,
Index *data, Index *unmatch) const
{
bool find_one = false;
Index tn_idx = ROOT;
for (Index i = 0; i < n_words; i++) {
Index base = nodes[tn_idx].base;
if (base >= BASE) {
Index end = NEXT_INDEX(base, end_word);
if (EXPECT(end < n_alloced_nodes, 1) && nodes[end].prev == tn_idx) {
Index end_base = nodes[end].base;
if (EXPECT(end_base <= 0, 1)) {
assert(-end_base < n_alloced_tails);
struct _Tail *tail = tails - end_base;
if (EXPECT(!tail->n_words, 1)) {
if (data) {
*data = tail->data;
}
if (unmatch) {
*unmatch = i;
}
find_one = true;
}
}
}
Index next = NEXT_INDEX(base, words[i]);
if (EXPECT(next >= n_alloced_nodes, 0) || nodes[next].prev != tn_idx) {
return find_one;
}
tn_idx = next;
} else {
assert(base <= 0);
assert(-base < n_alloced_tails);
struct _Tail *tail = tails - base;
Index j = 0;
if (!tail->n_words || tail->words[tail->n_words - 1] != end_word) {
return find_one;
}
while (j < tail->n_words - 1 && i < n_words) {
if (tail->words[j] != words[i]) {
break;
}
j++;
i++;
}
if (j == tail->n_words - 1) {
if (data) {
*data = tail->data;
}
if (unmatch) {
*unmatch = i;
}
return true;
}
return find_one;
}
}
Index base = nodes[tn_idx].base;
if (base >= BASE) {
Index end = NEXT_INDEX(base, end_word);
if (EXPECT(end < n_alloced_nodes, 1) && nodes[end].prev == tn_idx) {
Index end_base = nodes[end].base;
if (EXPECT(end_base <= 0, 1)) {
assert(-end_base < n_alloced_tails);
struct _Tail *tail = tails - end_base;
if (EXPECT(!tail->n_words, 1)) {
if (data) {
*data = tail->data;
}
if (unmatch) {
*unmatch = n_words;
}
return true;
}
}
}
} else {
assert(base <= 0);
assert(-base < n_alloced_tails);
struct _Tail *tail = tails - base;
if (tail->n_words == 1 && tail->words[0] == end_word) {
if (data) {
*data = tail->data;
}
if (unmatch) {
*unmatch = n_words;
}
return true;
}
}
return find_one;
}
bool _Trie::segment_min_match(const Word words[], Index n_words, Word end_word,
Index *data, Index *unmatch) const
{
Index tn_idx = ROOT;
for (Index i = 0; i < n_words; i++) {
Index base = nodes[tn_idx].base;
if (base >= BASE) {
Index end = NEXT_INDEX(base, end_word);
if (EXPECT(end < n_alloced_nodes, 1) && nodes[end].prev == tn_idx) {
Index end_base = nodes[end].base;
if (EXPECT(end_base <= 0, 1)) {
assert(-end_base < n_alloced_tails);
struct _Tail *tail = tails - end_base;
if (EXPECT(!tail->n_words, 1)) {
if (data) {
*data = tail->data;
}
if (unmatch) {
*unmatch = i;
}
return true;
}
}
}
Index next = NEXT_INDEX(base, words[i]);
if (EXPECT(next >= n_alloced_nodes, 0) || nodes[next].prev != tn_idx) {
return false;
}
tn_idx = next;
} else {
assert(base <= 0);
assert(-base < n_alloced_tails);
struct _Tail *tail = tails - base;
Index j = 0;
if (!tail->n_words || tail->words[tail->n_words - 1] != end_word) {
return false;
}
while (j < tail->n_words - 1 && i < n_words) {
if (tail->words[j] != words[i]) {
break;
}
j++;
i++;
}
if (j == tail->n_words - 1) {
if (data) {
*data = tail->data;
}
if (unmatch) {
*unmatch = i;
}
return true;
}
return false;
}
}
Index base = nodes[tn_idx].base;
if (base >= BASE) {
Index end = NEXT_INDEX(base, end_word);
if (EXPECT(end < n_alloced_nodes, 1) && nodes[end].prev == tn_idx) {
Index end_base = nodes[end].base;
if (EXPECT(end_base <= 0, 1)) {
assert(-end_base < n_alloced_tails);
struct _Tail *tail = tails - end_base;
if (EXPECT(!tail->n_words, 1)) {
if (data) {
*data = tail->data;
}
if (unmatch) {
*unmatch = n_words;
}
return true;
}
}
}
} else {
assert(base <= 0);
assert(-base < n_alloced_tails);
struct _Tail *tail = tails - base;
if (tail->n_words == 1 && tail->words[0] == end_word) {
if (data) {
*data = tail->data;
}
if (unmatch) {
*unmatch = n_words;
}
return true;
}
}
return false;
}
void _Trie::prefix(const Word words[], Index n_words, vector<Index>& results) const
{
Index tn_idx = ROOT;
for (Index i = 0; i < n_words; i++) {
Index base = nodes[tn_idx].base;
if (base >= BASE) {
Index next = NEXT_INDEX(base, words[i]);
if (EXPECT(next >= n_alloced_nodes, 0) || nodes[next].prev != tn_idx) {
return;
}
tn_idx = next;
} else {
assert(base <= 0);
assert(-base < n_alloced_tails);
struct _Tail *tail = tails - base;
Index j = 0;
while (j < tail->n_words && i < n_words) {
if (tail->words[j] != words[i]) {
break;
}
j++;
i++;
}
if (i < n_words) {
return;
}
results.push_back(tail->data);
return;
}
}
Index base = nodes[tn_idx].base;
if (base <= 0) {
assert(-base < n_alloced_tails);
struct _Tail *tail = tails - base;
results.push_back(tail->data);
return;
}
assert(base >= BASE);
collect_all_subs(tn_idx, results);
}
void _Trie::collect_all_subs(Index tn_idx, vector<Index>& results) const
{
vector<Word> subs;
collect_sub_nodes(tn_idx, subs);
Index subs_size = (Index)subs.size();
for (Index i = 0; i < subs_size; i++) {
Index next = NEXT_INDEX(nodes[tn_idx].base, subs[i]);
Index base = nodes[next].base;
if (base >= BASE) {
collect_all_subs(next, results);
} else {
assert(base <= 0);
assert(-base < n_alloced_tails);
struct _Tail *tail = tails - base;
results.push_back(tail->data);
}
}
}
void _Trie::prefix(const Word words[], Index n_words, vector<Tail>& results) const
{
Index tn_idx = ROOT;
for (Index i = 0; i < n_words; i++) {
Index base = nodes[tn_idx].base;
if (base >= BASE) {
Index next = NEXT_INDEX(base, words[i]);
if (EXPECT(next >= n_alloced_nodes, 0) || nodes[next].prev != tn_idx) {
return;
}
tn_idx = next;
} else {
assert(base <= 0);
assert(-base < n_alloced_tails);
struct _Tail *tail = tails - base;
Index j = 0;
while (j < tail->n_words && i < n_words) {
if (tail->words[j] != words[i]) {
break;
}
j++;
i++;
}
if (i < n_words) {
return;
}
Tail result;
result.n_words = tail->n_words - j;
if (result.n_words) {
result.words = (Word *)malloc(result.n_words * sizeof *result.words);
if (!result.words) {
throw bad_alloc();
}
memcpy(result.words, tail->words + j,
result.n_words * sizeof *result.words);
} else {