/
ArrayTrie.java
479 lines (441 loc) · 13.8 KB
/
ArrayTrie.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
//
// ========================================================================
// Copyright (c) 1995-2021 Mort Bay Consulting Pty Ltd and others.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.util;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
/**
* <p>A Trie String lookup data structure using a fixed size array.</p>
* <p>This implementation is always case insensitive and is optimal for
* a small number of fixed strings with few special characters. The
* Trie is stored in an array of lookup tables, each indexed by the
* next character of the key. Frequently used characters are directly
* indexed in each lookup table, whilst infrequently used characters
* must use a big character table.
* </p>
* <p>This Trie is very space efficient if the key characters are
* from ' ', '+', '-', ':', ';', '.', 'A' to 'Z' or 'a' to 'z'.
* Other ISO-8859-1 characters can be used by the key, but less space
* efficiently.
* </p>
* <p>This Trie is not Threadsafe and contains no mutual exclusion
* or deliberate memory barriers. It is intended for an ArrayTrie to be
* built by a single thread and then used concurrently by multiple threads
* and not mutated during that access. If concurrent mutations of the
* Trie is required external locks need to be applied.
* </p>
*
* @param <V> the entry type
*/
public class ArrayTrie<V> extends AbstractTrie<V>
{
/**
* The Size of a Trie row is how many characters can be looked
* up directly without going to a big index. This is set at
* 32 to cover case insensitive alphabet and a few other common
* characters.
*/
private static final int ROW_SIZE = 32;
/**
* The index lookup table, this maps a character as a byte
* (ISO-8859-1 or UTF8) to an index within a Trie row
*/
private static final int[] __lookup =
{
// 0 1 2 3 4 5 6 7 8 9 A B C D E F
/*0*/-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
/*1*/-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
/*2*/31, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 26, -1, 27, 30, -1,
/*3*/-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 28, 29, -1, -1, -1, -1,
/*4*/-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
/*5*/15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
/*6*/-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
/*7*/15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1
};
/**
* The Trie rows in a single array which allows a lookup of row,character
* to the next row in the Trie. This is actually a 2 dimensional
* array that has been flattened to achieve locality of reference.
* The first ROW_SIZE entries are for row 0, then next ROW_SIZE
* entries are for row 1 etc. So in general instead of using
* _rows[row][index], we use _rows[row*ROW_SIZE+index] to look up
* the next row for a given character.
*
* The array is of characters rather than integers to save space.
*/
private final char[] _rowIndex;
/**
* The key (if any) for a Trie row.
* A row may be a leaf, a node or both in the Trie tree.
*/
private final String[] _key;
/**
* The value (if any) for a Trie row.
* A row may be a leaf, a node or both in the Trie tree.
*/
private final V[] _value;
/**
* A big index for each row.
* If a character outside of the lookup map is needed,
* then a big index will be created for the row, with
* 256 entries, one for each possible byte.
*/
private char[][] _bigIndex;
/**
* The number of rows allocated
*/
private char _rows;
public ArrayTrie()
{
this(128);
}
/**
* @param capacity The capacity of the trie, which at the worst case
* is the total number of characters of all keys stored in the Trie.
* The capacity needed is dependent of the shared prefixes of the keys.
* For example, a capacity of 6 nodes is required to store keys "foo"
* and "bar", but a capacity of only 4 is required to
* store "bar" and "bat".
*/
@SuppressWarnings("unchecked")
public ArrayTrie(int capacity)
{
super(true);
_value = (V[])new Object[capacity];
_rowIndex = new char[capacity * 32];
_key = new String[capacity];
}
@Override
public void clear()
{
_rows = 0;
Arrays.fill(_value, null);
Arrays.fill(_rowIndex, (char)0);
Arrays.fill(_key, null);
}
@Override
public boolean put(String s, V v)
{
int t = 0;
int k;
int limit = s.length();
for (k = 0; k < limit; k++)
{
char c = s.charAt(k);
int index = __lookup[c & 0x7f];
if (index >= 0)
{
int idx = t * ROW_SIZE + index;
t = _rowIndex[idx];
if (t == 0)
{
if (++_rows >= _value.length)
return false;
t = _rowIndex[idx] = _rows;
}
}
else if (c > 127)
throw new IllegalArgumentException("non ascii character");
else
{
if (_bigIndex == null)
_bigIndex = new char[_value.length][];
if (t >= _bigIndex.length)
return false;
char[] big = _bigIndex[t];
if (big == null)
big = _bigIndex[t] = new char[128];
t = big[c];
if (t == 0)
{
if (_rows == _value.length)
return false;
t = big[c] = ++_rows;
}
}
}
if (t >= _key.length)
{
_rows = (char)_key.length;
return false;
}
_key[t] = v == null ? null : s;
_value[t] = v;
return true;
}
@Override
public V get(String s, int offset, int len)
{
int t = 0;
for (int i = 0; i < len; i++)
{
char c = s.charAt(offset + i);
if (c > 0x7f)
return null;
int index = __lookup[c & 0x7f];
if (index >= 0)
{
int idx = t * ROW_SIZE + index;
t = _rowIndex[idx];
if (t == 0)
return null;
}
else
{
char[] big = _bigIndex == null ? null : _bigIndex[t];
if (big == null)
return null;
t = big[c & 0x7f];
if (t == 0)
return null;
}
}
return _value[t];
}
@Override
public V get(ByteBuffer b, int offset, int len)
{
int t = 0;
for (int i = 0; i < len; i++)
{
byte c = b.get(offset + i);
int index = __lookup[c & 0x7f];
if (index >= 0)
{
int idx = t * ROW_SIZE + index;
t = _rowIndex[idx];
if (t == 0)
return null;
}
else
{
char[] big = _bigIndex == null ? null : _bigIndex[t];
if (big == null)
return null;
t = big[c];
if (t == 0)
return null;
}
}
return _value[t];
}
@Override
public V getBest(byte[] b, int offset, int len)
{
return getBest(0, b, offset, len);
}
@Override
public V getBest(ByteBuffer b, int offset, int len)
{
if (b.hasArray())
return getBest(0, b.array(), b.arrayOffset() + b.position() + offset, len);
return getBest(0, b, offset, len);
}
@Override
public V getBest(String s, int offset, int len)
{
return getBest(0, s, offset, len);
}
private V getBest(int t, String s, int offset, int len)
{
int pos = offset;
for (int i = 0; i < len; i++)
{
char c = s.charAt(pos++);
int index = __lookup[c & 0x7f];
if (index >= 0)
{
int idx = t * ROW_SIZE + index;
int nt = _rowIndex[idx];
if (nt == 0)
break;
t = nt;
}
else
{
char[] big = _bigIndex == null ? null : _bigIndex[t];
if (big == null)
return null;
int nt = big[c];
if (nt == 0)
break;
t = nt;
}
// Is the next Trie is a match
if (_key[t] != null)
{
// Recurse so we can remember this possibility
V best = getBest(t, s, offset + i + 1, len - i - 1);
if (best != null)
return best;
return _value[t];
}
}
return _value[t];
}
private V getBest(int t, byte[] b, int offset, int len)
{
for (int i = 0; i < len; i++)
{
byte c = b[offset + i];
int index = __lookup[c & 0x7f];
if (index >= 0)
{
int idx = t * ROW_SIZE + index;
int nt = _rowIndex[idx];
if (nt == 0)
break;
t = nt;
}
else
{
char[] big = _bigIndex == null ? null : _bigIndex[t];
if (big == null)
return null;
int nt = big[c];
if (nt == 0)
break;
t = nt;
}
// Is the next Trie is a match
if (_key[t] != null)
{
// Recurse so we can remember this possibility
V best = getBest(t, b, offset + i + 1, len - i - 1);
if (best != null)
return best;
break;
}
}
return _value[t];
}
private V getBest(int t, ByteBuffer b, int offset, int len)
{
int pos = b.position() + offset;
for (int i = 0; i < len; i++)
{
byte c = b.get(pos++);
int index = __lookup[c & 0x7f];
if (index >= 0)
{
int idx = t * ROW_SIZE + index;
int nt = _rowIndex[idx];
if (nt == 0)
break;
t = nt;
}
else
{
char[] big = _bigIndex == null ? null : _bigIndex[t];
if (big == null)
return null;
int nt = big[c];
if (nt == 0)
break;
t = nt;
}
// Is the next Trie is a match
if (_key[t] != null)
{
// Recurse so we can remember this possibility
V best = getBest(t, b, offset + i + 1, len - i - 1);
if (best != null)
return best;
break;
}
}
return _value[t];
}
@Override
public String toString()
{
StringBuilder buf = new StringBuilder();
toString(buf, 0);
if (buf.length() == 0)
return "{}";
buf.setCharAt(0, '{');
buf.append('}');
return buf.toString();
}
private void toString(Appendable out, int t)
{
if (_value[t] != null)
{
try
{
out.append(',');
out.append(_key[t]);
out.append('=');
out.append(_value[t].toString());
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
for (int i = 0; i < ROW_SIZE; i++)
{
int idx = t * ROW_SIZE + i;
if (_rowIndex[idx] != 0)
toString(out, _rowIndex[idx]);
}
char[] big = _bigIndex == null ? null : _bigIndex[t];
if (big != null)
{
for (int i : big)
{
if (i != 0)
toString(out, i);
}
}
}
@Override
public Set<String> keySet()
{
Set<String> keys = new HashSet<>();
keySet(keys, 0);
return keys;
}
private void keySet(Set<String> set, int t)
{
if (t < _value.length && _value[t] != null)
set.add(_key[t]);
for (int i = 0; i < ROW_SIZE; i++)
{
int idx = t * ROW_SIZE + i;
if (idx < _rowIndex.length && _rowIndex[idx] != 0)
keySet(set, _rowIndex[idx]);
}
char[] big = _bigIndex == null || t >= _bigIndex.length ? null : _bigIndex[t];
if (big != null)
{
for (int i : big)
{
if (i != 0)
keySet(set, i);
}
}
}
@Override
public boolean isFull()
{
return _rows + 1 >= _key.length;
}
}