atomic-fns 1.2.0 → 2.0.0
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/README.md +397 -91
- package/dist/array/index.d.ts +9 -0
- package/dist/array/index.js +9 -0
- package/dist/array/list.d.ts +99 -0
- package/dist/array/list.js +133 -0
- package/dist/array/sets.d.ts +130 -0
- package/dist/array/sets.js +213 -0
- package/dist/async/index.d.ts +6 -0
- package/dist/async/index.js +6 -0
- package/dist/async/retry.d.ts +38 -0
- package/dist/async/retry.js +56 -0
- package/dist/async/sleep.d.ts +42 -0
- package/dist/async/sleep.js +76 -0
- package/dist/collections/bisect.d.ts +103 -0
- package/dist/collections/bisect.js +157 -0
- package/dist/collections/deque.d.ts +67 -97
- package/dist/collections/deque.js +197 -256
- package/dist/collections/entries.d.ts +89 -0
- package/dist/collections/entries.js +131 -0
- package/dist/collections/frozen-set.d.ts +42 -0
- package/dist/collections/frozen-set.js +64 -0
- package/dist/collections/group.d.ts +141 -0
- package/dist/collections/group.js +241 -0
- package/dist/collections/heap.d.ts +64 -0
- package/dist/collections/heap.js +205 -0
- package/dist/collections/index.d.ts +15 -623
- package/dist/collections/index.js +14 -844
- package/dist/collections/internal/arrayKeys.d.ts +19 -0
- package/dist/collections/internal/arrayKeys.js +83 -0
- package/dist/collections/internal/invariants.d.ts +4 -0
- package/dist/collections/internal/invariants.js +12 -0
- package/dist/collections/internal/iterable.d.ts +25 -0
- package/dist/collections/internal/iterable.js +62 -0
- package/dist/collections/iterate.d.ts +162 -0
- package/dist/collections/iterate.js +188 -0
- package/dist/collections/lru-cache.d.ts +58 -0
- package/dist/collections/lru-cache.js +164 -0
- package/dist/collections/order.d.ts +19 -0
- package/dist/collections/order.js +38 -0
- package/dist/collections/shape.d.ts +32 -0
- package/dist/collections/shape.js +86 -0
- package/dist/collections/sorted-map.d.ts +171 -0
- package/dist/collections/sorted-map.js +845 -0
- package/dist/collections/sorted-set.d.ts +103 -0
- package/dist/collections/sorted-set.js +147 -0
- package/dist/collections/splay-tree.d.ts +82 -0
- package/dist/collections/splay-tree.js +354 -0
- package/dist/collections/trie.d.ts +58 -0
- package/dist/collections/trie.js +196 -0
- package/dist/decimal/currency.d.ts +211 -0
- package/dist/decimal/currency.js +321 -0
- package/dist/decimal/decimal.d.ts +299 -0
- package/dist/decimal/decimal.js +660 -0
- package/dist/decimal/index.d.ts +11 -148
- package/dist/decimal/index.js +10 -410
- package/dist/decimal/internal/bits.d.ts +2 -0
- package/dist/decimal/internal/bits.js +7 -0
- package/dist/decimal/internal/echo.d.ts +4 -0
- package/dist/decimal/internal/echo.js +6 -0
- package/dist/decimal/internal/isqrt.d.ts +9 -0
- package/dist/decimal/internal/isqrt.js +34 -0
- package/dist/decimal/internal/locale.d.ts +7 -0
- package/dist/decimal/internal/locale.js +24 -0
- package/dist/decimal/internal/minor-units.d.ts +2 -0
- package/dist/decimal/internal/minor-units.js +33 -0
- package/dist/decimal/internal/pow10.d.ts +24 -0
- package/dist/decimal/internal/pow10.js +96 -0
- package/dist/encoding/base64.d.ts +39 -0
- package/dist/encoding/base64.js +53 -0
- package/dist/encoding/hex.d.ts +23 -0
- package/dist/encoding/hex.js +34 -0
- package/dist/encoding/index.d.ts +6 -0
- package/dist/encoding/index.js +6 -0
- package/dist/function/debounce.d.ts +68 -0
- package/dist/function/debounce.js +203 -0
- package/dist/function/index.d.ts +6 -0
- package/dist/function/index.js +6 -0
- package/dist/function/shared.d.ts +2 -0
- package/dist/function/shared.js +5 -0
- package/dist/function/wrap.d.ts +130 -0
- package/dist/function/wrap.js +113 -0
- package/dist/hash/digest.d.ts +18 -0
- package/dist/hash/digest.js +26 -0
- package/dist/hash/hash.d.ts +22 -0
- package/dist/hash/hash.js +252 -0
- package/dist/hash/hashString.d.ts +13 -0
- package/dist/hash/hashString.js +53 -0
- package/dist/hash/index.d.ts +8 -0
- package/dist/hash/index.js +8 -0
- package/dist/hash/stableStringify.d.ts +19 -0
- package/dist/hash/stableStringify.js +102 -0
- package/dist/id/index.d.ts +7 -0
- package/dist/id/index.js +7 -0
- package/dist/id/nanoid.d.ts +16 -0
- package/dist/id/nanoid.js +65 -0
- package/dist/id/uniqueId.d.ts +14 -0
- package/dist/id/uniqueId.js +20 -0
- package/dist/id/uuid.d.ts +25 -0
- package/dist/id/uuid.js +64 -0
- package/dist/index.d.ts +15 -8
- package/dist/index.js +15 -8
- package/dist/internal/bytes.d.ts +11 -0
- package/dist/internal/bytes.js +33 -0
- package/dist/internal/codec.d.ts +11 -0
- package/dist/internal/codec.js +84 -0
- package/dist/internal/compare.d.ts +10 -0
- package/dist/internal/compare.js +26 -0
- package/dist/internal/crypto.d.ts +5 -0
- package/dist/internal/crypto.js +11 -0
- package/dist/internal/keys.d.ts +12 -0
- package/dist/internal/keys.js +23 -0
- package/dist/internal/kind.d.ts +44 -0
- package/dist/internal/kind.js +139 -0
- package/dist/internal/sparse.d.ts +51 -0
- package/dist/internal/sparse.js +104 -0
- package/dist/internal/text.d.ts +7 -0
- package/dist/internal/text.js +15 -0
- package/dist/internal/timer.d.ts +10 -0
- package/dist/internal/timer.js +17 -0
- package/dist/internal/truncate.d.ts +10 -0
- package/dist/internal/truncate.js +22 -0
- package/dist/internal/walk.d.ts +47 -0
- package/dist/internal/walk.js +100 -0
- package/dist/is/index.d.ts +8 -0
- package/dist/is/index.js +8 -0
- package/dist/is/isArrayLike.d.ts +18 -0
- package/dist/is/isArrayLike.js +25 -0
- package/dist/is/isError.d.ts +13 -0
- package/dist/is/isError.js +18 -0
- package/dist/is/isNil.d.ts +12 -0
- package/dist/is/isNil.js +14 -0
- package/dist/is/isPlainObject.d.ts +15 -0
- package/dist/is/isPlainObject.js +34 -0
- package/dist/number/index.d.ts +7 -0
- package/dist/number/index.js +7 -0
- package/dist/number/round.d.ts +86 -0
- package/dist/number/round.js +130 -0
- package/dist/number/stats.d.ts +52 -0
- package/dist/number/stats.js +115 -0
- package/dist/object/cloneDeep.d.ts +28 -0
- package/dist/object/cloneDeep.js +200 -0
- package/dist/object/index.d.ts +14 -0
- package/dist/object/index.js +13 -0
- package/dist/object/isEqual.d.ts +64 -0
- package/dist/object/isEqual.js +289 -0
- package/dist/object/matches.d.ts +40 -0
- package/dist/object/matches.js +284 -0
- package/dist/object/merge.d.ts +66 -0
- package/dist/object/merge.js +272 -0
- package/dist/object/parsePath.d.ts +22 -0
- package/dist/object/parsePath.js +111 -0
- package/dist/object/path.d.ts +85 -0
- package/dist/object/path.js +208 -0
- package/dist/object/pick.d.ts +91 -0
- package/dist/object/pick.js +180 -0
- package/dist/object/reshape.d.ts +73 -0
- package/dist/object/reshape.js +144 -0
- package/dist/random/index.d.ts +96 -32
- package/dist/random/index.js +215 -53
- package/dist/string/case.d.ts +71 -0
- package/dist/string/case.js +152 -0
- package/dist/string/fold.d.ts +35 -0
- package/dist/string/fold.js +100 -0
- package/dist/string/index.d.ts +6 -187
- package/dist/string/index.js +6 -224
- package/dist/string/truncate.d.ts +25 -0
- package/dist/string/truncate.js +44 -0
- package/dist/string/words.d.ts +27 -0
- package/dist/string/words.js +241 -0
- package/package.json +99 -51
- package/dist/collections/BTree.d.ts +0 -230
- package/dist/collections/BTree.js +0 -1143
- package/dist/collections/Heap.d.ts +0 -69
- package/dist/collections/Heap.js +0 -181
- package/dist/collections/LRUCache.d.ts +0 -83
- package/dist/collections/LRUCache.js +0 -139
- package/dist/collections/SortedMap.d.ts +0 -92
- package/dist/collections/SortedMap.js +0 -131
- package/dist/collections/SortedSet.d.ts +0 -69
- package/dist/collections/SortedSet.js +0 -99
- package/dist/collections/SortedTree.d.ts +0 -103
- package/dist/collections/SortedTree.js +0 -142
- package/dist/collections/SplayTree.d.ts +0 -107
- package/dist/collections/SplayTree.js +0 -351
- package/dist/collections/Trie.d.ts +0 -42
- package/dist/collections/Trie.js +0 -104
- package/dist/collections/abc.d.ts +0 -152
- package/dist/collections/abc.js +0 -39
- package/dist/collections/frozenset.d.ts +0 -19
- package/dist/collections/frozenset.js +0 -33
- package/dist/functools/index.d.ts +0 -106
- package/dist/functools/index.js +0 -203
- package/dist/globals/index.d.ts +0 -268
- package/dist/globals/index.js +0 -447
- package/dist/i18n/currency.d.ts +0 -27
- package/dist/i18n/currency.js +0 -100
- package/dist/i18n/date/index.d.ts +0 -210
- package/dist/i18n/date/index.js +0 -552
- package/dist/i18n/date/utils.d.ts +0 -64
- package/dist/i18n/date/utils.js +0 -192
- package/dist/i18n/duration.d.ts +0 -113
- package/dist/i18n/duration.js +0 -398
- package/dist/i18n/format.d.ts +0 -34
- package/dist/i18n/format.js +0 -378
- package/dist/i18n/index.d.ts +0 -9
- package/dist/i18n/index.js +0 -8
- package/dist/itertools/index.d.ts +0 -251
- package/dist/itertools/index.js +0 -434
- package/dist/math/index.d.ts +0 -79
- package/dist/math/index.js +0 -81
- package/dist/mod.d.ts +0 -1
- package/dist/mod.js +0 -1
- package/dist/operators/index.d.ts +0 -148
- package/dist/operators/index.js +0 -325
- package/dist/types.d.ts +0 -19
- package/dist/types.js +0 -1
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import { defaultCompare, sameValueZero } from '../internal/compare.js';
|
|
2
|
+
import { arrayFromIterable } from './internal/iterable.js';
|
|
3
|
+
/**
|
|
4
|
+
* Hole-based sift-up: compare while finding the final index, then shift and place once, so a
|
|
5
|
+
* throwing comparator writes nothing.
|
|
6
|
+
* @returns The index where `item` landed.
|
|
7
|
+
*/
|
|
8
|
+
function siftUp(heap, start, item, compareFn) {
|
|
9
|
+
let pos = start;
|
|
10
|
+
while (pos > 0) {
|
|
11
|
+
const parent = (pos - 1) >> 1;
|
|
12
|
+
if (compareFn(heap[parent], item) <= 0)
|
|
13
|
+
break;
|
|
14
|
+
pos = parent;
|
|
15
|
+
}
|
|
16
|
+
let hole = start;
|
|
17
|
+
while (hole > pos) {
|
|
18
|
+
const parent = (hole - 1) >> 1;
|
|
19
|
+
heap[hole] = heap[parent];
|
|
20
|
+
hole = parent;
|
|
21
|
+
}
|
|
22
|
+
heap[hole] = item;
|
|
23
|
+
return pos;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Undo the last `siftUp(heap, start, ...)`, whose item landed at `pos`: move each ancestor on the
|
|
27
|
+
* path back down to its old slot and drop slot `start`. Makes no comparisons.
|
|
28
|
+
*/
|
|
29
|
+
function undoSiftUp(heap, start, pos) {
|
|
30
|
+
const path = [];
|
|
31
|
+
for (let index = start; index > pos; index = (index - 1) >> 1)
|
|
32
|
+
path.push(index);
|
|
33
|
+
path.push(pos);
|
|
34
|
+
for (let k = path.length - 1; k > 0; --k)
|
|
35
|
+
heap[path[k]] = heap[path[k - 1]];
|
|
36
|
+
heap.length = start;
|
|
37
|
+
}
|
|
38
|
+
/** Hole-based sift-down over `heap[0..end)`, placing `item` at `start`. */
|
|
39
|
+
function siftDown(heap, start, end, item, compareFn) {
|
|
40
|
+
const path = [];
|
|
41
|
+
let pos = start;
|
|
42
|
+
const half = end >> 1;
|
|
43
|
+
while (pos < half) {
|
|
44
|
+
let child = (pos << 1) | 1;
|
|
45
|
+
const right = child + 1;
|
|
46
|
+
if (right < end && compareFn(heap[child], heap[right]) > 0)
|
|
47
|
+
child = right;
|
|
48
|
+
if (compareFn(heap[child], item) >= 0)
|
|
49
|
+
break;
|
|
50
|
+
path.push(child);
|
|
51
|
+
pos = child;
|
|
52
|
+
}
|
|
53
|
+
let hole = start;
|
|
54
|
+
for (const child of path) {
|
|
55
|
+
heap[hole] = heap[child];
|
|
56
|
+
hole = child;
|
|
57
|
+
}
|
|
58
|
+
heap[hole] = item;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* A binary min-heap: `peek` and `pop` give the smallest item by the comparator, so a reversed
|
|
62
|
+
* comparator makes a max-heap. Iteration is in heap order, not sorted order. `has` and `delete`
|
|
63
|
+
* use SameValueZero, as `Set` does. A comparator that throws leaves the heap unchanged, and a `push`
|
|
64
|
+
* of several items then adds none of them. With the default comparator, items are numbers, bigints
|
|
65
|
+
* or strings, checked as they enter: `NaN` throws `RangeError`, and `null`, `undefined` and other
|
|
66
|
+
* values throw `TypeError`; pass a comparator to order them.
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* ```ts
|
|
70
|
+
* const heap = new Heap([5, 1, 3])
|
|
71
|
+
* heap.push(2)
|
|
72
|
+
* heap.pop() // 1
|
|
73
|
+
* heap.peek() // 2
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
export class Heap {
|
|
77
|
+
#heap;
|
|
78
|
+
#compare;
|
|
79
|
+
#checksItems;
|
|
80
|
+
/**
|
|
81
|
+
* Build a heap from `items` in linear time.
|
|
82
|
+
* @param items Initial items, copied. `null` means none.
|
|
83
|
+
* @param compareFn Comparator; the smallest item comes out first. Defaults to the primitive
|
|
84
|
+
* order (numbers, bigints and strings).
|
|
85
|
+
* @throws {TypeError} when `items` is not iterable, `compareFn` is not a function, or the default
|
|
86
|
+
* comparator cannot order an item.
|
|
87
|
+
* @throws {RangeError} when `items` is a huge sparse array, or holds `NaN` under the default
|
|
88
|
+
* comparator.
|
|
89
|
+
*/
|
|
90
|
+
constructor(items, compareFn) {
|
|
91
|
+
if (compareFn != null && typeof compareFn !== 'function') {
|
|
92
|
+
throw new TypeError('comparator must be a function');
|
|
93
|
+
}
|
|
94
|
+
this.#compare = compareFn ?? defaultCompare;
|
|
95
|
+
this.#checksItems = compareFn == null;
|
|
96
|
+
const heap = arrayFromIterable(items ?? []);
|
|
97
|
+
if (this.#checksItems)
|
|
98
|
+
for (const item of heap)
|
|
99
|
+
defaultCompare(item, item);
|
|
100
|
+
for (let parent = (heap.length - 1) >> 1; parent >= 0; --parent) {
|
|
101
|
+
siftDown(heap, parent, heap.length, heap[parent], this.#compare);
|
|
102
|
+
}
|
|
103
|
+
this.#heap = heap;
|
|
104
|
+
}
|
|
105
|
+
/** The number of items. */
|
|
106
|
+
get size() {
|
|
107
|
+
return this.#heap.length;
|
|
108
|
+
}
|
|
109
|
+
/** `'Heap'`, for `Object.prototype.toString`. */
|
|
110
|
+
get [Symbol.toStringTag]() {
|
|
111
|
+
return 'Heap';
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Add one or more items. When the comparator throws, none of them is added.
|
|
115
|
+
* @returns The new size.
|
|
116
|
+
* @throws {TypeError} when the default comparator cannot order an item.
|
|
117
|
+
* @throws {RangeError} when an item is `NaN` under the default comparator.
|
|
118
|
+
*/
|
|
119
|
+
push(...items) {
|
|
120
|
+
const heap = this.#heap;
|
|
121
|
+
const firstStart = heap.length;
|
|
122
|
+
const landed = [];
|
|
123
|
+
try {
|
|
124
|
+
for (const item of items) {
|
|
125
|
+
if (this.#checksItems)
|
|
126
|
+
defaultCompare(item, item);
|
|
127
|
+
landed.push(siftUp(heap, heap.length, item, this.#compare));
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
catch (error) {
|
|
131
|
+
for (let k = landed.length - 1; k >= 0; --k)
|
|
132
|
+
undoSiftUp(heap, firstStart + k, landed[k]);
|
|
133
|
+
throw error;
|
|
134
|
+
}
|
|
135
|
+
return heap.length;
|
|
136
|
+
}
|
|
137
|
+
/** Remove and return the smallest item, or `undefined` when empty. */
|
|
138
|
+
pop() {
|
|
139
|
+
const heap = this.#heap;
|
|
140
|
+
if (heap.length === 0)
|
|
141
|
+
return undefined;
|
|
142
|
+
const top = heap[0];
|
|
143
|
+
siftDown(heap, 0, heap.length - 1, heap[heap.length - 1], this.#compare);
|
|
144
|
+
heap.pop();
|
|
145
|
+
return top;
|
|
146
|
+
}
|
|
147
|
+
/** The smallest item without removing it, or `undefined` when empty. */
|
|
148
|
+
peek() {
|
|
149
|
+
return this.#heap[0];
|
|
150
|
+
}
|
|
151
|
+
/** Whether `item` is in the heap (SameValueZero). */
|
|
152
|
+
has(item) {
|
|
153
|
+
return this.#heap.includes(item);
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Remove one occurrence of `item` (SameValueZero).
|
|
157
|
+
* @returns Whether it was present.
|
|
158
|
+
*/
|
|
159
|
+
delete(item) {
|
|
160
|
+
const heap = this.#heap;
|
|
161
|
+
const index = heap.findIndex((x) => sameValueZero(x, item));
|
|
162
|
+
if (index < 0)
|
|
163
|
+
return false;
|
|
164
|
+
if (index === 0) {
|
|
165
|
+
this.pop();
|
|
166
|
+
return true;
|
|
167
|
+
}
|
|
168
|
+
const end = heap.length - 1;
|
|
169
|
+
if (index < end) {
|
|
170
|
+
const last = heap[end];
|
|
171
|
+
const parent = (index - 1) >> 1;
|
|
172
|
+
if (this.#compare(heap[parent], last) > 0)
|
|
173
|
+
siftUp(heap, index, last, this.#compare);
|
|
174
|
+
else
|
|
175
|
+
siftDown(heap, index, end, last, this.#compare);
|
|
176
|
+
}
|
|
177
|
+
heap.pop();
|
|
178
|
+
return true;
|
|
179
|
+
}
|
|
180
|
+
/** Remove every item. */
|
|
181
|
+
clear() {
|
|
182
|
+
this.#heap.length = 0;
|
|
183
|
+
}
|
|
184
|
+
/** The items, in heap order. */
|
|
185
|
+
values() {
|
|
186
|
+
return this.#heap.values();
|
|
187
|
+
}
|
|
188
|
+
/** The items, in heap order, as `Set.prototype.keys` gives them. */
|
|
189
|
+
keys() {
|
|
190
|
+
return this.values();
|
|
191
|
+
}
|
|
192
|
+
/** `[item, item]` pairs, in heap order, as `Set.prototype.entries` gives them. */
|
|
193
|
+
entries() {
|
|
194
|
+
return this.values().map((v) => [v, v]);
|
|
195
|
+
}
|
|
196
|
+
/** Call `callback(item, item, heap)` for each item, in heap order. */
|
|
197
|
+
forEach(callback, thisArg) {
|
|
198
|
+
for (const v of this.values())
|
|
199
|
+
Reflect.apply(callback, thisArg, [v, v, this]);
|
|
200
|
+
}
|
|
201
|
+
/** The items, in heap order. */
|
|
202
|
+
[Symbol.iterator]() {
|
|
203
|
+
return this.values();
|
|
204
|
+
}
|
|
205
|
+
}
|