-
Notifications
You must be signed in to change notification settings - Fork 285
/
AhoCorasickDoubleArrayTrie.java
978 lines (896 loc) · 27.8 KB
/
AhoCorasickDoubleArrayTrie.java
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
/*
* AhoCorasickDoubleArrayTrie Project
* https://github.com/hankcs/AhoCorasickDoubleArrayTrie
*
* Copyright 2008-2016 hankcs <me@hankcs.com>
* You may modify and redistribute as long as this attribution remains.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hankcs.algorithm;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.*;
/**
* An implementation of Aho Corasick algorithm based on Double Array Trie
*
* @author hankcs
*/
public class AhoCorasickDoubleArrayTrie<V> implements Serializable
{
/**
* check array of the Double Array Trie structure
*/
protected int[] check;
/**
* base array of the Double Array Trie structure
*/
protected int[] base;
/**
* fail table of the Aho Corasick automata
*/
protected int[] fail;
/**
* output table of the Aho Corasick automata
*/
protected int[][] output;
/**
* outer value array
*/
protected V[] v;
/**
* the length of every key
*/
protected int[] l;
/**
* the size of base and check array
*/
protected int size;
/**
* Parse text
*
* @param text The text
* @return a list of outputs
*/
public List<Hit<V>> parseText(CharSequence text)
{
int position = 1;
int currentState = 0;
List<Hit<V>> collectedEmits = new ArrayList<Hit<V>>();
for (int i = 0; i < text.length(); ++i)
{
currentState = getState(currentState, text.charAt(i));
storeEmits(position, currentState, collectedEmits);
++position;
}
return collectedEmits;
}
/**
* Parse text
*
* @param text The text
* @param processor A processor which handles the output
*/
public void parseText(CharSequence text, IHit<V> processor)
{
int position = 1;
int currentState = 0;
for (int i = 0; i < text.length(); ++i)
{
currentState = getState(currentState, text.charAt(i));
int[] hitArray = output[currentState];
if (hitArray != null)
{
for (int hit : hitArray)
{
processor.hit(position - l[hit], position, v[hit]);
}
}
++position;
}
}
/**
* Parse text
*
* @param text The text
* @param processor A processor which handles the output
*/
public void parseText(CharSequence text, IHitCancellable<V> processor)
{
int currentState = 0;
for (int i = 0; i < text.length(); i++)
{
final int position = i + 1;
currentState = getState(currentState, text.charAt(i));
int[] hitArray = output[currentState];
if (hitArray != null)
{
for (int hit : hitArray)
{
boolean proceed = processor.hit(position - l[hit], position, v[hit]);
if (!proceed)
{
return;
}
}
}
}
}
/**
* Parse text
*
* @param text The text
* @param processor A processor which handles the output
*/
public void parseText(char[] text, IHit<V> processor)
{
int position = 1;
int currentState = 0;
for (char c : text)
{
currentState = getState(currentState, c);
int[] hitArray = output[currentState];
if (hitArray != null)
{
for (int hit : hitArray)
{
processor.hit(position - l[hit], position, v[hit]);
}
}
++position;
}
}
/**
* Parse text
*
* @param text The text
* @param processor A processor which handles the output
*/
public void parseText(char[] text, IHitFull<V> processor)
{
int position = 1;
int currentState = 0;
for (char c : text)
{
currentState = getState(currentState, c);
int[] hitArray = output[currentState];
if (hitArray != null)
{
for (int hit : hitArray)
{
processor.hit(position - l[hit], position, v[hit], hit);
}
}
++position;
}
}
/**
* Checks that string contains at least one substring
*
* @param text source text to check
* @return {@code true} if string contains at least one substring
*/
public boolean matches(String text)
{
int currentState = 0;
for (int i = 0; i < text.length(); ++i)
{
currentState = getState(currentState, text.charAt(i));
int[] hitArray = output[currentState];
if (hitArray != null)
{
return true;
}
}
return false;
}
/**
* Search first match in string
*
* @param text source text to check
* @return first match or {@code null} if there are no matches
*/
public Hit<V> findFirst(String text)
{
int position = 1;
int currentState = 0;
for (int i = 0; i < text.length(); ++i)
{
currentState = getState(currentState, text.charAt(i));
int[] hitArray = output[currentState];
if (hitArray != null)
{
int hitIndex = hitArray[0];
return new Hit<V>(position - l[hitIndex], position, v[hitIndex]);
}
++position;
}
return null;
}
/**
* Save
*
* @param out An ObjectOutputStream object
* @throws IOException Some IOException
*/
public void save(ObjectOutputStream out) throws IOException
{
out.writeObject(base);
out.writeObject(check);
out.writeObject(fail);
out.writeObject(output);
out.writeObject(l);
out.writeObject(v);
}
/**
* Load data from [ObjectInputStream]
*
* @param in An ObjectInputStream object
* @throws IOException If can't read the file from path
* @throws ClassNotFoundException If the class doesn't exist or matched
*/
public void load(ObjectInputStream in) throws IOException, ClassNotFoundException
{
base = (int[]) in.readObject();
check = (int[]) in.readObject();
fail = (int[]) in.readObject();
output = (int[][]) in.readObject();
l = (int[]) in.readObject();
v = (V[]) in.readObject();
}
/**
* Get value by a String key, just like a map.get() method
*
* @param key The key
* @return value if exist otherwise it return null
*/
public V get(String key)
{
int index = exactMatchSearch(key);
if (index >= 0)
{
return v[index];
}
return null;
}
/**
* Update a value corresponding to a key
* @param key the key
* @param value the value
* @return successful or not(failure if there is no key)
*/
public boolean set(String key, V value)
{
int index = exactMatchSearch(key);
if (index >= 0)
{
v[index] = value;
return true;
}
return false;
}
/**
* Pick the value by index in value array <br>
* Notice that to be more efficiently, this method DO NOT check the parameter
*
* @param index The index
* @return The value
*/
public V get(int index)
{
return v[index];
}
/**
* Processor handles the output when hit a keyword
*/
public interface IHit<V>
{
/**
* Hit a keyword, you can use some code like text.substring(begin, end) to get the keyword
*
* @param begin the beginning index, inclusive.
* @param end the ending index, exclusive.
* @param value the value assigned to the keyword
*/
void hit(int begin, int end, V value);
}
/**
* Processor handles the output when hit a keyword, with more detail
*/
public interface IHitFull<V>
{
/**
* Hit a keyword, you can use some code like text.substring(begin, end) to get the keyword
*
* @param begin the beginning index, inclusive.
* @param end the ending index, exclusive.
* @param value the value assigned to the keyword
* @param index the index of the value assigned to the keyword, you can use the integer as a perfect hash value
*/
void hit(int begin, int end, V value, int index);
}
/**
* Callback that allows to cancel the search process.
*/
public interface IHitCancellable<V>
{
/**
* Hit a keyword, you can use some code like text.substring(begin, end) to get the keyword
*
* @param begin the beginning index, inclusive.
* @param end the ending index, exclusive.
* @param value the value assigned to the keyword
* @return Return true for continuing the search and false for stopping it.
*/
boolean hit(int begin, int end, V value);
}
/**
* A result output
*
* @param <V> the value type
*/
public static class Hit<V>
{
/**
* the beginning index, inclusive.
*/
public final int begin;
/**
* the ending index, exclusive.
*/
public final int end;
/**
* the value assigned to the keyword
*/
public final V value;
public Hit(int begin, int end, V value)
{
this.begin = begin;
this.end = end;
this.value = value;
}
@Override
public String toString()
{
return String.format("[%d:%d]=%s", begin, end, value);
}
}
/**
* transmit state, supports failure function
*
* @param currentState
* @param character
* @return
*/
private int getState(int currentState, char character)
{
int newCurrentState = transitionWithRoot(currentState, character); // 先按success跳转
while (newCurrentState == -1) // 跳转失败的话,按failure跳转
{
currentState = fail[currentState];
newCurrentState = transitionWithRoot(currentState, character);
}
return newCurrentState;
}
/**
* store output
*
* @param position
* @param currentState
* @param collectedEmits
*/
private void storeEmits(int position, int currentState, List<Hit<V>> collectedEmits)
{
int[] hitArray = output[currentState];
if (hitArray != null)
{
for (int hit : hitArray)
{
collectedEmits.add(new Hit<V>(position - l[hit], position, v[hit]));
}
}
}
/**
* transition of a state
*
* @param current
* @param c
* @return
*/
protected int transition(int current, char c)
{
int b = current;
int p;
p = b + c + 1;
if (b == check[p])
b = base[p];
else
return -1;
p = b;
return p;
}
/**
* transition of a state, if the state is root and it failed, then returns the root
*
* @param nodePos
* @param c
* @return
*/
protected int transitionWithRoot(int nodePos, char c)
{
int b = base[nodePos];
int p;
p = b + c + 1;
if (b != check[p])
{
if (nodePos == 0) return 0;
return -1;
}
return p;
}
/**
* Build a AhoCorasickDoubleArrayTrie from a map
*
* @param map a map containing key-value pairs
*/
public void build(Map<String, V> map)
{
new Builder().build(map);
}
/**
* match exactly by a key
*
* @param key the key
* @return the index of the key, you can use it as a perfect hash function
*/
public int exactMatchSearch(String key)
{
return exactMatchSearch(key, 0, 0, 0);
}
/**
* match exactly by a key
*
* @param key
* @param pos
* @param len
* @param nodePos
* @return
*/
private int exactMatchSearch(String key, int pos, int len, int nodePos)
{
if (len <= 0)
len = key.length();
if (nodePos <= 0)
nodePos = 0;
int result = -1;
char[] keyChars = key.toCharArray();
return getMatched(pos, len, result, keyChars, base[nodePos]);
}
private int getMatched(int pos, int len, int result, char[] keyChars, int b1) {
int b = b1;
int p;
for (int i = pos; i < len; i++) {
p = b + (int) (keyChars[i]) + 1;
if (b == check[p])
b = base[p];
else
return result;
}
p = b;
int n = base[p];
if (b == check[p] && n < 0) {
result = -n - 1;
}
return result;
}
/**
* match exactly by a key
*
* @param keyChars the char array of the key
* @param pos the begin index of char array
* @param len the length of the key
* @param nodePos the starting position of the node for searching
* @return the value index of the key, minus indicates null
*/
private int exactMatchSearch(char[] keyChars, int pos, int len, int nodePos)
{
int result = -1;
return getMatched(pos, len, result, keyChars, base[nodePos]);
}
// /**
// * Just for debug when I wrote it
// */
// public void debug()
// {
// System.out.println("base:");
// for (int i = 0; i < base.length; i++)
// {
// if (base[i] < 0)
// {
// System.out.println(i + " : " + -base[i]);
// }
// }
//
// System.out.println("output:");
// for (int i = 0; i < output.length; i++)
// {
// if (output[i] != null)
// {
// System.out.println(i + " : " + Arrays.toString(output[i]));
// }
// }
//
// System.out.println("fail:");
// for (int i = 0; i < fail.length; i++)
// {
// if (fail[i] != 0)
// {
// System.out.println(i + " : " + fail[i]);
// }
// }
//
// System.out.println(this);
// }
//
// @Override
// public String toString()
// {
// String infoIndex = "i = ";
// String infoChar = "char = ";
// String infoBase = "base = ";
// String infoCheck = "check= ";
// for (int i = 0; i < Math.min(base.length, 200); ++i)
// {
// if (base[i] != 0 || check[i] != 0)
// {
// infoChar += " " + (i == check[i] ? " ×" : (char) (i - check[i] - 1));
// infoIndex += " " + String.format("%5d", i);
// infoBase += " " + String.format("%5d", base[i]);
// infoCheck += " " + String.format("%5d", check[i]);
// }
// }
// return "DoubleArrayTrie:" +
// "\n" + infoChar +
// "\n" + infoIndex +
// "\n" + infoBase +
// "\n" + infoCheck + "\n" +
//// "check=" + Arrays.toString(check) +
//// ", base=" + Arrays.toString(base) +
//// ", used=" + Arrays.toString(used) +
// "size=" + size
//// ", length=" + Arrays.toString(length) +
//// ", value=" + Arrays.toString(value) +
// ;
// }
//
// /**
// * 一个顺序输出变量名与变量值的调试类
// */
// private static class DebugArray
// {
// Map<String, String> nameValueMap = new LinkedHashMap<String, String>();
//
// public void add(String name, int value)
// {
// String valueInMap = nameValueMap.get(name);
// if (valueInMap == null)
// {
// valueInMap = "";
// }
//
// valueInMap += " " + String.format("%5d", value);
//
// nameValueMap.put(name, valueInMap);
// }
//
// @Override
// public String toString()
// {
// String text = "";
// for (Map.Entry<String, String> entry : nameValueMap.entrySet())
// {
// String name = entry.getKey();
// String value = entry.getValue();
// text += String.format("%-5s", name) + "= " + value + '\n';
// }
//
// return text;
// }
//
// public void println()
// {
// System.out.print(this);
// }
// }
/**
* @return the size of the keywords
*/
public int size() { return v.length; }
/**
* A builder to build the AhoCorasickDoubleArrayTrie
*/
private class Builder
{
/**
* the root state of trie
*/
private State rootState = new State();
/**
* whether the position has been used
*/
private boolean[] used;
/**
* the allocSize of the dynamic array
*/
private int allocSize;
/**
* a parameter controls the memory growth speed of the dynamic array
*/
private int progress;
/**
* the next position to check unused memory
*/
private int nextCheckPos;
/**
* the size of the key-pair sets
*/
private int keySize;
/**
* Build from a map
*
* @param map a map containing key-value pairs
*/
@SuppressWarnings("unchecked")
public void build(Map<String, V> map)
{
// 把值保存下来
v = (V[]) map.values().toArray();
l = new int[v.length];
Set<String> keySet = map.keySet();
// 构建二分trie树
addAllKeyword(keySet);
// 在二分trie树的基础上构建双数组trie树
buildDoubleArrayTrie(keySet.size());
used = null;
// 构建failure表并且合并output表
constructFailureStates();
rootState = null;
loseWeight();
}
/**
* fetch siblings of a parent node
*
* @param parent parent node
* @param siblings parent node's child nodes, i . e . the siblings
* @return the amount of the siblings
*/
private int fetch(State parent, List<Map.Entry<Integer, State>> siblings)
{
if (parent.isAcceptable())
{
State fakeNode = new State(-(parent.getDepth() + 1)); // 此节点是parent的子节点,同时具备parent的输出
fakeNode.addEmit(parent.getLargestValueId());
siblings.add(new AbstractMap.SimpleEntry<Integer, State>(0, fakeNode));
}
for (Map.Entry<Character, State> entry : parent.getSuccess().entrySet())
{
siblings.add(new AbstractMap.SimpleEntry<Integer, State>(entry.getKey() + 1, entry.getValue()));
}
return siblings.size();
}
/**
* add a keyword
*
* @param keyword a keyword
* @param index the index of the keyword
*/
private void addKeyword(String keyword, int index)
{
State currentState = this.rootState;
for (Character character : keyword.toCharArray())
{
currentState = currentState.addState(character);
}
currentState.addEmit(index);
l[index] = keyword.length();
}
/**
* add a collection of keywords
*
* @param keywordSet the collection holding keywords
*/
private void addAllKeyword(Collection<String> keywordSet)
{
int i = 0;
for (String keyword : keywordSet)
{
addKeyword(keyword, i++);
}
}
/**
* construct failure table
*/
private void constructFailureStates()
{
fail = new int[size + 1];
fail[1] = base[0];
output = new int[size + 1][];
Queue<State> queue = new ArrayDeque<State>();
// 第一步,将深度为1的节点的failure设为根节点
for (State depthOneState : this.rootState.getStates())
{
depthOneState.setFailure(this.rootState, fail);
queue.add(depthOneState);
constructOutput(depthOneState);
}
// 第二步,为深度 > 1 的节点建立failure表,这是一个bfs
while (!queue.isEmpty())
{
State currentState = queue.remove();
for (Character transition : currentState.getTransitions())
{
State targetState = currentState.nextState(transition);
queue.add(targetState);
State traceFailureState = currentState.failure();
while (traceFailureState.nextState(transition) == null)
{
traceFailureState = traceFailureState.failure();
}
State newFailureState = traceFailureState.nextState(transition);
targetState.setFailure(newFailureState, fail);
targetState.addEmit(newFailureState.emit());
constructOutput(targetState);
}
}
}
/**
* construct output table
*/
private void constructOutput(State targetState)
{
Collection<Integer> emit = targetState.emit();
if (emit == null || emit.size() == 0) return;
int[] output = new int[emit.size()];
Iterator<Integer> it = emit.iterator();
for (int i = 0; i < output.length; ++i)
{
output[i] = it.next();
}
AhoCorasickDoubleArrayTrie.this.output[targetState.getIndex()] = output;
}
private void buildDoubleArrayTrie(int keySize)
{
progress = 0;
this.keySize = keySize;
resize(65536 * 32); // 32个双字节
base[0] = 1;
nextCheckPos = 0;
State root_node = this.rootState;
List<Map.Entry<Integer, State>> siblings = new ArrayList<Map.Entry<Integer, State>>(root_node.getSuccess().entrySet().size());
fetch(root_node, siblings);
insert(siblings);
}
/**
* allocate the memory of the dynamic array
*
* @param newSize of the new array
* @return the new-allocated-size
*/
private int resize(int newSize)
{
int[] base2 = new int[newSize];
int[] check2 = new int[newSize];
boolean[] used2 = new boolean[newSize];
if (allocSize > 0)
{
System.arraycopy(base, 0, base2, 0, allocSize);
System.arraycopy(check, 0, check2, 0, allocSize);
System.arraycopy(used, 0, used2, 0, allocSize);
}
base = base2;
check = check2;
used = used2;
return allocSize = newSize;
}
/**
* insert the siblings to double array trie
*
* @param siblings the siblings being inserted
* @return the position to insert them
*/
private int insert(List<Map.Entry<Integer, State>> siblings)
{
int begin = 0;
int pos = Math.max(siblings.get(0).getKey() + 1, nextCheckPos) - 1;
int nonzero_num = 0;
int first = 0;
if (allocSize <= pos)
resize(pos + 1);
outer:
// 此循环体的目标是找出满足base[begin + a1...an] == 0的n个空闲空间,a1...an是siblings中的n个节点
while (true)
{
pos++;
if (allocSize <= pos)
resize(pos + 1);
if (check[pos] != 0)
{
nonzero_num++;
continue;
}
else if (first == 0)
{
nextCheckPos = pos;
first = 1;
}
begin = pos - siblings.get(0).getKey(); // 当前位置离第一个兄弟节点的距离
if (allocSize <= (begin + siblings.get(siblings.size() - 1).getKey()))
{
// progress can be zero // 防止progress产生除零错误
double l = (1.05 > 1.0 * keySize / (progress + 1)) ? 1.05 : 1.0 * keySize / (progress + 1);
resize((int) (allocSize * l));
}
if (used[begin])
continue;
for (int i = 1; i < siblings.size(); i++)
if (check[begin + siblings.get(i).getKey()] != 0)
continue outer;
break;
}
// -- Simple heuristics --
// if the percentage of non-empty contents in check between the
// index
// 'next_check_pos' and 'check' is greater than some constant value
// (e.g. 0.9),
// new 'next_check_pos' index is written by 'check'.
if (1.0 * nonzero_num / (pos - nextCheckPos + 1) >= 0.95)
nextCheckPos = pos; // 从位置 next_check_pos 开始到 pos 间,如果已占用的空间在95%以上,下次插入节点时,直接从 pos 位置处开始查找
used[begin] = true;
size = (size > begin + siblings.get(siblings.size() - 1).getKey() + 1) ? size : begin + siblings.get(siblings.size() - 1).getKey() + 1;
for (Map.Entry<Integer, State> sibling : siblings)
{
check[begin + sibling.getKey()] = begin;
}
for (Map.Entry<Integer, State> sibling : siblings)
{
List<Map.Entry<Integer, State>> new_siblings = new ArrayList<Map.Entry<Integer, State>>(sibling.getValue().getSuccess().entrySet().size() + 1);
if (fetch(sibling.getValue(), new_siblings) == 0) // 一个词的终止且不为其他词的前缀,其实就是叶子节点
{
base[begin + sibling.getKey()] = (-sibling.getValue().getLargestValueId() - 1);
progress++;
}
else
{
int h = insert(new_siblings); // dfs
base[begin + sibling.getKey()] = h;
}
sibling.getValue().setIndex(begin + sibling.getKey());
}
return begin;
}
/**
* free the unnecessary memory
*/
private void loseWeight()
{
int[] nbase = new int[size + 65535];
System.arraycopy(base, 0, nbase, 0, size);
base = nbase;
int[] ncheck = new int[size + 65535];
System.arraycopy(check, 0, ncheck, 0, size);
check = ncheck;
}
}
}