-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathradix-tree.mjs
544 lines (451 loc) · 14.1 KB
/
radix-tree.mjs
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
/* ================================= $ J $ =====================================
// <radix-tree.mjs>
//
// Idiomatic implementation of the Radix trie data structure
// that is fully compatible with `Map` and comes with built-in converter
// to regular expression.
//
// The keys can only be strings though.
//
// Copyright garnetius.
// -------------------------------------------------------------------------- */
"use strict"
/* ===--------------------------------------------------------------------------
// Radix tree internal data structure used to distinguish from real values */
class RadixNode extends Map {
constructor() {
super();
}}
/* ===--------------------------------------------------------------------------
// Radix tree itself: provides `Map`-like interface */
class RadixTree extends Map {
get [Symbol.toStringTag]() {
return "RadixTree";
}
/* =============================================================================
// Traversal
// -----------------------------------------------------------------------------
// Main generator template */
*$traverse (op, word, node) {
if (node instanceof RadixNode) {
for (let key of node.keys()) {
/* Construct the entry key */
let wordKey;
const value = node.get (key);
if (key === RadixTree.sentinel) wordKey = word;
else wordKey = word + key;
if (value instanceof RadixNode) {
/* This must never happen if the key is a sentinel */
yield* this.$traverse (op, wordKey, value);
} else {
/* Leaf node */
if (op === RadixTree.traverseKind.key) yield wordKey;
if (op === RadixTree.traverseKind.value) yield value;
if (op === RadixTree.traverseKind.pair) yield [wordKey, value];
}
}
} else {
/* The yielded value depends on the kind of operation */
if (op === RadixTree.traverseKind.key) yield word;
if (op === RadixTree.traverseKind.value) yield node;
if (op === RadixTree.traverseKind.pair) yield [word, node];
}
}
*keys (prefix="") {
const start = this.startsWith (prefix);
if (start === undefined) return;
const [word, node] = start;
yield* this.$traverse (RadixTree.traverseKind.key, word, node);
}
*values (prefix="") {
const start = this.startsWith (prefix);
if (start === undefined) return;
const [word, node] = start;
yield* this.$traverse (RadixTree.traverseKind.value, word, node);
}
/* Key/value pairs */
*entries (prefix="") {
const start = this.startsWith (prefix);
if (start === undefined) return;
const [word, node] = start;
yield* this.$traverse (RadixTree.traverseKind.pair, word, node);
}
/* Classic `forEach()` traversal with optional prefix
// as a last parameter */
forEach (func, that=this, prefix="") {
for (let [key, value] of this.entries (prefix)) {
func.call (that, value, key, this);
}
}
/* Default iterator (without prefix) */
[Symbol.iterator]() {
return this.entries();
}
/* =============================================================================
// Mutation
// -----------------------------------------------------------------------------
// Compact the node after deletion of any of its child nodes */
$normalize (key, node, sub) {
if (sub.size === 1) {
const [subKey, subValue] = sub.entries().next().value;
if (subKey === RadixTree.sentinel) {
/* Only the sentinel is left: replace the upper node key
// with the sentinel's value */
node.set (key, subValue);
} else {
/* The sentinel is gone: merge the remaining key
// with the upper key and delete the original key */
node.set (key + subKey, subValue);
node.delete (key);
}
}
}
/* ===--------------------------------------------------------------------------
// Template for all operations on a tree */
$op (op, node, word, wordIdx, value) {
const wordLen = word.length;
for (let key of node.keys()) {
/* Skip the sentinel */
if (key === RadixTree.sentinel) {
continue;
}
let keyIdx = 0;
const currIdx = wordIdx;
const keyLen = key.length;
/* JavaScript at this time doesn't have the common string prefix function */
while (key.charCodeAt(keyIdx) === word.charCodeAt(wordIdx)) {
++keyIdx;
++wordIdx;
if (keyIdx === keyLen) {
/* The key matches */
const sub = node.get (key);
if (wordIdx !== wordLen) {
/* But the word is incomplete */
if (sub instanceof RadixNode) {
/* Look up the sub node */
const ret = this.$op (op, sub, word, wordIdx, value);
if (op === RadixTree.opKind.delete) {
this.$normalize (key, node, sub);
}
return ret;
}
if (op === RadixTree.opKind.set) {
/* Add the rest of the word */
const wordName = word.substring (wordIdx);
const newSub = new RadixNode();
newSub.set (RadixTree.sentinel, sub);
newSub.set (wordName, value);
node.set (key, newSub);
return true;
} else if (op === RadixTree.opKind.get
|| op === RadixTree.opKind.startsWith) {
return undefined;
} else {
return false;
}
}
/* Complete match */
if (op === RadixTree.opKind.set) {
/* Replace the old value */
let add;
if (sub instanceof RadixNode) {
/* Provide the value via a sentinel key */
const oldSize = sub.size;
sub.set (RadixTree.sentinel, value);
add = oldSize !== sub.size;
} else {
const oldSize = node.size;
node.set (key, value);
add = oldSize !== node.size;
}
return add;
} else {
if (sub instanceof RadixNode) {
/* The value is in a sentinel key */
if (op === RadixTree.opKind.get) {
return sub.get (RadixTree.sentinel);
} else if (op === RadixTree.opKind.startsWith) {
return [word, sub];
} else {
if (op === RadixTree.opKind.delete) {
return sub.delete (RadixTree.sentinel);
}
return sub.has (RadixTree.sentinel);
}
}
/* The sub node itself is a value */
if (op === RadixTree.opKind.get) {
return sub;
} else if (op === RadixTree.opKind.startsWith) {
return [word, sub];
} else {
if (op === RadixTree.opKind.delete) {
return node.delete (key);
}
return true;
}
}
}
if (wordIdx === wordLen) {
/* The word is a substring of another word
// that is present in this tree */
if (op === RadixTree.opKind.set) {
/* Break the key */
const newName = key.substring (0, keyIdx);
const subName = key.substring (keyIdx);
const sub = node.get (key);
const brk = new RadixNode();
brk.set (RadixTree.sentinel, value);
brk.set (subName, sub);
node.set (newName, brk);
node.delete (key);
return true;
} else if (op === RadixTree.opKind.get) {
return undefined;
} else if (op === RadixTree.opKind.startsWith) {
return [word + key.substring (keyIdx), node.get (key)];
} else {
return false;
}
}
}
if (currIdx !== wordIdx) {
/* Partial match */
if (op === RadixTree.opKind.set) {
/* Break the key and add the rest of the word */
const newName = key.substring (0, keyIdx);
const subName = key.substring (keyIdx);
const wordName = word.substring (wordIdx);
const sub = node.get (key);
const brk = new RadixNode();
brk.set (subName, sub);
brk.set (wordName, value);
node.set (newName, brk);
node.delete (key);
return true;
} else if (op === RadixTree.opKind.get
|| op === RadixTree.opKind.startsWith) {
return undefined;
} else {
return false;
}
}
}
/* No match */
if (op === RadixTree.opKind.set) {
/* Add the whole word */
const wordName = word.substring (wordIdx);
node.set (wordName, value);
return true;
} else if (op === RadixTree.opKind.get
|| op === RadixTree.opKind.startsWith) {
return undefined;
} else {
return false;
}
}
/* Mutation */
set (word, value=null) {
let add;
if (word === "") {
const oldSize = this.root.size;
this.root.set (RadixTree.sentinel, value);
add = oldSize !== this.root.size;
} else {
add = this.$op (RadixTree.opKind.set, this.root, word, 0, value);
}
this.size += add;
/* Chainable */
return this;
}
delete (word) {
let del;
if (word === "") del = this.root.delete (RadixTree.sentinel);
else del = this.$op (RadixTree.opKind.delete, this.root, word, 0);
this.size -= del;
return del;
}
clear() {
this.root.clear();
this.size = 0;
}
/* Fuzzy get */
startsWith (prefix) {
if (prefix === "") return [prefix, this.root];
return this.$op (RadixTree.opKind.startsWith, this.root, prefix, 0);
}
get (word) {
if (word === "") return this.root.get (RadixTree.sentinel);
return this.$op (RadixTree.opKind.get, this.root, word, 0);
}
has (word) {
if (word === "") return this.root.has (RadixTree.sentinel);
return this.$op (RadixTree.opKind.has, this.root, word, 0);
}
/* =============================================================================
// Build the string representation of a tree
// suitable for use in a regular expression
// -------------------------------------------------------------------------- */
$toStringNode (str, node, opt, brace, reverse) {
const reverseString = (str) => {
return str.split ('').reverse().join ('');
}
let arr = [];
let hasMaps = false;
for (let [key, sub] of node) {
arr.push ([key, sub]);
}
/* Compress leaf suffixes */
let tree = new RadixTree();
for (let idx = 0; idx !== arr.length; ++idx) {
let [key, sub] = arr[idx];
if (key === RadixTree.sentinel) {
continue;
}
if (sub instanceof RadixNode) {
/* Don't look for common suffix inside branches:
// it becomes way too complex */
hasMaps = true;
continue;
}
tree.set (reverseString (key));
}
let compact = false;
for (let [key, sub] of tree.root) {
if (sub instanceof RadixNode) {
/* Means this set of leaves
// can be compressed */
compact = true;
/* Remove leaves from array.
// This simplifies code ahead. */
for (let len = arr.length; len !== 0; --len) {
const idx = len - 1;
if (arr[idx][0] === RadixTree.sentinel) {
continue;
}
if (!(arr[idx][1] instanceof RadixNode)) {
arr.splice (idx, 1);
}
}
break;
}
}
/* Now we know if this branch must be surrounded with braces */
const braces = brace && (opt || !compact || tree.root.size
/*- tree.root.has (RadixTree.sentinel) */!== 1);
str.value += braces ? '(' : '';
if (compact) {
str.value += tree.$toString (true);
if (hasMaps) {
str.value += '|';
}
}
/* Output branches */
for (let idx = 0; idx !== arr.length; ++idx) {
let [key, sub] = arr[idx];
if (key === RadixTree.sentinel) {
continue;
}
if (!reverse) {
str.value += key;
}
if (sub instanceof RadixNode) {
/* See if this whole branch is optional */
const subOpt = sub.has (RadixTree.sentinel);
/* Additional logic to avoid spurious braces */
const one = sub.size - subOpt === 1;
let subBrace = !one;
if (one) {
const keyIter = sub.keys();
let key = keyIter.next().value;
if (key === RadixTree.sentinel) {
key = keyIter.next().value;
}
const value = sub.get (key);
subBrace = key.length !== 1/* || value instanceof RadixNode*/;
}
this.$toStringNode (str, sub, subOpt, subBrace, reverse);
}
if (reverse) {
str.value += reverseString (key);
}
if (idx < arr.length - opt - 1) {
str.value += '|';
}
}
/* Close the group if needed */
str.value += braces ? ')' : '';
str.value += opt ? '?' : '';
}
$toString (reverse) {
const str = {value: ""};
this.$toStringNode (str, this.root, this.root.has (RadixTree.sentinel)
, false, reverse);
return str.value;
}
toString() {
return this.$toString (false);
}
toRegExp (opts) {
return new RegExp(this.toString(), opts);
}
/* =============================================================================
// Constructors
// -------------------------------------------------------------------------- */
$fromArray (arr) {
for (let item of arr) {
this.set (item);
}
}
$fromMap (map) {
for (let [key, value] of map) {
this.set (key, value);
}
}
constructor (iterable) {
super();
/* ES6 class properties are broken crap */
Object.defineProperties (this, {
root: {value: new RadixNode()},
size: {value: 0, writable: true}
});
if (iterable !== undefined) {
/* Populate from existing container */
if ((Array.isArray (iterable) && !Array.isArray (iterable[0]))
|| iterable instanceof String || typeof iterable === "string") {
/* Accept strings as well (insert each code point) */
this.$fromArray (iterable);
} else if (typeof iterable[Symbol.iterator] === "function") {
this.$fromMap (iterable);
} else if (iterable instanceof Object) {
this.$fromMap (Object.entries (iterable));
} else {
throw new TypeError();
}
}
}}
/* ===--------------------------------------------------------------------------
// Static properties (immutable) */
Object.defineProperties (RadixTree, {
/* Special key value to indicate leaf nodes */
sentinel: {value: null/*Symbol()*/},
opKind: {value: {
set: 0,
get: 1,
has: 2,
delete: 3,
startsWith: 4
}},
traverseKind: {value: {
key: 0,
value: 1,
pair: 2
}}
});
/* ===--------------------------------------------------------------------------
// Exports */
export {
//RadixNode,
RadixTree
}
/* ===------------------------------ =^.^= -------------------------------=== */