atomic-fns 0.3.1 → 0.3.3-beta
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 +62 -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 +152 -0
- package/core/collections/LRUCache.js +409 -0
- package/core/collections/LRUCache.ts +486 -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 +69 -0
- package/core/collections/SortedSet.js +99 -0
- package/core/collections/SortedSet.ts +110 -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 +43 -162
- package/{src → core}/collections/index.js +59 -165
- package/{src → core}/collections/index.ts +64 -226
- 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}/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 -1
- package/{src → core}/index.js +0 -1
- package/{src → core}/index.ts +0 -1
- 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/datetime/index.d.ts +0 -6
- package/src/datetime/index.js +0 -6
- package/src/datetime/index.ts +0 -7
- 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,409 @@
|
|
|
1
|
+
import { NotImplementedError, ValueError } from '../globals/index.js';
|
|
2
|
+
import { IterableIterator } from '../itertools/index.js';
|
|
3
|
+
import { Mapping } from './abc.js';
|
|
4
|
+
import { getPointerArray } from './typedArrays.js';
|
|
5
|
+
/**
|
|
6
|
+
* Base append-only Cache collection that supports adding and accessing keys in O(1).
|
|
7
|
+
* The collection has a fixed given size (default 1024) and when full, it evicts keys using LRU approach. This operation is also O(1).
|
|
8
|
+
* @template K, V
|
|
9
|
+
*/
|
|
10
|
+
export class Cache extends Mapping {
|
|
11
|
+
capacity;
|
|
12
|
+
head = 0;
|
|
13
|
+
tail = 0;
|
|
14
|
+
count = 0;
|
|
15
|
+
items = new Map();
|
|
16
|
+
forward;
|
|
17
|
+
backward;
|
|
18
|
+
K;
|
|
19
|
+
V;
|
|
20
|
+
/**
|
|
21
|
+
* Create a new cache object of fixed size.
|
|
22
|
+
* @param {number} [capacity=1024] The maximum number of keys to keep in cache.
|
|
23
|
+
* @param {Function} [Keys=Array] The constructor to use for the Keys array. When using numeric values, a TypedArray constructor can be used.
|
|
24
|
+
* @param {Function} [Values=Array] The constructor to use for the Values array. When using numeric values, a TypedArray constructor can be used.
|
|
25
|
+
*/
|
|
26
|
+
constructor(capacity = 1024, Keys = Array, Values = Array) {
|
|
27
|
+
super();
|
|
28
|
+
this.capacity = capacity;
|
|
29
|
+
if (typeof this.capacity !== 'number' || this.capacity <= 0)
|
|
30
|
+
throw new ValueError('LRUCache capacity should be a positive integer.');
|
|
31
|
+
else if (!isFinite(this.capacity) || Math.floor(this.capacity) !== this.capacity)
|
|
32
|
+
throw new ValueError('LRUCache capacity should be a finite positive integer.');
|
|
33
|
+
const PointerArray = getPointerArray(capacity);
|
|
34
|
+
this.forward = new PointerArray(capacity);
|
|
35
|
+
this.backward = new PointerArray(capacity);
|
|
36
|
+
this.K = new Keys(capacity);
|
|
37
|
+
this.V = new Values(capacity);
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* This is just an alias of {@link Cache.set}.
|
|
41
|
+
* @param {K} key The key to add (note value will be `undefined`).
|
|
42
|
+
*/
|
|
43
|
+
add(key) {
|
|
44
|
+
return this.set(key, undefined);
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Method used to check whether the key exists in the cache.
|
|
48
|
+
*
|
|
49
|
+
* @param {any} key - Key.
|
|
50
|
+
* @return {boolean}
|
|
51
|
+
*/
|
|
52
|
+
contains(key) {
|
|
53
|
+
return this.items.has(key);
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Method used to clear the structure.
|
|
57
|
+
*/
|
|
58
|
+
clear() {
|
|
59
|
+
this.count = 0;
|
|
60
|
+
this.head = 0;
|
|
61
|
+
this.tail = 0;
|
|
62
|
+
this.items = new Map();
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Returns the number of keys in the cache.
|
|
66
|
+
*/
|
|
67
|
+
get size() {
|
|
68
|
+
return this.count;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Method used to splay a value on top.
|
|
72
|
+
*
|
|
73
|
+
* @param {number} pointer - Pointer of the value to splay on top.
|
|
74
|
+
* @return {Cache}
|
|
75
|
+
*/
|
|
76
|
+
splayOnTop(pointer) {
|
|
77
|
+
const oldHead = this.head;
|
|
78
|
+
if (this.head === pointer)
|
|
79
|
+
return this;
|
|
80
|
+
const previous = this.backward[pointer];
|
|
81
|
+
const next = this.forward[pointer];
|
|
82
|
+
if (this.tail === pointer) {
|
|
83
|
+
this.tail = previous;
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
this.backward[next] = previous;
|
|
87
|
+
}
|
|
88
|
+
this.forward[previous] = next;
|
|
89
|
+
this.backward[oldHead] = pointer;
|
|
90
|
+
this.head = pointer;
|
|
91
|
+
this.forward[pointer] = oldHead;
|
|
92
|
+
return this;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Method used to get the value attached to the given key. Will move the
|
|
96
|
+
* related key to the front of the underlying linked list.
|
|
97
|
+
*
|
|
98
|
+
* @param {any} key - Key.
|
|
99
|
+
* @return {any}
|
|
100
|
+
*/
|
|
101
|
+
get(key) {
|
|
102
|
+
const pointer = this.items.get(key);
|
|
103
|
+
if (typeof pointer === 'undefined')
|
|
104
|
+
return;
|
|
105
|
+
this.splayOnTop(pointer);
|
|
106
|
+
return this.V[pointer];
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Method used to set the value for the given key in the cache.
|
|
110
|
+
*
|
|
111
|
+
* @param {any} key - Key.
|
|
112
|
+
* @param {any} value - Value.
|
|
113
|
+
* @return {undefined}
|
|
114
|
+
*/
|
|
115
|
+
set(key, value) {
|
|
116
|
+
let pointer = this.items.get(key);
|
|
117
|
+
// The key already exists, we just need to update the value and splay on top
|
|
118
|
+
if (typeof pointer !== 'undefined') {
|
|
119
|
+
this.splayOnTop(pointer);
|
|
120
|
+
this.V[pointer] = value;
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
// The cache is not yet full
|
|
124
|
+
if (this.count < this.capacity) {
|
|
125
|
+
pointer = this.count++;
|
|
126
|
+
}
|
|
127
|
+
// Cache is full, we need to drop the last value
|
|
128
|
+
else {
|
|
129
|
+
pointer = this.tail;
|
|
130
|
+
this.tail = this.backward[pointer];
|
|
131
|
+
this.items.delete(this.K[pointer]);
|
|
132
|
+
}
|
|
133
|
+
// Storing key & value
|
|
134
|
+
this.items.set(key, pointer);
|
|
135
|
+
this.K[pointer] = key;
|
|
136
|
+
this.V[pointer] = value;
|
|
137
|
+
// Moving the item at the front of the list
|
|
138
|
+
this.forward[pointer] = this.head;
|
|
139
|
+
this.backward[this.head] = pointer;
|
|
140
|
+
this.head = pointer;
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Method used to get the value attached to the given key. Does not modify
|
|
144
|
+
* the ordering of the underlying linked list.
|
|
145
|
+
*
|
|
146
|
+
* @param {any} key - Key.
|
|
147
|
+
* @return {any}
|
|
148
|
+
*/
|
|
149
|
+
peek(key) {
|
|
150
|
+
const pointer = this.items.get(key);
|
|
151
|
+
if (typeof pointer === 'undefined')
|
|
152
|
+
return;
|
|
153
|
+
return this.V[pointer];
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Method used to iterate over the cache's entries using a callback.
|
|
157
|
+
*
|
|
158
|
+
* @param {Iteratee<V>} iteratee - Function to call for each item.
|
|
159
|
+
*/
|
|
160
|
+
forEach(iteratee) {
|
|
161
|
+
let i = 0;
|
|
162
|
+
const l = this.count;
|
|
163
|
+
let pointer = this.head;
|
|
164
|
+
const keys = this.K;
|
|
165
|
+
const values = this.V;
|
|
166
|
+
const forward = this.forward;
|
|
167
|
+
while (i < l) {
|
|
168
|
+
iteratee(values[pointer], keys[pointer], this);
|
|
169
|
+
pointer = forward[pointer];
|
|
170
|
+
i++;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Method used to create an iterator over the cache's keys from most
|
|
175
|
+
* recently used to least recently used.
|
|
176
|
+
*
|
|
177
|
+
* @return {Iterator<K>}
|
|
178
|
+
*/
|
|
179
|
+
keys() {
|
|
180
|
+
let i = 0;
|
|
181
|
+
const l = this.count;
|
|
182
|
+
let pointer = this.head;
|
|
183
|
+
const keys = this.K;
|
|
184
|
+
const forward = this.forward;
|
|
185
|
+
return IterableIterator(() => {
|
|
186
|
+
if (i >= l)
|
|
187
|
+
return { done: true };
|
|
188
|
+
const key = keys[pointer];
|
|
189
|
+
i++;
|
|
190
|
+
if (i < l)
|
|
191
|
+
pointer = forward[pointer];
|
|
192
|
+
return {
|
|
193
|
+
done: false,
|
|
194
|
+
value: key
|
|
195
|
+
};
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Method used to create an iterator over the cache's values from most
|
|
200
|
+
* recently used to least recently used.
|
|
201
|
+
*
|
|
202
|
+
* @return {Iterator<V>}
|
|
203
|
+
*/
|
|
204
|
+
values() {
|
|
205
|
+
let i = 0;
|
|
206
|
+
const l = this.count;
|
|
207
|
+
let pointer = this.head;
|
|
208
|
+
const values = this.V;
|
|
209
|
+
const forward = this.forward;
|
|
210
|
+
return IterableIterator(() => {
|
|
211
|
+
if (i >= l)
|
|
212
|
+
return { done: true };
|
|
213
|
+
const value = values[pointer];
|
|
214
|
+
i++;
|
|
215
|
+
if (i < l)
|
|
216
|
+
pointer = forward[pointer];
|
|
217
|
+
return {
|
|
218
|
+
done: false,
|
|
219
|
+
value
|
|
220
|
+
};
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Method used to create an iterator over the cache's entries from most
|
|
225
|
+
* recently used to least recently used.
|
|
226
|
+
*
|
|
227
|
+
* @return {Iterator<[K, V]>}
|
|
228
|
+
*/
|
|
229
|
+
entries() {
|
|
230
|
+
let i = 0;
|
|
231
|
+
const l = this.count;
|
|
232
|
+
let pointer = this.head;
|
|
233
|
+
const keys = this.K;
|
|
234
|
+
const values = this.V;
|
|
235
|
+
const forward = this.forward;
|
|
236
|
+
return IterableIterator(() => {
|
|
237
|
+
if (i >= l)
|
|
238
|
+
return { done: true };
|
|
239
|
+
const key = keys[pointer];
|
|
240
|
+
const value = values[pointer];
|
|
241
|
+
i++;
|
|
242
|
+
if (i < l)
|
|
243
|
+
pointer = forward[pointer];
|
|
244
|
+
return { value: [key, value] };
|
|
245
|
+
});
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* NotImplemented -- throws an exception.
|
|
249
|
+
* @param key
|
|
250
|
+
* @returns
|
|
251
|
+
*/
|
|
252
|
+
remove(key) {
|
|
253
|
+
throw new NotImplementedError(`Base Cache class does not implement remove().`);
|
|
254
|
+
}
|
|
255
|
+
[Symbol.iterator]() {
|
|
256
|
+
return this.entries();
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* Convenience known methods.
|
|
260
|
+
*/
|
|
261
|
+
inspect() {
|
|
262
|
+
const proxy = new Map();
|
|
263
|
+
const iterator = this.entries();
|
|
264
|
+
let step;
|
|
265
|
+
while (((step = iterator.next()), !step.done))
|
|
266
|
+
proxy.set(step.value[0], step.value[1]);
|
|
267
|
+
// Trick so that node displays the name of the constructor
|
|
268
|
+
Object.defineProperty(proxy, 'constructor', {
|
|
269
|
+
value: Cache,
|
|
270
|
+
enumerable: false
|
|
271
|
+
});
|
|
272
|
+
return proxy;
|
|
273
|
+
}
|
|
274
|
+
[Symbol.for('nodejs.util.inspect.custom')]() {
|
|
275
|
+
return this.inspect();
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
* Implements a Least Recently Used fixed-capacity cache which supports updating, removing, and accessing keys in O(1).
|
|
280
|
+
*/
|
|
281
|
+
export class LRUCache extends Cache {
|
|
282
|
+
deletedSize = 0;
|
|
283
|
+
deleted;
|
|
284
|
+
/**
|
|
285
|
+
* Create a new LRU cache object of fixed size.
|
|
286
|
+
* @param {number} [capacity=1024] The maximum number of keys to keep in cache.
|
|
287
|
+
* @param {Function} [Keys=Array] The constructor to use for the Keys array. When using numeric values, a TypedArray constructor can be used.
|
|
288
|
+
* @param {Function} [Values=Array] The constructor to use for the Values array. When using numeric values, a TypedArray constructor can be used.
|
|
289
|
+
*/
|
|
290
|
+
constructor(capacity = 1024, Keys = Array, Values = Array) {
|
|
291
|
+
super(capacity, Keys, Values);
|
|
292
|
+
const PointerArray = getPointerArray(capacity);
|
|
293
|
+
this.deleted = new PointerArray(capacity);
|
|
294
|
+
}
|
|
295
|
+
/** Remove all elements in the cache. */
|
|
296
|
+
clear() {
|
|
297
|
+
super.clear();
|
|
298
|
+
this.deletedSize = 0;
|
|
299
|
+
}
|
|
300
|
+
/**
|
|
301
|
+
* Method used to set the value for the given key in the cache.
|
|
302
|
+
*
|
|
303
|
+
* @param {any} key - Key.
|
|
304
|
+
* @param {any} value - Value.
|
|
305
|
+
* @return {undefined}
|
|
306
|
+
*/
|
|
307
|
+
set(key, value) {
|
|
308
|
+
let pointer = this.items.get(key);
|
|
309
|
+
// The key already exists, we just need to update the value and splay on top
|
|
310
|
+
if (typeof pointer !== 'undefined') {
|
|
311
|
+
this.splayOnTop(pointer);
|
|
312
|
+
this.V[pointer] = value;
|
|
313
|
+
return;
|
|
314
|
+
}
|
|
315
|
+
// The cache is not yet full
|
|
316
|
+
if (this.count < this.capacity) {
|
|
317
|
+
if (this.deletedSize > 0) {
|
|
318
|
+
// If there is a "hole" in the pointer list, reuse it
|
|
319
|
+
pointer = this.deleted[--this.deletedSize];
|
|
320
|
+
}
|
|
321
|
+
else {
|
|
322
|
+
// otherwise append to the pointer list
|
|
323
|
+
pointer = this.count;
|
|
324
|
+
}
|
|
325
|
+
this.count++;
|
|
326
|
+
}
|
|
327
|
+
// Cache is full, we need to drop the last value
|
|
328
|
+
else {
|
|
329
|
+
pointer = this.tail;
|
|
330
|
+
this.tail = this.backward[pointer];
|
|
331
|
+
this.items.delete(this.K[pointer]);
|
|
332
|
+
}
|
|
333
|
+
// Storing key & value
|
|
334
|
+
this.items.set(key, pointer);
|
|
335
|
+
this.K[pointer] = key;
|
|
336
|
+
this.V[pointer] = value;
|
|
337
|
+
// Moving the item at the front of the list
|
|
338
|
+
this.forward[pointer] = this.head;
|
|
339
|
+
this.backward[this.head] = pointer;
|
|
340
|
+
this.head = pointer;
|
|
341
|
+
}
|
|
342
|
+
/**
|
|
343
|
+
* Method used to delete the entry for the given key in the cache.
|
|
344
|
+
*
|
|
345
|
+
* @param {any} key - Key.
|
|
346
|
+
* @return {boolean} - true if the item was present
|
|
347
|
+
*/
|
|
348
|
+
delete(key) {
|
|
349
|
+
const pointer = this.items.get(key);
|
|
350
|
+
if (typeof pointer === 'undefined') {
|
|
351
|
+
return false;
|
|
352
|
+
}
|
|
353
|
+
this.items.delete(key);
|
|
354
|
+
if (this.count === 1) {
|
|
355
|
+
this.count = 0;
|
|
356
|
+
this.head = 0;
|
|
357
|
+
this.tail = 0;
|
|
358
|
+
this.deletedSize = 0;
|
|
359
|
+
return true;
|
|
360
|
+
}
|
|
361
|
+
const previous = this.backward[pointer];
|
|
362
|
+
const next = this.forward[pointer];
|
|
363
|
+
if (this.head === pointer) {
|
|
364
|
+
this.head = next;
|
|
365
|
+
}
|
|
366
|
+
if (this.tail === pointer) {
|
|
367
|
+
this.tail = previous;
|
|
368
|
+
}
|
|
369
|
+
this.forward[previous] = next;
|
|
370
|
+
this.backward[next] = previous;
|
|
371
|
+
this.count--;
|
|
372
|
+
this.deleted[this.deletedSize++] = pointer;
|
|
373
|
+
return true;
|
|
374
|
+
}
|
|
375
|
+
/**
|
|
376
|
+
* Method used to remove and return the value for the given key in the cache.
|
|
377
|
+
*
|
|
378
|
+
* @param {any} key - Key.
|
|
379
|
+
* @return {any} The value, if present; the missing indicator if absent
|
|
380
|
+
*/
|
|
381
|
+
remove(key) {
|
|
382
|
+
const pointer = this.items.get(key);
|
|
383
|
+
if (typeof pointer === 'undefined') {
|
|
384
|
+
return;
|
|
385
|
+
}
|
|
386
|
+
const dead = this.V[pointer];
|
|
387
|
+
this.items.delete(key);
|
|
388
|
+
if (this.count === 1) {
|
|
389
|
+
this.count = 0;
|
|
390
|
+
this.head = 0;
|
|
391
|
+
this.tail = 0;
|
|
392
|
+
this.deletedSize = 0;
|
|
393
|
+
return dead;
|
|
394
|
+
}
|
|
395
|
+
const previous = this.backward[pointer];
|
|
396
|
+
const next = this.forward[pointer];
|
|
397
|
+
if (this.head === pointer) {
|
|
398
|
+
this.head = next;
|
|
399
|
+
}
|
|
400
|
+
if (this.tail === pointer) {
|
|
401
|
+
this.tail = previous;
|
|
402
|
+
}
|
|
403
|
+
this.forward[previous] = next;
|
|
404
|
+
this.backward[next] = previous;
|
|
405
|
+
this.count--;
|
|
406
|
+
this.deleted[this.deletedSize++] = pointer;
|
|
407
|
+
return dead;
|
|
408
|
+
}
|
|
409
|
+
}
|