atomic-fns 0.3.5 → 0.4.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 (109) hide show
  1. package/README.md +1 -12
  2. package/{core → dist}/collections/BTree.d.ts +6 -6
  3. package/{core → dist}/collections/BTree.js +17 -17
  4. package/{core → dist}/collections/Heap.d.ts +3 -3
  5. package/{core → dist}/collections/Heap.js +0 -0
  6. package/dist/collections/LRUCache.d.ts +80 -0
  7. package/dist/collections/LRUCache.js +126 -0
  8. package/{core → dist}/collections/SortedMap.d.ts +2 -2
  9. package/{core → dist}/collections/SortedMap.js +0 -0
  10. package/{core → dist}/collections/SortedSet.d.ts +3 -3
  11. package/{core → dist}/collections/SortedSet.js +1 -1
  12. package/{core → dist}/collections/SortedTree.d.ts +3 -3
  13. package/{core → dist}/collections/SortedTree.js +1 -1
  14. package/{core → dist}/collections/SplayTree.d.ts +5 -5
  15. package/{core → dist}/collections/SplayTree.js +0 -0
  16. package/{core → dist}/collections/abc.d.ts +0 -0
  17. package/{core → dist}/collections/abc.js +0 -0
  18. package/{core → dist}/collections/deque.d.ts +7 -7
  19. package/{core → dist}/collections/deque.js +6 -8
  20. package/{core → dist}/collections/frozenset.d.ts +0 -0
  21. package/{core → dist}/collections/frozenset.js +3 -6
  22. package/{core → dist}/collections/index.d.ts +207 -105
  23. package/{core → dist}/collections/index.js +179 -436
  24. package/{core → dist}/functools/index.d.ts +30 -16
  25. package/{core → dist}/functools/index.js +34 -12
  26. package/dist/globals/decimal.d.ts +142 -0
  27. package/dist/globals/decimal.js +371 -0
  28. package/{core → dist}/globals/index.d.ts +26 -25
  29. package/{core → dist}/globals/index.js +16 -37
  30. package/dist/i18n/currency.d.ts +24 -0
  31. package/dist/i18n/currency.js +85 -0
  32. package/dist/i18n/date/index.d.ts +204 -0
  33. package/dist/i18n/date/index.js +530 -0
  34. package/dist/i18n/date/utils.d.ts +65 -0
  35. package/dist/i18n/date/utils.js +290 -0
  36. package/dist/i18n/duration.d.ts +112 -0
  37. package/dist/i18n/duration.js +392 -0
  38. package/dist/i18n/format.d.ts +26 -0
  39. package/dist/i18n/format.js +495 -0
  40. package/{core → dist}/i18n/index.d.ts +2 -1
  41. package/{core → dist}/i18n/index.js +2 -1
  42. package/{core → dist}/index.d.ts +0 -0
  43. package/{core → dist}/index.js +0 -0
  44. package/{core → dist}/itertools/index.d.ts +74 -46
  45. package/{core → dist}/itertools/index.js +28 -56
  46. package/{core → dist}/math/index.d.ts +9 -10
  47. package/{core → dist}/math/index.js +3 -1
  48. package/{core → dist}/operators/index.d.ts +4 -4
  49. package/{core → dist}/operators/index.js +2 -8
  50. package/{core → dist}/random/index.d.ts +1 -1
  51. package/{core → dist}/random/index.js +5 -5
  52. package/package.json +28 -42
  53. package/.editorconfig +0 -12
  54. package/.esdoc.json +0 -27
  55. package/.eslintignore +0 -1
  56. package/.eslintrc.json +0 -35
  57. package/.firebaserc +0 -5
  58. package/.prettierrc +0 -19
  59. package/core/collections/BTree.ts +0 -1340
  60. package/core/collections/Heap.ts +0 -189
  61. package/core/collections/LRUCache.d.ts +0 -152
  62. package/core/collections/LRUCache.js +0 -409
  63. package/core/collections/LRUCache.ts +0 -486
  64. package/core/collections/SortedMap.ts +0 -146
  65. package/core/collections/SortedSet.ts +0 -110
  66. package/core/collections/SortedTree.ts +0 -160
  67. package/core/collections/SplayTree.ts +0 -354
  68. package/core/collections/abc.ts +0 -176
  69. package/core/collections/deque.ts +0 -338
  70. package/core/collections/frozenset.ts +0 -37
  71. package/core/collections/index.ts +0 -1024
  72. package/core/collections/test.html +0 -10
  73. package/core/collections/typedArrays.d.ts +0 -31
  74. package/core/collections/typedArrays.js +0 -143
  75. package/core/collections/typedArrays.ts +0 -163
  76. package/core/functools/index.ts +0 -260
  77. package/core/globals/index.ts +0 -481
  78. package/core/i18n/date/format.d.ts +0 -16
  79. package/core/i18n/date/format.js +0 -307
  80. package/core/i18n/date/format.ts +0 -343
  81. package/core/i18n/date/index.d.ts +0 -224
  82. package/core/i18n/date/index.js +0 -387
  83. package/core/i18n/date/index.ts +0 -471
  84. package/core/i18n/date/test.html +0 -67
  85. package/core/i18n/index.ts +0 -8
  86. package/core/index.ts +0 -8
  87. package/core/itertools/index.ts +0 -494
  88. package/core/math/index.ts +0 -152
  89. package/core/operators/index.ts +0 -310
  90. package/core/random/index.ts +0 -66
  91. package/firebase.json +0 -10
  92. package/jasmine.json +0 -14
  93. package/tests/BTree.spec.ts +0 -143
  94. package/tests/Cache.spec.ts +0 -161
  95. package/tests/Heap.spec.ts +0 -66
  96. package/tests/SortedMap.spec.ts +0 -144
  97. package/tests/SortedSet.spec.ts +0 -84
  98. package/tests/SortedTree.spec.ts +0 -164
  99. package/tests/SplayTree.spec.ts +0 -121
  100. package/tests/collections.spec.ts +0 -288
  101. package/tests/deque.spec.ts +0 -107
  102. package/tests/globals.spec.ts +0 -270
  103. package/tests/itertools.spec.ts +0 -266
  104. package/tests/math.spec.ts +0 -25
  105. package/tests/operators.spec.ts +0 -40
  106. package/tests/random.spec.ts +0 -46
  107. package/tsconfig.json +0 -126
  108. package/tsconfig.node.json +0 -18
  109. package/tsconfig.xo.json +0 -9
@@ -1,409 +0,0 @@
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
- }