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.
Files changed (85) 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 +12 -161
  34. package/{src → core}/collections/index.js +13 -164
  35. package/{src → core}/collections/index.ts +13 -224
  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/{src → core}/datetime/index.d.ts +0 -0
  41. package/{src → core}/datetime/index.js +0 -0
  42. package/{src → core}/datetime/index.ts +0 -0
  43. package/core/datetime/test.html +0 -0
  44. package/{src → core}/functools/index.d.ts +2 -2
  45. package/{src → core}/functools/index.js +2 -2
  46. package/{src → core}/functools/index.ts +2 -2
  47. package/{src → core}/globals/index.d.ts +2 -7
  48. package/{src → core}/globals/index.js +5 -2
  49. package/{src → core}/globals/index.ts +6 -9
  50. package/{src → core}/index.d.ts +0 -0
  51. package/{src → core}/index.js +0 -0
  52. package/{src → core}/index.ts +0 -0
  53. package/{src → core}/itertools/index.d.ts +9 -7
  54. package/{src → core}/itertools/index.js +25 -9
  55. package/{src → core}/itertools/index.ts +39 -16
  56. package/{src → core}/math/index.d.ts +0 -0
  57. package/{src → core}/math/index.js +0 -0
  58. package/{src → core}/math/index.ts +0 -0
  59. package/core/operators/index.d.ts +148 -0
  60. package/core/operators/index.js +271 -0
  61. package/core/operators/index.ts +282 -0
  62. package/{src → core}/random/index.d.ts +0 -0
  63. package/{src → core}/random/index.js +0 -0
  64. package/{src → core}/random/index.ts +0 -0
  65. package/package.json +6 -6
  66. package/tests/BTree.spec.ts +143 -0
  67. package/tests/Cache.spec.ts +161 -0
  68. package/tests/Heap.spec.ts +66 -0
  69. package/tests/SortedMap.spec.ts +144 -0
  70. package/tests/SortedSet.spec.ts +84 -0
  71. package/tests/SortedTree.spec.ts +164 -0
  72. package/tests/SplayTree.spec.ts +121 -0
  73. package/tests/collections.spec.ts +1 -1
  74. package/tests/deque.spec.ts +107 -0
  75. package/tests/globals.spec.ts +2 -2
  76. package/tests/itertools.spec.ts +2 -2
  77. package/tests/math.spec.ts +1 -1
  78. package/tests/operators.spec.ts +5 -5
  79. package/tests/random.spec.ts +10 -10
  80. package/tsconfig.json +10 -10
  81. package/tsconfig.node.json +2 -2
  82. package/tsconfig.xo.json +2 -2
  83. package/src/operators/index.d.ts +0 -31
  84. package/src/operators/index.js +0 -171
  85. package/src/operators/index.ts +0 -175
