atomic-fns 0.3.1 → 0.3.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/.esdoc.json +2 -2
- package/README.md +48 -13
- package/core/collections/BTree.d.ts +229 -0
- package/core/collections/BTree.js +1165 -0
- package/core/collections/BTree.ts +1340 -0
- package/core/collections/Heap.d.ts +69 -0
- package/core/collections/Heap.js +181 -0
- package/core/collections/Heap.ts +189 -0
- package/core/collections/LRUCache.d.ts +131 -0
- package/core/collections/LRUCache.js +388 -0
- package/core/collections/LRUCache.ts +465 -0
- package/core/collections/SortedMap.d.ts +92 -0
- package/core/collections/SortedMap.js +130 -0
- package/core/collections/SortedMap.ts +146 -0
- package/core/collections/SortedSet.d.ts +63 -0
- package/core/collections/SortedSet.js +93 -0
- package/core/collections/SortedSet.ts +104 -0
- package/core/collections/SortedTree.d.ts +103 -0
- package/core/collections/SortedTree.js +141 -0
- package/core/collections/SortedTree.ts +160 -0
- package/core/collections/SplayTree.d.ts +105 -0
- package/core/collections/SplayTree.js +338 -0
- package/core/collections/SplayTree.ts +354 -0
- package/core/collections/abc.d.ts +152 -0
- package/core/collections/abc.js +39 -0
- package/core/collections/abc.ts +176 -0
- package/core/collections/deque.d.ts +110 -0
- package/core/collections/deque.js +285 -0
- package/core/collections/deque.ts +329 -0
- package/core/collections/frozenset.d.ts +19 -0
- package/core/collections/frozenset.js +33 -0
- package/core/collections/frozenset.ts +37 -0
- package/{src → core}/collections/index.d.ts +12 -161
- package/{src → core}/collections/index.js +13 -164
- package/{src → core}/collections/index.ts +13 -224
- package/core/collections/test.html +10 -0
- package/core/collections/typedArrays.d.ts +31 -0
- package/core/collections/typedArrays.js +143 -0
- package/core/collections/typedArrays.ts +163 -0
- package/{src → core}/datetime/index.d.ts +0 -0
- package/{src → core}/datetime/index.js +0 -0
- package/{src → core}/datetime/index.ts +0 -0
- package/core/datetime/test.html +0 -0
- package/{src → core}/functools/index.d.ts +2 -2
- package/{src → core}/functools/index.js +2 -2
- package/{src → core}/functools/index.ts +2 -2
- package/{src → core}/globals/index.d.ts +2 -7
- package/{src → core}/globals/index.js +5 -2
- package/{src → core}/globals/index.ts +6 -9
- package/{src → core}/index.d.ts +0 -0
- package/{src → core}/index.js +0 -0
- package/{src → core}/index.ts +0 -0
- package/{src → core}/itertools/index.d.ts +9 -7
- package/{src → core}/itertools/index.js +25 -9
- package/{src → core}/itertools/index.ts +39 -16
- package/{src → core}/math/index.d.ts +0 -0
- package/{src → core}/math/index.js +0 -0
- package/{src → core}/math/index.ts +0 -0
- package/core/operators/index.d.ts +148 -0
- package/core/operators/index.js +271 -0
- package/core/operators/index.ts +282 -0
- package/{src → core}/random/index.d.ts +0 -0
- package/{src → core}/random/index.js +0 -0
- package/{src → core}/random/index.ts +0 -0
- package/package.json +6 -6
- package/tests/BTree.spec.ts +143 -0
- package/tests/Cache.spec.ts +161 -0
- package/tests/Heap.spec.ts +66 -0
- package/tests/SortedMap.spec.ts +144 -0
- package/tests/SortedSet.spec.ts +84 -0
- package/tests/SortedTree.spec.ts +164 -0
- package/tests/SplayTree.spec.ts +121 -0
- package/tests/collections.spec.ts +1 -1
- package/tests/deque.spec.ts +107 -0
- package/tests/globals.spec.ts +2 -2
- package/tests/itertools.spec.ts +2 -2
- package/tests/math.spec.ts +1 -1
- package/tests/operators.spec.ts +5 -5
- package/tests/random.spec.ts +10 -10
- package/tsconfig.json +10 -10
- package/tsconfig.node.json +2 -2
- package/tsconfig.xo.json +2 -2
- package/src/operators/index.d.ts +0 -31
- package/src/operators/index.js +0 -171
- package/src/operators/index.ts +0 -175
|
@@ -0,0 +1,354 @@
|
|
|
1
|
+
import { compare, Comparer, eq } from '../operators/index.js'
|
|
2
|
+
import { Mapping } from './abc.js'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Base SplayTree node class.
|
|
6
|
+
* @private
|
|
7
|
+
*/
|
|
8
|
+
class Node<K = any, V = any> {
|
|
9
|
+
key: K
|
|
10
|
+
value: V
|
|
11
|
+
left?: Node<K, V>
|
|
12
|
+
right?: Node<K, V>
|
|
13
|
+
|
|
14
|
+
constructor(key: K, value: V) {
|
|
15
|
+
this.key = key
|
|
16
|
+
this.value = value
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
get height() {
|
|
20
|
+
if (this.left && this.right) return 1 + Math.max(this.right.height, this.left.height)
|
|
21
|
+
if (this.right) return 1 + this.right.height
|
|
22
|
+
if (this.left) return 1 + this.left.height
|
|
23
|
+
return 0
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// In-order traversal of a tree
|
|
28
|
+
function* traverse(node?: Node) {
|
|
29
|
+
while (node) {
|
|
30
|
+
yield* traverse(node.left)
|
|
31
|
+
yield node
|
|
32
|
+
node = node.right
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* A splay tree is a *self-balancing binary* search tree with the additional property that recently accessed elements are quick to access again.
|
|
38
|
+
* Insertion, look-up and removal in O(log n) *amortized* time.
|
|
39
|
+
* For many patterns of non-random operations splay trees can take better than logarithmic time, without requiring advance knowledge of the pattern.
|
|
40
|
+
* @see {@link https://en.wikipedia.org/wiki/Splay_tree Splay tree}
|
|
41
|
+
* @see {@link https://web.archive.org/web/20220930173835/http://www14.in.tum.de/personen/albers/papers/ipl02.pdf Randomized Splay Trees}
|
|
42
|
+
* @template K, V
|
|
43
|
+
*/
|
|
44
|
+
export class SplayTree<K, V = any> extends Mapping<K, V> {
|
|
45
|
+
protected root?: Node<K, V>
|
|
46
|
+
protected cmp: Comparer
|
|
47
|
+
protected randSplay: number
|
|
48
|
+
protected count: number
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Create a new randomized Splay Tree
|
|
52
|
+
* @param compareFn
|
|
53
|
+
* @param p When an element is accessed, with probability `p` splay the tree. With probability `1 - p`, leave the tree as it is.
|
|
54
|
+
*/
|
|
55
|
+
constructor(compareFn?, p = 0.5) {
|
|
56
|
+
super()
|
|
57
|
+
this.cmp = compareFn ?? compare
|
|
58
|
+
this.count = 0
|
|
59
|
+
this.randSplay = p
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
top() {
|
|
63
|
+
return this.root?.key
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
get size() {
|
|
67
|
+
return this.count
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
empty() {
|
|
71
|
+
return !this.root
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
get height() {
|
|
75
|
+
if (!this.root) return 0
|
|
76
|
+
return this.root.height
|
|
77
|
+
}
|
|
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: K) {
|
|
85
|
+
if (!this.root) return
|
|
86
|
+
|
|
87
|
+
// Note: deterministic splaying (always moves key to front):
|
|
88
|
+
// this.splay(key)
|
|
89
|
+
// if (this.cmp(key, this.root.key) === 0) return this.root.value
|
|
90
|
+
|
|
91
|
+
// Randomized Splay Trees https://doi.org/10.1016%2Fs0020-0190%2801%2900230-7
|
|
92
|
+
// The randomized idea is only moving accessed keys with a certain probability `p`.
|
|
93
|
+
// Values of `p` closer to 1 tend to perform well.
|
|
94
|
+
if (Math.random() <= this.randSplay) {
|
|
95
|
+
this.splay(key)
|
|
96
|
+
if (this.cmp(key, this.root.key) === 0) return this.root.value
|
|
97
|
+
else return undefined
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// Lookup the node down the tree
|
|
101
|
+
let current = this.root as any
|
|
102
|
+
while (current) {
|
|
103
|
+
const rank = this.cmp(key, current.key)
|
|
104
|
+
if (rank === 0) return current.value
|
|
105
|
+
if (rank < 0) current = current.left
|
|
106
|
+
else current = current.right
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Inserts the `key` and `value` in the tree if the `key` does not exist.
|
|
112
|
+
* @param {K} key A key which can be any comparable value
|
|
113
|
+
* @param {V} value A value associated with the key
|
|
114
|
+
* @returns
|
|
115
|
+
*/
|
|
116
|
+
set(key: K, value: V) {
|
|
117
|
+
if (!this.root) {
|
|
118
|
+
this.root = new Node(key, value)
|
|
119
|
+
this.count += 1
|
|
120
|
+
return
|
|
121
|
+
}
|
|
122
|
+
// Splay on the key to move the last node on the search path for
|
|
123
|
+
// the key to the root of the tree.
|
|
124
|
+
this.splay(key)
|
|
125
|
+
const rank = this.cmp(key, this.root.key)
|
|
126
|
+
if (rank === 0) {
|
|
127
|
+
// Update the value
|
|
128
|
+
this.root.value = value
|
|
129
|
+
return
|
|
130
|
+
}
|
|
131
|
+
const node = new Node(key, value)
|
|
132
|
+
if (rank > 0) {
|
|
133
|
+
node.left = this.root
|
|
134
|
+
node.right = this.root.right
|
|
135
|
+
this.root.right = undefined
|
|
136
|
+
} else {
|
|
137
|
+
node.right = this.root
|
|
138
|
+
node.left = this.root.left
|
|
139
|
+
this.root.left = undefined
|
|
140
|
+
}
|
|
141
|
+
this.root = node
|
|
142
|
+
this.count += 1
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Removes a specified key if it exists in the tree. The removed value is returned or `undefined` if not found.
|
|
147
|
+
* @param {K} key The key to remove.
|
|
148
|
+
* @returns {V} The removed value associated with `key`.
|
|
149
|
+
*/
|
|
150
|
+
remove(key: K) {
|
|
151
|
+
if (!this.root) {
|
|
152
|
+
return
|
|
153
|
+
}
|
|
154
|
+
this.splay(key)
|
|
155
|
+
if (!eq(key, this.root.key)) {
|
|
156
|
+
return
|
|
157
|
+
}
|
|
158
|
+
const removed = this.root
|
|
159
|
+
if (!this.root.left) {
|
|
160
|
+
this.root = this.root.right
|
|
161
|
+
} else {
|
|
162
|
+
const right = this.root.right
|
|
163
|
+
this.root = this.root.left
|
|
164
|
+
//
|
|
165
|
+
// Splay to make sure that the new root has an empty right child.
|
|
166
|
+
//
|
|
167
|
+
this.splay(key)
|
|
168
|
+
//
|
|
169
|
+
// Insert the original right child as the right child of the new
|
|
170
|
+
// root.
|
|
171
|
+
//
|
|
172
|
+
this.root.right = right
|
|
173
|
+
}
|
|
174
|
+
this.count -= 1
|
|
175
|
+
return removed.value
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
//
|
|
179
|
+
// function find(key)
|
|
180
|
+
// @param {number} key Key to find in the tree.
|
|
181
|
+
// @return {SplayTree.Node} Node having the specified key.
|
|
182
|
+
//
|
|
183
|
+
//
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Returns the minimum key in the tree or subtree.
|
|
187
|
+
* @returns The minimum key.
|
|
188
|
+
*/
|
|
189
|
+
min(root = this.root) {
|
|
190
|
+
if (!root) return
|
|
191
|
+
while (root.left) {
|
|
192
|
+
root = root.left
|
|
193
|
+
}
|
|
194
|
+
return root.key
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Returns the maximum key in the tree or subtree.
|
|
199
|
+
* @returns The maximum key.
|
|
200
|
+
*/
|
|
201
|
+
max(root = this.root) {
|
|
202
|
+
if (!root) return
|
|
203
|
+
while (root.right) {
|
|
204
|
+
root = root.right
|
|
205
|
+
}
|
|
206
|
+
return root.key
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Returns the largest key that is less than a given key.
|
|
211
|
+
* @param key The given key
|
|
212
|
+
* @returns The largest key found or `undefined`.
|
|
213
|
+
*/
|
|
214
|
+
lowerBound(key) {
|
|
215
|
+
if (!this.root) return
|
|
216
|
+
|
|
217
|
+
// Splay on the key to move the node with the given key or the last
|
|
218
|
+
// node on the search path to the top of the tree.
|
|
219
|
+
this.splay(key)
|
|
220
|
+
|
|
221
|
+
// Now the result is either the root node or the greatest node in
|
|
222
|
+
// the left subtree.
|
|
223
|
+
if (this.cmp(this.root.key, key) < 0) {
|
|
224
|
+
return this.root
|
|
225
|
+
} else if (this.root.left) {
|
|
226
|
+
return this.max(this.root.left)
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* 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}.
|
|
232
|
+
* @param key
|
|
233
|
+
* @see {@link https://doi.org/10.1145%2F3828.3835 Self-Adjusting Binary Search Trees}
|
|
234
|
+
*/
|
|
235
|
+
splay(key) {
|
|
236
|
+
if (!this.root) return
|
|
237
|
+
|
|
238
|
+
let stub, left, right
|
|
239
|
+
// eslint-disable-next-line
|
|
240
|
+
stub = left = right = new Node(null, null)
|
|
241
|
+
|
|
242
|
+
let root = this.root
|
|
243
|
+
|
|
244
|
+
while (true) {
|
|
245
|
+
const rank = this.cmp(key, root.key)
|
|
246
|
+
if (rank < 0) {
|
|
247
|
+
if (!root.left) {
|
|
248
|
+
break
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
if (this.cmp(key, root.left.key) < 0) {
|
|
252
|
+
// Rotate right.
|
|
253
|
+
const tmp = root.left
|
|
254
|
+
root.left = tmp.right
|
|
255
|
+
tmp.right = root
|
|
256
|
+
root = tmp
|
|
257
|
+
if (!root.left) {
|
|
258
|
+
break
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
// Link right.
|
|
263
|
+
right.left = root
|
|
264
|
+
right = root
|
|
265
|
+
root = root.left
|
|
266
|
+
} else if (rank > 0) {
|
|
267
|
+
if (!root.right) {
|
|
268
|
+
break
|
|
269
|
+
}
|
|
270
|
+
if (this.cmp(key, root.right.key) > 0) {
|
|
271
|
+
// Rotate left.
|
|
272
|
+
const tmp = root.right
|
|
273
|
+
root.right = tmp.left
|
|
274
|
+
tmp.left = root
|
|
275
|
+
root = tmp
|
|
276
|
+
if (!root.right) {
|
|
277
|
+
break
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
// Link left.
|
|
281
|
+
left.right = root
|
|
282
|
+
left = root
|
|
283
|
+
root = root.right
|
|
284
|
+
} else {
|
|
285
|
+
break
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
// Assemble.
|
|
290
|
+
left.right = root.left
|
|
291
|
+
right.left = root.right
|
|
292
|
+
root.left = stub.right
|
|
293
|
+
root.right = stub.left
|
|
294
|
+
this.root = root
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
add(key: K) {
|
|
298
|
+
return this.set(key, null as V)
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* Returns `true` if the given key is in the tree.
|
|
303
|
+
* @param {K} key The key to find.
|
|
304
|
+
* @returns `true` if found.
|
|
305
|
+
*/
|
|
306
|
+
contains(key: K): boolean {
|
|
307
|
+
return this.get(key) !== undefined
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/** Alias of remove but just returns `true` instead of the deleted value. */
|
|
311
|
+
delete(key: K): boolean {
|
|
312
|
+
// since remove throws, this will throw too
|
|
313
|
+
this.remove(key)
|
|
314
|
+
return true
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
clear() {
|
|
318
|
+
if (!this.root) return
|
|
319
|
+
this.root.left = undefined
|
|
320
|
+
this.root.right = undefined
|
|
321
|
+
this.root = undefined
|
|
322
|
+
this.count = 0
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* Returns an ordered iterable of all the keys in the tree.
|
|
327
|
+
* @returns {Iterable<K>} An iterable of keys in-order.
|
|
328
|
+
*/
|
|
329
|
+
*keys(): Iterable<K> {
|
|
330
|
+
for (const node of traverse(this.root)) {
|
|
331
|
+
yield node.key as K
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/**
|
|
336
|
+
* Returns an ordered iterable of all the keys and values in the tree.
|
|
337
|
+
* @returns {Iterable<[K,V]>} A iterable of `[key, value]` pairs sorted by key.
|
|
338
|
+
*/
|
|
339
|
+
*entries(): Iterable<[K, V]> {
|
|
340
|
+
for (const node of traverse(this.root)) {
|
|
341
|
+
yield [node.key as K, node.value as V]
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/**
|
|
346
|
+
* Returns an ordered iterable of all the keys and values in the tree.
|
|
347
|
+
* @returns {Iterable<V>} A iterable of `[key, value]` pairs sorted by key.
|
|
348
|
+
*/
|
|
349
|
+
*values(): Iterable<V> {
|
|
350
|
+
for (const node of traverse(this.root)) {
|
|
351
|
+
yield node.value as V
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
}
|
|
@@ -0,0 +1,152 @@
|
|
|
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 = unknown> {
|
|
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
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A container type that provides contains().
|
|
3
|
+
*
|
|
4
|
+
*
|
|
5
|
+
* @interface Container
|
|
6
|
+
*/
|
|
7
|
+
export interface Container {
|
|
8
|
+
contains: (x) => boolean
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Implements an equality check between two values.
|
|
13
|
+
* @interface Ordered
|
|
14
|
+
*/
|
|
15
|
+
export interface Equals {
|
|
16
|
+
eq: (other) => boolean
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Implements an iterable that allows comparing two elements to determine ordering.
|
|
21
|
+
* @interface Ordered
|
|
22
|
+
*/
|
|
23
|
+
export interface Comparable {
|
|
24
|
+
compare: (other) => number
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Implements an iterable that allows sorting elements.
|
|
29
|
+
* @interface Ordered
|
|
30
|
+
*/
|
|
31
|
+
export interface Ordered<T> {
|
|
32
|
+
lt: (other) => boolean
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Implements an iterable that allows backward iteration.
|
|
37
|
+
* @interface Reversible
|
|
38
|
+
*/
|
|
39
|
+
export interface Reversible<T = unknown> {
|
|
40
|
+
reversed: () => Iterable<T>
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* A `Collection` is an iterable {@link Container} type.
|
|
45
|
+
* This is an abstract base class for user-defined collection types.
|
|
46
|
+
*
|
|
47
|
+
*
|
|
48
|
+
* @abstract
|
|
49
|
+
* @class Collection
|
|
50
|
+
* @implements {Container}
|
|
51
|
+
*/
|
|
52
|
+
export abstract class Collection implements Container {
|
|
53
|
+
/**
|
|
54
|
+
* Adds a new item to the collection.
|
|
55
|
+
* @param item - The item to add to the collection.
|
|
56
|
+
*/
|
|
57
|
+
abstract add(item): any
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Checks if item is present in the collection.
|
|
61
|
+
* @param item - The item to search for in the collection.
|
|
62
|
+
* @returns `true` when the element is found, else `false`
|
|
63
|
+
*/
|
|
64
|
+
abstract contains(item): boolean
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Remove all items from the collection
|
|
68
|
+
*/
|
|
69
|
+
abstract clear()
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Returns the total number of elements in the collection.
|
|
73
|
+
*/
|
|
74
|
+
abstract get size(): number
|
|
75
|
+
|
|
76
|
+
get length() {
|
|
77
|
+
return this.size
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Check if there are no items.
|
|
82
|
+
* @returns {boolean} `true` if is empty.
|
|
83
|
+
*/
|
|
84
|
+
empty() {
|
|
85
|
+
return !this.size
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* A sequence is an iterable {@link Collection} type with efficient index-based access.
|
|
91
|
+
* @abstract
|
|
92
|
+
* @class Sequence
|
|
93
|
+
* @extends {Collection}
|
|
94
|
+
* @template V
|
|
95
|
+
*/
|
|
96
|
+
export abstract class Sequence<V = any, K = any> extends Collection {
|
|
97
|
+
/**
|
|
98
|
+
* Return the item at the given key or index
|
|
99
|
+
* @param {K} key
|
|
100
|
+
* @memberof Sequence
|
|
101
|
+
*/
|
|
102
|
+
abstract get(key: K)
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Set a new value at the given key or index
|
|
106
|
+
*
|
|
107
|
+
* @param {K} key
|
|
108
|
+
* @param {V} value
|
|
109
|
+
* @memberof Sequence
|
|
110
|
+
*/
|
|
111
|
+
abstract set(key: K, value: V)
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Adds a new item to the end of the sequence. Alias of {@link Collection.add}.
|
|
115
|
+
*
|
|
116
|
+
* @param {V} x
|
|
117
|
+
* @memberof Sequence
|
|
118
|
+
*/
|
|
119
|
+
abstract append(x: V | Iterable<V>)
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Remove a given value from the collection if exists.
|
|
123
|
+
* @returns the value that was removed or `undefined`.
|
|
124
|
+
*/
|
|
125
|
+
abstract remove(x: V)
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Returns an iterator of all the `keys` in order.
|
|
129
|
+
*/
|
|
130
|
+
abstract keys(): Iterable<K>
|
|
131
|
+
/**
|
|
132
|
+
* Returns an iterator of all the `values` in order.
|
|
133
|
+
*/
|
|
134
|
+
abstract values(): Iterable<V>
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Returns an iterator of all the `[key, value]` pairs in order.
|
|
138
|
+
*/
|
|
139
|
+
abstract entries(): Iterable<[K, V]>
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* A mapping is a {@link Collection} indexed by keys that may have associated values.
|
|
144
|
+
* @abstract
|
|
145
|
+
* @class Mapping
|
|
146
|
+
* @extends {Collection}
|
|
147
|
+
* @template K, V
|
|
148
|
+
*/
|
|
149
|
+
export abstract class Mapping<K, V> extends Collection {
|
|
150
|
+
/**
|
|
151
|
+
* Returns the value for the given key, or return `defaults` if not found.
|
|
152
|
+
* @param {K} key
|
|
153
|
+
* @param {*} [defaults]
|
|
154
|
+
*/
|
|
155
|
+
abstract get(key: K, defaults?): V | undefined
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Sets the value for the given key.
|
|
159
|
+
* @param {K} key
|
|
160
|
+
*/
|
|
161
|
+
abstract set(key: K, value: V)
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Returns an iterator of all the `keys` in the map.
|
|
165
|
+
*/
|
|
166
|
+
abstract keys(): Iterable<K>
|
|
167
|
+
/**
|
|
168
|
+
* Returns an iterator of all the `values` in the map.
|
|
169
|
+
*/
|
|
170
|
+
abstract values(): Iterable<V>
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Returns an iterator of all the `[key, value]` pairs in the map.
|
|
174
|
+
*/
|
|
175
|
+
abstract entries(): Iterable<[K, V]>
|
|
176
|
+
}
|