atomic-fns 1.2.0 → 2.0.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/README.md +397 -91
- package/dist/array/index.d.ts +9 -0
- package/dist/array/index.js +9 -0
- package/dist/array/list.d.ts +99 -0
- package/dist/array/list.js +133 -0
- package/dist/array/sets.d.ts +130 -0
- package/dist/array/sets.js +213 -0
- package/dist/async/index.d.ts +6 -0
- package/dist/async/index.js +6 -0
- package/dist/async/retry.d.ts +38 -0
- package/dist/async/retry.js +56 -0
- package/dist/async/sleep.d.ts +42 -0
- package/dist/async/sleep.js +76 -0
- package/dist/collections/bisect.d.ts +103 -0
- package/dist/collections/bisect.js +157 -0
- package/dist/collections/deque.d.ts +67 -97
- package/dist/collections/deque.js +197 -256
- package/dist/collections/entries.d.ts +89 -0
- package/dist/collections/entries.js +131 -0
- package/dist/collections/frozen-set.d.ts +42 -0
- package/dist/collections/frozen-set.js +64 -0
- package/dist/collections/group.d.ts +141 -0
- package/dist/collections/group.js +241 -0
- package/dist/collections/heap.d.ts +64 -0
- package/dist/collections/heap.js +205 -0
- package/dist/collections/index.d.ts +15 -623
- package/dist/collections/index.js +14 -844
- package/dist/collections/internal/arrayKeys.d.ts +19 -0
- package/dist/collections/internal/arrayKeys.js +83 -0
- package/dist/collections/internal/invariants.d.ts +4 -0
- package/dist/collections/internal/invariants.js +12 -0
- package/dist/collections/internal/iterable.d.ts +25 -0
- package/dist/collections/internal/iterable.js +62 -0
- package/dist/collections/iterate.d.ts +162 -0
- package/dist/collections/iterate.js +188 -0
- package/dist/collections/lru-cache.d.ts +58 -0
- package/dist/collections/lru-cache.js +164 -0
- package/dist/collections/order.d.ts +19 -0
- package/dist/collections/order.js +38 -0
- package/dist/collections/shape.d.ts +32 -0
- package/dist/collections/shape.js +86 -0
- package/dist/collections/sorted-map.d.ts +171 -0
- package/dist/collections/sorted-map.js +845 -0
- package/dist/collections/sorted-set.d.ts +103 -0
- package/dist/collections/sorted-set.js +147 -0
- package/dist/collections/splay-tree.d.ts +82 -0
- package/dist/collections/splay-tree.js +354 -0
- package/dist/collections/trie.d.ts +58 -0
- package/dist/collections/trie.js +196 -0
- package/dist/decimal/currency.d.ts +211 -0
- package/dist/decimal/currency.js +321 -0
- package/dist/decimal/decimal.d.ts +299 -0
- package/dist/decimal/decimal.js +660 -0
- package/dist/decimal/index.d.ts +11 -148
- package/dist/decimal/index.js +10 -410
- package/dist/decimal/internal/bits.d.ts +2 -0
- package/dist/decimal/internal/bits.js +7 -0
- package/dist/decimal/internal/echo.d.ts +4 -0
- package/dist/decimal/internal/echo.js +6 -0
- package/dist/decimal/internal/isqrt.d.ts +9 -0
- package/dist/decimal/internal/isqrt.js +34 -0
- package/dist/decimal/internal/locale.d.ts +7 -0
- package/dist/decimal/internal/locale.js +24 -0
- package/dist/decimal/internal/minor-units.d.ts +2 -0
- package/dist/decimal/internal/minor-units.js +33 -0
- package/dist/decimal/internal/pow10.d.ts +24 -0
- package/dist/decimal/internal/pow10.js +96 -0
- package/dist/encoding/base64.d.ts +39 -0
- package/dist/encoding/base64.js +53 -0
- package/dist/encoding/hex.d.ts +23 -0
- package/dist/encoding/hex.js +34 -0
- package/dist/encoding/index.d.ts +6 -0
- package/dist/encoding/index.js +6 -0
- package/dist/function/debounce.d.ts +68 -0
- package/dist/function/debounce.js +203 -0
- package/dist/function/index.d.ts +6 -0
- package/dist/function/index.js +6 -0
- package/dist/function/shared.d.ts +2 -0
- package/dist/function/shared.js +5 -0
- package/dist/function/wrap.d.ts +130 -0
- package/dist/function/wrap.js +113 -0
- package/dist/hash/digest.d.ts +18 -0
- package/dist/hash/digest.js +26 -0
- package/dist/hash/hash.d.ts +22 -0
- package/dist/hash/hash.js +252 -0
- package/dist/hash/hashString.d.ts +13 -0
- package/dist/hash/hashString.js +53 -0
- package/dist/hash/index.d.ts +8 -0
- package/dist/hash/index.js +8 -0
- package/dist/hash/stableStringify.d.ts +19 -0
- package/dist/hash/stableStringify.js +102 -0
- package/dist/id/index.d.ts +7 -0
- package/dist/id/index.js +7 -0
- package/dist/id/nanoid.d.ts +16 -0
- package/dist/id/nanoid.js +65 -0
- package/dist/id/uniqueId.d.ts +14 -0
- package/dist/id/uniqueId.js +20 -0
- package/dist/id/uuid.d.ts +25 -0
- package/dist/id/uuid.js +64 -0
- package/dist/index.d.ts +15 -8
- package/dist/index.js +15 -8
- package/dist/internal/bytes.d.ts +11 -0
- package/dist/internal/bytes.js +33 -0
- package/dist/internal/codec.d.ts +11 -0
- package/dist/internal/codec.js +84 -0
- package/dist/internal/compare.d.ts +10 -0
- package/dist/internal/compare.js +26 -0
- package/dist/internal/crypto.d.ts +5 -0
- package/dist/internal/crypto.js +11 -0
- package/dist/internal/keys.d.ts +12 -0
- package/dist/internal/keys.js +23 -0
- package/dist/internal/kind.d.ts +44 -0
- package/dist/internal/kind.js +139 -0
- package/dist/internal/sparse.d.ts +51 -0
- package/dist/internal/sparse.js +104 -0
- package/dist/internal/text.d.ts +7 -0
- package/dist/internal/text.js +15 -0
- package/dist/internal/timer.d.ts +10 -0
- package/dist/internal/timer.js +17 -0
- package/dist/internal/truncate.d.ts +10 -0
- package/dist/internal/truncate.js +22 -0
- package/dist/internal/walk.d.ts +47 -0
- package/dist/internal/walk.js +100 -0
- package/dist/is/index.d.ts +8 -0
- package/dist/is/index.js +8 -0
- package/dist/is/isArrayLike.d.ts +18 -0
- package/dist/is/isArrayLike.js +25 -0
- package/dist/is/isError.d.ts +13 -0
- package/dist/is/isError.js +18 -0
- package/dist/is/isNil.d.ts +12 -0
- package/dist/is/isNil.js +14 -0
- package/dist/is/isPlainObject.d.ts +15 -0
- package/dist/is/isPlainObject.js +34 -0
- package/dist/number/index.d.ts +7 -0
- package/dist/number/index.js +7 -0
- package/dist/number/round.d.ts +86 -0
- package/dist/number/round.js +130 -0
- package/dist/number/stats.d.ts +52 -0
- package/dist/number/stats.js +115 -0
- package/dist/object/cloneDeep.d.ts +28 -0
- package/dist/object/cloneDeep.js +200 -0
- package/dist/object/index.d.ts +14 -0
- package/dist/object/index.js +13 -0
- package/dist/object/isEqual.d.ts +64 -0
- package/dist/object/isEqual.js +289 -0
- package/dist/object/matches.d.ts +40 -0
- package/dist/object/matches.js +284 -0
- package/dist/object/merge.d.ts +66 -0
- package/dist/object/merge.js +272 -0
- package/dist/object/parsePath.d.ts +22 -0
- package/dist/object/parsePath.js +111 -0
- package/dist/object/path.d.ts +85 -0
- package/dist/object/path.js +208 -0
- package/dist/object/pick.d.ts +91 -0
- package/dist/object/pick.js +180 -0
- package/dist/object/reshape.d.ts +73 -0
- package/dist/object/reshape.js +144 -0
- package/dist/random/index.d.ts +96 -32
- package/dist/random/index.js +215 -53
- package/dist/string/case.d.ts +71 -0
- package/dist/string/case.js +152 -0
- package/dist/string/fold.d.ts +35 -0
- package/dist/string/fold.js +100 -0
- package/dist/string/index.d.ts +6 -187
- package/dist/string/index.js +6 -224
- package/dist/string/truncate.d.ts +25 -0
- package/dist/string/truncate.js +44 -0
- package/dist/string/words.d.ts +27 -0
- package/dist/string/words.js +241 -0
- package/package.json +99 -51
- package/dist/collections/BTree.d.ts +0 -230
- package/dist/collections/BTree.js +0 -1143
- package/dist/collections/Heap.d.ts +0 -69
- package/dist/collections/Heap.js +0 -181
- package/dist/collections/LRUCache.d.ts +0 -83
- package/dist/collections/LRUCache.js +0 -139
- package/dist/collections/SortedMap.d.ts +0 -92
- package/dist/collections/SortedMap.js +0 -131
- package/dist/collections/SortedSet.d.ts +0 -69
- package/dist/collections/SortedSet.js +0 -99
- package/dist/collections/SortedTree.d.ts +0 -103
- package/dist/collections/SortedTree.js +0 -142
- package/dist/collections/SplayTree.d.ts +0 -107
- package/dist/collections/SplayTree.js +0 -351
- package/dist/collections/Trie.d.ts +0 -42
- package/dist/collections/Trie.js +0 -104
- package/dist/collections/abc.d.ts +0 -152
- package/dist/collections/abc.js +0 -39
- package/dist/collections/frozenset.d.ts +0 -19
- package/dist/collections/frozenset.js +0 -33
- package/dist/functools/index.d.ts +0 -106
- package/dist/functools/index.js +0 -203
- package/dist/globals/index.d.ts +0 -268
- package/dist/globals/index.js +0 -447
- package/dist/i18n/currency.d.ts +0 -27
- package/dist/i18n/currency.js +0 -100
- package/dist/i18n/date/index.d.ts +0 -210
- package/dist/i18n/date/index.js +0 -552
- package/dist/i18n/date/utils.d.ts +0 -64
- package/dist/i18n/date/utils.js +0 -192
- package/dist/i18n/duration.d.ts +0 -113
- package/dist/i18n/duration.js +0 -398
- package/dist/i18n/format.d.ts +0 -34
- package/dist/i18n/format.js +0 -378
- package/dist/i18n/index.d.ts +0 -9
- package/dist/i18n/index.js +0 -8
- package/dist/itertools/index.d.ts +0 -251
- package/dist/itertools/index.js +0 -434
- package/dist/math/index.d.ts +0 -79
- package/dist/math/index.js +0 -81
- package/dist/mod.d.ts +0 -1
- package/dist/mod.js +0 -1
- package/dist/operators/index.d.ts +0 -148
- package/dist/operators/index.js +0 -325
- package/dist/types.d.ts +0 -19
- package/dist/types.js +0 -1
|
@@ -1,351 +0,0 @@
|
|
|
1
|
-
import { compare } from '../operators/index.js';
|
|
2
|
-
import { Mapping } from './abc.js';
|
|
3
|
-
/**
|
|
4
|
-
* Base SplayTree node class.
|
|
5
|
-
* @private
|
|
6
|
-
*/
|
|
7
|
-
class Node {
|
|
8
|
-
key;
|
|
9
|
-
value;
|
|
10
|
-
left;
|
|
11
|
-
right;
|
|
12
|
-
constructor(key, value) {
|
|
13
|
-
this.key = key;
|
|
14
|
-
this.value = value;
|
|
15
|
-
}
|
|
16
|
-
get height() {
|
|
17
|
-
let height = -1;
|
|
18
|
-
let level = [this];
|
|
19
|
-
while (level.length) {
|
|
20
|
-
height += 1;
|
|
21
|
-
level = level.flatMap((node) => [node.left, node.right].filter(Boolean));
|
|
22
|
-
}
|
|
23
|
-
return height;
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
// In-order snapshot of a tree: reads splay it, so a live walk would lose its place
|
|
27
|
-
function traverse(node) {
|
|
28
|
-
const nodes = [];
|
|
29
|
-
const stack = [];
|
|
30
|
-
while (node || stack.length) {
|
|
31
|
-
while (node) {
|
|
32
|
-
stack.push(node);
|
|
33
|
-
node = node.left;
|
|
34
|
-
}
|
|
35
|
-
node = stack.pop();
|
|
36
|
-
nodes.push(node);
|
|
37
|
-
node = node.right;
|
|
38
|
-
}
|
|
39
|
-
return nodes;
|
|
40
|
-
}
|
|
41
|
-
/**
|
|
42
|
-
* A splay tree is a *self-balancing binary* search tree with the additional property that recently accessed elements are quick to access again.
|
|
43
|
-
* Insertion, look-up and removal in O(log n) *amortized* time.
|
|
44
|
-
* For many patterns of non-random operations splay trees can take better than logarithmic time, without requiring advance knowledge of the pattern.
|
|
45
|
-
* @see {@link https://en.wikipedia.org/wiki/Splay_tree Splay tree}
|
|
46
|
-
* @see {@link https://web.archive.org/web/20220930173835/http://www14.in.tum.de/personen/albers/papers/ipl02.pdf Randomized Splay Trees}
|
|
47
|
-
* @template K, V
|
|
48
|
-
*/
|
|
49
|
-
export class SplayTree extends Mapping {
|
|
50
|
-
root;
|
|
51
|
-
cmp;
|
|
52
|
-
randSplay;
|
|
53
|
-
count;
|
|
54
|
-
/**
|
|
55
|
-
* Create a new randomized Splay Tree
|
|
56
|
-
* @param compareFn
|
|
57
|
-
* @param p When an element is accessed, with probability `p` splay the tree. With probability `1 - p`, leave the tree as it is.
|
|
58
|
-
*/
|
|
59
|
-
constructor(compareFn, p = 0.5) {
|
|
60
|
-
super();
|
|
61
|
-
this.cmp = compareFn ?? compare;
|
|
62
|
-
this.count = 0;
|
|
63
|
-
this.randSplay = p;
|
|
64
|
-
}
|
|
65
|
-
top() {
|
|
66
|
-
return this.root?.key;
|
|
67
|
-
}
|
|
68
|
-
get size() {
|
|
69
|
-
return this.count;
|
|
70
|
-
}
|
|
71
|
-
empty() {
|
|
72
|
-
return !this.root;
|
|
73
|
-
}
|
|
74
|
-
get height() {
|
|
75
|
-
if (!this.root)
|
|
76
|
-
return 0;
|
|
77
|
-
return this.root.height;
|
|
78
|
-
}
|
|
79
|
-
/**
|
|
80
|
-
* Returns the value for `key` or `undefined` if the key is not found.
|
|
81
|
-
* @param key The key to find.
|
|
82
|
-
* @returns {?V} The value for the key or `undefined`.
|
|
83
|
-
*/
|
|
84
|
-
get(key) {
|
|
85
|
-
return this.findNode(key)?.value;
|
|
86
|
-
}
|
|
87
|
-
findNode(key) {
|
|
88
|
-
if (!this.root)
|
|
89
|
-
return;
|
|
90
|
-
// Note: deterministic splaying (always moves key to front):
|
|
91
|
-
// this.splay(key)
|
|
92
|
-
// if (this.cmp(key, this.root.key) === 0) return this.root.value
|
|
93
|
-
// Randomized Splay Trees https://doi.org/10.1016%2Fs0020-0190%2801%2900230-7
|
|
94
|
-
// The randomized idea is only moving accessed keys with a certain probability `p`.
|
|
95
|
-
// Values of `p` closer to 1 tend to perform well.
|
|
96
|
-
if (Math.random() <= this.randSplay) {
|
|
97
|
-
this.splay(key);
|
|
98
|
-
if (this.cmp(key, this.root.key) === 0)
|
|
99
|
-
return this.root;
|
|
100
|
-
else
|
|
101
|
-
return undefined;
|
|
102
|
-
}
|
|
103
|
-
// Lookup the node down the tree
|
|
104
|
-
let current = this.root;
|
|
105
|
-
while (current) {
|
|
106
|
-
const rank = this.cmp(key, current.key);
|
|
107
|
-
if (rank === 0)
|
|
108
|
-
return current;
|
|
109
|
-
if (rank < 0)
|
|
110
|
-
current = current.left;
|
|
111
|
-
else
|
|
112
|
-
current = current.right;
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
/**
|
|
116
|
-
* Inserts the `key` and `value` in the tree, or updates the value if the `key` exists.
|
|
117
|
-
* @param {K} key A key which can be any comparable value
|
|
118
|
-
* @param {V} value A value associated with the key
|
|
119
|
-
* @returns
|
|
120
|
-
*/
|
|
121
|
-
set(key, value) {
|
|
122
|
-
this.insert(key, value, true);
|
|
123
|
-
}
|
|
124
|
-
insert(key, value, overwrite) {
|
|
125
|
-
if (!this.root) {
|
|
126
|
-
this.root = new Node(key, value);
|
|
127
|
-
this.count += 1;
|
|
128
|
-
return;
|
|
129
|
-
}
|
|
130
|
-
// Splay on the key to move the last node on the search path for
|
|
131
|
-
// the key to the root of the tree.
|
|
132
|
-
this.splay(key);
|
|
133
|
-
const rank = this.cmp(key, this.root.key);
|
|
134
|
-
if (rank === 0) {
|
|
135
|
-
// Update the value
|
|
136
|
-
if (overwrite)
|
|
137
|
-
this.root.value = value;
|
|
138
|
-
return;
|
|
139
|
-
}
|
|
140
|
-
const node = new Node(key, value);
|
|
141
|
-
if (rank > 0) {
|
|
142
|
-
node.left = this.root;
|
|
143
|
-
node.right = this.root.right;
|
|
144
|
-
this.root.right = undefined;
|
|
145
|
-
}
|
|
146
|
-
else {
|
|
147
|
-
node.right = this.root;
|
|
148
|
-
node.left = this.root.left;
|
|
149
|
-
this.root.left = undefined;
|
|
150
|
-
}
|
|
151
|
-
this.root = node;
|
|
152
|
-
this.count += 1;
|
|
153
|
-
}
|
|
154
|
-
/**
|
|
155
|
-
* Removes a specified key if it exists in the tree. The removed value is returned or `undefined` if not found.
|
|
156
|
-
* @param {K} key The key to remove.
|
|
157
|
-
* @returns {?V} The removed value associated with `key`.
|
|
158
|
-
*/
|
|
159
|
-
remove(key) {
|
|
160
|
-
if (!this.root) {
|
|
161
|
-
return;
|
|
162
|
-
}
|
|
163
|
-
this.splay(key);
|
|
164
|
-
if (this.cmp(key, this.root.key) !== 0) {
|
|
165
|
-
return;
|
|
166
|
-
}
|
|
167
|
-
const removed = this.root;
|
|
168
|
-
if (!this.root.left) {
|
|
169
|
-
this.root = this.root.right;
|
|
170
|
-
}
|
|
171
|
-
else {
|
|
172
|
-
const right = this.root.right;
|
|
173
|
-
this.root = this.root.left;
|
|
174
|
-
//
|
|
175
|
-
// Splay to make sure that the new root has an empty right child.
|
|
176
|
-
//
|
|
177
|
-
this.splay(key);
|
|
178
|
-
//
|
|
179
|
-
// Insert the original right child as the right child of the new
|
|
180
|
-
// root.
|
|
181
|
-
//
|
|
182
|
-
this.root.right = right;
|
|
183
|
-
}
|
|
184
|
-
this.count -= 1;
|
|
185
|
-
return removed.value;
|
|
186
|
-
}
|
|
187
|
-
//
|
|
188
|
-
// function find(key)
|
|
189
|
-
// @param {number} key Key to find in the tree.
|
|
190
|
-
// @return {SplayTree.Node} Node having the specified key.
|
|
191
|
-
//
|
|
192
|
-
//
|
|
193
|
-
/**
|
|
194
|
-
* Returns the minimum key in the tree or subtree.
|
|
195
|
-
* @returns The minimum key.
|
|
196
|
-
*/
|
|
197
|
-
min(root = this.root) {
|
|
198
|
-
if (!root)
|
|
199
|
-
return;
|
|
200
|
-
while (root.left) {
|
|
201
|
-
root = root.left;
|
|
202
|
-
}
|
|
203
|
-
return root.key;
|
|
204
|
-
}
|
|
205
|
-
/**
|
|
206
|
-
* Returns the maximum key in the tree or subtree.
|
|
207
|
-
* @returns The maximum key.
|
|
208
|
-
*/
|
|
209
|
-
max(root = this.root) {
|
|
210
|
-
if (!root)
|
|
211
|
-
return;
|
|
212
|
-
while (root.right) {
|
|
213
|
-
root = root.right;
|
|
214
|
-
}
|
|
215
|
-
return root.key;
|
|
216
|
-
}
|
|
217
|
-
/**
|
|
218
|
-
* Returns the largest key that is less than a given key.
|
|
219
|
-
* @param key The given key
|
|
220
|
-
* @returns {?K} The largest key found or `undefined`.
|
|
221
|
-
*/
|
|
222
|
-
lowerBound(key) {
|
|
223
|
-
if (!this.root)
|
|
224
|
-
return;
|
|
225
|
-
// Splay on the key to move the node with the given key or the last
|
|
226
|
-
// node on the search path to the top of the tree.
|
|
227
|
-
this.splay(key);
|
|
228
|
-
// Now the result is either the root node or the greatest node in
|
|
229
|
-
// the left subtree.
|
|
230
|
-
if (this.cmp(this.root.key, key) < 0) {
|
|
231
|
-
return this.root.key;
|
|
232
|
-
}
|
|
233
|
-
else if (this.root.left) {
|
|
234
|
-
return this.max(this.root.left);
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
/**
|
|
238
|
-
* This is the simplified top-down splaying method proposed by Sleator and Tarjan in {@link https://doi.org/10.1145%2F3828.3835 Self-Adjusting Binary Search Trees}.
|
|
239
|
-
* @param key
|
|
240
|
-
* @see {@link https://doi.org/10.1145%2F3828.3835 Self-Adjusting Binary Search Trees}
|
|
241
|
-
*/
|
|
242
|
-
splay(key) {
|
|
243
|
-
if (!this.root)
|
|
244
|
-
return;
|
|
245
|
-
let stub, left, right;
|
|
246
|
-
stub = left = right = new Node(null, null);
|
|
247
|
-
let root = this.root;
|
|
248
|
-
while (true) {
|
|
249
|
-
const rank = this.cmp(key, root.key);
|
|
250
|
-
if (rank < 0) {
|
|
251
|
-
if (!root.left) {
|
|
252
|
-
break;
|
|
253
|
-
}
|
|
254
|
-
if (this.cmp(key, root.left.key) < 0) {
|
|
255
|
-
// Rotate right.
|
|
256
|
-
const tmp = root.left;
|
|
257
|
-
root.left = tmp.right;
|
|
258
|
-
tmp.right = root;
|
|
259
|
-
root = tmp;
|
|
260
|
-
if (!root.left) {
|
|
261
|
-
break;
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
|
-
// Link right.
|
|
265
|
-
right.left = root;
|
|
266
|
-
right = root;
|
|
267
|
-
root = root.left;
|
|
268
|
-
}
|
|
269
|
-
else if (rank > 0) {
|
|
270
|
-
if (!root.right) {
|
|
271
|
-
break;
|
|
272
|
-
}
|
|
273
|
-
if (this.cmp(key, root.right.key) > 0) {
|
|
274
|
-
// Rotate left.
|
|
275
|
-
const tmp = root.right;
|
|
276
|
-
root.right = tmp.left;
|
|
277
|
-
tmp.left = root;
|
|
278
|
-
root = tmp;
|
|
279
|
-
if (!root.right) {
|
|
280
|
-
break;
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
// Link left.
|
|
284
|
-
left.right = root;
|
|
285
|
-
left = root;
|
|
286
|
-
root = root.right;
|
|
287
|
-
}
|
|
288
|
-
else {
|
|
289
|
-
break;
|
|
290
|
-
}
|
|
291
|
-
}
|
|
292
|
-
// Assemble.
|
|
293
|
-
left.right = root.left;
|
|
294
|
-
right.left = root.right;
|
|
295
|
-
root.left = stub.right;
|
|
296
|
-
root.right = stub.left;
|
|
297
|
-
this.root = root;
|
|
298
|
-
}
|
|
299
|
-
add(key) {
|
|
300
|
-
this.insert(key, null, false);
|
|
301
|
-
}
|
|
302
|
-
/**
|
|
303
|
-
* Returns `true` if the given key is in the tree.
|
|
304
|
-
* @param {K} key The key to find.
|
|
305
|
-
* @returns `true` if found.
|
|
306
|
-
*/
|
|
307
|
-
contains(key) {
|
|
308
|
-
return this.findNode(key) !== undefined;
|
|
309
|
-
}
|
|
310
|
-
/** Alias of remove but returns `true` if the key was found instead of the deleted value. */
|
|
311
|
-
delete(key) {
|
|
312
|
-
const size = this.count;
|
|
313
|
-
this.remove(key);
|
|
314
|
-
return this.count < size;
|
|
315
|
-
}
|
|
316
|
-
clear() {
|
|
317
|
-
if (!this.root)
|
|
318
|
-
return;
|
|
319
|
-
this.root.left = undefined;
|
|
320
|
-
this.root.right = undefined;
|
|
321
|
-
this.root = undefined;
|
|
322
|
-
this.count = 0;
|
|
323
|
-
}
|
|
324
|
-
/**
|
|
325
|
-
* Returns an ordered iterable of all the keys in the tree.
|
|
326
|
-
* @returns {Iterable<K>} An iterable of keys in-order.
|
|
327
|
-
*/
|
|
328
|
-
*keys() {
|
|
329
|
-
for (const node of traverse(this.root)) {
|
|
330
|
-
yield node.key;
|
|
331
|
-
}
|
|
332
|
-
}
|
|
333
|
-
/**
|
|
334
|
-
* Returns an ordered iterable of all the keys and values in the tree.
|
|
335
|
-
* @returns {Iterable<[K,V]>} A iterable of `[key, value]` pairs sorted by key.
|
|
336
|
-
*/
|
|
337
|
-
*entries() {
|
|
338
|
-
for (const node of traverse(this.root)) {
|
|
339
|
-
yield [node.key, node.value];
|
|
340
|
-
}
|
|
341
|
-
}
|
|
342
|
-
/**
|
|
343
|
-
* Returns an ordered iterable of all the values in the tree.
|
|
344
|
-
* @returns {Iterable<V>} An iterable of values sorted by key.
|
|
345
|
-
*/
|
|
346
|
-
*values() {
|
|
347
|
-
for (const node of traverse(this.root)) {
|
|
348
|
-
yield node.value;
|
|
349
|
-
}
|
|
350
|
-
}
|
|
351
|
-
}
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import { Collection } from './abc.js';
|
|
2
|
-
/**
|
|
3
|
-
* A Trie is a tree for efficient storing and retrieval of strings such as words in a dictionary. They are useful for checking all the strings that have a common prefix.
|
|
4
|
-
* Insertions, lookups and removals in `O(s)` where `s` is the search term.
|
|
5
|
-
* @see {@link https://en.wikipedia.org/wiki/Trie Trie}
|
|
6
|
-
*/
|
|
7
|
-
export declare class Trie extends Collection {
|
|
8
|
-
private root;
|
|
9
|
-
private count;
|
|
10
|
-
constructor(words?: string[]);
|
|
11
|
-
/**
|
|
12
|
-
* Adds the given string to the tree.
|
|
13
|
-
* @param word A string
|
|
14
|
-
*/
|
|
15
|
-
add(word: string): this;
|
|
16
|
-
/**
|
|
17
|
-
* Removes the given string from the tree.
|
|
18
|
-
* @param word The string to remove
|
|
19
|
-
* @returns {boolean} Returns `true` if the string was found and removed.
|
|
20
|
-
*/
|
|
21
|
-
remove(word: string): boolean;
|
|
22
|
-
private findNode;
|
|
23
|
-
/**
|
|
24
|
-
* Generates all results that start with the given string.
|
|
25
|
-
* @param word A prefix string
|
|
26
|
-
* @param {number} [limit] The number of results to return. Defaults to every result.
|
|
27
|
-
*/
|
|
28
|
-
matches: (word: string, limit?: number) => Generator<string>;
|
|
29
|
-
/**
|
|
30
|
-
* Returns `true` if the given string is found in the tree.
|
|
31
|
-
* @param {string} word
|
|
32
|
-
*/
|
|
33
|
-
contains(word: string): boolean;
|
|
34
|
-
/**
|
|
35
|
-
* Removes all strings from the tree.
|
|
36
|
-
*/
|
|
37
|
-
clear(): void;
|
|
38
|
-
/**
|
|
39
|
-
* Returns the total number of strings in the tree.
|
|
40
|
-
*/
|
|
41
|
-
get size(): number;
|
|
42
|
-
}
|
package/dist/collections/Trie.js
DELETED
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
import { itake } from '../itertools/index.js';
|
|
2
|
-
import { Collection } from './abc.js';
|
|
3
|
-
function* traverse(root, prefix) {
|
|
4
|
-
const stack = [[root, prefix]];
|
|
5
|
-
while (stack.length) {
|
|
6
|
-
const [node, path] = stack.pop();
|
|
7
|
-
if (node.isWord)
|
|
8
|
-
yield path;
|
|
9
|
-
for (const key of Object.keys(node).reverse()) {
|
|
10
|
-
stack.push([node[key], path + key]);
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
/**
|
|
15
|
-
* A Trie is a tree for efficient storing and retrieval of strings such as words in a dictionary. They are useful for checking all the strings that have a common prefix.
|
|
16
|
-
* Insertions, lookups and removals in `O(s)` where `s` is the search term.
|
|
17
|
-
* @see {@link https://en.wikipedia.org/wiki/Trie Trie}
|
|
18
|
-
*/
|
|
19
|
-
export class Trie extends Collection {
|
|
20
|
-
root = Object.create(null);
|
|
21
|
-
count = 0;
|
|
22
|
-
constructor(words) {
|
|
23
|
-
super();
|
|
24
|
-
if (words) {
|
|
25
|
-
for (const word of words) {
|
|
26
|
-
this.add(word);
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
/**
|
|
31
|
-
* Adds the given string to the tree.
|
|
32
|
-
* @param word A string
|
|
33
|
-
*/
|
|
34
|
-
add(word) {
|
|
35
|
-
let current = this.root;
|
|
36
|
-
for (let i = 0; i < word.length; i++) {
|
|
37
|
-
const c = word[i];
|
|
38
|
-
if (!current[c])
|
|
39
|
-
current[c] = Object.create(null);
|
|
40
|
-
current = current[c];
|
|
41
|
-
}
|
|
42
|
-
if (!current.isWord)
|
|
43
|
-
this.count++;
|
|
44
|
-
// Set as end of the word
|
|
45
|
-
Object.defineProperty(current, 'isWord', {
|
|
46
|
-
value: true,
|
|
47
|
-
enumerable: false,
|
|
48
|
-
writable: true
|
|
49
|
-
});
|
|
50
|
-
return this;
|
|
51
|
-
}
|
|
52
|
-
/**
|
|
53
|
-
* Removes the given string from the tree.
|
|
54
|
-
* @param word The string to remove
|
|
55
|
-
* @returns {boolean} Returns `true` if the string was found and removed.
|
|
56
|
-
*/
|
|
57
|
-
remove(word) {
|
|
58
|
-
const node = this.findNode(word);
|
|
59
|
-
if (!node?.isWord)
|
|
60
|
-
return false;
|
|
61
|
-
node.isWord = false;
|
|
62
|
-
this.count--;
|
|
63
|
-
return true;
|
|
64
|
-
}
|
|
65
|
-
findNode(word) {
|
|
66
|
-
let node = this.root;
|
|
67
|
-
for (let i = 0; i < word.length && node; i++) {
|
|
68
|
-
const letter = word[i];
|
|
69
|
-
node = node[letter];
|
|
70
|
-
}
|
|
71
|
-
return node;
|
|
72
|
-
}
|
|
73
|
-
/**
|
|
74
|
-
* Generates all results that start with the given string.
|
|
75
|
-
* @param word A prefix string
|
|
76
|
-
* @param {number} [limit] The number of results to return. Defaults to every result.
|
|
77
|
-
*/
|
|
78
|
-
matches = function* (word, limit) {
|
|
79
|
-
const node = this.findNode(word);
|
|
80
|
-
if (node)
|
|
81
|
-
yield* itake(limit ?? Infinity, traverse(node, word));
|
|
82
|
-
};
|
|
83
|
-
/**
|
|
84
|
-
* Returns `true` if the given string is found in the tree.
|
|
85
|
-
* @param {string} word
|
|
86
|
-
*/
|
|
87
|
-
contains(word) {
|
|
88
|
-
const node = this.findNode(word);
|
|
89
|
-
return !!node?.isWord;
|
|
90
|
-
}
|
|
91
|
-
/**
|
|
92
|
-
* Removes all strings from the tree.
|
|
93
|
-
*/
|
|
94
|
-
clear() {
|
|
95
|
-
this.root = Object.create(null);
|
|
96
|
-
this.count = 0;
|
|
97
|
-
}
|
|
98
|
-
/**
|
|
99
|
-
* Returns the total number of strings in the tree.
|
|
100
|
-
*/
|
|
101
|
-
get size() {
|
|
102
|
-
return this.count;
|
|
103
|
-
}
|
|
104
|
-
}
|
|
@@ -1,152 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* A container type that provides contains().
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
* @interface Container
|
|
6
|
-
*/
|
|
7
|
-
export interface Container {
|
|
8
|
-
contains: (x: any) => boolean;
|
|
9
|
-
}
|
|
10
|
-
/**
|
|
11
|
-
* Implements an equality check between two values.
|
|
12
|
-
* @interface Ordered
|
|
13
|
-
*/
|
|
14
|
-
export interface Equals {
|
|
15
|
-
eq: (other: any) => boolean;
|
|
16
|
-
}
|
|
17
|
-
/**
|
|
18
|
-
* Implements an iterable that allows comparing two elements to determine ordering.
|
|
19
|
-
* @interface Ordered
|
|
20
|
-
*/
|
|
21
|
-
export interface Comparable {
|
|
22
|
-
compare: (other: any) => number;
|
|
23
|
-
}
|
|
24
|
-
/**
|
|
25
|
-
* Implements an iterable that allows sorting elements.
|
|
26
|
-
* @interface Ordered
|
|
27
|
-
*/
|
|
28
|
-
export interface Ordered<T> {
|
|
29
|
-
lt: (other: any) => boolean;
|
|
30
|
-
}
|
|
31
|
-
/**
|
|
32
|
-
* Implements an iterable that allows backward iteration.
|
|
33
|
-
* @interface Reversible
|
|
34
|
-
*/
|
|
35
|
-
export interface Reversible<T = any> {
|
|
36
|
-
reversed: () => Iterable<T>;
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* A `Collection` is an iterable {@link Container} type.
|
|
40
|
-
* This is an abstract base class for user-defined collection types.
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
* @abstract
|
|
44
|
-
* @class Collection
|
|
45
|
-
* @implements {Container}
|
|
46
|
-
*/
|
|
47
|
-
export declare abstract class Collection implements Container {
|
|
48
|
-
/**
|
|
49
|
-
* Adds a new item to the collection.
|
|
50
|
-
* @param item - The item to add to the collection.
|
|
51
|
-
*/
|
|
52
|
-
abstract add(item: any): any;
|
|
53
|
-
/**
|
|
54
|
-
* Checks if item is present in the collection.
|
|
55
|
-
* @param item - The item to search for in the collection.
|
|
56
|
-
* @returns `true` when the element is found, else `false`
|
|
57
|
-
*/
|
|
58
|
-
abstract contains(item: any): boolean;
|
|
59
|
-
/**
|
|
60
|
-
* Remove all items from the collection
|
|
61
|
-
*/
|
|
62
|
-
abstract clear(): any;
|
|
63
|
-
/**
|
|
64
|
-
* Returns the total number of elements in the collection.
|
|
65
|
-
*/
|
|
66
|
-
abstract get size(): number;
|
|
67
|
-
get length(): number;
|
|
68
|
-
/**
|
|
69
|
-
* Check if there are no items.
|
|
70
|
-
* @returns {boolean} `true` if is empty.
|
|
71
|
-
*/
|
|
72
|
-
empty(): boolean;
|
|
73
|
-
}
|
|
74
|
-
/**
|
|
75
|
-
* A sequence is an iterable {@link Collection} type with efficient index-based access.
|
|
76
|
-
* @abstract
|
|
77
|
-
* @class Sequence
|
|
78
|
-
* @extends {Collection}
|
|
79
|
-
* @template V
|
|
80
|
-
*/
|
|
81
|
-
export declare abstract class Sequence<V = any, K = any> extends Collection {
|
|
82
|
-
/**
|
|
83
|
-
* Return the item at the given key or index
|
|
84
|
-
* @param {K} key
|
|
85
|
-
* @memberof Sequence
|
|
86
|
-
*/
|
|
87
|
-
abstract get(key: K): any;
|
|
88
|
-
/**
|
|
89
|
-
* Set a new value at the given key or index
|
|
90
|
-
*
|
|
91
|
-
* @param {K} key
|
|
92
|
-
* @param {V} value
|
|
93
|
-
* @memberof Sequence
|
|
94
|
-
*/
|
|
95
|
-
abstract set(key: K, value: V): any;
|
|
96
|
-
/**
|
|
97
|
-
* Adds a new item to the end of the sequence. Alias of {@link Collection.add}.
|
|
98
|
-
*
|
|
99
|
-
* @param {V} x
|
|
100
|
-
* @memberof Sequence
|
|
101
|
-
*/
|
|
102
|
-
abstract append(x: V | Iterable<V>): any;
|
|
103
|
-
/**
|
|
104
|
-
* Remove a given value from the collection if exists.
|
|
105
|
-
* @returns the value that was removed or `undefined`.
|
|
106
|
-
*/
|
|
107
|
-
abstract remove(x: V): any;
|
|
108
|
-
/**
|
|
109
|
-
* Returns an iterator of all the `keys` in order.
|
|
110
|
-
*/
|
|
111
|
-
abstract keys(): Iterable<K>;
|
|
112
|
-
/**
|
|
113
|
-
* Returns an iterator of all the `values` in order.
|
|
114
|
-
*/
|
|
115
|
-
abstract values(): Iterable<V>;
|
|
116
|
-
/**
|
|
117
|
-
* Returns an iterator of all the `[key, value]` pairs in order.
|
|
118
|
-
*/
|
|
119
|
-
abstract entries(): Iterable<[K, V]>;
|
|
120
|
-
}
|
|
121
|
-
/**
|
|
122
|
-
* A mapping is a {@link Collection} indexed by keys that may have associated values.
|
|
123
|
-
* @abstract
|
|
124
|
-
* @class Mapping
|
|
125
|
-
* @extends {Collection}
|
|
126
|
-
* @template K, V
|
|
127
|
-
*/
|
|
128
|
-
export declare abstract class Mapping<K, V> extends Collection {
|
|
129
|
-
/**
|
|
130
|
-
* Returns the value for the given key, or return `defaults` if not found.
|
|
131
|
-
* @param {K} key
|
|
132
|
-
* @param {*} [defaults]
|
|
133
|
-
*/
|
|
134
|
-
abstract get(key: K, defaults?: any): V | undefined;
|
|
135
|
-
/**
|
|
136
|
-
* Sets the value for the given key.
|
|
137
|
-
* @param {K} key
|
|
138
|
-
*/
|
|
139
|
-
abstract set(key: K, value: V): any;
|
|
140
|
-
/**
|
|
141
|
-
* Returns an iterator of all the `keys` in the map.
|
|
142
|
-
*/
|
|
143
|
-
abstract keys(): Iterable<K>;
|
|
144
|
-
/**
|
|
145
|
-
* Returns an iterator of all the `values` in the map.
|
|
146
|
-
*/
|
|
147
|
-
abstract values(): Iterable<V>;
|
|
148
|
-
/**
|
|
149
|
-
* Returns an iterator of all the `[key, value]` pairs in the map.
|
|
150
|
-
*/
|
|
151
|
-
abstract entries(): Iterable<[K, V]>;
|
|
152
|
-
}
|
package/dist/collections/abc.js
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* A `Collection` is an iterable {@link Container} type.
|
|
3
|
-
* This is an abstract base class for user-defined collection types.
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
* @abstract
|
|
7
|
-
* @class Collection
|
|
8
|
-
* @implements {Container}
|
|
9
|
-
*/
|
|
10
|
-
export class Collection {
|
|
11
|
-
get length() {
|
|
12
|
-
return this.size;
|
|
13
|
-
}
|
|
14
|
-
/**
|
|
15
|
-
* Check if there are no items.
|
|
16
|
-
* @returns {boolean} `true` if is empty.
|
|
17
|
-
*/
|
|
18
|
-
empty() {
|
|
19
|
-
return !this.size;
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
/**
|
|
23
|
-
* A sequence is an iterable {@link Collection} type with efficient index-based access.
|
|
24
|
-
* @abstract
|
|
25
|
-
* @class Sequence
|
|
26
|
-
* @extends {Collection}
|
|
27
|
-
* @template V
|
|
28
|
-
*/
|
|
29
|
-
export class Sequence extends Collection {
|
|
30
|
-
}
|
|
31
|
-
/**
|
|
32
|
-
* A mapping is a {@link Collection} indexed by keys that may have associated values.
|
|
33
|
-
* @abstract
|
|
34
|
-
* @class Mapping
|
|
35
|
-
* @extends {Collection}
|
|
36
|
-
* @template K, V
|
|
37
|
-
*/
|
|
38
|
-
export class Mapping extends Collection {
|
|
39
|
-
}
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Returns a new immutable Set object with elements from `iterable`. Its contents cannot be altered after it's created.
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
* @class FrozenSet
|
|
6
|
-
* @extends {Set<T>}
|
|
7
|
-
* @template T
|
|
8
|
-
*/
|
|
9
|
-
export declare class FrozenSet<T = any> extends Set<T> {
|
|
10
|
-
constructor(iterable: Iterable<T>);
|
|
11
|
-
/** @private */
|
|
12
|
-
add(): this;
|
|
13
|
-
/** @private */
|
|
14
|
-
delete(): boolean;
|
|
15
|
-
/** @private */
|
|
16
|
-
clear(): this;
|
|
17
|
-
/** @private */
|
|
18
|
-
freeze(): Readonly<this>;
|
|
19
|
-
}
|