@@ -0,0 +1,69 @@
1
+ import type { Comparer } from '../operators/index.js';
2
+ import { compare } from '../operators/index.js';
3
+ import { Collection } from './abc.js';
4
+ export declare class Heap<T> extends Collection {
5
+ private readonly heap;
6
+ private readonly compare;
7
+ private count;
8
+ /**
9
+ * Initializes a new Heap instance.
10
+ *
11
+ * **Note:** When constructing the heap from an array, it will operate directly on this array. For other iterables, it will create a new array.
12
+ *
13
+ * @param {Iterable<T>} [container=[]] The initial values.
14
+ * @param {Comparer} [cmp=compare] Compare function. Defaults to smaller values first.
15
+ */
16
+ constructor(container?: Iterable<T>, cmp?: Comparer);
17
+ at(n: number): T | undefined;
18
+ get size(): number;
19
+ clear(): void;
20
+ /**
21
+ * Push element into a container in order.
22
+ * @param item The element to push.
23
+ */
24
+ add(item: T): void;
25
+ /**
26
+ * Removes the top element.
27
+ */
28
+ pop(): T | undefined;
29
+ /**
30
+ * Accesses the top element.
31
+ */
32
+ top(): T | undefined;
33
+ /**
34
+ * Check if element is in heap.
35
+ * @param item The item want to find.
36
+ * @return `true` if element exists.
37
+ */
38
+ contains(item: T): boolean;
39
+ /**
40
+ * Remove specified item from heap.
41
+ * @param item The item want to remove.
42
+ * @return `true` if the item was removed.
43
+ */
44
+ remove(item: T): boolean;
45
+ /**
46
+ * Returns an iterable with all the values in the heap.
47
+ * @returns {Iterable<T>} The values in the heap.
48
+ */
49
+ values(): IterableIterator<T>;
50
+ }
51
+ /**
52
+ * Transform any array into a heap, in-place, in linear time.
53
+ * @param {Array} heap
54
+ * @param {Comparer} [compareFn=compare] Custom compare function
55
+ */
56
+ export declare function heapify(heap: any, compareFn?: typeof compare): void;
57
+ /**
58
+ * Push the value `item` onto the `heap`, maintaining the heap invariant.
59
+ * @param {Array} heap
60
+ * @param {*} item
61
+ * @param {Comparer} [compareFn=compare] Custom compare function
62
+ */
63
+ export declare function heappush<T>(heap: T[], item: T, compareFn?: typeof compare): void;
64
+ /**
65
+ * Pop and return the smallest item from the `heap`, maintaining the heap invariant.
66
+ * @param {Array} heap
67
+ * @param {Comparer} [compareFn=compare] Custom compare function
68
+ */
69
+ export declare function heappop<T>(heap: T[], compareFn?: typeof compare): T;
@@ -0,0 +1,181 @@
1
+ import { compare } from '../operators/index.js';
2
+ import { Collection } from './abc.js';
3
+ export class Heap extends Collection {
4
+ heap;
5
+ compare;
6
+ count = 0;
7
+ /**
8
+ * Initializes a new Heap instance.
9
+ *
10
+ * **Note:** When constructing the heap from an array, it will operate directly on this array. For other iterables, it will create a new array.
11
+ *
12
+ * @param {Iterable<T>} [container=[]] The initial values.
13
+ * @param {Comparer} [cmp=compare] Compare function. Defaults to smaller values first.
14
+ */
15
+ constructor(container = [], cmp = compare) {
16
+ super();
17
+ this.compare = cmp;
18
+ if (Array.isArray(container)) {
19
+ // use the provided array to avoid copying.
20
+ this.heap = container;
21
+ }
22
+ else {
23
+ this.heap = Array.from(container);
24
+ }
25
+ this.count = this.heap.length;
26
+ const halfLength = this.count >> 1;
27
+ // Heapify the items
28
+ for (let parent = (this.count - 1) >> 1; parent >= 0; --parent) {
29
+ heapifyDown(this.heap, parent, halfLength, this.compare);
30
+ }
31
+ }
32
+ at(n) {
33
+ return this.heap.at(n);
34
+ }
35
+ get size() {
36
+ return this.count;
37
+ }
38
+ clear() {
39
+ this.count = 0;
40
+ this.heap.length = 0;
41
+ }
42
+ /**
43
+ * Push element into a container in order.
44
+ * @param item The element to push.
45
+ */
46
+ add(item) {
47
+ this.heap.push(item);
48
+ heapifyUp(this.heap, this.count, this.compare);
49
+ this.count += 1;
50
+ }
51
+ /**
52
+ * Removes the top element.
53
+ */
54
+ pop() {
55
+ if (!this.count)
56
+ return;
57
+ const value = this.heap[0];
58
+ const last = this.heap.pop();
59
+ this.count -= 1;
60
+ if (this.count) {
61
+ this.heap[0] = last;
62
+ heapifyDown(this.heap, 0, this.count >> 1, this.compare);
63
+ }
64
+ return value;
65
+ }
66
+ /**
67
+ * Accesses the top element.
68
+ */
69
+ top() {
70
+ if (!this.count)
71
+ return;
72
+ return this.heap[0];
73
+ }
74
+ /**
75
+ * Check if element is in heap.
76
+ * @param item The item want to find.
77
+ * @return `true` if element exists.
78
+ */
79
+ contains(item) {
80
+ if (!this.count)
81
+ return false;
82
+ return this.heap.includes(item);
83
+ }
84
+ /**
85
+ * Remove specified item from heap.
86
+ * @param item The item want to remove.
87
+ * @return `true` if the item was removed.
88
+ */
89
+ remove(item) {
90
+ const index = this.heap.indexOf(item);
91
+ if (index < 0)
92
+ return false;
93
+ if (index === 0) {
94
+ this.pop();
95
+ }
96
+ else if (index === this.count - 1) {
97
+ this.heap.pop();
98
+ this.count -= 1;
99
+ }
100
+ else {
101
+ this.heap.splice(index, 1, this.heap.pop());
102
+ this.count -= 1;
103
+ heapifyUp(this.heap, index, this.compare);
104
+ heapifyDown(this.heap, index, this.count >> 1, this.compare);
105
+ }
106
+ return true;
107
+ }
108
+ /**
109
+ * Returns an iterable with all the values in the heap.
110
+ * @returns {Iterable<T>} The values in the heap.
111
+ */
112
+ values() {
113
+ return this.heap[Symbol.iterator]();
114
+ }
115
+ }
116
+ function heapifyDown(heap, i, halfLength, compareFn = compare) {
117
+ const item = heap[i];
118
+ while (i < halfLength) {
119
+ let left = (i << 1) | 1;
120
+ const right = left + 1;
121
+ let minItem = heap[left];
122
+ if (right < heap.length && compareFn(minItem, heap[right]) > 0) {
123
+ left = right;
124
+ minItem = heap[right];
125
+ }
126
+ if (compareFn(minItem, item) >= 0)
127
+ break;
128
+ heap[i] = minItem;
129
+ i = left;
130
+ }
131
+ heap[i] = item;
132
+ }
133
+ function heapifyUp(heap, i, compareFn) {
134
+ const item = heap[i];
135
+ while (i > 0) {
136
+ const parent = (i - 1) >> 1;
137
+ const parentItem = heap[parent];
138
+ if (compareFn(parentItem, item) <= 0)
139
+ break;
140
+ heap[i] = parentItem;
141
+ i = parent;
142
+ }
143
+ heap[i] = item;
144
+ }
145
+ /**
146
+ * Transform any array into a heap, in-place, in linear time.
147
+ * @param {Array} heap
148
+ * @param {Comparer} [compareFn=compare] Custom compare function
149
+ */
150
+ export function heapify(heap, compareFn = compare) {
151
+ const size = heap.length;
152
+ const halfLength = size >> 1;
153
+ for (let parent = (size - 1) >> 1; parent >= 0; --parent) {
154
+ heapifyDown(heap, parent, halfLength, compareFn);
155
+ }
156
+ }
157
+ /**
158
+ * Push the value `item` onto the `heap`, maintaining the heap invariant.
159
+ * @param {Array} heap
160
+ * @param {*} item
161
+ * @param {Comparer} [compareFn=compare] Custom compare function
162
+ */
163
+ export function heappush(heap, item, compareFn = compare) {
164
+ heap.push(item);
165
+ heapifyUp(heap, heap.length - 1, compareFn);
166
+ }
167
+ /**
168
+ * Pop and return the smallest item from the `heap`, maintaining the heap invariant.
169
+ * @param {Array} heap
170
+ * @param {Comparer} [compareFn=compare] Custom compare function
171
+ */
172
+ export function heappop(heap, compareFn = compare) {
173
+ const last = heap.pop();
174
+ if (heap.length) {
175
+ const item = heap[0];
176
+ heap[0] = last;
177
+ heapifyDown(heap, 0, heap.length >> 1, compareFn);
178
+ return item;
179
+ }
180
+ return last;
181
+ }
@@ -0,0 +1,189 @@
1
+ import type { Comparer } from '../operators/index.js'
2
+ import { compare } from '../operators/index.js'
3
+ import { Collection } from './abc.js'
4
+
5
+ export class Heap<T> extends Collection {
6
+ private readonly heap: T[]
7
+ private readonly compare: Comparer
8
+ private count: number = 0
9
+
10
+ /**
11
+ * Initializes a new Heap instance.
12
+ *
13
+ * **Note:** When constructing the heap from an array, it will operate directly on this array. For other iterables, it will create a new array.
14
+ *
15
+ * @param {Iterable<T>} [container=[]] The initial values.
16
+ * @param {Comparer} [cmp=compare] Compare function. Defaults to smaller values first.
17
+ */
18
+ constructor(container: Iterable<T> = [], cmp: Comparer = compare) {
19
+ super()
20
+ this.compare = cmp
21
+ if (Array.isArray(container)) {
22
+ // use the provided array to avoid copying.
23
+ this.heap = container
24
+ } else {
25
+ this.heap = Array.from(container)
26
+ }
27
+ this.count = this.heap.length
28
+ const halfLength = this.count >> 1
29
+ // Heapify the items
30
+ for (let parent = (this.count - 1) >> 1; parent >= 0; --parent) {
31
+ heapifyDown(this.heap, parent, halfLength, this.compare)
32
+ }
33
+ }
34
+
35
+ at(n: number) {
36
+ return this.heap.at(n)
37
+ }
38
+
39
+ get size() {
40
+ return this.count
41
+ }
42
+
43
+ clear() {
44
+ this.count = 0
45
+ this.heap.length = 0
46
+ }
47
+
48
+ /**
49
+ * Push element into a container in order.
50
+ * @param item The element to push.
51
+ */
52
+ add(item: T) {
53
+ this.heap.push(item)
54
+ heapifyUp(this.heap, this.count, this.compare)
55
+ this.count += 1
56
+ }
57
+
58
+ /**
59
+ * Removes the top element.
60
+ */
61
+ pop() {
62
+ if (!this.count) return
63
+ const value = this.heap[0]
64
+ const last = this.heap.pop() as T
65
+ this.count -= 1
66
+ if (this.count) {
67
+ this.heap[0] = last
68
+ heapifyDown(this.heap, 0, this.count >> 1, this.compare)
69
+ }
70
+ return value
71
+ }
72
+
73
+ /**
74
+ * Accesses the top element.
75
+ */
76
+ top() {
77
+ if (!this.count) return
78
+ return this.heap[0] as T | undefined
79
+ }
80
+
81
+ /**
82
+ * Check if element is in heap.
83
+ * @param item The item want to find.
84
+ * @return `true` if element exists.
85
+ */
86
+ contains(item: T) {
87
+ if (!this.count) return false
88
+ return this.heap.includes(item)
89
+ }
90
+
91
+ /**
92
+ * Remove specified item from heap.
93
+ * @param item The item want to remove.
94
+ * @return `true` if the item was removed.
95
+ */
96
+ remove(item: T) {
97
+ const index = this.heap.indexOf(item)
98
+ if (index < 0) return false
99
+ if (index === 0) {
100
+ this.pop()
101
+ } else if (index === this.count - 1) {
102
+ this.heap.pop()
103
+ this.count -= 1
104
+ } else {
105
+ this.heap.splice(index, 1, this.heap.pop() as T)
106
+ this.count -= 1
107
+ heapifyUp(this.heap, index, this.compare)
108
+ heapifyDown(this.heap, index, this.count >> 1, this.compare)
109
+ }
110
+ return true
111
+ }
112
+
113
+ /**
114
+ * Returns an iterable with all the values in the heap.
115
+ * @returns {Iterable<T>} The values in the heap.
116
+ */
117
+ values() {
118
+ return this.heap[Symbol.iterator]()
119
+ }
120
+ }
121
+
122
+ function heapifyDown(heap, i: number, halfLength: number, compareFn = compare) {
123
+ const item = heap[i]
124
+ while (i < halfLength) {
125
+ let left = (i << 1) | 1
126
+ const right = left + 1
127
+ let minItem = heap[left]
128
+ if (right < heap.length && compareFn(minItem, heap[right]) > 0) {
129
+ left = right
130
+ minItem = heap[right]
131
+ }
132
+ if (compareFn(minItem, item) >= 0) break
133
+ heap[i] = minItem
134
+ i = left
135
+ }
136
+ heap[i] = item
137
+ }
138
+
139
+ function heapifyUp(heap, i: number, compareFn) {
140
+ const item = heap[i]
141
+ while (i > 0) {
142
+ const parent = (i - 1) >> 1
143
+ const parentItem = heap[parent]
144
+ if (compareFn(parentItem, item) <= 0) break
145
+ heap[i] = parentItem
146
+ i = parent
147
+ }
148
+ heap[i] = item
149
+ }
150
+
151
+ /**
152
+ * Transform any array into a heap, in-place, in linear time.
153
+ * @param {Array} heap
154
+ * @param {Comparer} [compareFn=compare] Custom compare function
155
+ */
156
+ export function heapify(heap, compareFn = compare) {
157
+ const size = heap.length
158
+ const halfLength = size >> 1
159
+ for (let parent = (size - 1) >> 1; parent >= 0; --parent) {
160
+ heapifyDown(heap, parent, halfLength, compareFn)
161
+ }
162
+ }
163
+
164
+ /**
165
+ * Push the value `item` onto the `heap`, maintaining the heap invariant.
166
+ * @param {Array} heap
167
+ * @param {*} item
168
+ * @param {Comparer} [compareFn=compare] Custom compare function
169
+ */
170
+ export function heappush<T>(heap: T[], item: T, compareFn = compare) {
171
+ heap.push(item)
172
+ heapifyUp(heap, heap.length - 1, compareFn)
173
+ }
174
+
175
+ /**
176
+ * Pop and return the smallest item from the `heap`, maintaining the heap invariant.
177
+ * @param {Array} heap
178
+ * @param {Comparer} [compareFn=compare] Custom compare function
179
+ */
180
+ export function heappop<T>(heap: T[], compareFn = compare) {
181
+ const last = heap.pop() as T
182
+ if (heap.length) {
183
+ const item = heap[0]
184
+ heap[0] = last
185
+ heapifyDown(heap, 0, heap.length >> 1, compareFn)
186
+ return item
187
+ }
188
+ return last
189
+ }
@@ -0,0 +1,131 @@
1
+ import { Iteratee } from '../globals/index.js';
2
+ import { Mapping } from './abc.js';
3
+ /**
4
+ * Base append-only Cache collection.
5
+ */
6
+ export declare class Cache<K, V = any> extends Mapping<K, V> {
7
+ capacity: number;
8
+ head: number;
9
+ tail: number;
10
+ count: number;
11
+ protected items: Map<K, number>;
12
+ protected forward: Uint8Array | Uint16Array | Uint32Array;
13
+ protected backward: Uint8Array | Uint16Array | Uint32Array;
14
+ protected K: K[];
15
+ protected V: V[];
16
+ constructor(capacity?: number, Keys?: ArrayConstructor, Values?: ArrayConstructor);
17
+ /**
18
+ * This is just an alias of {@link Cache.set}.
19
+ * @param {K} key The key to add (note value will be `undefined`).
20
+ */
21
+ add(key: K): void;
22
+ /**
23
+ * Method used to check whether the key exists in the cache.
24
+ *
25
+ * @param {any} key - Key.
26
+ * @return {boolean}
27
+ */
28
+ contains(key: any): boolean;
29
+ /**
30
+ * Method used to clear the structure.
31
+ */
32
+ clear(): void;
33
+ get size(): number;
34
+ /**
35
+ * Method used to splay a value on top.
36
+ *
37
+ * @param {number} pointer - Pointer of the value to splay on top.
38
+ * @return {Cache}
39
+ */
40
+ protected splayOnTop(pointer: any): this;
41
+ /**
42
+ * Method used to get the value attached to the given key. Will move the
43
+ * related key to the front of the underlying linked list.
44
+ *
45
+ * @param {any} key - Key.
46
+ * @return {any}
47
+ */
48
+ get(key: any): V | undefined;
49
+ /**
50
+ * Method used to set the value for the given key in the cache.
51
+ *
52
+ * @param {any} key - Key.
53
+ * @param {any} value - Value.
54
+ * @return {undefined}
55
+ */
56
+ set(key: any, value: any): void;
57
+ /**
58
+ * Method used to get the value attached to the given key. Does not modify
59
+ * the ordering of the underlying linked list.
60
+ *
61
+ * @param {any} key - Key.
62
+ * @return {any}
63
+ */
64
+ peek(key: any): V | undefined;
65
+ /**
66
+ * Method used to iterate over the cache's entries using a callback.
67
+ *
68
+ * @param {Iteratee<V>} iteratee - Function to call for each item.
69
+ */
70
+ forEach(iteratee: Iteratee<V>): void;
71
+ /**
72
+ * Method used to create an iterator over the cache's keys from most
73
+ * recently used to least recently used.
74
+ *
75
+ * @return {Iterator<K>}
76
+ */
77
+ keys(): Iterator<any, any, undefined> & Iterable<any>;
78
+ /**
79
+ * Method used to create an iterator over the cache's values from most
80
+ * recently used to least recently used.
81
+ *
82
+ * @return {Iterator<V>}
83
+ */
84
+ values(): Iterator<any, any, undefined> & Iterable<any>;
85
+ /**
86
+ * Method used to create an iterator over the cache's entries from most
87
+ * recently used to least recently used.
88
+ *
89
+ * @return {Iterator<[K, V]>}
90
+ */
91
+ entries(): Iterator<[K, V], any, undefined> & Iterable<[K, V]>;
92
+ /**
93
+ * NotImplemented -- throws an exception.
94
+ * @param key
95
+ * @returns
96
+ */
97
+ remove(key: any): void;
98
+ [Symbol.iterator](): Iterator<[K, V], any, undefined> & Iterable<[K, V]>;
99
+ /**
100
+ * Convenience known methods.
101
+ */
102
+ inspect(): Map<any, any>;
103
+ }
104
+ export declare class LRUCache<K, V> extends Cache<K, V> {
105
+ deletedSize: number;
106
+ deleted: Uint8Array | Uint16Array | Uint32Array;
107
+ constructor(capacity?: number, Keys?: ArrayConstructor, Values?: ArrayConstructor);
108
+ clear(): void;
109
+ /**
110
+ * Method used to set the value for the given key in the cache.
111
+ *
112
+ * @param {any} key - Key.
113
+ * @param {any} value - Value.
114
+ * @return {undefined}
115
+ */
116
+ set(key: any, value: any): void;
117
+ /**
118
+ * Method used to delete the entry for the given key in the cache.
119
+ *
120
+ * @param {any} key - Key.
121
+ * @return {boolean} - true if the item was present
122
+ */
123
+ delete(key: any): boolean;
124
+ /**
125
+ * Method used to remove and return the value for the given key in the cache.
126
+ *
127
+ * @param {any} key - Key.
128
+ * @return {any} The value, if present; the missing indicator if absent
129
+ */
130
+ remove(key: any): V | undefined;
131
+ }