data-structure-typed 2.2.7 → 2.3.0
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 +9 -0
- package/CHANGELOG.md +1 -1
- package/README.md +14 -3
- package/README_CN.md +119 -275
- package/benchmark/report.html +1 -1
- package/benchmark/report.json +20 -324
- package/dist/cjs/index.cjs +689 -182
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +693 -185
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +689 -182
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +693 -185
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/leetcode/avl-tree-counter.mjs +2957 -0
- package/dist/leetcode/avl-tree-multi-map.mjs +2889 -0
- package/dist/leetcode/avl-tree.mjs +2720 -0
- package/dist/leetcode/binary-tree.mjs +1594 -0
- package/dist/leetcode/bst.mjs +2398 -0
- package/dist/leetcode/deque.mjs +683 -0
- package/dist/leetcode/directed-graph.mjs +1733 -0
- package/dist/leetcode/doubly-linked-list.mjs +709 -0
- package/dist/leetcode/hash-map.mjs +493 -0
- package/dist/leetcode/heap.mjs +542 -0
- package/dist/leetcode/max-heap.mjs +375 -0
- package/dist/leetcode/max-priority-queue.mjs +383 -0
- package/dist/leetcode/min-heap.mjs +363 -0
- package/dist/leetcode/min-priority-queue.mjs +371 -0
- package/dist/leetcode/priority-queue.mjs +363 -0
- package/dist/leetcode/queue.mjs +943 -0
- package/dist/leetcode/red-black-tree.mjs +2765 -0
- package/dist/leetcode/singly-linked-list.mjs +754 -0
- package/dist/leetcode/stack.mjs +217 -0
- package/dist/leetcode/tree-counter.mjs +3039 -0
- package/dist/leetcode/tree-multi-map.mjs +2913 -0
- package/dist/leetcode/trie.mjs +413 -0
- package/dist/leetcode/undirected-graph.mjs +1650 -0
- package/dist/types/data-structures/base/linear-base.d.ts +6 -6
- package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +10 -10
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +25 -27
- package/dist/types/data-structures/binary-tree/bst.d.ts +13 -12
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +151 -21
- package/dist/types/data-structures/binary-tree/tree-counter.d.ts +4 -4
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -2
- package/dist/types/interfaces/binary-tree.d.ts +1 -1
- package/dist/umd/data-structure-typed.js +689 -181
- package/dist/umd/data-structure-typed.js.map +1 -1
- package/dist/umd/data-structure-typed.min.js +3 -3
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +50 -172
- package/src/data-structures/base/linear-base.ts +2 -12
- package/src/data-structures/binary-tree/avl-tree-counter.ts +6 -6
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +13 -13
- package/src/data-structures/binary-tree/avl-tree.ts +15 -15
- package/src/data-structures/binary-tree/binary-tree.ts +57 -60
- package/src/data-structures/binary-tree/bst.ts +100 -26
- package/src/data-structures/binary-tree/red-black-tree.ts +586 -76
- package/src/data-structures/binary-tree/tree-counter.ts +25 -13
- package/src/data-structures/binary-tree/tree-multi-map.ts +13 -13
- package/src/data-structures/queue/deque.ts +10 -0
- package/src/interfaces/binary-tree.ts +1 -1
- package/test/performance/data-structures/binary-tree/red-black-tree.test.ts +1 -2
- package/test/unit/data-structures/base/iterable-element-base.coverage.test.ts +106 -0
- package/test/unit/data-structures/base/iterable-element-base.more-branches.coverage.test.ts +61 -0
- package/test/unit/data-structures/base/linear-base.array.coverage.test.ts +168 -0
- package/test/unit/data-structures/base/linear-base.concat-else.coverage.test.ts +82 -0
- package/test/unit/data-structures/base/linear-base.coverage.test.ts +72 -0
- package/test/unit/data-structures/base/linear-base.more-branches.coverage.test.ts +417 -0
- package/test/unit/data-structures/binary-tree/avl-tree-counter.more-branches-3.coverage.test.ts +146 -0
- package/test/unit/data-structures/binary-tree/avl-tree-counter.more-branches.coverage.test.ts +93 -0
- package/test/unit/data-structures/binary-tree/avl-tree-counter.test.ts +30 -30
- package/test/unit/data-structures/binary-tree/avl-tree-multi-map.coverage.test.ts +108 -0
- package/test/unit/data-structures/binary-tree/avl-tree-multi-map.more-branches-2.coverage.test.ts +85 -0
- package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +46 -46
- package/test/unit/data-structures/binary-tree/avl-tree-node.familyPosition-root-left.coverage.test.ts +17 -0
- package/test/unit/data-structures/binary-tree/avl-tree.more-branches-2.coverage.test.ts +99 -0
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +43 -43
- package/test/unit/data-structures/binary-tree/binary-indexed-tree.more-branches.coverage.test.ts +18 -0
- package/test/unit/data-structures/binary-tree/binary-tree.more-branches.coverage.test.ts +56 -0
- package/test/unit/data-structures/binary-tree/binary-tree.remaining-branches.coverage.test.ts +229 -0
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +151 -151
- package/test/unit/data-structures/binary-tree/bst.bound-by-predicate.coverage.test.ts +33 -0
- package/test/unit/data-structures/binary-tree/bst.coverage.test.ts +94 -0
- package/test/unit/data-structures/binary-tree/bst.deletebykey.coverage.test.ts +70 -0
- package/test/unit/data-structures/binary-tree/bst.deletewhere.coverage.test.ts +37 -0
- package/test/unit/data-structures/binary-tree/bst.floor-lower-predicate.coverage.test.ts +29 -0
- package/test/unit/data-structures/binary-tree/bst.floor-setmany.coverage.test.ts +72 -0
- package/test/unit/data-structures/binary-tree/bst.getnode.range-ensure.coverage.test.ts +22 -0
- package/test/unit/data-structures/binary-tree/bst.misc-branches.coverage.test.ts +100 -0
- package/test/unit/data-structures/binary-tree/bst.more-branches-2.coverage.test.ts +133 -0
- package/test/unit/data-structures/binary-tree/bst.more-branches-3.coverage.test.ts +45 -0
- package/test/unit/data-structures/binary-tree/bst.more-branches-4.coverage.test.ts +36 -0
- package/test/unit/data-structures/binary-tree/bst.more-branches-5.coverage.test.ts +40 -0
- package/test/unit/data-structures/binary-tree/bst.more.coverage.test.ts +39 -0
- package/test/unit/data-structures/binary-tree/bst.node-family.coverage.test.ts +29 -0
- package/test/unit/data-structures/binary-tree/bst.range-pruning.coverage.test.ts +43 -0
- package/test/unit/data-structures/binary-tree/bst.search-fastpath.coverage.test.ts +30 -0
- package/test/unit/data-structures/binary-tree/bst.test.ts +124 -154
- package/test/unit/data-structures/binary-tree/overall.test.ts +20 -20
- package/test/unit/data-structures/binary-tree/red-black-tree.boundary-corruption-repair.coverage.test.ts +66 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.boundary-max-update.coverage.test.ts +18 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.boundary-null.coverage.test.ts +53 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.boundary-stale-cache.coverage.test.ts +25 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.boundary-update.coverage.test.ts +23 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.cache-delete.coverage.test.ts +49 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.cache-edge.coverage.test.ts +37 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.cache-stale-insert.coverage.test.ts +39 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.coverage.test.ts +334 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.delete-fixup.coverage.test.ts +68 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.delete-successor.coverage.test.ts +75 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.factories.coverage.test.ts +26 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.hint-cache-compare-update.coverage.test.ts +74 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.hint-cache-no-update.coverage.test.ts +44 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.hint-cache-nullish.coverage.test.ts +61 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.hint-mapmode-defined.coverage.test.ts +35 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.hint-mapmode-undefined.coverage.test.ts +43 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.hint-more.coverage.test.ts +99 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.hint.coverage.test.ts +60 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.insert-cache-nullish.coverage.test.ts +29 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.insert-header-parent-nullish.coverage.test.ts +17 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.internal-walk.coverage.test.ts +57 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.minmax-cache.test.ts +65 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.misc-inputs.coverage.test.ts +17 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.more-branches-2.coverage.test.ts +121 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.more-branches-3.coverage.test.ts +55 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.more-branches-4.coverage.test.ts +44 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.predsucc.coverage.test.ts +40 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.remaining-branches.coverage.test.ts +123 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.set-inputs.coverage.test.ts +64 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.setkvnode-parent-cache.coverage.test.ts +79 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.setkvnode-remaining.coverage.test.ts +44 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.setkvnode-uncovered.coverage.test.ts +74 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.test.ts +141 -141
- package/test/unit/data-structures/binary-tree/red-black-tree.update-branches.coverage.test.ts +30 -0
- package/test/unit/data-structures/binary-tree/segment-tree.more-branches.coverage.test.ts +31 -0
- package/test/unit/data-structures/binary-tree/tree-counter.coverage.test.ts +115 -0
- package/test/unit/data-structures/binary-tree/tree-counter.more-branches.coverage.test.ts +244 -0
- package/test/unit/data-structures/binary-tree/tree-counter.test.ts +41 -39
- package/test/unit/data-structures/binary-tree/tree-multi-map.coverage.test.ts +104 -0
- package/test/unit/data-structures/binary-tree/tree-multi-map.more-branches-2.coverage.test.ts +59 -0
- package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +145 -145
- package/test/unit/data-structures/graph/abstract-graph.more-branches-2.coverage.test.ts +40 -0
- package/test/unit/data-structures/graph/abstract-graph.more-branches-3.coverage.test.ts +65 -0
- package/test/unit/data-structures/graph/abstract-graph.more-branches-4.coverage.test.ts +98 -0
- package/test/unit/data-structures/graph/abstract-graph.more-branches-5.coverage.test.ts +51 -0
- package/test/unit/data-structures/graph/abstract-graph.more-branches.coverage.test.ts +62 -0
- package/test/unit/data-structures/graph/directed-graph.more-branches-2.coverage.test.ts +38 -0
- package/test/unit/data-structures/graph/directed-graph.more-branches-3.coverage.test.ts +25 -0
- package/test/unit/data-structures/graph/directed-graph.more-branches.coverage.test.ts +82 -0
- package/test/unit/data-structures/graph/map-graph.more-branches.coverage.test.ts +22 -0
- package/test/unit/data-structures/graph/undirected-graph.more-branches-2.coverage.test.ts +35 -0
- package/test/unit/data-structures/graph/undirected-graph.more-branches.coverage.test.ts +87 -0
- package/test/unit/data-structures/hash/hash-map.more-branches.coverage.test.ts +64 -0
- package/test/unit/data-structures/hash/hash-map.toEntryFn-branch.coverage.test.ts +9 -0
- package/test/unit/data-structures/heap/heap.misc-branches.coverage.test.ts +110 -0
- package/test/unit/data-structures/heap/heap.remaining-branches.coverage.test.ts +22 -0
- package/test/unit/data-structures/heap/max-heap.coverage.test.ts +29 -0
- package/test/unit/data-structures/linked-list/doubly-linked-list.more-branches.coverage.test.ts +72 -0
- package/test/unit/data-structures/linked-list/linked-list.unshiftMany-else.coverage.test.ts +15 -0
- package/test/unit/data-structures/linked-list/singly-linked-list.coverage.test.ts +221 -0
- package/test/unit/data-structures/linked-list/singly-linked-list.more-branches.coverage.test.ts +86 -0
- package/test/unit/data-structures/linked-list/skip-linked-list.more-branches.coverage.test.ts +31 -0
- package/test/unit/data-structures/matrix/matrix.more-branches.coverage.test.ts +81 -0
- package/test/unit/data-structures/matrix/matrix.pivotElement-nullish.coverage.test.ts +28 -0
- package/test/unit/data-structures/priority-queue/max-priority-queue.more-branches.coverage.test.ts +10 -0
- package/test/unit/data-structures/priority-queue/priority-queue.coverage.test.ts +21 -0
- package/test/unit/data-structures/queue/deque.coverage.test.ts +173 -0
- package/test/unit/data-structures/queue/deque.more-branches-2.coverage.test.ts +39 -0
- package/test/unit/data-structures/queue/deque.more-branches-3.coverage.test.ts +9 -0
- package/test/unit/data-structures/queue/deque.more-branches.coverage.test.ts +95 -0
- package/test/unit/data-structures/queue/queue.coverage.test.ts +138 -0
- package/test/unit/data-structures/queue/queue.more-branches-2.coverage.test.ts +27 -0
- package/test/unit/data-structures/stack/stack.coverage.test.ts +112 -0
- package/test/unit/data-structures/tree/tree.more-branches.coverage.test.ts +9 -0
- package/test/unit/data-structures/trie/trie.more-branches-2.coverage.test.ts +51 -0
- package/test/utils/patch.ts +33 -0
- package/tsup.config.js +50 -21
- package/tsup.umd.config.js +29 -0
- package/tsup.node.config.js +0 -83
|
@@ -0,0 +1,413 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
+
var IterableElementBase = class {
|
|
4
|
+
static {
|
|
5
|
+
__name(this, "IterableElementBase");
|
|
6
|
+
}
|
|
7
|
+
constructor(options) {
|
|
8
|
+
if (options) {
|
|
9
|
+
const { toElementFn } = options;
|
|
10
|
+
if (typeof toElementFn === "function") this._toElementFn = toElementFn;
|
|
11
|
+
else if (toElementFn) throw new TypeError("toElementFn must be a function type");
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
_toElementFn;
|
|
15
|
+
get toElementFn() {
|
|
16
|
+
return this._toElementFn;
|
|
17
|
+
}
|
|
18
|
+
*[Symbol.iterator](...args) {
|
|
19
|
+
yield* this._getIterator(...args);
|
|
20
|
+
}
|
|
21
|
+
*values() {
|
|
22
|
+
for (const item of this) yield item;
|
|
23
|
+
}
|
|
24
|
+
every(predicate, thisArg) {
|
|
25
|
+
let index = 0;
|
|
26
|
+
for (const item of this) {
|
|
27
|
+
if (thisArg === void 0) {
|
|
28
|
+
if (!predicate(item, index++, this)) return false;
|
|
29
|
+
} else {
|
|
30
|
+
const fn = predicate;
|
|
31
|
+
if (!fn.call(thisArg, item, index++, this)) return false;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
some(predicate, thisArg) {
|
|
37
|
+
let index = 0;
|
|
38
|
+
for (const item of this) {
|
|
39
|
+
if (thisArg === void 0) {
|
|
40
|
+
if (predicate(item, index++, this)) return true;
|
|
41
|
+
} else {
|
|
42
|
+
const fn = predicate;
|
|
43
|
+
if (fn.call(thisArg, item, index++, this)) return true;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
forEach(callbackfn, thisArg) {
|
|
49
|
+
let index = 0;
|
|
50
|
+
for (const item of this) {
|
|
51
|
+
if (thisArg === void 0) {
|
|
52
|
+
callbackfn(item, index++, this);
|
|
53
|
+
} else {
|
|
54
|
+
const fn = callbackfn;
|
|
55
|
+
fn.call(thisArg, item, index++, this);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
find(predicate, thisArg) {
|
|
60
|
+
let index = 0;
|
|
61
|
+
for (const item of this) {
|
|
62
|
+
if (thisArg === void 0) {
|
|
63
|
+
if (predicate(item, index++, this)) return item;
|
|
64
|
+
} else {
|
|
65
|
+
const fn = predicate;
|
|
66
|
+
if (fn.call(thisArg, item, index++, this)) return item;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
has(element) {
|
|
72
|
+
for (const ele of this) if (ele === element) return true;
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
reduce(callbackfn, initialValue) {
|
|
76
|
+
let index = 0;
|
|
77
|
+
const iter = this[Symbol.iterator]();
|
|
78
|
+
let acc;
|
|
79
|
+
if (arguments.length >= 2) {
|
|
80
|
+
acc = initialValue;
|
|
81
|
+
} else {
|
|
82
|
+
const first = iter.next();
|
|
83
|
+
if (first.done) throw new TypeError("Reduce of empty structure with no initial value");
|
|
84
|
+
acc = first.value;
|
|
85
|
+
index = 1;
|
|
86
|
+
}
|
|
87
|
+
for (const value of iter) {
|
|
88
|
+
acc = callbackfn(acc, value, index++, this);
|
|
89
|
+
}
|
|
90
|
+
return acc;
|
|
91
|
+
}
|
|
92
|
+
toArray() {
|
|
93
|
+
return [...this];
|
|
94
|
+
}
|
|
95
|
+
toVisual() {
|
|
96
|
+
return [...this];
|
|
97
|
+
}
|
|
98
|
+
print() {
|
|
99
|
+
console.log(this.toVisual());
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
var TrieNode = class {
|
|
103
|
+
static {
|
|
104
|
+
__name(this, "TrieNode");
|
|
105
|
+
}
|
|
106
|
+
constructor(key) {
|
|
107
|
+
this._key = key;
|
|
108
|
+
this._isEnd = false;
|
|
109
|
+
this._children = new Map();
|
|
110
|
+
}
|
|
111
|
+
_key;
|
|
112
|
+
get key() {
|
|
113
|
+
return this._key;
|
|
114
|
+
}
|
|
115
|
+
set key(value) {
|
|
116
|
+
this._key = value;
|
|
117
|
+
}
|
|
118
|
+
_children;
|
|
119
|
+
get children() {
|
|
120
|
+
return this._children;
|
|
121
|
+
}
|
|
122
|
+
set children(value) {
|
|
123
|
+
this._children = value;
|
|
124
|
+
}
|
|
125
|
+
_isEnd;
|
|
126
|
+
get isEnd() {
|
|
127
|
+
return this._isEnd;
|
|
128
|
+
}
|
|
129
|
+
set isEnd(value) {
|
|
130
|
+
this._isEnd = value;
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
var Trie = class extends IterableElementBase {
|
|
134
|
+
static {
|
|
135
|
+
__name(this, "Trie");
|
|
136
|
+
}
|
|
137
|
+
constructor(words = [], options) {
|
|
138
|
+
super(options);
|
|
139
|
+
if (options) {
|
|
140
|
+
const { caseSensitive } = options;
|
|
141
|
+
if (caseSensitive !== void 0) this._caseSensitive = caseSensitive;
|
|
142
|
+
}
|
|
143
|
+
if (words) {
|
|
144
|
+
this.addMany(words);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
_size = 0;
|
|
148
|
+
get size() {
|
|
149
|
+
return this._size;
|
|
150
|
+
}
|
|
151
|
+
_caseSensitive = true;
|
|
152
|
+
get caseSensitive() {
|
|
153
|
+
return this._caseSensitive;
|
|
154
|
+
}
|
|
155
|
+
_root = new TrieNode("");
|
|
156
|
+
get root() {
|
|
157
|
+
return this._root;
|
|
158
|
+
}
|
|
159
|
+
get _total() {
|
|
160
|
+
return this._size;
|
|
161
|
+
}
|
|
162
|
+
add(word) {
|
|
163
|
+
word = this._caseProcess(word);
|
|
164
|
+
let cur = this.root;
|
|
165
|
+
let isNewWord = false;
|
|
166
|
+
for (const c of word) {
|
|
167
|
+
let nodeC = cur.children.get(c);
|
|
168
|
+
if (!nodeC) {
|
|
169
|
+
nodeC = new TrieNode(c);
|
|
170
|
+
cur.children.set(c, nodeC);
|
|
171
|
+
}
|
|
172
|
+
cur = nodeC;
|
|
173
|
+
}
|
|
174
|
+
if (!cur.isEnd) {
|
|
175
|
+
isNewWord = true;
|
|
176
|
+
cur.isEnd = true;
|
|
177
|
+
this._size++;
|
|
178
|
+
}
|
|
179
|
+
return isNewWord;
|
|
180
|
+
}
|
|
181
|
+
addMany(words) {
|
|
182
|
+
const ans = [];
|
|
183
|
+
for (const word of words) {
|
|
184
|
+
if (this.toElementFn) {
|
|
185
|
+
ans.push(this.add(this.toElementFn(word)));
|
|
186
|
+
} else {
|
|
187
|
+
ans.push(this.add(word));
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
return ans;
|
|
191
|
+
}
|
|
192
|
+
has(word) {
|
|
193
|
+
word = this._caseProcess(word);
|
|
194
|
+
let cur = this.root;
|
|
195
|
+
for (const c of word) {
|
|
196
|
+
const nodeC = cur.children.get(c);
|
|
197
|
+
if (!nodeC) return false;
|
|
198
|
+
cur = nodeC;
|
|
199
|
+
}
|
|
200
|
+
return cur.isEnd;
|
|
201
|
+
}
|
|
202
|
+
isEmpty() {
|
|
203
|
+
return this._size === 0;
|
|
204
|
+
}
|
|
205
|
+
clear() {
|
|
206
|
+
this._size = 0;
|
|
207
|
+
this._root = new TrieNode("");
|
|
208
|
+
}
|
|
209
|
+
delete(word) {
|
|
210
|
+
word = this._caseProcess(word);
|
|
211
|
+
let isDeleted = false;
|
|
212
|
+
const dfs = __name((cur, i) => {
|
|
213
|
+
const char = word[i];
|
|
214
|
+
const child = cur.children.get(char);
|
|
215
|
+
if (child) {
|
|
216
|
+
if (i === word.length - 1) {
|
|
217
|
+
if (child.isEnd) {
|
|
218
|
+
if (child.children.size > 0) {
|
|
219
|
+
child.isEnd = false;
|
|
220
|
+
} else {
|
|
221
|
+
cur.children.delete(char);
|
|
222
|
+
}
|
|
223
|
+
isDeleted = true;
|
|
224
|
+
return true;
|
|
225
|
+
}
|
|
226
|
+
return false;
|
|
227
|
+
}
|
|
228
|
+
const res = dfs(child, i + 1);
|
|
229
|
+
if (res && !cur.isEnd && child.children.size === 0) {
|
|
230
|
+
cur.children.delete(char);
|
|
231
|
+
return true;
|
|
232
|
+
}
|
|
233
|
+
return false;
|
|
234
|
+
}
|
|
235
|
+
return false;
|
|
236
|
+
}, "dfs");
|
|
237
|
+
dfs(this.root, 0);
|
|
238
|
+
if (isDeleted) {
|
|
239
|
+
this._size--;
|
|
240
|
+
}
|
|
241
|
+
return isDeleted;
|
|
242
|
+
}
|
|
243
|
+
getHeight() {
|
|
244
|
+
const startNode = this.root;
|
|
245
|
+
let maxDepth = 0;
|
|
246
|
+
if (startNode) {
|
|
247
|
+
const bfs = __name((node, level) => {
|
|
248
|
+
if (level > maxDepth) {
|
|
249
|
+
maxDepth = level;
|
|
250
|
+
}
|
|
251
|
+
const { children } = node;
|
|
252
|
+
if (children) {
|
|
253
|
+
for (const child of children.entries()) {
|
|
254
|
+
bfs(child[1], level + 1);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
}, "bfs");
|
|
258
|
+
bfs(startNode, 0);
|
|
259
|
+
}
|
|
260
|
+
return maxDepth;
|
|
261
|
+
}
|
|
262
|
+
hasPurePrefix(input) {
|
|
263
|
+
input = this._caseProcess(input);
|
|
264
|
+
let cur = this.root;
|
|
265
|
+
for (const c of input) {
|
|
266
|
+
const nodeC = cur.children.get(c);
|
|
267
|
+
if (!nodeC) return false;
|
|
268
|
+
cur = nodeC;
|
|
269
|
+
}
|
|
270
|
+
return !cur.isEnd;
|
|
271
|
+
}
|
|
272
|
+
hasPrefix(input) {
|
|
273
|
+
input = this._caseProcess(input);
|
|
274
|
+
let cur = this.root;
|
|
275
|
+
for (const c of input) {
|
|
276
|
+
const nodeC = cur.children.get(c);
|
|
277
|
+
if (!nodeC) return false;
|
|
278
|
+
cur = nodeC;
|
|
279
|
+
}
|
|
280
|
+
return true;
|
|
281
|
+
}
|
|
282
|
+
hasCommonPrefix(input) {
|
|
283
|
+
input = this._caseProcess(input);
|
|
284
|
+
let commonPre = "";
|
|
285
|
+
const dfs = __name((cur) => {
|
|
286
|
+
commonPre += cur.key;
|
|
287
|
+
if (commonPre === input) return;
|
|
288
|
+
if (cur.isEnd) return;
|
|
289
|
+
if (cur && cur.children && cur.children.size === 1) dfs(Array.from(cur.children.values())[0]);
|
|
290
|
+
else return;
|
|
291
|
+
}, "dfs");
|
|
292
|
+
dfs(this.root);
|
|
293
|
+
return commonPre === input;
|
|
294
|
+
}
|
|
295
|
+
getLongestCommonPrefix() {
|
|
296
|
+
let commonPre = "";
|
|
297
|
+
const dfs = __name((cur) => {
|
|
298
|
+
commonPre += cur.key;
|
|
299
|
+
if (cur.isEnd) return;
|
|
300
|
+
if (cur && cur.children && cur.children.size === 1) dfs(Array.from(cur.children.values())[0]);
|
|
301
|
+
else return;
|
|
302
|
+
}, "dfs");
|
|
303
|
+
dfs(this.root);
|
|
304
|
+
return commonPre;
|
|
305
|
+
}
|
|
306
|
+
getWords(prefix = "", max = Number.MAX_SAFE_INTEGER, isAllWhenEmptyPrefix = false) {
|
|
307
|
+
prefix = this._caseProcess(prefix);
|
|
308
|
+
const words = [];
|
|
309
|
+
let found = 0;
|
|
310
|
+
function dfs(node, word) {
|
|
311
|
+
for (const char of node.children.keys()) {
|
|
312
|
+
const charNode = node.children.get(char);
|
|
313
|
+
if (charNode !== void 0) {
|
|
314
|
+
dfs(charNode, word.concat(char));
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
if (node.isEnd) {
|
|
318
|
+
if (found > max - 1) return;
|
|
319
|
+
words.push(word);
|
|
320
|
+
found++;
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
__name(dfs, "dfs");
|
|
324
|
+
let startNode = this.root;
|
|
325
|
+
if (prefix) {
|
|
326
|
+
for (const c of prefix) {
|
|
327
|
+
const nodeC = startNode.children.get(c);
|
|
328
|
+
if (nodeC) {
|
|
329
|
+
startNode = nodeC;
|
|
330
|
+
} else {
|
|
331
|
+
return [];
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
if (isAllWhenEmptyPrefix || startNode !== this.root) dfs(startNode, prefix);
|
|
336
|
+
return words;
|
|
337
|
+
}
|
|
338
|
+
clone() {
|
|
339
|
+
const next = this._createInstance();
|
|
340
|
+
for (const x of this) next.add(x);
|
|
341
|
+
return next;
|
|
342
|
+
}
|
|
343
|
+
filter(predicate, thisArg) {
|
|
344
|
+
const results = this._createInstance();
|
|
345
|
+
let index = 0;
|
|
346
|
+
for (const word of this) {
|
|
347
|
+
if (predicate.call(thisArg, word, index, this)) {
|
|
348
|
+
results.add(word);
|
|
349
|
+
}
|
|
350
|
+
index++;
|
|
351
|
+
}
|
|
352
|
+
return results;
|
|
353
|
+
}
|
|
354
|
+
map(callback, options, thisArg) {
|
|
355
|
+
const newTrie = this._createLike([], options);
|
|
356
|
+
let i = 0;
|
|
357
|
+
for (const x of this) {
|
|
358
|
+
const v = thisArg === void 0 ? callback(x, i++, this) : callback.call(thisArg, x, i++, this);
|
|
359
|
+
if (typeof v !== "string") {
|
|
360
|
+
throw new TypeError(`Trie.map callback must return string; got ${typeof v}`);
|
|
361
|
+
}
|
|
362
|
+
newTrie.add(v);
|
|
363
|
+
}
|
|
364
|
+
return newTrie;
|
|
365
|
+
}
|
|
366
|
+
mapSame(callback, thisArg) {
|
|
367
|
+
const next = this._createInstance();
|
|
368
|
+
let i = 0;
|
|
369
|
+
for (const key of this) {
|
|
370
|
+
const mapped = thisArg === void 0 ? callback(key, i++, this) : callback.call(thisArg, key, i++, this);
|
|
371
|
+
next.add(mapped);
|
|
372
|
+
}
|
|
373
|
+
return next;
|
|
374
|
+
}
|
|
375
|
+
_createInstance(options) {
|
|
376
|
+
const Ctor = this.constructor;
|
|
377
|
+
const next = new Ctor([], {
|
|
378
|
+
toElementFn: this.toElementFn,
|
|
379
|
+
caseSensitive: this.caseSensitive,
|
|
380
|
+
...options ?? {}
|
|
381
|
+
});
|
|
382
|
+
return next;
|
|
383
|
+
}
|
|
384
|
+
_createLike(elements = [], options) {
|
|
385
|
+
const Ctor = this.constructor;
|
|
386
|
+
return new Ctor(elements, options);
|
|
387
|
+
}
|
|
388
|
+
_spawnLike(options) {
|
|
389
|
+
return this._createLike([], options);
|
|
390
|
+
}
|
|
391
|
+
*_getIterator() {
|
|
392
|
+
function* _dfs(node, path) {
|
|
393
|
+
if (node.isEnd) {
|
|
394
|
+
yield path;
|
|
395
|
+
}
|
|
396
|
+
for (const [char, childNode] of node.children) {
|
|
397
|
+
yield* _dfs(childNode, path + char);
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
__name(_dfs, "_dfs");
|
|
401
|
+
yield* _dfs(this.root, "");
|
|
402
|
+
}
|
|
403
|
+
_caseProcess(str) {
|
|
404
|
+
if (!this._caseSensitive) {
|
|
405
|
+
str = str.toLowerCase();
|
|
406
|
+
}
|
|
407
|
+
return str;
|
|
408
|
+
}
|
|
409
|
+
};
|
|
410
|
+
export {
|
|
411
|
+
Trie,
|
|
412
|
+
TrieNode
|
|
413
|
+
};
|