atomic-fns 0.3.1 → 0.3.3

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