data-structure-typed 2.5.3 → 2.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/ci.yml +7 -2
- package/.github/workflows/release-package.yml +9 -2
- package/.husky/pre-commit +3 -0
- package/CHANGELOG.md +1 -1
- package/MIGRATION.md +48 -0
- package/README.md +20 -2
- package/README_CN.md +20 -2
- package/SPECIFICATION.md +24 -0
- package/SPECIFICATION.zh-CN.md +24 -0
- package/dist/cjs/binary-tree.cjs +1897 -19
- package/dist/cjs/graph.cjs +174 -0
- package/dist/cjs/hash.cjs +33 -0
- package/dist/cjs/heap.cjs +71 -0
- package/dist/cjs/index.cjs +2383 -3
- package/dist/cjs/linked-list.cjs +224 -2
- package/dist/cjs/matrix.cjs +24 -0
- package/dist/cjs/priority-queue.cjs +71 -0
- package/dist/cjs/queue.cjs +221 -1
- package/dist/cjs/stack.cjs +59 -0
- package/dist/cjs/trie.cjs +62 -0
- package/dist/cjs-legacy/binary-tree.cjs +1897 -19
- package/dist/cjs-legacy/graph.cjs +174 -0
- package/dist/cjs-legacy/hash.cjs +33 -0
- package/dist/cjs-legacy/heap.cjs +71 -0
- package/dist/cjs-legacy/index.cjs +2383 -3
- package/dist/cjs-legacy/linked-list.cjs +224 -2
- package/dist/cjs-legacy/matrix.cjs +24 -0
- package/dist/cjs-legacy/priority-queue.cjs +71 -0
- package/dist/cjs-legacy/queue.cjs +221 -1
- package/dist/cjs-legacy/stack.cjs +59 -0
- package/dist/cjs-legacy/trie.cjs +62 -0
- package/dist/esm/binary-tree.mjs +1897 -19
- package/dist/esm/graph.mjs +174 -0
- package/dist/esm/hash.mjs +33 -0
- package/dist/esm/heap.mjs +71 -0
- package/dist/esm/index.mjs +2383 -3
- package/dist/esm/linked-list.mjs +224 -2
- package/dist/esm/matrix.mjs +24 -0
- package/dist/esm/priority-queue.mjs +71 -0
- package/dist/esm/queue.mjs +221 -1
- package/dist/esm/stack.mjs +59 -0
- package/dist/esm/trie.mjs +62 -0
- package/dist/esm-legacy/binary-tree.mjs +1897 -19
- package/dist/esm-legacy/graph.mjs +174 -0
- package/dist/esm-legacy/hash.mjs +33 -0
- package/dist/esm-legacy/heap.mjs +71 -0
- package/dist/esm-legacy/index.mjs +2383 -3
- package/dist/esm-legacy/linked-list.mjs +224 -2
- package/dist/esm-legacy/matrix.mjs +24 -0
- package/dist/esm-legacy/priority-queue.mjs +71 -0
- package/dist/esm-legacy/queue.mjs +221 -1
- package/dist/esm-legacy/stack.mjs +59 -0
- package/dist/esm-legacy/trie.mjs +62 -0
- package/dist/types/data-structures/base/iterable-element-base.d.ts +17 -0
- package/dist/types/data-structures/base/linear-base.d.ts +6 -0
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +36 -0
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +42 -0
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +75 -0
- package/dist/types/data-structures/binary-tree/bst.d.ts +72 -0
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +57 -0
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +18 -0
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +375 -0
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +389 -0
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +330 -0
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +438 -0
- package/dist/types/data-structures/graph/directed-graph.d.ts +30 -0
- package/dist/types/data-structures/graph/undirected-graph.d.ts +27 -0
- package/dist/types/data-structures/hash/hash-map.d.ts +33 -0
- package/dist/types/data-structures/heap/heap.d.ts +42 -0
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +75 -2
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +45 -0
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +54 -0
- package/dist/types/data-structures/matrix/matrix.d.ts +24 -0
- package/dist/types/data-structures/queue/deque.d.ts +90 -1
- package/dist/types/data-structures/queue/queue.d.ts +36 -0
- package/dist/types/data-structures/stack/stack.d.ts +30 -0
- package/dist/types/data-structures/trie/trie.d.ts +36 -0
- package/dist/umd/data-structure-typed.js +2383 -3
- package/dist/umd/data-structure-typed.min.js +3 -3
- package/docs-site-docusaurus/docs/api/classes/AVLTree.md +108 -108
- package/docs-site-docusaurus/docs/api/classes/BST.md +101 -101
- package/docs-site-docusaurus/docs/api/classes/BinaryIndexedTree.md +13 -13
- package/docs-site-docusaurus/docs/api/classes/BinaryTree.md +66 -66
- package/docs-site-docusaurus/docs/api/classes/Deque.md +235 -51
- package/docs-site-docusaurus/docs/api/classes/DirectedGraph.md +21 -21
- package/docs-site-docusaurus/docs/api/classes/DoublyLinkedList.md +231 -67
- package/docs-site-docusaurus/docs/api/classes/FibonacciHeap.md +9 -9
- package/docs-site-docusaurus/docs/api/classes/FibonacciHeapNode.md +1 -1
- package/docs-site-docusaurus/docs/api/classes/HashMap.md +14 -14
- package/docs-site-docusaurus/docs/api/classes/Heap.md +117 -34
- package/docs-site-docusaurus/docs/api/classes/IterableElementBase.md +83 -13
- package/docs-site-docusaurus/docs/api/classes/LinearBase.md +124 -20
- package/docs-site-docusaurus/docs/api/classes/LinearLinkedBase.md +140 -32
- package/docs-site-docusaurus/docs/api/classes/LinkedHashMap.md +30 -26
- package/docs-site-docusaurus/docs/api/classes/LinkedListQueue.md +159 -51
- package/docs-site-docusaurus/docs/api/classes/MapGraph.md +20 -20
- package/docs-site-docusaurus/docs/api/classes/Matrix.md +23 -23
- package/docs-site-docusaurus/docs/api/classes/MaxHeap.md +117 -34
- package/docs-site-docusaurus/docs/api/classes/MaxPriorityQueue.md +117 -34
- package/docs-site-docusaurus/docs/api/classes/MinHeap.md +117 -34
- package/docs-site-docusaurus/docs/api/classes/MinPriorityQueue.md +117 -34
- package/docs-site-docusaurus/docs/api/classes/PriorityQueue.md +117 -34
- package/docs-site-docusaurus/docs/api/classes/Queue.md +142 -34
- package/docs-site-docusaurus/docs/api/classes/RedBlackTree.md +117 -117
- package/docs-site-docusaurus/docs/api/classes/SegmentTree.md +8 -8
- package/docs-site-docusaurus/docs/api/classes/SinglyLinkedList.md +158 -50
- package/docs-site-docusaurus/docs/api/classes/SkipList.md +21 -21
- package/docs-site-docusaurus/docs/api/classes/Stack.md +108 -26
- package/docs-site-docusaurus/docs/api/classes/TreeMap.md +33 -33
- package/docs-site-docusaurus/docs/api/classes/TreeMultiMap.md +75 -39
- package/docs-site-docusaurus/docs/api/classes/TreeSet.md +301 -39
- package/docs-site-docusaurus/docs/api/classes/Trie.md +110 -28
- package/docs-site-docusaurus/docs/api/classes/UndirectedGraph.md +20 -20
- package/jest.integration.config.js +1 -2
- package/package.json +51 -50
- package/src/common/error.ts +15 -32
- package/src/common/index.ts +0 -3
- package/src/data-structures/base/iterable-element-base.ts +32 -3
- package/src/data-structures/base/linear-base.ts +13 -36
- package/src/data-structures/binary-tree/avl-tree.ts +31 -493
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +47 -530
- package/src/data-structures/binary-tree/binary-tree.ts +326 -1236
- package/src/data-structures/binary-tree/bst.ts +158 -1010
- package/src/data-structures/binary-tree/red-black-tree.ts +451 -1233
- package/src/data-structures/binary-tree/segment-tree.ts +73 -333
- package/src/data-structures/binary-tree/tree-map.ts +462 -4749
- package/src/data-structures/binary-tree/tree-multi-map.ts +310 -4530
- package/src/data-structures/binary-tree/tree-multi-set.ts +300 -3652
- package/src/data-structures/binary-tree/tree-set.ts +437 -4443
- package/src/data-structures/graph/abstract-graph.ts +98 -167
- package/src/data-structures/graph/directed-graph.ts +137 -532
- package/src/data-structures/graph/map-graph.ts +0 -3
- package/src/data-structures/graph/undirected-graph.ts +132 -484
- package/src/data-structures/hash/hash-map.ts +154 -549
- package/src/data-structures/heap/heap.ts +200 -753
- package/src/data-structures/linked-list/doubly-linked-list.ts +153 -809
- package/src/data-structures/linked-list/singly-linked-list.ts +122 -749
- package/src/data-structures/linked-list/skip-linked-list.ts +211 -864
- package/src/data-structures/matrix/matrix.ts +179 -494
- package/src/data-structures/matrix/navigator.ts +0 -1
- package/src/data-structures/priority-queue/max-priority-queue.ts +1 -6
- package/src/data-structures/priority-queue/min-priority-queue.ts +6 -11
- package/src/data-structures/priority-queue/priority-queue.ts +1 -2
- package/src/data-structures/queue/deque.ts +241 -807
- package/src/data-structures/queue/queue.ts +102 -589
- package/src/data-structures/stack/stack.ts +76 -475
- package/src/data-structures/trie/trie.ts +98 -592
- package/src/types/common.ts +0 -10
- package/src/types/data-structures/binary-tree/bst.ts +0 -7
- package/src/types/data-structures/binary-tree/red-black-tree.ts +0 -1
- package/src/types/data-structures/graph/abstract-graph.ts +0 -2
- package/src/types/data-structures/hash/hash-map.ts +0 -3
- package/src/types/data-structures/hash/index.ts +0 -1
- package/src/types/data-structures/matrix/navigator.ts +0 -2
- package/src/types/utils/utils.ts +0 -7
- package/src/types/utils/validate-type.ts +0 -7
- package/src/utils/number.ts +0 -2
- package/src/utils/utils.ts +0 -5
|
@@ -5,7 +5,6 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
|
|
9
8
|
import type { ElementCallback, TrieOptions } from '../../types';
|
|
10
9
|
import { IterableElementBase } from '../base';
|
|
11
10
|
import { ERR, raise } from '../../common';
|
|
@@ -20,7 +19,6 @@ export class TrieNode {
|
|
|
20
19
|
* @remarks Time O(1), Space O(1)
|
|
21
20
|
* @returns New TrieNode instance.
|
|
22
21
|
*/
|
|
23
|
-
|
|
24
22
|
constructor(key: string) {
|
|
25
23
|
this._key = key;
|
|
26
24
|
this._isEnd = false;
|
|
@@ -34,7 +32,6 @@ export class TrieNode {
|
|
|
34
32
|
* @remarks Time O(1), Space O(1)
|
|
35
33
|
* @returns Character key string.
|
|
36
34
|
*/
|
|
37
|
-
|
|
38
35
|
get key(): string {
|
|
39
36
|
return this._key;
|
|
40
37
|
}
|
|
@@ -45,7 +42,6 @@ export class TrieNode {
|
|
|
45
42
|
* @param value - New character key.
|
|
46
43
|
* @returns void
|
|
47
44
|
*/
|
|
48
|
-
|
|
49
45
|
set key(value: string) {
|
|
50
46
|
this._key = value;
|
|
51
47
|
}
|
|
@@ -57,7 +53,6 @@ export class TrieNode {
|
|
|
57
53
|
* @remarks Time O(1), Space O(1)
|
|
58
54
|
* @returns Map from character to child node.
|
|
59
55
|
*/
|
|
60
|
-
|
|
61
56
|
get children(): Map<string, TrieNode> {
|
|
62
57
|
return this._children;
|
|
63
58
|
}
|
|
@@ -68,7 +63,6 @@ export class TrieNode {
|
|
|
68
63
|
* @param value - New map of character → node.
|
|
69
64
|
* @returns void
|
|
70
65
|
*/
|
|
71
|
-
|
|
72
66
|
set children(value: Map<string, TrieNode>) {
|
|
73
67
|
this._children = value;
|
|
74
68
|
}
|
|
@@ -80,7 +74,6 @@ export class TrieNode {
|
|
|
80
74
|
* @remarks Time O(1), Space O(1)
|
|
81
75
|
* @returns True if this node ends a word.
|
|
82
76
|
*/
|
|
83
|
-
|
|
84
77
|
get isEnd(): boolean {
|
|
85
78
|
return this._isEnd;
|
|
86
79
|
}
|
|
@@ -91,7 +84,6 @@ export class TrieNode {
|
|
|
91
84
|
* @param value - Whether this node ends a word.
|
|
92
85
|
* @returns void
|
|
93
86
|
*/
|
|
94
|
-
|
|
95
87
|
set isEnd(value: boolean) {
|
|
96
88
|
this._isEnd = value;
|
|
97
89
|
}
|
|
@@ -221,7 +213,6 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
|
|
|
221
213
|
* @param [options] - Options such as toElementFn and caseSensitive.
|
|
222
214
|
* @returns New Trie instance.
|
|
223
215
|
*/
|
|
224
|
-
|
|
225
216
|
constructor(words: Iterable<string> | Iterable<R> = [], options?: TrieOptions<R>) {
|
|
226
217
|
super(options);
|
|
227
218
|
if (options) {
|
|
@@ -240,7 +231,6 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
|
|
|
240
231
|
* @remarks Time O(1), Space O(1)
|
|
241
232
|
* @returns Word count.
|
|
242
233
|
*/
|
|
243
|
-
|
|
244
234
|
get size(): number {
|
|
245
235
|
return this._size;
|
|
246
236
|
}
|
|
@@ -252,7 +242,6 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
|
|
|
252
242
|
* @remarks Time O(1), Space O(1)
|
|
253
243
|
* @returns True if case-sensitive.
|
|
254
244
|
*/
|
|
255
|
-
|
|
256
245
|
get caseSensitive(): boolean {
|
|
257
246
|
return this._caseSensitive;
|
|
258
247
|
}
|
|
@@ -264,7 +253,6 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
|
|
|
264
253
|
* @remarks Time O(1), Space O(1)
|
|
265
254
|
* @returns Root TrieNode.
|
|
266
255
|
*/
|
|
267
|
-
|
|
268
256
|
get root() {
|
|
269
257
|
return this._root;
|
|
270
258
|
}
|
|
@@ -274,7 +262,6 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
|
|
|
274
262
|
* @remarks Time O(1), Space O(1)
|
|
275
263
|
* @returns Total number of elements.
|
|
276
264
|
*/
|
|
277
|
-
|
|
278
265
|
protected get _total() {
|
|
279
266
|
return this._size;
|
|
280
267
|
}
|
|
@@ -284,62 +271,22 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
|
|
|
284
271
|
* @remarks Time O(L), Space O(L)
|
|
285
272
|
* @param word - Word to insert.
|
|
286
273
|
* @returns True if the word was newly added.
|
|
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
|
-
* @example
|
|
327
|
-
* // basic Trie creation and add words
|
|
328
|
-
* // Create a simple Trie with initial words
|
|
329
|
-
* const trie = new Trie(['apple', 'app', 'apply']);
|
|
330
|
-
*
|
|
331
|
-
* // Verify size
|
|
332
|
-
* console.log(trie.size); // 3;
|
|
333
|
-
*
|
|
334
|
-
* // Check if words exist
|
|
335
|
-
* console.log(trie.has('apple')); // true;
|
|
336
|
-
* console.log(trie.has('app')); // true;
|
|
337
|
-
*
|
|
338
|
-
* // Add a new word
|
|
339
|
-
* trie.add('application');
|
|
340
|
-
* console.log(trie.size); // 4;
|
|
274
|
+
* @example
|
|
275
|
+
* // basic Trie creation and add words
|
|
276
|
+
* // Create a simple Trie with initial words
|
|
277
|
+
* const trie = new Trie(['apple', 'app', 'apply']);
|
|
278
|
+
*
|
|
279
|
+
* // Verify size
|
|
280
|
+
* console.log(trie.size); // 3;
|
|
281
|
+
*
|
|
282
|
+
* // Check if words exist
|
|
283
|
+
* console.log(trie.has('apple')); // true;
|
|
284
|
+
* console.log(trie.has('app')); // true;
|
|
285
|
+
*
|
|
286
|
+
* // Add a new word
|
|
287
|
+
* trie.add('application');
|
|
288
|
+
* console.log(trie.size); // 4;
|
|
341
289
|
*/
|
|
342
|
-
|
|
343
290
|
add(word: string): boolean {
|
|
344
291
|
word = this._caseProcess(word);
|
|
345
292
|
let cur = this.root;
|
|
@@ -365,51 +312,14 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
|
|
|
365
312
|
* @remarks Time O(ΣL), Space O(ΣL)
|
|
366
313
|
* @param words - Iterable of strings (or raw records if toElementFn is provided).
|
|
367
314
|
* @returns Array of per-word 'added' flags.
|
|
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
|
-
* @example
|
|
405
|
-
* // Add multiple words
|
|
406
|
-
* const trie = new Trie();
|
|
407
|
-
* trie.addMany(['cat', 'car', 'card']);
|
|
408
|
-
* console.log(trie.has('cat')); // true;
|
|
409
|
-
* console.log(trie.has('car')); // true;
|
|
410
|
-
* console.log(trie.size); // 3;
|
|
315
|
+
* @example
|
|
316
|
+
* // Add multiple words
|
|
317
|
+
* const trie = new Trie();
|
|
318
|
+
* trie.addMany(['cat', 'car', 'card']);
|
|
319
|
+
* console.log(trie.has('cat')); // true;
|
|
320
|
+
* console.log(trie.has('car')); // true;
|
|
321
|
+
* console.log(trie.size); // 3;
|
|
411
322
|
*/
|
|
412
|
-
|
|
413
323
|
addMany(words: Iterable<string> | Iterable<R>): boolean[] {
|
|
414
324
|
const ans: boolean[] = [];
|
|
415
325
|
for (const word of words) {
|
|
@@ -427,54 +337,14 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
|
|
|
427
337
|
* @remarks Time O(L), Space O(1)
|
|
428
338
|
* @param word - Word to search for.
|
|
429
339
|
* @returns True if present.
|
|
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
|
-
* @example
|
|
470
|
-
* // Check if a word exists
|
|
471
|
-
* const dict = new Trie(['apple', 'app', 'application']);
|
|
472
|
-
*
|
|
473
|
-
* console.log(dict.has('app')); // true;
|
|
474
|
-
* console.log(dict.has('apple')); // true;
|
|
475
|
-
* console.log(dict.has('ap')); // false;
|
|
340
|
+
* @example
|
|
341
|
+
* // Check if a word exists
|
|
342
|
+
* const dict = new Trie(['apple', 'app', 'application']);
|
|
343
|
+
*
|
|
344
|
+
* console.log(dict.has('app')); // true;
|
|
345
|
+
* console.log(dict.has('apple')); // true;
|
|
346
|
+
* console.log(dict.has('ap')); // false;
|
|
476
347
|
*/
|
|
477
|
-
|
|
478
348
|
override has(word: string): boolean {
|
|
479
349
|
word = this._caseProcess(word);
|
|
480
350
|
let cur = this.root;
|
|
@@ -490,50 +360,13 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
|
|
|
490
360
|
* Check whether the trie is empty.
|
|
491
361
|
* @remarks Time O(1), Space O(1)
|
|
492
362
|
* @returns True if size is 0.
|
|
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
|
-
* @example
|
|
530
|
-
* // Check if empty
|
|
531
|
-
* const trie = new Trie();
|
|
532
|
-
* console.log(trie.isEmpty()); // true;
|
|
533
|
-
* trie.add('word');
|
|
534
|
-
* console.log(trie.isEmpty()); // false;
|
|
363
|
+
* @example
|
|
364
|
+
* // Check if empty
|
|
365
|
+
* const trie = new Trie();
|
|
366
|
+
* console.log(trie.isEmpty()); // true;
|
|
367
|
+
* trie.add('word');
|
|
368
|
+
* console.log(trie.isEmpty()); // false;
|
|
535
369
|
*/
|
|
536
|
-
|
|
537
370
|
isEmpty(): boolean {
|
|
538
371
|
return this._size === 0;
|
|
539
372
|
}
|
|
@@ -542,49 +375,12 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
|
|
|
542
375
|
* Remove all words and reset to a fresh root.
|
|
543
376
|
* @remarks Time O(1), Space O(1)
|
|
544
377
|
* @returns void
|
|
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
|
-
* @example
|
|
582
|
-
* // Remove all words
|
|
583
|
-
* const trie = new Trie(['a', 'b', 'c']);
|
|
584
|
-
* trie.clear();
|
|
585
|
-
* console.log(trie.isEmpty()); // true;
|
|
378
|
+
* @example
|
|
379
|
+
* // Remove all words
|
|
380
|
+
* const trie = new Trie(['a', 'b', 'c']);
|
|
381
|
+
* trie.clear();
|
|
382
|
+
* console.log(trie.isEmpty()); // true;
|
|
586
383
|
*/
|
|
587
|
-
|
|
588
384
|
clear(): void {
|
|
589
385
|
this._size = 0;
|
|
590
386
|
this._root = new TrieNode('');
|
|
@@ -595,64 +391,24 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
|
|
|
595
391
|
* @remarks Time O(L), Space O(1)
|
|
596
392
|
* @param word - Word to delete.
|
|
597
393
|
* @returns True if a word was removed.
|
|
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
|
-
* @example
|
|
638
|
-
* // Trie delete and iteration
|
|
639
|
-
* const trie = new Trie(['car', 'card', 'care', 'careful', 'can', 'cat']);
|
|
640
|
-
*
|
|
641
|
-
* // Delete a word
|
|
642
|
-
* trie.delete('card');
|
|
643
|
-
* console.log(trie.has('card')); // false;
|
|
644
|
-
*
|
|
645
|
-
* // Word with same prefix still exists
|
|
646
|
-
* console.log(trie.has('care')); // true;
|
|
647
|
-
*
|
|
648
|
-
* // Size decreased
|
|
649
|
-
* console.log(trie.size); // 5;
|
|
650
|
-
*
|
|
651
|
-
* // Iterate through all words
|
|
652
|
-
* const allWords = [...trie];
|
|
653
|
-
* console.log(allWords.length); // 5;
|
|
394
|
+
* @example
|
|
395
|
+
* // Trie delete and iteration
|
|
396
|
+
* const trie = new Trie(['car', 'card', 'care', 'careful', 'can', 'cat']);
|
|
397
|
+
*
|
|
398
|
+
* // Delete a word
|
|
399
|
+
* trie.delete('card');
|
|
400
|
+
* console.log(trie.has('card')); // false;
|
|
401
|
+
*
|
|
402
|
+
* // Word with same prefix still exists
|
|
403
|
+
* console.log(trie.has('care')); // true;
|
|
404
|
+
*
|
|
405
|
+
* // Size decreased
|
|
406
|
+
* console.log(trie.size); // 5;
|
|
407
|
+
*
|
|
408
|
+
* // Iterate through all words
|
|
409
|
+
* const allWords = [...trie];
|
|
410
|
+
* console.log(allWords.length); // 5;
|
|
654
411
|
*/
|
|
655
|
-
|
|
656
412
|
delete(word: string): boolean {
|
|
657
413
|
word = this._caseProcess(word);
|
|
658
414
|
let isDeleted = false;
|
|
@@ -681,7 +437,6 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
|
|
|
681
437
|
}
|
|
682
438
|
return false;
|
|
683
439
|
};
|
|
684
|
-
|
|
685
440
|
dfs(this.root, 0);
|
|
686
441
|
if (isDeleted) {
|
|
687
442
|
this._size--;
|
|
@@ -694,7 +449,6 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
|
|
|
694
449
|
* @remarks Time O(N), Space O(H)
|
|
695
450
|
* @returns Maximum depth from root to a leaf.
|
|
696
451
|
*/
|
|
697
|
-
|
|
698
452
|
getHeight(): number {
|
|
699
453
|
const startNode = this.root;
|
|
700
454
|
let maxDepth = 0;
|
|
@@ -721,7 +475,6 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
|
|
|
721
475
|
* @param input - String to test as prefix.
|
|
722
476
|
* @returns True if input is a prefix but not a full word.
|
|
723
477
|
*/
|
|
724
|
-
|
|
725
478
|
hasPurePrefix(input: string): boolean {
|
|
726
479
|
input = this._caseProcess(input);
|
|
727
480
|
let cur = this.root;
|
|
@@ -738,54 +491,14 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
|
|
|
738
491
|
* @remarks Time O(L), Space O(1)
|
|
739
492
|
* @param input - String to test as prefix.
|
|
740
493
|
* @returns True if input matches a path from root.
|
|
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
|
-
* @example
|
|
781
|
-
* // Check if a prefix exists
|
|
782
|
-
* const trie = new Trie(['hello', 'help', 'world']);
|
|
783
|
-
*
|
|
784
|
-
* console.log(trie.hasPrefix('hel')); // true;
|
|
785
|
-
* console.log(trie.hasPrefix('wor')); // true;
|
|
786
|
-
* console.log(trie.hasPrefix('xyz')); // false;
|
|
494
|
+
* @example
|
|
495
|
+
* // Check if a prefix exists
|
|
496
|
+
* const trie = new Trie(['hello', 'help', 'world']);
|
|
497
|
+
*
|
|
498
|
+
* console.log(trie.hasPrefix('hel')); // true;
|
|
499
|
+
* console.log(trie.hasPrefix('wor')); // true;
|
|
500
|
+
* console.log(trie.hasPrefix('xyz')); // false;
|
|
787
501
|
*/
|
|
788
|
-
|
|
789
502
|
hasPrefix(input: string): boolean {
|
|
790
503
|
input = this._caseProcess(input);
|
|
791
504
|
let cur = this.root;
|
|
@@ -803,7 +516,6 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
|
|
|
803
516
|
* @param input - Candidate longest common prefix.
|
|
804
517
|
* @returns True if input equals the common prefix.
|
|
805
518
|
*/
|
|
806
|
-
|
|
807
519
|
hasCommonPrefix(input: string): boolean {
|
|
808
520
|
input = this._caseProcess(input);
|
|
809
521
|
let commonPre = '';
|
|
@@ -822,52 +534,12 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
|
|
|
822
534
|
* Return the longest common prefix among all words.
|
|
823
535
|
* @remarks Time O(H), Space O(1)
|
|
824
536
|
* @returns The longest common prefix string.
|
|
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
|
-
* @example
|
|
865
|
-
* // Find shared prefix
|
|
866
|
-
* const trie = new Trie(['flower', 'flow', 'flight']);
|
|
867
|
-
*
|
|
868
|
-
* console.log(trie.getLongestCommonPrefix()); // 'fl';
|
|
537
|
+
* @example
|
|
538
|
+
* // Find shared prefix
|
|
539
|
+
* const trie = new Trie(['flower', 'flow', 'flight']);
|
|
540
|
+
*
|
|
541
|
+
* console.log(trie.getLongestCommonPrefix()); // 'fl';
|
|
869
542
|
*/
|
|
870
|
-
|
|
871
543
|
getLongestCommonPrefix(): string {
|
|
872
544
|
let commonPre = '';
|
|
873
545
|
const dfs = (cur: TrieNode) => {
|
|
@@ -887,63 +559,22 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
|
|
|
887
559
|
* @param [max] - Maximum number of words to return; default is Number.MAX_SAFE_INTEGER.
|
|
888
560
|
* @param [isAllWhenEmptyPrefix] - When true, collect from root even if prefix is empty.
|
|
889
561
|
* @returns Array of collected words (at most max).
|
|
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
|
-
* @example
|
|
930
|
-
* // Trie getWords and prefix search
|
|
931
|
-
* const trie = new Trie(['apple', 'app', 'apply', 'application', 'apricot']);
|
|
932
|
-
*
|
|
933
|
-
* // Get all words with prefix 'app'
|
|
934
|
-
* const appWords = trie.getWords('app');
|
|
935
|
-
* console.log(appWords); // contains 'app';
|
|
936
|
-
* console.log(appWords); // contains 'apple';
|
|
937
|
-
* console.log(appWords); // contains 'apply';
|
|
938
|
-
* console.log(appWords); // contains 'application';
|
|
939
|
-
* expect(appWords).not.toContain('apricot');
|
|
562
|
+
* @example
|
|
563
|
+
* // Trie getWords and prefix search
|
|
564
|
+
* const trie = new Trie(['apple', 'app', 'apply', 'application', 'apricot']);
|
|
565
|
+
*
|
|
566
|
+
* // Get all words with prefix 'app'
|
|
567
|
+
* const appWords = trie.getWords('app');
|
|
568
|
+
* console.log(appWords); // contains 'app';
|
|
569
|
+
* console.log(appWords); // contains 'apple';
|
|
570
|
+
* console.log(appWords); // contains 'apply';
|
|
571
|
+
* console.log(appWords); // contains 'application';
|
|
572
|
+
* expect(appWords).not.toContain('apricot');
|
|
940
573
|
*/
|
|
941
|
-
|
|
942
574
|
getWords(prefix = '', max = Number.MAX_SAFE_INTEGER, isAllWhenEmptyPrefix = false): string[] {
|
|
943
575
|
prefix = this._caseProcess(prefix);
|
|
944
576
|
const words: string[] = [];
|
|
945
577
|
let found = 0;
|
|
946
|
-
|
|
947
578
|
const dfs = (node: TrieNode, word: string): void => {
|
|
948
579
|
for (const [char, childNode] of node.children) {
|
|
949
580
|
if (found >= max) return;
|
|
@@ -955,9 +586,7 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
|
|
|
955
586
|
found++;
|
|
956
587
|
}
|
|
957
588
|
};
|
|
958
|
-
|
|
959
589
|
let startNode = this.root;
|
|
960
|
-
|
|
961
590
|
if (prefix) {
|
|
962
591
|
for (const c of prefix) {
|
|
963
592
|
const nodeC = startNode.children.get(c);
|
|
@@ -968,9 +597,7 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
|
|
|
968
597
|
}
|
|
969
598
|
}
|
|
970
599
|
}
|
|
971
|
-
|
|
972
600
|
if (isAllWhenEmptyPrefix || startNode !== this.root) dfs(startNode, prefix);
|
|
973
|
-
|
|
974
601
|
return words;
|
|
975
602
|
}
|
|
976
603
|
|
|
@@ -978,50 +605,13 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
|
|
|
978
605
|
* Deep clone this trie by iterating and inserting all words.
|
|
979
606
|
* @remarks Time O(ΣL), Space O(ΣL)
|
|
980
607
|
* @returns A new trie with the same words and options.
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
* @example
|
|
1018
|
-
* // Create independent copy
|
|
1019
|
-
* const trie = new Trie(['hello', 'world']);
|
|
1020
|
-
* const copy = trie.clone();
|
|
1021
|
-
* copy.delete('hello');
|
|
1022
|
-
* console.log(trie.has('hello')); // true;
|
|
608
|
+
* @example
|
|
609
|
+
* // Create independent copy
|
|
610
|
+
* const trie = new Trie(['hello', 'world']);
|
|
611
|
+
* const copy = trie.clone();
|
|
612
|
+
* copy.delete('hello');
|
|
613
|
+
* console.log(trie.has('hello')); // true;
|
|
1023
614
|
*/
|
|
1024
|
-
|
|
1025
615
|
clone(): this {
|
|
1026
616
|
const next = this._createInstance();
|
|
1027
617
|
for (const x of this) next.add(x);
|
|
@@ -1034,49 +624,12 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
|
|
|
1034
624
|
* @param predicate - Predicate (word, index, trie) → boolean to keep word.
|
|
1035
625
|
* @param [thisArg] - Value for `this` inside the predicate.
|
|
1036
626
|
* @returns A new trie containing words that satisfy the predicate.
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
* @example
|
|
1074
|
-
* // Filter words
|
|
1075
|
-
* const trie = new Trie(['cat', 'car', 'dog', 'card']);
|
|
1076
|
-
* const result = trie.filter(w => w.startsWith('ca'));
|
|
1077
|
-
* console.log(result.size); // 3;
|
|
627
|
+
* @example
|
|
628
|
+
* // Filter words
|
|
629
|
+
* const trie = new Trie(['cat', 'car', 'dog', 'card']);
|
|
630
|
+
* const result = trie.filter(w => w.startsWith('ca'));
|
|
631
|
+
* console.log(result.size); // 3;
|
|
1078
632
|
*/
|
|
1079
|
-
|
|
1080
633
|
filter(predicate: ElementCallback<string, R, boolean>, thisArg?: unknown): this {
|
|
1081
634
|
const results = this._createInstance();
|
|
1082
635
|
let index = 0;
|
|
@@ -1089,49 +642,13 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
|
|
|
1089
642
|
return results;
|
|
1090
643
|
}
|
|
1091
644
|
|
|
1092
|
-
|
|
645
|
+
/**
|
|
1093
646
|
* Transform words
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
* @example
|
|
1131
|
-
* // Transform words
|
|
1132
|
-
* const trie = new Trie(['hello', 'world']);
|
|
1133
|
-
* const upper = trie.map(w => w.toUpperCase());
|
|
1134
|
-
* console.log(upper.has('HELLO')); // true;
|
|
647
|
+
* @example
|
|
648
|
+
* // Transform words
|
|
649
|
+
* const trie = new Trie(['hello', 'world']);
|
|
650
|
+
* const upper = trie.map(w => w.toUpperCase());
|
|
651
|
+
* console.log(upper.has('HELLO')); // true;
|
|
1135
652
|
*/
|
|
1136
653
|
map<RM>(callback: ElementCallback<string, R, string>, options?: TrieOptions<RM>, thisArg?: unknown): Trie<RM>;
|
|
1137
654
|
|
|
@@ -1145,13 +662,11 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
|
|
|
1145
662
|
* @param [thisArg] - Value for `this` inside the callback.
|
|
1146
663
|
* @returns A new Trie constructed from mapped words.
|
|
1147
664
|
*/
|
|
1148
|
-
|
|
1149
665
|
map<EM, RM>(
|
|
1150
666
|
callback: ElementCallback<string, R, EM>,
|
|
1151
667
|
options?: TrieOptions<RM>,
|
|
1152
668
|
thisArg?: unknown
|
|
1153
669
|
): IterableElementBase<EM, RM>;
|
|
1154
|
-
|
|
1155
670
|
map<EM, RM>(callback: ElementCallback<string, R, EM>, options?: TrieOptions<RM>, thisArg?: unknown): any {
|
|
1156
671
|
const newTrie = this._createLike<RM>([], options);
|
|
1157
672
|
let i = 0;
|
|
@@ -1172,7 +687,6 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
|
|
|
1172
687
|
* @param [thisArg] - Value for `this` inside the callback.
|
|
1173
688
|
* @returns A new trie with mapped words.
|
|
1174
689
|
*/
|
|
1175
|
-
|
|
1176
690
|
mapSame(callback: ElementCallback<string, R, string>, thisArg?: unknown): this {
|
|
1177
691
|
const next = this._createInstance();
|
|
1178
692
|
let i = 0;
|
|
@@ -1189,12 +703,8 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
|
|
|
1189
703
|
* @param [options] - Options forwarded to the constructor.
|
|
1190
704
|
* @returns An empty like-kind trie instance.
|
|
1191
705
|
*/
|
|
1192
|
-
|
|
1193
706
|
protected _createInstance(options?: TrieOptions<R>): this {
|
|
1194
|
-
const Ctor = this.constructor as new (
|
|
1195
|
-
elements?: Iterable<string> | Iterable<R>,
|
|
1196
|
-
options?: TrieOptions<R>
|
|
1197
|
-
) => this;
|
|
707
|
+
const Ctor = this.constructor as new (elements?: Iterable<string> | Iterable<R>, options?: TrieOptions<R>) => this;
|
|
1198
708
|
return new Ctor([], {
|
|
1199
709
|
toElementFn: this.toElementFn,
|
|
1200
710
|
caseSensitive: this.caseSensitive,
|
|
@@ -1210,7 +720,6 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
|
|
|
1210
720
|
* @param [options] - Options forwarded to the constructor.
|
|
1211
721
|
* @returns A like-kind Trie instance.
|
|
1212
722
|
*/
|
|
1213
|
-
|
|
1214
723
|
protected _createLike<RM>(elements: Iterable<string> | Iterable<RM> = [], options?: TrieOptions<RM>): Trie<RM> {
|
|
1215
724
|
const Ctor = this.constructor as new (
|
|
1216
725
|
elements?: Iterable<string> | Iterable<RM>,
|
|
@@ -1226,7 +735,6 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
|
|
|
1226
735
|
* @param [options] - Options forwarded to the constructor.
|
|
1227
736
|
* @returns An empty like-kind Trie instance.
|
|
1228
737
|
*/
|
|
1229
|
-
|
|
1230
738
|
protected _spawnLike<RM>(options?: TrieOptions<RM>): Trie<RM> {
|
|
1231
739
|
return this._createLike<RM>([], options);
|
|
1232
740
|
}
|
|
@@ -1236,7 +744,6 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
|
|
|
1236
744
|
* @remarks Time O(ΣL), Space O(H)
|
|
1237
745
|
* @returns Iterator of words.
|
|
1238
746
|
*/
|
|
1239
|
-
|
|
1240
747
|
protected *_getIterator(): IterableIterator<string> {
|
|
1241
748
|
function* _dfs(node: TrieNode, path: string): IterableIterator<string> {
|
|
1242
749
|
if (node.isEnd) {
|
|
@@ -1256,7 +763,6 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
|
|
|
1256
763
|
* @param str - Input string to normalize.
|
|
1257
764
|
* @returns Normalized string based on caseSensitive.
|
|
1258
765
|
*/
|
|
1259
|
-
|
|
1260
766
|
protected _caseProcess(str: string) {
|
|
1261
767
|
if (!this._caseSensitive) {
|
|
1262
768
|
str = str.toLowerCase();
|