atomic-fns 1.1.7 → 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 +405 -60
- 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 -249
- 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 -642
- package/dist/collections/index.js +14 -716
- 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 -145
- package/dist/decimal/index.js +10 -374
- 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 -50
- 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 -226
- 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 +101 -58
- package/dist/collections/BTree.d.ts +0 -229
- package/dist/collections/BTree.js +0 -1165
- package/dist/collections/Heap.d.ts +0 -69
- package/dist/collections/Heap.js +0 -181
- package/dist/collections/LRUCache.d.ts +0 -80
- package/dist/collections/LRUCache.js +0 -126
- 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 -141
- package/dist/collections/SplayTree.d.ts +0 -105
- package/dist/collections/SplayTree.js +0 -338
- 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 -30
- package/dist/functools/index.d.ts +0 -130
- package/dist/functools/index.js +0 -235
- package/dist/globals/index.d.ts +0 -285
- package/dist/globals/index.js +0 -463
- package/dist/i18n/currency.d.ts +0 -24
- package/dist/i18n/currency.js +0 -85
- package/dist/i18n/date/index.d.ts +0 -203
- package/dist/i18n/date/index.js +0 -531
- package/dist/i18n/date/utils.d.ts +0 -65
- package/dist/i18n/date/utils.js +0 -290
- package/dist/i18n/duration.d.ts +0 -112
- package/dist/i18n/duration.js +0 -392
- package/dist/i18n/format.d.ts +0 -31
- package/dist/i18n/format.js +0 -506
- package/dist/i18n/index.d.ts +0 -8
- package/dist/i18n/index.js +0 -8
- package/dist/itertools/index.d.ts +0 -255
- package/dist/itertools/index.js +0 -437
- package/dist/math/index.d.ts +0 -96
- package/dist/math/index.js +0 -98
- package/dist/mod.d.ts +0 -1
- package/dist/mod.js +0 -1
- package/dist/operators/index.d.ts +0 -152
- package/dist/operators/index.js +0 -305
- package/dist/types.d.ts +0 -19
- package/dist/types.js +0 -1
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { timerWait } from '../internal/timer.js';
|
|
2
|
+
/**
|
|
3
|
+
* Resolves after `ms` milliseconds. An aborted `signal` rejects with its `reason`, at once when it
|
|
4
|
+
* is already aborted. Negative waits clamp to 0; NaN, infinite or past-2147483647 waits reject with
|
|
5
|
+
* `RangeError`.
|
|
6
|
+
* @param ms Milliseconds to wait.
|
|
7
|
+
* @param options `signal` to cancel the wait.
|
|
8
|
+
* @returns A promise that resolves to `undefined`.
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* await sleep(100)
|
|
12
|
+
* const controller = new AbortController()
|
|
13
|
+
* const nap = sleep(1000, { signal: controller.signal })
|
|
14
|
+
* controller.abort()
|
|
15
|
+
* await nap.catch((reason) => console.log(reason)) // the abort reason
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
export function sleep(ms, options) {
|
|
19
|
+
return new Promise((resolve, reject) => {
|
|
20
|
+
const delay = timerWait(ms, 'ms');
|
|
21
|
+
const signal = options?.signal;
|
|
22
|
+
if (signal?.aborted) {
|
|
23
|
+
reject(signal.reason);
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
const onAbort = () => {
|
|
27
|
+
clearTimeout(timer);
|
|
28
|
+
reject(signal?.reason);
|
|
29
|
+
};
|
|
30
|
+
signal?.addEventListener('abort', onAbort, { once: true });
|
|
31
|
+
const timer = setTimeout(() => {
|
|
32
|
+
signal?.removeEventListener('abort', onAbort);
|
|
33
|
+
resolve();
|
|
34
|
+
}, delay);
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Settles as `promise` does, or rejects with a `DOMException` named `TimeoutError` when `ms`
|
|
39
|
+
* milliseconds pass first, as `AbortSignal.timeout` does. An aborted `signal` rejects with its
|
|
40
|
+
* `reason`. The timer and the abort listener are removed once the result is known; `promise`
|
|
41
|
+
* itself keeps running, so pass the same `signal` to the work to stop it too.
|
|
42
|
+
* @param promise The promise to wait for.
|
|
43
|
+
* @param ms Milliseconds before it times out; checked as in {@link sleep}.
|
|
44
|
+
* @param options `signal` to cancel the wait.
|
|
45
|
+
* @returns A promise that settles with `promise`, or rejects on timeout or abort.
|
|
46
|
+
* @example
|
|
47
|
+
* ```ts
|
|
48
|
+
* const response = await timeout(fetch('/api'), 5000)
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
export function timeout(promise, ms, options) {
|
|
52
|
+
return new Promise((resolve, reject) => {
|
|
53
|
+
const signal = options?.signal;
|
|
54
|
+
let timer;
|
|
55
|
+
const settle = () => {
|
|
56
|
+
clearTimeout(timer);
|
|
57
|
+
signal?.removeEventListener('abort', onAbort);
|
|
58
|
+
};
|
|
59
|
+
const onAbort = () => {
|
|
60
|
+
settle();
|
|
61
|
+
reject(signal?.reason);
|
|
62
|
+
};
|
|
63
|
+
// Subscribe before any early exit so a later rejection of `promise` is always handled.
|
|
64
|
+
Promise.resolve(promise).finally(settle).then(resolve, reject);
|
|
65
|
+
const delay = timerWait(ms, 'ms');
|
|
66
|
+
if (signal?.aborted) {
|
|
67
|
+
reject(signal.reason);
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
signal?.addEventListener('abort', onAbort, { once: true });
|
|
71
|
+
timer = setTimeout(() => {
|
|
72
|
+
settle();
|
|
73
|
+
reject(new DOMException('The operation timed out.', 'TimeoutError'));
|
|
74
|
+
}, delay);
|
|
75
|
+
});
|
|
76
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The leftmost index at which `value` can be inserted into the sorted `array` and keep it sorted:
|
|
3
|
+
* before any elements equal to it, as Python's `bisect_left`.
|
|
4
|
+
*
|
|
5
|
+
* @param array A sorted array or array-like.
|
|
6
|
+
* @param value The value to locate.
|
|
7
|
+
* @param compareFn Comparator the array is sorted by. Defaults to the primitive order (numbers,
|
|
8
|
+
* bigints and strings).
|
|
9
|
+
* @param lo First index to search, a non-negative integer. Defaults to 0.
|
|
10
|
+
* @param hi End of the search (exclusive): an integer or ±Infinity, clamped to the length.
|
|
11
|
+
* Defaults to the length.
|
|
12
|
+
* @throws {TypeError} when `lo`, `hi` or the length of `array` is not an integer (`hi` may also be
|
|
13
|
+
* ±Infinity).
|
|
14
|
+
* @throws {RangeError} when `lo` is negative or the length of `array` is not a non-negative safe
|
|
15
|
+
* integer.
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* bisectLeft([1, 2, 2, 3], 2) // 1
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export declare function bisectLeft<T>(array: ArrayLike<T>, value: T, compareFn?: (a: T, b: T) => number, lo?: number, hi?: number): number;
|
|
22
|
+
/**
|
|
23
|
+
* The rightmost index at which `value` can be inserted into the sorted `array` and keep it sorted:
|
|
24
|
+
* after any elements equal to it, as Python's `bisect_right`. Also exported as `bisect`, Python's
|
|
25
|
+
* short name.
|
|
26
|
+
*
|
|
27
|
+
* @param array A sorted array or array-like.
|
|
28
|
+
* @param value The value to locate.
|
|
29
|
+
* @param compareFn Comparator the array is sorted by. Defaults to the primitive order (numbers,
|
|
30
|
+
* bigints and strings).
|
|
31
|
+
* @param lo First index to search, a non-negative integer. Defaults to 0.
|
|
32
|
+
* @param hi End of the search (exclusive): an integer or ±Infinity, clamped to the length.
|
|
33
|
+
* Defaults to the length.
|
|
34
|
+
* @throws {TypeError} when `lo`, `hi` or the length of `array` is not an integer (`hi` may also be
|
|
35
|
+
* ±Infinity).
|
|
36
|
+
* @throws {RangeError} when `lo` is negative or the length of `array` is not a non-negative safe
|
|
37
|
+
* integer.
|
|
38
|
+
* @example
|
|
39
|
+
* ```ts
|
|
40
|
+
* bisectRight([1, 2, 2, 3], 2) // 3
|
|
41
|
+
* bisect(['a', 'c'], 'b') // 1
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
export declare function bisectRight<T>(array: ArrayLike<T>, value: T, compareFn?: (a: T, b: T) => number, lo?: number, hi?: number): number;
|
|
45
|
+
export { bisectRight as bisect };
|
|
46
|
+
/**
|
|
47
|
+
* Insert `value` into the sorted `array` after any equal elements, keeping it sorted, and return
|
|
48
|
+
* the array.
|
|
49
|
+
*
|
|
50
|
+
* @param array A sorted array; changed in place.
|
|
51
|
+
* @param value The value to insert.
|
|
52
|
+
* @param compareFn Comparator the array is sorted by. Defaults to the primitive order.
|
|
53
|
+
* @param lo First index to search, a non-negative integer. Defaults to 0.
|
|
54
|
+
* @param hi End of the search (exclusive): an integer or ±Infinity, clamped to the length.
|
|
55
|
+
* Defaults to the length.
|
|
56
|
+
* @throws {TypeError} when `lo` or `hi` is not an integer (`hi` may also be ±Infinity).
|
|
57
|
+
* @throws {RangeError} when `lo` is negative or `array` is a huge sparse array.
|
|
58
|
+
* @example
|
|
59
|
+
* ```ts
|
|
60
|
+
* insort([1, 3], 2) // [1, 2, 3]
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
export declare function insort<T>(array: T[], value: T, compareFn?: (a: T, b: T) => number, lo?: number, hi?: number): T[];
|
|
64
|
+
/**
|
|
65
|
+
* Insert `value` into the sorted `array` before any equal elements, keeping it sorted, and return
|
|
66
|
+
* the array.
|
|
67
|
+
*
|
|
68
|
+
* @param array A sorted array; changed in place.
|
|
69
|
+
* @param value The value to insert.
|
|
70
|
+
* @param compareFn Comparator the array is sorted by. Defaults to the primitive order.
|
|
71
|
+
* @param lo First index to search, a non-negative integer. Defaults to 0.
|
|
72
|
+
* @param hi End of the search (exclusive): an integer or ±Infinity, clamped to the length.
|
|
73
|
+
* Defaults to the length.
|
|
74
|
+
* @throws {TypeError} when `lo` or `hi` is not an integer (`hi` may also be ±Infinity).
|
|
75
|
+
* @throws {RangeError} when `lo` is negative or `array` is a huge sparse array.
|
|
76
|
+
* @example
|
|
77
|
+
* ```ts
|
|
78
|
+
* insortLeft([1, 3], 2) // [1, 2, 3]
|
|
79
|
+
* ```
|
|
80
|
+
*/
|
|
81
|
+
export declare function insortLeft<T>(array: T[], value: T, compareFn?: (a: T, b: T) => number, lo?: number, hi?: number): T[];
|
|
82
|
+
/**
|
|
83
|
+
* The index of `value` in the sorted `array`, or `-(insertion index) - 1` when it is absent, so a
|
|
84
|
+
* negative result `r` means `value` belongs at `-r - 1`.
|
|
85
|
+
*
|
|
86
|
+
* @param array A sorted array or array-like.
|
|
87
|
+
* @param value The value to find.
|
|
88
|
+
* @param compareFn Comparator the array is sorted by; 0 means found. Defaults to the primitive
|
|
89
|
+
* order.
|
|
90
|
+
* @param lo First index to search, a non-negative integer. Defaults to 0.
|
|
91
|
+
* @param hi End of the search (exclusive): an integer or ±Infinity, clamped to the length.
|
|
92
|
+
* Defaults to the length.
|
|
93
|
+
* @throws {TypeError} when `lo`, `hi` or the length of `array` is not an integer (`hi` may also be
|
|
94
|
+
* ±Infinity).
|
|
95
|
+
* @throws {RangeError} when `lo` is negative or the length of `array` is not a non-negative safe
|
|
96
|
+
* integer.
|
|
97
|
+
* @example
|
|
98
|
+
* ```ts
|
|
99
|
+
* binarySearch([1, 3, 5], 3) // 1
|
|
100
|
+
* binarySearch([1, 3, 5], 4) // -3
|
|
101
|
+
* ```
|
|
102
|
+
*/
|
|
103
|
+
export declare function binarySearch<T>(array: ArrayLike<T>, value: T, compareFn?: (a: T, b: T) => number, lo?: number, hi?: number): number;
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import { defaultCompare } from '../internal/compare.js';
|
|
2
|
+
import { assertNotHugeSparse } from './internal/arrayKeys.js';
|
|
3
|
+
/**
|
|
4
|
+
* Check the search bounds and return the exclusive end of the search: `hi` clamped to the length
|
|
5
|
+
* of `array`, or the length when `hi` is omitted. `lo` is checked first, as Python does.
|
|
6
|
+
*/
|
|
7
|
+
function searchBounds(array, lo, hi) {
|
|
8
|
+
if (!Number.isInteger(lo))
|
|
9
|
+
throw new TypeError('lo must be an integer');
|
|
10
|
+
if (lo < 0)
|
|
11
|
+
throw new RangeError('lo must be non-negative');
|
|
12
|
+
const length = array.length;
|
|
13
|
+
if (!Number.isInteger(length))
|
|
14
|
+
throw new TypeError('array length must be an integer');
|
|
15
|
+
if (length < 0 || length > Number.MAX_SAFE_INTEGER) {
|
|
16
|
+
throw new RangeError('array length must be a non-negative safe integer');
|
|
17
|
+
}
|
|
18
|
+
if (hi === undefined)
|
|
19
|
+
return length;
|
|
20
|
+
if (!Number.isInteger(hi) && hi !== Infinity && hi !== -Infinity) {
|
|
21
|
+
throw new TypeError('hi must be an integer');
|
|
22
|
+
}
|
|
23
|
+
return Math.min(hi, length);
|
|
24
|
+
}
|
|
25
|
+
/** Binary search over `[lo, end)`, with bounds already checked by {@link searchBounds}. */
|
|
26
|
+
function search(array, value, compareFn, lo, end, right) {
|
|
27
|
+
while (lo < end) {
|
|
28
|
+
const mid = lo + Math.floor((end - lo) / 2);
|
|
29
|
+
const goRight = right ? compareFn(value, array[mid]) >= 0 : compareFn(array[mid], value) < 0;
|
|
30
|
+
if (goRight)
|
|
31
|
+
lo = mid + 1;
|
|
32
|
+
else
|
|
33
|
+
end = mid;
|
|
34
|
+
}
|
|
35
|
+
return lo;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* The leftmost index at which `value` can be inserted into the sorted `array` and keep it sorted:
|
|
39
|
+
* before any elements equal to it, as Python's `bisect_left`.
|
|
40
|
+
*
|
|
41
|
+
* @param array A sorted array or array-like.
|
|
42
|
+
* @param value The value to locate.
|
|
43
|
+
* @param compareFn Comparator the array is sorted by. Defaults to the primitive order (numbers,
|
|
44
|
+
* bigints and strings).
|
|
45
|
+
* @param lo First index to search, a non-negative integer. Defaults to 0.
|
|
46
|
+
* @param hi End of the search (exclusive): an integer or ±Infinity, clamped to the length.
|
|
47
|
+
* Defaults to the length.
|
|
48
|
+
* @throws {TypeError} when `lo`, `hi` or the length of `array` is not an integer (`hi` may also be
|
|
49
|
+
* ±Infinity).
|
|
50
|
+
* @throws {RangeError} when `lo` is negative or the length of `array` is not a non-negative safe
|
|
51
|
+
* integer.
|
|
52
|
+
* @example
|
|
53
|
+
* ```ts
|
|
54
|
+
* bisectLeft([1, 2, 2, 3], 2) // 1
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
export function bisectLeft(array, value, compareFn = defaultCompare, lo = 0, hi) {
|
|
58
|
+
return search(array, value, compareFn, lo, searchBounds(array, lo, hi), false);
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* The rightmost index at which `value` can be inserted into the sorted `array` and keep it sorted:
|
|
62
|
+
* after any elements equal to it, as Python's `bisect_right`. Also exported as `bisect`, Python's
|
|
63
|
+
* short name.
|
|
64
|
+
*
|
|
65
|
+
* @param array A sorted array or array-like.
|
|
66
|
+
* @param value The value to locate.
|
|
67
|
+
* @param compareFn Comparator the array is sorted by. Defaults to the primitive order (numbers,
|
|
68
|
+
* bigints and strings).
|
|
69
|
+
* @param lo First index to search, a non-negative integer. Defaults to 0.
|
|
70
|
+
* @param hi End of the search (exclusive): an integer or ±Infinity, clamped to the length.
|
|
71
|
+
* Defaults to the length.
|
|
72
|
+
* @throws {TypeError} when `lo`, `hi` or the length of `array` is not an integer (`hi` may also be
|
|
73
|
+
* ±Infinity).
|
|
74
|
+
* @throws {RangeError} when `lo` is negative or the length of `array` is not a non-negative safe
|
|
75
|
+
* integer.
|
|
76
|
+
* @example
|
|
77
|
+
* ```ts
|
|
78
|
+
* bisectRight([1, 2, 2, 3], 2) // 3
|
|
79
|
+
* bisect(['a', 'c'], 'b') // 1
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
export function bisectRight(array, value, compareFn = defaultCompare, lo = 0, hi) {
|
|
83
|
+
return search(array, value, compareFn, lo, searchBounds(array, lo, hi), true);
|
|
84
|
+
}
|
|
85
|
+
export { bisectRight as bisect };
|
|
86
|
+
/**
|
|
87
|
+
* Insert `value` into the sorted `array` after any equal elements, keeping it sorted, and return
|
|
88
|
+
* the array.
|
|
89
|
+
*
|
|
90
|
+
* @param array A sorted array; changed in place.
|
|
91
|
+
* @param value The value to insert.
|
|
92
|
+
* @param compareFn Comparator the array is sorted by. Defaults to the primitive order.
|
|
93
|
+
* @param lo First index to search, a non-negative integer. Defaults to 0.
|
|
94
|
+
* @param hi End of the search (exclusive): an integer or ±Infinity, clamped to the length.
|
|
95
|
+
* Defaults to the length.
|
|
96
|
+
* @throws {TypeError} when `lo` or `hi` is not an integer (`hi` may also be ±Infinity).
|
|
97
|
+
* @throws {RangeError} when `lo` is negative or `array` is a huge sparse array.
|
|
98
|
+
* @example
|
|
99
|
+
* ```ts
|
|
100
|
+
* insort([1, 3], 2) // [1, 2, 3]
|
|
101
|
+
* ```
|
|
102
|
+
*/
|
|
103
|
+
export function insort(array, value, compareFn = defaultCompare, lo = 0, hi) {
|
|
104
|
+
const end = searchBounds(array, lo, hi);
|
|
105
|
+
assertNotHugeSparse(array);
|
|
106
|
+
array.splice(search(array, value, compareFn, lo, end, true), 0, value);
|
|
107
|
+
return array;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Insert `value` into the sorted `array` before any equal elements, keeping it sorted, and return
|
|
111
|
+
* the array.
|
|
112
|
+
*
|
|
113
|
+
* @param array A sorted array; changed in place.
|
|
114
|
+
* @param value The value to insert.
|
|
115
|
+
* @param compareFn Comparator the array is sorted by. Defaults to the primitive order.
|
|
116
|
+
* @param lo First index to search, a non-negative integer. Defaults to 0.
|
|
117
|
+
* @param hi End of the search (exclusive): an integer or ±Infinity, clamped to the length.
|
|
118
|
+
* Defaults to the length.
|
|
119
|
+
* @throws {TypeError} when `lo` or `hi` is not an integer (`hi` may also be ±Infinity).
|
|
120
|
+
* @throws {RangeError} when `lo` is negative or `array` is a huge sparse array.
|
|
121
|
+
* @example
|
|
122
|
+
* ```ts
|
|
123
|
+
* insortLeft([1, 3], 2) // [1, 2, 3]
|
|
124
|
+
* ```
|
|
125
|
+
*/
|
|
126
|
+
export function insortLeft(array, value, compareFn = defaultCompare, lo = 0, hi) {
|
|
127
|
+
const end = searchBounds(array, lo, hi);
|
|
128
|
+
assertNotHugeSparse(array);
|
|
129
|
+
array.splice(search(array, value, compareFn, lo, end, false), 0, value);
|
|
130
|
+
return array;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* The index of `value` in the sorted `array`, or `-(insertion index) - 1` when it is absent, so a
|
|
134
|
+
* negative result `r` means `value` belongs at `-r - 1`.
|
|
135
|
+
*
|
|
136
|
+
* @param array A sorted array or array-like.
|
|
137
|
+
* @param value The value to find.
|
|
138
|
+
* @param compareFn Comparator the array is sorted by; 0 means found. Defaults to the primitive
|
|
139
|
+
* order.
|
|
140
|
+
* @param lo First index to search, a non-negative integer. Defaults to 0.
|
|
141
|
+
* @param hi End of the search (exclusive): an integer or ±Infinity, clamped to the length.
|
|
142
|
+
* Defaults to the length.
|
|
143
|
+
* @throws {TypeError} when `lo`, `hi` or the length of `array` is not an integer (`hi` may also be
|
|
144
|
+
* ±Infinity).
|
|
145
|
+
* @throws {RangeError} when `lo` is negative or the length of `array` is not a non-negative safe
|
|
146
|
+
* integer.
|
|
147
|
+
* @example
|
|
148
|
+
* ```ts
|
|
149
|
+
* binarySearch([1, 3, 5], 3) // 1
|
|
150
|
+
* binarySearch([1, 3, 5], 4) // -3
|
|
151
|
+
* ```
|
|
152
|
+
*/
|
|
153
|
+
export function binarySearch(array, value, compareFn = defaultCompare, lo = 0, hi) {
|
|
154
|
+
const end = searchBounds(array, lo, hi);
|
|
155
|
+
const i = search(array, value, compareFn, lo, end, false);
|
|
156
|
+
return i < end && compareFn(array[i], value) === 0 ? i : -i - 1;
|
|
157
|
+
}
|
|
@@ -1,117 +1,87 @@
|
|
|
1
|
-
import { Iteratee } from '../types.js';
|
|
2
|
-
import { Sequence } from './abc.js';
|
|
3
1
|
/**
|
|
4
|
-
*
|
|
5
|
-
*
|
|
2
|
+
* A double-ended queue on a ring buffer, with Array's method names: `push` and `pop` at the end,
|
|
3
|
+
* `unshift` and `shift` at the front, `at` and `set` by index, all O(1) amortized. It grows by
|
|
4
|
+
* doubling when unbounded; with `maxlen` it evicts from the opposite end, as Python's `deque` does.
|
|
5
|
+
* `includes` uses SameValueZero.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* const recent = new Deque<number>([], { maxlen: 3 })
|
|
10
|
+
* recent.push(1, 2, 3, 4)
|
|
11
|
+
* ;[...recent] // [2, 3, 4]
|
|
12
|
+
* recent.shift() // 2
|
|
13
|
+
* ```
|
|
6
14
|
*/
|
|
7
|
-
export declare class Deque<T
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
*
|
|
15
|
-
* @param
|
|
16
|
-
* @
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
15
|
+
export declare class Deque<T> {
|
|
16
|
+
#private;
|
|
17
|
+
/**
|
|
18
|
+
* Create a deque holding `items`, in order.
|
|
19
|
+
* @param items Initial elements, read one at a time. When longer than `maxlen`, only the last
|
|
20
|
+
* `maxlen` remain.
|
|
21
|
+
* @param options.maxlen Maximum size; overflow evicts from the opposite end. `0` makes push/unshift
|
|
22
|
+
* no-ops. `null`, `undefined` and `Infinity` mean unbounded.
|
|
23
|
+
* @param options.arrType Typed-array (or similar) constructor used for storage.
|
|
24
|
+
* @throws {TypeError} when `items` is not iterable, or `maxlen` is not a number, `null` or
|
|
25
|
+
* `undefined`.
|
|
26
|
+
* @throws {RangeError} when `maxlen` is negative, not an integer, or above
|
|
27
|
+
* `Number.MAX_SAFE_INTEGER`, or when `items` is a huge sparse array.
|
|
28
|
+
*/
|
|
29
|
+
constructor(items?: Iterable<T> | null, options?: {
|
|
30
|
+
maxlen?: number | null;
|
|
31
|
+
arrType?: new (n: number) => {
|
|
32
|
+
[i: number]: T;
|
|
33
|
+
length: number;
|
|
34
|
+
};
|
|
35
|
+
});
|
|
36
|
+
/** The number of elements. */
|
|
20
37
|
get size(): number;
|
|
21
38
|
/**
|
|
22
|
-
*
|
|
39
|
+
* Maximum size when constructed with a finite `maxlen`, otherwise `undefined`.
|
|
23
40
|
*/
|
|
41
|
+
get maxlen(): number | undefined;
|
|
42
|
+
/** `'Deque'`, for `Object.prototype.toString`. */
|
|
43
|
+
get [Symbol.toStringTag](): string;
|
|
44
|
+
/** Remove every element and give back the buffer a burst grew. */
|
|
24
45
|
clear(): void;
|
|
25
46
|
/**
|
|
26
|
-
*
|
|
27
|
-
* @
|
|
28
|
-
* @returns {number} Returns the new size of the deque.
|
|
29
|
-
*/
|
|
30
|
-
add(item: any): number;
|
|
31
|
-
/**
|
|
32
|
-
* Appends a new element to the end of the deque.
|
|
33
|
-
* @param {T} item Item to append.
|
|
34
|
-
* @returns {number} Returns the new size of the deque.
|
|
47
|
+
* Append one or more elements at the end.
|
|
48
|
+
* @returns The new size.
|
|
35
49
|
*/
|
|
36
|
-
|
|
50
|
+
push(...xs: T[]): number;
|
|
37
51
|
/**
|
|
38
|
-
*
|
|
39
|
-
* @
|
|
40
|
-
* @returns {number} Returns the new size of the deque.
|
|
52
|
+
* Insert one or more elements at the front, preserving argument order.
|
|
53
|
+
* @returns The new size.
|
|
41
54
|
*/
|
|
42
|
-
|
|
55
|
+
unshift(...xs: T[]): number;
|
|
43
56
|
/**
|
|
44
|
-
*
|
|
45
|
-
* @returns {?T} Returns the popped item.
|
|
57
|
+
* Remove and return the last element, or `undefined` when empty.
|
|
46
58
|
*/
|
|
47
59
|
pop(): T | undefined;
|
|
48
60
|
/**
|
|
49
|
-
*
|
|
50
|
-
* @returns {?T} Returns the popped item.
|
|
51
|
-
*/
|
|
52
|
-
remove(): T | undefined;
|
|
53
|
-
/**
|
|
54
|
-
* Removes and returns the element at the beginning of the deque.
|
|
55
|
-
* @returns {?T} Returns the removed deque element.
|
|
56
|
-
*/
|
|
57
|
-
popleft(): T | undefined;
|
|
58
|
-
/**
|
|
59
|
-
* Returns the element at the beginning of the deque.
|
|
60
|
-
* @returns {?T} The element if exists
|
|
61
|
-
*/
|
|
62
|
-
first(): T | undefined;
|
|
63
|
-
/**
|
|
64
|
-
* Returns the element at a specified index position in the deque.
|
|
65
|
-
* @param {number} index The given index
|
|
66
|
-
* @returns {?T} The element if exists
|
|
67
|
-
*/
|
|
68
|
-
get(index: any): T | undefined;
|
|
69
|
-
/**
|
|
70
|
-
* Update the element at a given `index`.
|
|
71
|
-
* @param {number} index The given index
|
|
72
|
-
*/
|
|
73
|
-
set(index: number, value: T): void;
|
|
74
|
-
/**
|
|
75
|
-
* Check if a given element is in the deque.
|
|
76
|
-
* @param {T} x The value to find
|
|
77
|
-
* @returns {boolean} `true` if the value is found
|
|
78
|
-
*/
|
|
79
|
-
contains(x: any): boolean;
|
|
80
|
-
/**
|
|
81
|
-
* Returns the first element that matches the `iteratee` function.
|
|
82
|
-
* @param {Iteratee<T>} iteratee A function that will be invoked per element
|
|
83
|
-
* @returns {?T} The element if exists
|
|
84
|
-
*/
|
|
85
|
-
find(iteratee: Iteratee<T>): T | undefined;
|
|
86
|
-
/**
|
|
87
|
-
* Calls `iteratee` function for every element in the deque.
|
|
88
|
-
* @param {Iteratee<T>} iteratee A function that will be invoked per element
|
|
89
|
-
*/
|
|
90
|
-
forEach(iteratee: any): void;
|
|
91
|
-
/**
|
|
92
|
-
* Returns a new array with all the elements in reverse order.
|
|
93
|
-
* @returns {Array<T>} The elements reversed
|
|
94
|
-
*/
|
|
95
|
-
reversed(): T[];
|
|
96
|
-
/**
|
|
97
|
-
* Returns a new array with all the elements in the order they were added to the deque.
|
|
98
|
-
* @returns {Array<T>} The elements array
|
|
99
|
-
*/
|
|
100
|
-
toArray(): T[];
|
|
101
|
-
/**
|
|
102
|
-
* Returns an iterator over the deque's values.
|
|
103
|
-
* @returns {Iterator}
|
|
61
|
+
* Remove and return the first element, or `undefined` when empty.
|
|
104
62
|
*/
|
|
105
|
-
|
|
63
|
+
shift(): T | undefined;
|
|
106
64
|
/**
|
|
107
|
-
*
|
|
65
|
+
* Element at index `i` (negative from the end). Out of range or empty -> `undefined`.
|
|
66
|
+
* The index coerces as in `Array.prototype.at`; values that coerce to `NaN` mean `0`.
|
|
108
67
|
*/
|
|
109
|
-
|
|
68
|
+
at(i: number): T | undefined;
|
|
110
69
|
/**
|
|
111
|
-
*
|
|
112
|
-
* @returns
|
|
70
|
+
* Replace the element at index `i` (negative from the end). The index coerces as in {@link at}.
|
|
71
|
+
* @returns `this`
|
|
72
|
+
* @throws {RangeError} when the index is out of range.
|
|
113
73
|
*/
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
74
|
+
set(i: number, v: T): this;
|
|
75
|
+
/** Whether `value` is present (SameValueZero). */
|
|
76
|
+
includes(value: T): boolean;
|
|
77
|
+
/** Call `callback(element, index, deque)` for each element, front to back. */
|
|
78
|
+
forEach(callback: (value: T, index: number, deque: this) => void, thisArg?: unknown): void;
|
|
79
|
+
/** The indices, front to back, as `Array.prototype.keys` gives them. */
|
|
80
|
+
keys(): Generator<number>;
|
|
81
|
+
/** The elements, front to back. */
|
|
82
|
+
values(): Generator<T>;
|
|
83
|
+
/** `[index, element]` pairs, front to back. */
|
|
84
|
+
entries(): Generator<[number, T]>;
|
|
85
|
+
/** The elements, front to back. */
|
|
86
|
+
[Symbol.iterator](): Generator<T>;
|
|
117
87
|
}
|