-
Notifications
You must be signed in to change notification settings - Fork 1
/
ChainSuffixMatcher.java
246 lines (212 loc) · 7.28 KB
/
ChainSuffixMatcher.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
package byteIndexof;
import java.util.Arrays;
public class ChainSuffixMatcher extends MatcherFactory
{
private static final class MatcherImpl extends Matcher
{
static final EmptyNode terminal = new EmptyNode ();
private static final class BuildNode
{
boolean boundary = false;
ByteMap<BuildNode> map = null;
BuildNode add (byte b)
{
if (map == null) {
map = new ByteMap<> ();
}
BuildNode n = map.get (b);
if (n == null) {
n = new BuildNode ();
map.put (b, n);
}
return n;
}
Node convert ()
{
if (map == null) {
return terminal;
}
if (map.size () == 1) {
ByteMap.Entry<BuildNode> e = map.iterator ().next ();
Node n = e.n.convert ();
n = n.addOne (boundary, e.b);
return n;
}
ArrayNode n = new ArrayNode (boundary);
for (ByteMap.Entry<BuildNode> e : map) {
n.put (e.b, e.n.convert ());
}
return n;
}
@Override
public String toString ()
{
return boundary + ": " + map;
}
}
private static abstract class Node
{
public final boolean boundary;
Node (boolean boundary) {
this.boundary = boundary;
}
Node addOne (boolean boundary, byte b)
{
return new SingleNode (boundary, b, this);
}
abstract int match (byte [] text, int pos, int i, int shift);
}
private static class EmptyNode extends Node
{
EmptyNode ()
{
super (true);
}
@Override
int match (byte [] text, int pos, int i, int shift)
{
return i;
}
}
private static final class SingleNode extends Node
{
private final byte b;
private final Node node;
SingleNode (boolean boundary, byte b, Node node)
{
super (boundary);
this.b = b;
this.node = node == terminal ? null : node;
}
@Override
Node addOne (boolean boundary, byte b)
{
return this.boundary ? super.addOne (boundary, b) : new ChainNode (boundary, new byte [] {this.b, b}, node);
}
@Override
int match (byte [] text, int pos, int i, int shift)
{
if (boundary) shift = i;
-- i;
if (text [pos + i] != b) {
return shift;
}
if (node == null) {
return i;
}
return node.match (text, pos, i, shift);
}
}
private static final class ArrayNode extends Node
{
private final Node [] nodes = new Node [256];
ArrayNode (boolean boundary)
{
super (boundary);
}
void put (byte b, Node node)
{
nodes [b & 0xFF] = node;
}
@Override
int match (byte [] text, int pos, int i, int shift)
{
if (boundary) shift = i;
-- i;
Node n = nodes [text [pos + i] & 0xFF];
if (n == null) {
return shift;
}
return n.match (text, pos, i, shift);
}
}
private static final class ChainNode extends Node
{
private final byte [] chain;
private final Node node;
ChainNode (boolean boundary, byte [] chain, Node node)
{
super (boundary);
this.chain = chain;
this.node = node;
}
@Override
Node addOne (boolean boundary, byte b)
{
if (this.boundary) return super.addOne (boundary, b);
byte [] new_chain = Arrays.copyOf (chain, chain.length + 1);
new_chain [chain.length] = b;
return new ChainNode (boundary, new_chain, node);
}
@Override
int match (byte [] text, int pos, int i, int shift)
{
if (boundary) shift = i;
i -= chain.length;
for (int j = 0; j < chain.length; j++) {
if (text [pos + i + j] != chain [j]) {
return shift;
}
}
if (node == null) {
return i;
}
return shift = node.match (text, pos, i, shift);
}
}
private final byte [] pattern;
private final Node root;
public MatcherImpl (byte [] pattern)
{
long mem1 = 0, t1 = 0;
if (DEBUG) {
System.gc ();
mem1 = Runtime.getRuntime ().totalMemory () - Runtime.getRuntime ().freeMemory ();
t1 = System.currentTimeMillis ();
}
this.pattern = pattern;
int pattern_len = pattern.length;
BuildNode build_root = new BuildNode ();
for (int prefix_len = 1; prefix_len <= pattern_len; prefix_len++) {
BuildNode node = build_root;
for (int i = prefix_len - 1; i >= 0; --i) {
node = node.add (pattern [i]);
}
node.boundary = true;
}
if (DEBUG) {
long t2 = System.currentTimeMillis ();
System.gc ();
long mem2 = Runtime.getRuntime ().totalMemory () - Runtime.getRuntime ().freeMemory ();
System.out.println ("Root Memory: " + pattern_len + ": " + (mem2 - mem1));
System.out.println ("Time building: " + pattern_len + ": " + (t2 - t1));
}
root = build_root.convert ();
if (DEBUG) {
long t3 = System.currentTimeMillis ();
System.out.println ("Time converting: " + pattern_len + ": " + (t3 - t1));
}
}
@Override
public int indexOf (byte[] text, int fromIndex)
{
int len = pattern.length;
for (int pos = fromIndex; pos < text.length - len + 1;) {
int shift = root.match (text, pos, len, len);
if (shift == 0) return pos;
pos += shift;
}
return -1;
}
}
@Override
public Matcher createMatcher (byte[] pattern)
{
return new MatcherImpl (pattern);
}
@Override
public String stats ()
{
return "";
}
}