trie-typed 2.1.0 → 2.1.2
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/dist/cjs/index.cjs +795 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/esm/index.mjs +790 -0
- package/dist/esm/index.mjs.map +1 -0
- package/dist/types/data-structures/base/index.d.ts +2 -1
- package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +182 -2
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +135 -2
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +291 -2
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +174 -1
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +754 -29
- package/dist/types/data-structures/binary-tree/bst.d.ts +413 -12
- package/dist/types/data-structures/binary-tree/index.d.ts +3 -2
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +208 -3
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +160 -1
- package/dist/types/data-structures/binary-tree/tree-counter.d.ts +190 -2
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +270 -2
- package/dist/types/data-structures/graph/abstract-graph.d.ts +340 -14
- package/dist/types/data-structures/graph/directed-graph.d.ts +207 -1
- package/dist/types/data-structures/graph/index.d.ts +2 -1
- package/dist/types/data-structures/graph/map-graph.d.ts +78 -1
- package/dist/types/data-structures/graph/undirected-graph.d.ts +188 -1
- package/dist/types/data-structures/hash/hash-map.d.ts +345 -19
- package/dist/types/data-structures/hash/index.d.ts +0 -1
- package/dist/types/data-structures/heap/heap.d.ts +503 -5
- package/dist/types/data-structures/heap/index.d.ts +2 -0
- package/dist/types/data-structures/heap/max-heap.d.ts +32 -1
- package/dist/types/data-structures/heap/min-heap.d.ts +33 -1
- package/dist/types/data-structures/index.d.ts +7 -7
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +769 -2
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +451 -2
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +27 -4
- package/dist/types/data-structures/matrix/index.d.ts +1 -1
- package/dist/types/data-structures/matrix/matrix.d.ts +168 -7
- package/dist/types/data-structures/matrix/navigator.d.ts +54 -13
- package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +27 -1
- package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +26 -1
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +15 -2
- package/dist/types/data-structures/queue/deque.d.ts +431 -4
- package/dist/types/data-structures/queue/queue.d.ts +308 -4
- package/dist/types/data-structures/stack/stack.d.ts +306 -2
- package/dist/types/data-structures/tree/tree.d.ts +62 -1
- package/dist/types/data-structures/trie/trie.d.ts +350 -4
- package/dist/types/index.d.ts +11 -2
- package/dist/{interfaces → types/interfaces}/binary-tree.d.ts +3 -3
- package/dist/types/types/data-structures/base/index.d.ts +1 -0
- package/dist/types/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -0
- package/dist/types/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -0
- package/dist/types/types/data-structures/binary-tree/avl-tree.d.ts +2 -0
- package/dist/types/types/data-structures/binary-tree/binary-indexed-tree.d.ts +1 -0
- package/dist/types/types/data-structures/binary-tree/binary-tree.d.ts +29 -0
- package/dist/types/types/data-structures/binary-tree/bst.d.ts +12 -0
- package/dist/{data-structures → types/types/data-structures}/binary-tree/index.d.ts +2 -3
- package/dist/types/types/data-structures/binary-tree/red-black-tree.d.ts +3 -0
- package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -0
- package/dist/types/types/data-structures/binary-tree/tree-counter.d.ts +2 -0
- package/dist/types/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -0
- package/dist/types/types/data-structures/graph/abstract-graph.d.ts +14 -0
- package/dist/types/types/data-structures/graph/directed-graph.d.ts +1 -0
- package/dist/{data-structures → types/types/data-structures}/graph/index.d.ts +1 -2
- package/dist/types/types/data-structures/graph/map-graph.d.ts +1 -0
- package/dist/types/types/data-structures/graph/undirected-graph.d.ts +1 -0
- package/dist/types/types/data-structures/hash/hash-map.d.ts +19 -0
- package/dist/types/types/data-structures/hash/index.d.ts +2 -0
- package/dist/types/types/data-structures/heap/heap.d.ts +5 -0
- package/dist/types/types/data-structures/heap/index.d.ts +1 -0
- package/dist/types/types/data-structures/heap/max-heap.d.ts +1 -0
- package/dist/types/types/data-structures/heap/min-heap.d.ts +1 -0
- package/dist/types/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -0
- package/dist/types/types/data-structures/linked-list/singly-linked-list.d.ts +2 -0
- package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +4 -0
- package/dist/types/types/data-structures/matrix/matrix.d.ts +7 -0
- package/dist/types/types/data-structures/matrix/navigator.d.ts +14 -0
- package/dist/types/types/data-structures/priority-queue/max-priority-queue.d.ts +1 -0
- package/dist/types/types/data-structures/priority-queue/min-priority-queue.d.ts +1 -0
- package/dist/types/types/data-structures/priority-queue/priority-queue.d.ts +2 -0
- package/dist/types/types/data-structures/queue/deque.d.ts +4 -0
- package/dist/types/types/data-structures/queue/queue.d.ts +4 -0
- package/dist/types/types/data-structures/stack/stack.d.ts +2 -0
- package/dist/types/types/data-structures/tree/tree.d.ts +1 -0
- package/dist/types/types/data-structures/trie/trie.d.ts +4 -0
- package/dist/types/types/index.d.ts +3 -0
- package/dist/types/types/utils/index.d.ts +2 -0
- package/dist/types/types/utils/utils.d.ts +22 -0
- package/dist/types/utils/index.d.ts +1 -1
- package/dist/types/utils/utils.d.ts +209 -22
- package/dist/umd/trie-typed.js +802 -0
- package/dist/umd/trie-typed.js.map +1 -0
- package/dist/umd/trie-typed.min.js +9 -0
- package/dist/umd/trie-typed.min.js.map +1 -0
- package/package.json +25 -5
- package/src/data-structures/binary-tree/avl-tree-counter.ts +8 -11
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +6 -11
- package/src/data-structures/binary-tree/avl-tree.ts +6 -8
- package/src/data-structures/binary-tree/binary-tree.ts +13 -15
- package/src/data-structures/binary-tree/bst.ts +6 -11
- package/src/data-structures/binary-tree/red-black-tree.ts +6 -11
- package/src/data-structures/binary-tree/tree-counter.ts +8 -13
- package/src/data-structures/binary-tree/tree-multi-map.ts +6 -11
- package/src/data-structures/heap/heap.ts +5 -5
- package/src/data-structures/linked-list/singly-linked-list.ts +2 -2
- package/src/interfaces/binary-tree.ts +3 -3
- package/tsconfig.base.json +23 -0
- package/tsconfig.json +8 -34
- package/tsconfig.test.json +8 -0
- package/tsconfig.types.json +15 -0
- package/tsup.config.js +28 -0
- package/tsup.node.config.js +37 -0
- package/dist/common/index.js +0 -28
- package/dist/constants/index.js +0 -8
- package/dist/data-structures/base/index.d.ts +0 -2
- package/dist/data-structures/base/index.js +0 -18
- package/dist/data-structures/base/iterable-element-base.js +0 -243
- package/dist/data-structures/base/iterable-entry-base.js +0 -183
- package/dist/data-structures/base/linear-base.js +0 -415
- package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +0 -182
- package/dist/data-structures/binary-tree/avl-tree-counter.js +0 -374
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +0 -135
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +0 -250
- package/dist/data-structures/binary-tree/avl-tree.d.ts +0 -291
- package/dist/data-structures/binary-tree/avl-tree.js +0 -611
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +0 -174
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +0 -294
- package/dist/data-structures/binary-tree/binary-tree.d.ts +0 -754
- package/dist/data-structures/binary-tree/binary-tree.js +0 -1925
- package/dist/data-structures/binary-tree/bst.d.ts +0 -413
- package/dist/data-structures/binary-tree/bst.js +0 -903
- package/dist/data-structures/binary-tree/index.js +0 -26
- package/dist/data-structures/binary-tree/red-black-tree.d.ts +0 -208
- package/dist/data-structures/binary-tree/red-black-tree.js +0 -546
- package/dist/data-structures/binary-tree/segment-tree.d.ts +0 -160
- package/dist/data-structures/binary-tree/segment-tree.js +0 -297
- package/dist/data-structures/binary-tree/tree-counter.d.ts +0 -190
- package/dist/data-structures/binary-tree/tree-counter.js +0 -413
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +0 -270
- package/dist/data-structures/binary-tree/tree-multi-map.js +0 -384
- package/dist/data-structures/graph/abstract-graph.d.ts +0 -340
- package/dist/data-structures/graph/abstract-graph.js +0 -896
- package/dist/data-structures/graph/directed-graph.d.ts +0 -207
- package/dist/data-structures/graph/directed-graph.js +0 -525
- package/dist/data-structures/graph/index.js +0 -20
- package/dist/data-structures/graph/map-graph.d.ts +0 -78
- package/dist/data-structures/graph/map-graph.js +0 -107
- package/dist/data-structures/graph/undirected-graph.d.ts +0 -188
- package/dist/data-structures/graph/undirected-graph.js +0 -424
- package/dist/data-structures/hash/hash-map.d.ts +0 -345
- package/dist/data-structures/hash/hash-map.js +0 -692
- package/dist/data-structures/hash/index.d.ts +0 -1
- package/dist/data-structures/hash/index.js +0 -17
- package/dist/data-structures/heap/heap.d.ts +0 -503
- package/dist/data-structures/heap/heap.js +0 -901
- package/dist/data-structures/heap/index.d.ts +0 -3
- package/dist/data-structures/heap/index.js +0 -19
- package/dist/data-structures/heap/max-heap.d.ts +0 -32
- package/dist/data-structures/heap/max-heap.js +0 -40
- package/dist/data-structures/heap/min-heap.d.ts +0 -33
- package/dist/data-structures/heap/min-heap.js +0 -31
- package/dist/data-structures/index.js +0 -28
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +0 -769
- package/dist/data-structures/linked-list/doubly-linked-list.js +0 -1111
- package/dist/data-structures/linked-list/index.js +0 -19
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +0 -451
- package/dist/data-structures/linked-list/singly-linked-list.js +0 -850
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +0 -27
- package/dist/data-structures/linked-list/skip-linked-list.js +0 -144
- package/dist/data-structures/matrix/index.js +0 -18
- package/dist/data-structures/matrix/matrix.d.ts +0 -168
- package/dist/data-structures/matrix/matrix.js +0 -448
- package/dist/data-structures/matrix/navigator.d.ts +0 -55
- package/dist/data-structures/matrix/navigator.js +0 -111
- package/dist/data-structures/priority-queue/index.js +0 -19
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +0 -27
- package/dist/data-structures/priority-queue/max-priority-queue.js +0 -34
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +0 -26
- package/dist/data-structures/priority-queue/min-priority-queue.js +0 -24
- package/dist/data-structures/priority-queue/priority-queue.d.ts +0 -15
- package/dist/data-structures/priority-queue/priority-queue.js +0 -20
- package/dist/data-structures/queue/deque.d.ts +0 -431
- package/dist/data-structures/queue/deque.js +0 -879
- package/dist/data-structures/queue/index.js +0 -18
- package/dist/data-structures/queue/queue.d.ts +0 -308
- package/dist/data-structures/queue/queue.js +0 -473
- package/dist/data-structures/stack/index.js +0 -17
- package/dist/data-structures/stack/stack.d.ts +0 -306
- package/dist/data-structures/stack/stack.js +0 -401
- package/dist/data-structures/tree/index.js +0 -17
- package/dist/data-structures/tree/tree.d.ts +0 -62
- package/dist/data-structures/tree/tree.js +0 -107
- package/dist/data-structures/trie/index.js +0 -17
- package/dist/data-structures/trie/trie.d.ts +0 -350
- package/dist/data-structures/trie/trie.js +0 -610
- package/dist/index.d.ts +0 -12
- package/dist/index.js +0 -28
- package/dist/interfaces/binary-tree.js +0 -2
- package/dist/interfaces/doubly-linked-list.js +0 -2
- package/dist/interfaces/graph.js +0 -2
- package/dist/interfaces/heap.js +0 -2
- package/dist/interfaces/index.js +0 -24
- package/dist/interfaces/navigator.js +0 -2
- package/dist/interfaces/priority-queue.js +0 -2
- package/dist/interfaces/segment-tree.js +0 -2
- package/dist/interfaces/singly-linked-list.js +0 -2
- package/dist/types/common.js +0 -2
- package/dist/types/data-structures/base/base.js +0 -2
- package/dist/types/data-structures/base/index.js +0 -17
- package/dist/types/data-structures/binary-tree/avl-tree-counter.js +0 -2
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.js +0 -2
- package/dist/types/data-structures/binary-tree/avl-tree.js +0 -2
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.js +0 -2
- package/dist/types/data-structures/binary-tree/binary-tree.js +0 -2
- package/dist/types/data-structures/binary-tree/bst.js +0 -2
- package/dist/types/data-structures/binary-tree/index.js +0 -25
- package/dist/types/data-structures/binary-tree/red-black-tree.js +0 -2
- package/dist/types/data-structures/binary-tree/segment-tree.js +0 -2
- package/dist/types/data-structures/binary-tree/tree-counter.js +0 -2
- package/dist/types/data-structures/binary-tree/tree-multi-map.js +0 -2
- package/dist/types/data-structures/graph/abstract-graph.js +0 -2
- package/dist/types/data-structures/graph/directed-graph.js +0 -2
- package/dist/types/data-structures/graph/index.js +0 -19
- package/dist/types/data-structures/graph/map-graph.js +0 -2
- package/dist/types/data-structures/graph/undirected-graph.js +0 -2
- package/dist/types/data-structures/hash/hash-map.js +0 -2
- package/dist/types/data-structures/hash/index.js +0 -17
- package/dist/types/data-structures/heap/heap.js +0 -2
- package/dist/types/data-structures/heap/index.js +0 -17
- package/dist/types/data-structures/heap/max-heap.js +0 -2
- package/dist/types/data-structures/heap/min-heap.js +0 -2
- package/dist/types/data-structures/index.js +0 -28
- package/dist/types/data-structures/linked-list/doubly-linked-list.js +0 -2
- package/dist/types/data-structures/linked-list/index.js +0 -19
- package/dist/types/data-structures/linked-list/singly-linked-list.js +0 -2
- package/dist/types/data-structures/linked-list/skip-linked-list.js +0 -2
- package/dist/types/data-structures/matrix/index.js +0 -18
- package/dist/types/data-structures/matrix/matrix.js +0 -2
- package/dist/types/data-structures/matrix/navigator.js +0 -2
- package/dist/types/data-structures/priority-queue/index.js +0 -19
- package/dist/types/data-structures/priority-queue/max-priority-queue.js +0 -2
- package/dist/types/data-structures/priority-queue/min-priority-queue.js +0 -2
- package/dist/types/data-structures/priority-queue/priority-queue.js +0 -2
- package/dist/types/data-structures/queue/deque.js +0 -2
- package/dist/types/data-structures/queue/index.js +0 -18
- package/dist/types/data-structures/queue/queue.js +0 -2
- package/dist/types/data-structures/stack/index.js +0 -17
- package/dist/types/data-structures/stack/stack.js +0 -2
- package/dist/types/data-structures/tree/index.js +0 -17
- package/dist/types/data-structures/tree/tree.js +0 -2
- package/dist/types/data-structures/trie/index.js +0 -17
- package/dist/types/data-structures/trie/trie.js +0 -2
- package/dist/types/index.js +0 -19
- package/dist/types/utils/index.js +0 -18
- package/dist/types/utils/utils.js +0 -2
- package/dist/types/utils/validate-type.js +0 -2
- package/dist/utils/index.d.ts +0 -2
- package/dist/utils/index.js +0 -18
- package/dist/utils/number.js +0 -24
- package/dist/utils/utils.d.ts +0 -209
- package/dist/utils/utils.js +0 -353
- package/dist/{common → types/common}/index.d.ts +0 -0
- package/dist/{constants → types/constants}/index.d.ts +0 -0
- package/dist/{data-structures → types/data-structures}/base/iterable-element-base.d.ts +0 -0
- package/dist/{data-structures → types/data-structures}/base/iterable-entry-base.d.ts +0 -0
- package/dist/{data-structures → types/data-structures}/base/linear-base.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/doubly-linked-list.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/graph.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/heap.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/index.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/navigator.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/priority-queue.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/segment-tree.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/singly-linked-list.d.ts +0 -0
- package/dist/types/{common.d.ts → types/common.d.ts} +0 -0
- package/dist/types/{data-structures → types/data-structures}/base/base.d.ts +0 -0
- package/dist/{data-structures → types/types/data-structures}/index.d.ts +7 -7
- package/dist/{data-structures → types/types/data-structures}/linked-list/index.d.ts +0 -0
- package/dist/{data-structures → types/types/data-structures}/matrix/index.d.ts +1 -1
- /package/dist/{data-structures → types/types/data-structures}/priority-queue/index.d.ts +0 -0
- /package/dist/{data-structures → types/types/data-structures}/queue/index.d.ts +0 -0
- /package/dist/{data-structures → types/types/data-structures}/stack/index.d.ts +0 -0
- /package/dist/{data-structures → types/types/data-structures}/tree/index.d.ts +0 -0
- /package/dist/{data-structures → types/types/data-structures}/trie/index.d.ts +0 -0
- /package/dist/types/{utils → types/utils}/validate-type.d.ts +0 -0
- /package/dist/{utils → types/utils}/number.d.ts +0 -0
|
@@ -0,0 +1,795 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
5
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
6
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
7
|
+
|
|
8
|
+
// src/data-structures/base/iterable-element-base.ts
|
|
9
|
+
var _IterableElementBase = class _IterableElementBase {
|
|
10
|
+
/**
|
|
11
|
+
* Create a new iterable base.
|
|
12
|
+
*
|
|
13
|
+
* @param options Optional behavior overrides. When provided, a `toElementFn`
|
|
14
|
+
* is used to convert a raw element (`R`) into a public element (`E`).
|
|
15
|
+
*
|
|
16
|
+
* @remarks
|
|
17
|
+
* Time O(1), Space O(1).
|
|
18
|
+
*/
|
|
19
|
+
constructor(options) {
|
|
20
|
+
/**
|
|
21
|
+
* The converter used to transform a raw element (`R`) into a public element (`E`).
|
|
22
|
+
*
|
|
23
|
+
* @remarks
|
|
24
|
+
* Time O(1), Space O(1).
|
|
25
|
+
*/
|
|
26
|
+
__publicField(this, "_toElementFn");
|
|
27
|
+
if (options) {
|
|
28
|
+
const { toElementFn } = options;
|
|
29
|
+
if (typeof toElementFn === "function") this._toElementFn = toElementFn;
|
|
30
|
+
else if (toElementFn) throw new TypeError("toElementFn must be a function type");
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Exposes the current `toElementFn`, if configured.
|
|
35
|
+
*
|
|
36
|
+
* @returns The converter function or `undefined` when not set.
|
|
37
|
+
* @remarks
|
|
38
|
+
* Time O(1), Space O(1).
|
|
39
|
+
*/
|
|
40
|
+
get toElementFn() {
|
|
41
|
+
return this._toElementFn;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Returns an iterator over the structure's elements.
|
|
45
|
+
*
|
|
46
|
+
* @param args Optional iterator arguments forwarded to the internal iterator.
|
|
47
|
+
* @returns An `IterableIterator<E>` that yields the elements in traversal order.
|
|
48
|
+
*
|
|
49
|
+
* @remarks
|
|
50
|
+
* Producing the iterator is O(1); consuming the entire iterator is Time O(n) with O(1) extra space.
|
|
51
|
+
*/
|
|
52
|
+
*[Symbol.iterator](...args) {
|
|
53
|
+
yield* this._getIterator(...args);
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Returns an iterator over the values (alias of the default iterator).
|
|
57
|
+
*
|
|
58
|
+
* @returns An `IterableIterator<E>` over all elements.
|
|
59
|
+
* @remarks
|
|
60
|
+
* Creating the iterator is O(1); full iteration is Time O(n), Space O(1).
|
|
61
|
+
*/
|
|
62
|
+
*values() {
|
|
63
|
+
for (const item of this) yield item;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Tests whether all elements satisfy the predicate.
|
|
67
|
+
*
|
|
68
|
+
* @template TReturn
|
|
69
|
+
* @param predicate Function invoked for each element with signature `(value, index, self)`.
|
|
70
|
+
* @param thisArg Optional `this` binding for the predicate.
|
|
71
|
+
* @returns `true` if every element passes; otherwise `false`.
|
|
72
|
+
*
|
|
73
|
+
* @remarks
|
|
74
|
+
* Time O(n) in the worst case; may exit early when the first failure is found. Space O(1).
|
|
75
|
+
*/
|
|
76
|
+
every(predicate, thisArg) {
|
|
77
|
+
let index = 0;
|
|
78
|
+
for (const item of this) {
|
|
79
|
+
if (thisArg === void 0) {
|
|
80
|
+
if (!predicate(item, index++, this)) return false;
|
|
81
|
+
} else {
|
|
82
|
+
const fn = predicate;
|
|
83
|
+
if (!fn.call(thisArg, item, index++, this)) return false;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return true;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Tests whether at least one element satisfies the predicate.
|
|
90
|
+
*
|
|
91
|
+
* @param predicate Function invoked for each element with signature `(value, index, self)`.
|
|
92
|
+
* @param thisArg Optional `this` binding for the predicate.
|
|
93
|
+
* @returns `true` if any element passes; otherwise `false`.
|
|
94
|
+
*
|
|
95
|
+
* @remarks
|
|
96
|
+
* Time O(n) in the worst case; may exit early on first success. Space O(1).
|
|
97
|
+
*/
|
|
98
|
+
some(predicate, thisArg) {
|
|
99
|
+
let index = 0;
|
|
100
|
+
for (const item of this) {
|
|
101
|
+
if (thisArg === void 0) {
|
|
102
|
+
if (predicate(item, index++, this)) return true;
|
|
103
|
+
} else {
|
|
104
|
+
const fn = predicate;
|
|
105
|
+
if (fn.call(thisArg, item, index++, this)) return true;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return false;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Invokes a callback for each element in iteration order.
|
|
112
|
+
*
|
|
113
|
+
* @param callbackfn Function invoked per element with signature `(value, index, self)`.
|
|
114
|
+
* @param thisArg Optional `this` binding for the callback.
|
|
115
|
+
* @returns `void`.
|
|
116
|
+
*
|
|
117
|
+
* @remarks
|
|
118
|
+
* Time O(n), Space O(1).
|
|
119
|
+
*/
|
|
120
|
+
forEach(callbackfn, thisArg) {
|
|
121
|
+
let index = 0;
|
|
122
|
+
for (const item of this) {
|
|
123
|
+
if (thisArg === void 0) {
|
|
124
|
+
callbackfn(item, index++, this);
|
|
125
|
+
} else {
|
|
126
|
+
const fn = callbackfn;
|
|
127
|
+
fn.call(thisArg, item, index++, this);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
// Implementation signature
|
|
132
|
+
find(predicate, thisArg) {
|
|
133
|
+
let index = 0;
|
|
134
|
+
for (const item of this) {
|
|
135
|
+
if (thisArg === void 0) {
|
|
136
|
+
if (predicate(item, index++, this)) return item;
|
|
137
|
+
} else {
|
|
138
|
+
const fn = predicate;
|
|
139
|
+
if (fn.call(thisArg, item, index++, this)) return item;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Checks whether a strictly-equal element exists in the structure.
|
|
146
|
+
*
|
|
147
|
+
* @param element The element to test with `===` equality.
|
|
148
|
+
* @returns `true` if an equal element is found; otherwise `false`.
|
|
149
|
+
*
|
|
150
|
+
* @remarks
|
|
151
|
+
* Time O(n) in the worst case. Space O(1).
|
|
152
|
+
*/
|
|
153
|
+
has(element) {
|
|
154
|
+
for (const ele of this) if (ele === element) return true;
|
|
155
|
+
return false;
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Reduces all elements to a single accumulated value.
|
|
159
|
+
*
|
|
160
|
+
* @overload
|
|
161
|
+
* @param callbackfn Reducer of signature `(acc, value, index, self) => nextAcc`. The first element is used as the initial accumulator.
|
|
162
|
+
* @returns The final accumulated value typed as `E`.
|
|
163
|
+
*
|
|
164
|
+
* @overload
|
|
165
|
+
* @param callbackfn Reducer of signature `(acc, value, index, self) => nextAcc`.
|
|
166
|
+
* @param initialValue The initial accumulator value of type `E`.
|
|
167
|
+
* @returns The final accumulated value typed as `E`.
|
|
168
|
+
*
|
|
169
|
+
* @overload
|
|
170
|
+
* @template U The accumulator type when it differs from `E`.
|
|
171
|
+
* @param callbackfn Reducer of signature `(acc: U, value, index, self) => U`.
|
|
172
|
+
* @param initialValue The initial accumulator value of type `U`.
|
|
173
|
+
* @returns The final accumulated value typed as `U`.
|
|
174
|
+
*
|
|
175
|
+
* @remarks
|
|
176
|
+
* Time O(n), Space O(1). Throws if called on an empty structure without `initialValue`.
|
|
177
|
+
*/
|
|
178
|
+
reduce(callbackfn, initialValue) {
|
|
179
|
+
let index = 0;
|
|
180
|
+
const iter = this[Symbol.iterator]();
|
|
181
|
+
let acc;
|
|
182
|
+
if (arguments.length >= 2) {
|
|
183
|
+
acc = initialValue;
|
|
184
|
+
} else {
|
|
185
|
+
const first = iter.next();
|
|
186
|
+
if (first.done) throw new TypeError("Reduce of empty structure with no initial value");
|
|
187
|
+
acc = first.value;
|
|
188
|
+
index = 1;
|
|
189
|
+
}
|
|
190
|
+
for (const value of iter) {
|
|
191
|
+
acc = callbackfn(acc, value, index++, this);
|
|
192
|
+
}
|
|
193
|
+
return acc;
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Materializes the elements into a new array.
|
|
197
|
+
*
|
|
198
|
+
* @returns A shallow array copy of the iteration order.
|
|
199
|
+
* @remarks
|
|
200
|
+
* Time O(n), Space O(n).
|
|
201
|
+
*/
|
|
202
|
+
toArray() {
|
|
203
|
+
return [...this];
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Returns a representation of the structure suitable for quick visualization.
|
|
207
|
+
* Defaults to an array of elements; subclasses may override to provide richer visuals.
|
|
208
|
+
*
|
|
209
|
+
* @returns A visual representation (array by default).
|
|
210
|
+
* @remarks
|
|
211
|
+
* Time O(n), Space O(n).
|
|
212
|
+
*/
|
|
213
|
+
toVisual() {
|
|
214
|
+
return [...this];
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Prints `toVisual()` to the console. Intended for quick debugging.
|
|
218
|
+
*
|
|
219
|
+
* @returns `void`.
|
|
220
|
+
* @remarks
|
|
221
|
+
* Time O(n) due to materialization, Space O(n) for the intermediate representation.
|
|
222
|
+
*/
|
|
223
|
+
print() {
|
|
224
|
+
console.log(this.toVisual());
|
|
225
|
+
}
|
|
226
|
+
};
|
|
227
|
+
__name(_IterableElementBase, "IterableElementBase");
|
|
228
|
+
var IterableElementBase = _IterableElementBase;
|
|
229
|
+
|
|
230
|
+
// src/data-structures/trie/trie.ts
|
|
231
|
+
var _TrieNode = class _TrieNode {
|
|
232
|
+
/**
|
|
233
|
+
* Create a Trie node with a character key.
|
|
234
|
+
* @remarks Time O(1), Space O(1)
|
|
235
|
+
* @returns New TrieNode instance.
|
|
236
|
+
*/
|
|
237
|
+
constructor(key) {
|
|
238
|
+
__publicField(this, "_key");
|
|
239
|
+
__publicField(this, "_children");
|
|
240
|
+
__publicField(this, "_isEnd");
|
|
241
|
+
this._key = key;
|
|
242
|
+
this._isEnd = false;
|
|
243
|
+
this._children = /* @__PURE__ */ new Map();
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* Get the character key of this node.
|
|
247
|
+
* @remarks Time O(1), Space O(1)
|
|
248
|
+
* @returns Character key string.
|
|
249
|
+
*/
|
|
250
|
+
get key() {
|
|
251
|
+
return this._key;
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* Set the character key of this node.
|
|
255
|
+
* @remarks Time O(1), Space O(1)
|
|
256
|
+
* @param value - New character key.
|
|
257
|
+
* @returns void
|
|
258
|
+
*/
|
|
259
|
+
set key(value) {
|
|
260
|
+
this._key = value;
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Get the child map of this node.
|
|
264
|
+
* @remarks Time O(1), Space O(1)
|
|
265
|
+
* @returns Map from character to child node.
|
|
266
|
+
*/
|
|
267
|
+
get children() {
|
|
268
|
+
return this._children;
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* Replace the child map of this node.
|
|
272
|
+
* @remarks Time O(1), Space O(1)
|
|
273
|
+
* @param value - New map of character → node.
|
|
274
|
+
* @returns void
|
|
275
|
+
*/
|
|
276
|
+
set children(value) {
|
|
277
|
+
this._children = value;
|
|
278
|
+
}
|
|
279
|
+
/**
|
|
280
|
+
* Check whether this node marks the end of a word.
|
|
281
|
+
* @remarks Time O(1), Space O(1)
|
|
282
|
+
* @returns True if this node ends a word.
|
|
283
|
+
*/
|
|
284
|
+
get isEnd() {
|
|
285
|
+
return this._isEnd;
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* Mark this node as the end of a word or not.
|
|
289
|
+
* @remarks Time O(1), Space O(1)
|
|
290
|
+
* @param value - Whether this node ends a word.
|
|
291
|
+
* @returns void
|
|
292
|
+
*/
|
|
293
|
+
set isEnd(value) {
|
|
294
|
+
this._isEnd = value;
|
|
295
|
+
}
|
|
296
|
+
};
|
|
297
|
+
__name(_TrieNode, "TrieNode");
|
|
298
|
+
var TrieNode = _TrieNode;
|
|
299
|
+
var _Trie = class _Trie extends IterableElementBase {
|
|
300
|
+
/**
|
|
301
|
+
* Create a Trie and optionally bulk-insert words.
|
|
302
|
+
* @remarks Time O(totalChars), Space O(totalChars)
|
|
303
|
+
* @param [words] - Iterable of strings (or raw records if toElementFn is provided).
|
|
304
|
+
* @param [options] - Options such as toElementFn and caseSensitive.
|
|
305
|
+
* @returns New Trie instance.
|
|
306
|
+
*/
|
|
307
|
+
constructor(words = [], options) {
|
|
308
|
+
super(options);
|
|
309
|
+
__publicField(this, "_size", 0);
|
|
310
|
+
__publicField(this, "_caseSensitive", true);
|
|
311
|
+
__publicField(this, "_root", new TrieNode(""));
|
|
312
|
+
if (options) {
|
|
313
|
+
const { caseSensitive } = options;
|
|
314
|
+
if (caseSensitive !== void 0) this._caseSensitive = caseSensitive;
|
|
315
|
+
}
|
|
316
|
+
if (words) {
|
|
317
|
+
this.addMany(words);
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
/**
|
|
321
|
+
* Get the number of stored words.
|
|
322
|
+
* @remarks Time O(1), Space O(1)
|
|
323
|
+
* @returns Word count.
|
|
324
|
+
*/
|
|
325
|
+
get size() {
|
|
326
|
+
return this._size;
|
|
327
|
+
}
|
|
328
|
+
/**
|
|
329
|
+
* Get whether comparisons are case-sensitive.
|
|
330
|
+
* @remarks Time O(1), Space O(1)
|
|
331
|
+
* @returns True if case-sensitive.
|
|
332
|
+
*/
|
|
333
|
+
get caseSensitive() {
|
|
334
|
+
return this._caseSensitive;
|
|
335
|
+
}
|
|
336
|
+
/**
|
|
337
|
+
* Get the root node.
|
|
338
|
+
* @remarks Time O(1), Space O(1)
|
|
339
|
+
* @returns Root TrieNode.
|
|
340
|
+
*/
|
|
341
|
+
get root() {
|
|
342
|
+
return this._root;
|
|
343
|
+
}
|
|
344
|
+
/**
|
|
345
|
+
* (Protected) Get total count for base class iteration.
|
|
346
|
+
* @remarks Time O(1), Space O(1)
|
|
347
|
+
* @returns Total number of elements.
|
|
348
|
+
*/
|
|
349
|
+
get _total() {
|
|
350
|
+
return this._size;
|
|
351
|
+
}
|
|
352
|
+
/**
|
|
353
|
+
* Insert one word into the trie.
|
|
354
|
+
* @remarks Time O(L), Space O(L)
|
|
355
|
+
* @param word - Word to insert.
|
|
356
|
+
* @returns True if the word was newly added.
|
|
357
|
+
*/
|
|
358
|
+
add(word) {
|
|
359
|
+
word = this._caseProcess(word);
|
|
360
|
+
let cur = this.root;
|
|
361
|
+
let isNewWord = false;
|
|
362
|
+
for (const c of word) {
|
|
363
|
+
let nodeC = cur.children.get(c);
|
|
364
|
+
if (!nodeC) {
|
|
365
|
+
nodeC = new TrieNode(c);
|
|
366
|
+
cur.children.set(c, nodeC);
|
|
367
|
+
}
|
|
368
|
+
cur = nodeC;
|
|
369
|
+
}
|
|
370
|
+
if (!cur.isEnd) {
|
|
371
|
+
isNewWord = true;
|
|
372
|
+
cur.isEnd = true;
|
|
373
|
+
this._size++;
|
|
374
|
+
}
|
|
375
|
+
return isNewWord;
|
|
376
|
+
}
|
|
377
|
+
/**
|
|
378
|
+
* Insert many words from an iterable.
|
|
379
|
+
* @remarks Time O(ΣL), Space O(ΣL)
|
|
380
|
+
* @param words - Iterable of strings (or raw records if toElementFn is provided).
|
|
381
|
+
* @returns Array of per-word 'added' flags.
|
|
382
|
+
*/
|
|
383
|
+
addMany(words) {
|
|
384
|
+
const ans = [];
|
|
385
|
+
for (const word of words) {
|
|
386
|
+
if (this.toElementFn) {
|
|
387
|
+
ans.push(this.add(this.toElementFn(word)));
|
|
388
|
+
} else {
|
|
389
|
+
ans.push(this.add(word));
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
return ans;
|
|
393
|
+
}
|
|
394
|
+
/**
|
|
395
|
+
* Check whether a word exists.
|
|
396
|
+
* @remarks Time O(L), Space O(1)
|
|
397
|
+
* @param word - Word to search for.
|
|
398
|
+
* @returns True if present.
|
|
399
|
+
*/
|
|
400
|
+
has(word) {
|
|
401
|
+
word = this._caseProcess(word);
|
|
402
|
+
let cur = this.root;
|
|
403
|
+
for (const c of word) {
|
|
404
|
+
const nodeC = cur.children.get(c);
|
|
405
|
+
if (!nodeC) return false;
|
|
406
|
+
cur = nodeC;
|
|
407
|
+
}
|
|
408
|
+
return cur.isEnd;
|
|
409
|
+
}
|
|
410
|
+
/**
|
|
411
|
+
* Check whether the trie is empty.
|
|
412
|
+
* @remarks Time O(1), Space O(1)
|
|
413
|
+
* @returns True if size is 0.
|
|
414
|
+
*/
|
|
415
|
+
isEmpty() {
|
|
416
|
+
return this._size === 0;
|
|
417
|
+
}
|
|
418
|
+
/**
|
|
419
|
+
* Remove all words and reset to a fresh root.
|
|
420
|
+
* @remarks Time O(1), Space O(1)
|
|
421
|
+
* @returns void
|
|
422
|
+
*/
|
|
423
|
+
clear() {
|
|
424
|
+
this._size = 0;
|
|
425
|
+
this._root = new TrieNode("");
|
|
426
|
+
}
|
|
427
|
+
/**
|
|
428
|
+
* Delete one word if present.
|
|
429
|
+
* @remarks Time O(L), Space O(1)
|
|
430
|
+
* @param word - Word to delete.
|
|
431
|
+
* @returns True if a word was removed.
|
|
432
|
+
*/
|
|
433
|
+
delete(word) {
|
|
434
|
+
word = this._caseProcess(word);
|
|
435
|
+
let isDeleted = false;
|
|
436
|
+
const dfs = /* @__PURE__ */ __name((cur, i) => {
|
|
437
|
+
const char = word[i];
|
|
438
|
+
const child = cur.children.get(char);
|
|
439
|
+
if (child) {
|
|
440
|
+
if (i === word.length - 1) {
|
|
441
|
+
if (child.isEnd) {
|
|
442
|
+
if (child.children.size > 0) {
|
|
443
|
+
child.isEnd = false;
|
|
444
|
+
} else {
|
|
445
|
+
cur.children.delete(char);
|
|
446
|
+
}
|
|
447
|
+
isDeleted = true;
|
|
448
|
+
return true;
|
|
449
|
+
}
|
|
450
|
+
return false;
|
|
451
|
+
}
|
|
452
|
+
const res = dfs(child, i + 1);
|
|
453
|
+
if (res && !cur.isEnd && child.children.size === 0) {
|
|
454
|
+
cur.children.delete(char);
|
|
455
|
+
return true;
|
|
456
|
+
}
|
|
457
|
+
return false;
|
|
458
|
+
}
|
|
459
|
+
return false;
|
|
460
|
+
}, "dfs");
|
|
461
|
+
dfs(this.root, 0);
|
|
462
|
+
if (isDeleted) {
|
|
463
|
+
this._size--;
|
|
464
|
+
}
|
|
465
|
+
return isDeleted;
|
|
466
|
+
}
|
|
467
|
+
/**
|
|
468
|
+
* Compute the height (max depth) of the trie.
|
|
469
|
+
* @remarks Time O(N), Space O(H)
|
|
470
|
+
* @returns Maximum depth from root to a leaf.
|
|
471
|
+
*/
|
|
472
|
+
getHeight() {
|
|
473
|
+
const startNode = this.root;
|
|
474
|
+
let maxDepth = 0;
|
|
475
|
+
if (startNode) {
|
|
476
|
+
const bfs = /* @__PURE__ */ __name((node, level) => {
|
|
477
|
+
if (level > maxDepth) {
|
|
478
|
+
maxDepth = level;
|
|
479
|
+
}
|
|
480
|
+
const { children } = node;
|
|
481
|
+
if (children) {
|
|
482
|
+
for (const child of children.entries()) {
|
|
483
|
+
bfs(child[1], level + 1);
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
}, "bfs");
|
|
487
|
+
bfs(startNode, 0);
|
|
488
|
+
}
|
|
489
|
+
return maxDepth;
|
|
490
|
+
}
|
|
491
|
+
/**
|
|
492
|
+
* Check whether input is a proper prefix of at least one word.
|
|
493
|
+
* @remarks Time O(L), Space O(1)
|
|
494
|
+
* @param input - String to test as prefix.
|
|
495
|
+
* @returns True if input is a prefix but not a full word.
|
|
496
|
+
*/
|
|
497
|
+
hasPurePrefix(input) {
|
|
498
|
+
input = this._caseProcess(input);
|
|
499
|
+
let cur = this.root;
|
|
500
|
+
for (const c of input) {
|
|
501
|
+
const nodeC = cur.children.get(c);
|
|
502
|
+
if (!nodeC) return false;
|
|
503
|
+
cur = nodeC;
|
|
504
|
+
}
|
|
505
|
+
return !cur.isEnd;
|
|
506
|
+
}
|
|
507
|
+
/**
|
|
508
|
+
* Check whether any word starts with input.
|
|
509
|
+
* @remarks Time O(L), Space O(1)
|
|
510
|
+
* @param input - String to test as prefix.
|
|
511
|
+
* @returns True if input matches a path from root.
|
|
512
|
+
*/
|
|
513
|
+
hasPrefix(input) {
|
|
514
|
+
input = this._caseProcess(input);
|
|
515
|
+
let cur = this.root;
|
|
516
|
+
for (const c of input) {
|
|
517
|
+
const nodeC = cur.children.get(c);
|
|
518
|
+
if (!nodeC) return false;
|
|
519
|
+
cur = nodeC;
|
|
520
|
+
}
|
|
521
|
+
return true;
|
|
522
|
+
}
|
|
523
|
+
/**
|
|
524
|
+
* Check whether the trie’s longest common prefix equals input.
|
|
525
|
+
* @remarks Time O(min(H,L)), Space O(1)
|
|
526
|
+
* @param input - Candidate longest common prefix.
|
|
527
|
+
* @returns True if input equals the common prefix.
|
|
528
|
+
*/
|
|
529
|
+
hasCommonPrefix(input) {
|
|
530
|
+
input = this._caseProcess(input);
|
|
531
|
+
let commonPre = "";
|
|
532
|
+
const dfs = /* @__PURE__ */ __name((cur) => {
|
|
533
|
+
commonPre += cur.key;
|
|
534
|
+
if (commonPre === input) return;
|
|
535
|
+
if (cur.isEnd) return;
|
|
536
|
+
if (cur && cur.children && cur.children.size === 1) dfs(Array.from(cur.children.values())[0]);
|
|
537
|
+
else return;
|
|
538
|
+
}, "dfs");
|
|
539
|
+
dfs(this.root);
|
|
540
|
+
return commonPre === input;
|
|
541
|
+
}
|
|
542
|
+
/**
|
|
543
|
+
* Return the longest common prefix among all words.
|
|
544
|
+
* @remarks Time O(H), Space O(1)
|
|
545
|
+
* @returns The longest common prefix string.
|
|
546
|
+
*/
|
|
547
|
+
getLongestCommonPrefix() {
|
|
548
|
+
let commonPre = "";
|
|
549
|
+
const dfs = /* @__PURE__ */ __name((cur) => {
|
|
550
|
+
commonPre += cur.key;
|
|
551
|
+
if (cur.isEnd) return;
|
|
552
|
+
if (cur && cur.children && cur.children.size === 1) dfs(Array.from(cur.children.values())[0]);
|
|
553
|
+
else return;
|
|
554
|
+
}, "dfs");
|
|
555
|
+
dfs(this.root);
|
|
556
|
+
return commonPre;
|
|
557
|
+
}
|
|
558
|
+
/**
|
|
559
|
+
* Collect words under a prefix up to a maximum count.
|
|
560
|
+
* @remarks Time O(K·L), Space O(K·L)
|
|
561
|
+
* @param [prefix] - Prefix to match; default empty string for root.
|
|
562
|
+
* @param [max] - Maximum number of words to return; default is Number.MAX_SAFE_INTEGER.
|
|
563
|
+
* @param [isAllWhenEmptyPrefix] - When true, collect from root even if prefix is empty.
|
|
564
|
+
* @returns Array of collected words (at most max).
|
|
565
|
+
*/
|
|
566
|
+
getWords(prefix = "", max = Number.MAX_SAFE_INTEGER, isAllWhenEmptyPrefix = false) {
|
|
567
|
+
prefix = this._caseProcess(prefix);
|
|
568
|
+
const words = [];
|
|
569
|
+
let found = 0;
|
|
570
|
+
function dfs(node, word) {
|
|
571
|
+
for (const char of node.children.keys()) {
|
|
572
|
+
const charNode = node.children.get(char);
|
|
573
|
+
if (charNode !== void 0) {
|
|
574
|
+
dfs(charNode, word.concat(char));
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
if (node.isEnd) {
|
|
578
|
+
if (found > max - 1) return;
|
|
579
|
+
words.push(word);
|
|
580
|
+
found++;
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
__name(dfs, "dfs");
|
|
584
|
+
let startNode = this.root;
|
|
585
|
+
if (prefix) {
|
|
586
|
+
for (const c of prefix) {
|
|
587
|
+
const nodeC = startNode.children.get(c);
|
|
588
|
+
if (nodeC) {
|
|
589
|
+
startNode = nodeC;
|
|
590
|
+
} else {
|
|
591
|
+
return [];
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
if (isAllWhenEmptyPrefix || startNode !== this.root) dfs(startNode, prefix);
|
|
596
|
+
return words;
|
|
597
|
+
}
|
|
598
|
+
/**
|
|
599
|
+
* Deep clone this trie by iterating and inserting all words.
|
|
600
|
+
* @remarks Time O(ΣL), Space O(ΣL)
|
|
601
|
+
* @returns A new trie with the same words and options.
|
|
602
|
+
*/
|
|
603
|
+
clone() {
|
|
604
|
+
const next = this._createInstance();
|
|
605
|
+
for (const x of this) next.add(x);
|
|
606
|
+
return next;
|
|
607
|
+
}
|
|
608
|
+
/**
|
|
609
|
+
* Filter words into a new trie of the same class.
|
|
610
|
+
* @remarks Time O(ΣL), Space O(ΣL)
|
|
611
|
+
* @param predicate - Predicate (word, index, trie) → boolean to keep word.
|
|
612
|
+
* @param [thisArg] - Value for `this` inside the predicate.
|
|
613
|
+
* @returns A new trie containing words that satisfy the predicate.
|
|
614
|
+
*/
|
|
615
|
+
filter(predicate, thisArg) {
|
|
616
|
+
const results = this._createInstance();
|
|
617
|
+
let index = 0;
|
|
618
|
+
for (const word of this) {
|
|
619
|
+
if (predicate.call(thisArg, word, index, this)) {
|
|
620
|
+
results.add(word);
|
|
621
|
+
}
|
|
622
|
+
index++;
|
|
623
|
+
}
|
|
624
|
+
return results;
|
|
625
|
+
}
|
|
626
|
+
map(callback, options, thisArg) {
|
|
627
|
+
const newTrie = this._createLike([], options);
|
|
628
|
+
let i = 0;
|
|
629
|
+
for (const x of this) {
|
|
630
|
+
const v = thisArg === void 0 ? callback(x, i++, this) : callback.call(thisArg, x, i++, this);
|
|
631
|
+
if (typeof v !== "string") {
|
|
632
|
+
throw new TypeError(`Trie.map callback must return string; got ${typeof v}`);
|
|
633
|
+
}
|
|
634
|
+
newTrie.add(v);
|
|
635
|
+
}
|
|
636
|
+
return newTrie;
|
|
637
|
+
}
|
|
638
|
+
/**
|
|
639
|
+
* Map words into a new trie of the same element type.
|
|
640
|
+
* @remarks Time O(ΣL), Space O(ΣL)
|
|
641
|
+
* @param callback - Mapping function (word, index, trie) → string.
|
|
642
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
643
|
+
* @returns A new trie with mapped words.
|
|
644
|
+
*/
|
|
645
|
+
mapSame(callback, thisArg) {
|
|
646
|
+
const next = this._createInstance();
|
|
647
|
+
let i = 0;
|
|
648
|
+
for (const key of this) {
|
|
649
|
+
const mapped = thisArg === void 0 ? callback(key, i++, this) : callback.call(thisArg, key, i++, this);
|
|
650
|
+
next.add(mapped);
|
|
651
|
+
}
|
|
652
|
+
return next;
|
|
653
|
+
}
|
|
654
|
+
/**
|
|
655
|
+
* (Protected) Create an empty instance of the same concrete class.
|
|
656
|
+
* @remarks Time O(1), Space O(1)
|
|
657
|
+
* @param [options] - Options forwarded to the constructor.
|
|
658
|
+
* @returns An empty like-kind trie instance.
|
|
659
|
+
*/
|
|
660
|
+
_createInstance(options) {
|
|
661
|
+
const Ctor = this.constructor;
|
|
662
|
+
const next = new Ctor([], {
|
|
663
|
+
toElementFn: this.toElementFn,
|
|
664
|
+
caseSensitive: this.caseSensitive,
|
|
665
|
+
...options != null ? options : {}
|
|
666
|
+
});
|
|
667
|
+
return next;
|
|
668
|
+
}
|
|
669
|
+
/**
|
|
670
|
+
* (Protected) Create a like-kind trie and seed it from an iterable.
|
|
671
|
+
* @remarks Time O(ΣL), Space O(ΣL)
|
|
672
|
+
* @template RM
|
|
673
|
+
* @param [elements] - Iterable used to seed the new trie.
|
|
674
|
+
* @param [options] - Options forwarded to the constructor.
|
|
675
|
+
* @returns A like-kind Trie instance.
|
|
676
|
+
*/
|
|
677
|
+
_createLike(elements = [], options) {
|
|
678
|
+
const Ctor = this.constructor;
|
|
679
|
+
return new Ctor(elements, options);
|
|
680
|
+
}
|
|
681
|
+
/**
|
|
682
|
+
* (Protected) Spawn an empty like-kind trie instance.
|
|
683
|
+
* @remarks Time O(1), Space O(1)
|
|
684
|
+
* @template RM
|
|
685
|
+
* @param [options] - Options forwarded to the constructor.
|
|
686
|
+
* @returns An empty like-kind Trie instance.
|
|
687
|
+
*/
|
|
688
|
+
_spawnLike(options) {
|
|
689
|
+
return this._createLike([], options);
|
|
690
|
+
}
|
|
691
|
+
/**
|
|
692
|
+
* (Protected) Iterate all words in lexicographic order of edges.
|
|
693
|
+
* @remarks Time O(ΣL), Space O(H)
|
|
694
|
+
* @returns Iterator of words.
|
|
695
|
+
*/
|
|
696
|
+
*_getIterator() {
|
|
697
|
+
function* _dfs(node, path) {
|
|
698
|
+
if (node.isEnd) {
|
|
699
|
+
yield path;
|
|
700
|
+
}
|
|
701
|
+
for (const [char, childNode] of node.children) {
|
|
702
|
+
yield* _dfs(childNode, path + char);
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
__name(_dfs, "_dfs");
|
|
706
|
+
yield* _dfs(this.root, "");
|
|
707
|
+
}
|
|
708
|
+
/**
|
|
709
|
+
* (Protected) Normalize a string according to case sensitivity.
|
|
710
|
+
* @remarks Time O(L), Space O(L)
|
|
711
|
+
* @param str - Input string to normalize.
|
|
712
|
+
* @returns Normalized string based on caseSensitive.
|
|
713
|
+
*/
|
|
714
|
+
_caseProcess(str) {
|
|
715
|
+
if (!this._caseSensitive) {
|
|
716
|
+
str = str.toLowerCase();
|
|
717
|
+
}
|
|
718
|
+
return str;
|
|
719
|
+
}
|
|
720
|
+
};
|
|
721
|
+
__name(_Trie, "Trie");
|
|
722
|
+
var Trie = _Trie;
|
|
723
|
+
|
|
724
|
+
// src/utils/utils.ts
|
|
725
|
+
function isPrimitiveComparable(value) {
|
|
726
|
+
const valueType = typeof value;
|
|
727
|
+
if (valueType === "number") return true;
|
|
728
|
+
return valueType === "bigint" || valueType === "string" || valueType === "boolean";
|
|
729
|
+
}
|
|
730
|
+
__name(isPrimitiveComparable, "isPrimitiveComparable");
|
|
731
|
+
function tryObjectToPrimitive(obj) {
|
|
732
|
+
if (typeof obj.valueOf === "function") {
|
|
733
|
+
const valueOfResult = obj.valueOf();
|
|
734
|
+
if (valueOfResult !== obj) {
|
|
735
|
+
if (isPrimitiveComparable(valueOfResult)) return valueOfResult;
|
|
736
|
+
if (typeof valueOfResult === "object" && valueOfResult !== null) return tryObjectToPrimitive(valueOfResult);
|
|
737
|
+
}
|
|
738
|
+
}
|
|
739
|
+
if (typeof obj.toString === "function") {
|
|
740
|
+
const stringResult = obj.toString();
|
|
741
|
+
if (stringResult !== "[object Object]") return stringResult;
|
|
742
|
+
}
|
|
743
|
+
return null;
|
|
744
|
+
}
|
|
745
|
+
__name(tryObjectToPrimitive, "tryObjectToPrimitive");
|
|
746
|
+
function isComparable(value, isForceObjectComparable = false) {
|
|
747
|
+
if (value === null || value === void 0) return false;
|
|
748
|
+
if (isPrimitiveComparable(value)) return true;
|
|
749
|
+
if (typeof value !== "object") return false;
|
|
750
|
+
if (value instanceof Date) return true;
|
|
751
|
+
if (isForceObjectComparable) return true;
|
|
752
|
+
const comparableValue = tryObjectToPrimitive(value);
|
|
753
|
+
if (comparableValue === null || comparableValue === void 0) return false;
|
|
754
|
+
return isPrimitiveComparable(comparableValue);
|
|
755
|
+
}
|
|
756
|
+
__name(isComparable, "isComparable");
|
|
757
|
+
|
|
758
|
+
// src/common/index.ts
|
|
759
|
+
var DFSOperation = /* @__PURE__ */ ((DFSOperation2) => {
|
|
760
|
+
DFSOperation2[DFSOperation2["VISIT"] = 0] = "VISIT";
|
|
761
|
+
DFSOperation2[DFSOperation2["PROCESS"] = 1] = "PROCESS";
|
|
762
|
+
return DFSOperation2;
|
|
763
|
+
})(DFSOperation || {});
|
|
764
|
+
var _Range = class _Range {
|
|
765
|
+
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
766
|
+
this.low = low;
|
|
767
|
+
this.high = high;
|
|
768
|
+
this.includeLow = includeLow;
|
|
769
|
+
this.includeHigh = includeHigh;
|
|
770
|
+
if (!(isComparable(low) && isComparable(high))) throw new RangeError("low or high is not comparable");
|
|
771
|
+
if (low > high) throw new RangeError("low must be less than or equal to high");
|
|
772
|
+
}
|
|
773
|
+
// Determine whether a key is within the range
|
|
774
|
+
isInRange(key, comparator) {
|
|
775
|
+
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
776
|
+
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
777
|
+
return lowCheck && highCheck;
|
|
778
|
+
}
|
|
779
|
+
};
|
|
780
|
+
__name(_Range, "Range");
|
|
781
|
+
var Range = _Range;
|
|
782
|
+
/**
|
|
783
|
+
* data-structure-typed
|
|
784
|
+
*
|
|
785
|
+
* @author Pablo Zeng
|
|
786
|
+
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
787
|
+
* @license MIT License
|
|
788
|
+
*/
|
|
789
|
+
|
|
790
|
+
exports.DFSOperation = DFSOperation;
|
|
791
|
+
exports.Range = Range;
|
|
792
|
+
exports.Trie = Trie;
|
|
793
|
+
exports.TrieNode = TrieNode;
|
|
794
|
+
//# sourceMappingURL=index.cjs.map
|
|
795
|
+
//# sourceMappingURL=index.cjs.map
|