@zakkster/lite-bisect 0.0.0-stage → 1.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/Bisect.d.ts +243 -0
- package/Bisect.js +450 -0
- package/CHANGELOG.md +125 -0
- package/LICENSE.txt +21 -0
- package/README.md +365 -2
- package/llms.txt +105 -0
- package/package.json +67 -4
package/Bisect.d.ts
ADDED
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @zakkster/lite-bisect -- zero-GC binary search kernels for sorted typed arrays.
|
|
3
|
+
* One monomorphic copy of every kernel per array type, suffix F64 / F32 / U32 / U16 / I32
|
|
4
|
+
* (Float64Array, Float32Array, Uint32Array, Uint16Array, Int32Array).
|
|
5
|
+
*
|
|
6
|
+
* Contract (every kernel):
|
|
7
|
+
* a a sorted, non-decreasing typed array of the suffix's type, no NaN inside.
|
|
8
|
+
* [lo, hi) the caller owns the range: integers, 0 <= lo <= hi <= a.length, hi - lo <= 2^30 - 1.
|
|
9
|
+
* The hot kernels trust the caller: no range check, no throw. The cold asserts
|
|
10
|
+
* assertRange(a, lo, hi) and assertSorted(a, lo, hi) check it and throw RangeError;
|
|
11
|
+
* no kernel calls them. A caller error such as a bad range may return a wrong index
|
|
12
|
+
* or never return.
|
|
13
|
+
* x a NaN query compares false everywhere: every lower / upper / hint kernel returns lo,
|
|
14
|
+
* segment returns lo - 1, nearest returns lo.
|
|
15
|
+
*
|
|
16
|
+
* Allocation: zero allocation where the kernel inlines into its caller, or every number argument (the query; the hint of lowerBoundHint*; lo / hi) is a Smi; a non-Smi number computed at run time or read from a typed array (a fractional query, a NaN hint read from a Float64Array, -0 for lo / hi) and passed across a call V8 does not inline is boxed by the caller: one 16 B HeapNumber per such argument on Node. For no hint pass lo or the literal NaN: neither boxes.
|
|
17
|
+
*
|
|
18
|
+
* MIT (c) Zahary Shinikchiev <shinikchiev@yahoo.com>
|
|
19
|
+
*
|
|
20
|
+
* @packageDocumentation
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
/** Any array type a kernel accepts; the cold asserts take every one of them. */
|
|
24
|
+
export type BisectArray = Float64Array | Float32Array | Uint32Array | Uint16Array | Int32Array;
|
|
25
|
+
|
|
26
|
+
// --- Float64Array -----------------------------------------------------------------------------
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* lower bound: the first i in [lo, hi) with a[i] >= x, else hi. NaN x -> lo.
|
|
30
|
+
* Branchless. Maglev does not inline it at its default 100 B cap; TurboFan inlines it (traced).
|
|
31
|
+
*/
|
|
32
|
+
export function lowerBoundBranchlessF64(a: Float64Array, lo: number, hi: number, x: number): number;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* upper bound: the first i in [lo, hi) with a[i] > x, else hi. NaN x -> lo.
|
|
36
|
+
* Branchless. Maglev does not inline it at its default 100 B cap; TurboFan inlines it (traced).
|
|
37
|
+
*/
|
|
38
|
+
export function upperBoundBranchlessF64(a: Float64Array, lo: number, hi: number, x: number): number;
|
|
39
|
+
|
|
40
|
+
/** lower bound: the first i in [lo, hi) with a[i] >= x, else hi. NaN x -> lo. Branchy loop. */
|
|
41
|
+
export function lowerBoundBranchyF64(a: Float64Array, lo: number, hi: number, x: number): number;
|
|
42
|
+
|
|
43
|
+
/** upper bound: the first i in [lo, hi) with a[i] > x, else hi. NaN x -> lo. Branchy loop. */
|
|
44
|
+
export function upperBoundBranchyF64(a: Float64Array, lo: number, hi: number, x: number): number;
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* lower bound galloping out from the hint h: the first i in [lo, hi) with a[i] >= x, else hi. NaN x -> lo.
|
|
48
|
+
* For coherent streams only (each h near its answer, e.g. the previous result); on incoherent streams it can be slower than lowerBoundBranchless* (STATISTICAL for F64: ab.yml run 43145179770, two CPUs, n <= 65,536).
|
|
49
|
+
* h is an integer index or NaN (NaN -> lo; outside [lo, hi] clamped). A non-integer h is a caller error
|
|
50
|
+
* that may return a wrong index or never return.
|
|
51
|
+
*/
|
|
52
|
+
export function lowerBoundHintF64(a: Float64Array, lo: number, hi: number, x: number, h: number): number;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* segment: the last i in [lo, hi) with a[i] <= x (upper bound - 1); lo - 1 when x < a[lo], on a NaN x,
|
|
56
|
+
* on an empty range.
|
|
57
|
+
*/
|
|
58
|
+
export function segmentF64(a: Float64Array, lo: number, hi: number, x: number): number;
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* nearest: the index in [lo, hi) closest to x. An exact match wins (the first i with a[i] === x); a tie
|
|
62
|
+
* between the last value below x and the first above goes to the LOWER index; within a run of equal
|
|
63
|
+
* values, the index next to x; past either end -> lo or hi - 1; an empty range -> lo; NaN x -> lo.
|
|
64
|
+
*/
|
|
65
|
+
export function nearestF64(a: Float64Array, lo: number, hi: number, x: number): number;
|
|
66
|
+
|
|
67
|
+
// --- Float32Array -----------------------------------------------------------------------------
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* lower bound: the first i in [lo, hi) with a[i] >= x, else hi. NaN x -> lo.
|
|
71
|
+
* Branchless. Maglev does not inline it at its default 100 B cap; TurboFan inlines it (traced).
|
|
72
|
+
*/
|
|
73
|
+
export function lowerBoundBranchlessF32(a: Float32Array, lo: number, hi: number, x: number): number;
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* upper bound: the first i in [lo, hi) with a[i] > x, else hi. NaN x -> lo.
|
|
77
|
+
* Branchless. Maglev does not inline it at its default 100 B cap; TurboFan inlines it (traced).
|
|
78
|
+
*/
|
|
79
|
+
export function upperBoundBranchlessF32(a: Float32Array, lo: number, hi: number, x: number): number;
|
|
80
|
+
|
|
81
|
+
/** lower bound: the first i in [lo, hi) with a[i] >= x, else hi. NaN x -> lo. Branchy loop. */
|
|
82
|
+
export function lowerBoundBranchyF32(a: Float32Array, lo: number, hi: number, x: number): number;
|
|
83
|
+
|
|
84
|
+
/** upper bound: the first i in [lo, hi) with a[i] > x, else hi. NaN x -> lo. Branchy loop. */
|
|
85
|
+
export function upperBoundBranchyF32(a: Float32Array, lo: number, hi: number, x: number): number;
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* lower bound galloping out from the hint h: the first i in [lo, hi) with a[i] >= x, else hi. NaN x -> lo.
|
|
89
|
+
* For coherent streams only (each h near its answer, e.g. the previous result); on incoherent streams it can be slower than lowerBoundBranchless* (STATISTICAL for F64: ab.yml run 43145179770, two CPUs, n <= 65,536; this copy's type is unmeasured).
|
|
90
|
+
* h is an integer index or NaN (NaN -> lo; outside [lo, hi] clamped). A non-integer h is a caller error
|
|
91
|
+
* that may return a wrong index or never return.
|
|
92
|
+
*/
|
|
93
|
+
export function lowerBoundHintF32(a: Float32Array, lo: number, hi: number, x: number, h: number): number;
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* segment: the last i in [lo, hi) with a[i] <= x (upper bound - 1); lo - 1 when x < a[lo], on a NaN x,
|
|
97
|
+
* on an empty range.
|
|
98
|
+
*/
|
|
99
|
+
export function segmentF32(a: Float32Array, lo: number, hi: number, x: number): number;
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* nearest: the index in [lo, hi) closest to x. An exact match wins (the first i with a[i] === x); a tie
|
|
103
|
+
* between the last value below x and the first above goes to the LOWER index; within a run of equal
|
|
104
|
+
* values, the index next to x; past either end -> lo or hi - 1; an empty range -> lo; NaN x -> lo.
|
|
105
|
+
*/
|
|
106
|
+
export function nearestF32(a: Float32Array, lo: number, hi: number, x: number): number;
|
|
107
|
+
|
|
108
|
+
// --- Uint32Array ------------------------------------------------------------------------------
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* lower bound: the first i in [lo, hi) with a[i] >= x, else hi. NaN x -> lo.
|
|
112
|
+
* Branchless. Maglev does not inline it at its default 100 B cap; TurboFan inlines it (traced).
|
|
113
|
+
*/
|
|
114
|
+
export function lowerBoundBranchlessU32(a: Uint32Array, lo: number, hi: number, x: number): number;
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* upper bound: the first i in [lo, hi) with a[i] > x, else hi. NaN x -> lo.
|
|
118
|
+
* Branchless. Maglev does not inline it at its default 100 B cap; TurboFan inlines it (traced).
|
|
119
|
+
*/
|
|
120
|
+
export function upperBoundBranchlessU32(a: Uint32Array, lo: number, hi: number, x: number): number;
|
|
121
|
+
|
|
122
|
+
/** lower bound: the first i in [lo, hi) with a[i] >= x, else hi. NaN x -> lo. Branchy loop. */
|
|
123
|
+
export function lowerBoundBranchyU32(a: Uint32Array, lo: number, hi: number, x: number): number;
|
|
124
|
+
|
|
125
|
+
/** upper bound: the first i in [lo, hi) with a[i] > x, else hi. NaN x -> lo. Branchy loop. */
|
|
126
|
+
export function upperBoundBranchyU32(a: Uint32Array, lo: number, hi: number, x: number): number;
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* lower bound galloping out from the hint h: the first i in [lo, hi) with a[i] >= x, else hi. NaN x -> lo.
|
|
130
|
+
* For coherent streams only (each h near its answer, e.g. the previous result); on incoherent streams it can be slower than lowerBoundBranchless* (STATISTICAL for F64: ab.yml run 43145179770, two CPUs, n <= 65,536; this copy's type is unmeasured).
|
|
131
|
+
* h is an integer index or NaN (NaN -> lo; outside [lo, hi] clamped). A non-integer h is a caller error
|
|
132
|
+
* that may return a wrong index or never return.
|
|
133
|
+
*/
|
|
134
|
+
export function lowerBoundHintU32(a: Uint32Array, lo: number, hi: number, x: number, h: number): number;
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* segment: the last i in [lo, hi) with a[i] <= x (upper bound - 1); lo - 1 when x < a[lo], on a NaN x,
|
|
138
|
+
* on an empty range.
|
|
139
|
+
*/
|
|
140
|
+
export function segmentU32(a: Uint32Array, lo: number, hi: number, x: number): number;
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* nearest: the index in [lo, hi) closest to x. An exact match wins (the first i with a[i] === x); a tie
|
|
144
|
+
* between the last value below x and the first above goes to the LOWER index; within a run of equal
|
|
145
|
+
* values, the index next to x; past either end -> lo or hi - 1; an empty range -> lo; NaN x -> lo.
|
|
146
|
+
*/
|
|
147
|
+
export function nearestU32(a: Uint32Array, lo: number, hi: number, x: number): number;
|
|
148
|
+
|
|
149
|
+
// --- Uint16Array ------------------------------------------------------------------------------
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* lower bound: the first i in [lo, hi) with a[i] >= x, else hi. NaN x -> lo.
|
|
153
|
+
* Branchless. Maglev does not inline it at its default 100 B cap; TurboFan inlines it (traced).
|
|
154
|
+
*/
|
|
155
|
+
export function lowerBoundBranchlessU16(a: Uint16Array, lo: number, hi: number, x: number): number;
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* upper bound: the first i in [lo, hi) with a[i] > x, else hi. NaN x -> lo.
|
|
159
|
+
* Branchless. Maglev does not inline it at its default 100 B cap; TurboFan inlines it (traced).
|
|
160
|
+
*/
|
|
161
|
+
export function upperBoundBranchlessU16(a: Uint16Array, lo: number, hi: number, x: number): number;
|
|
162
|
+
|
|
163
|
+
/** lower bound: the first i in [lo, hi) with a[i] >= x, else hi. NaN x -> lo. Branchy loop. */
|
|
164
|
+
export function lowerBoundBranchyU16(a: Uint16Array, lo: number, hi: number, x: number): number;
|
|
165
|
+
|
|
166
|
+
/** upper bound: the first i in [lo, hi) with a[i] > x, else hi. NaN x -> lo. Branchy loop. */
|
|
167
|
+
export function upperBoundBranchyU16(a: Uint16Array, lo: number, hi: number, x: number): number;
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* lower bound galloping out from the hint h: the first i in [lo, hi) with a[i] >= x, else hi. NaN x -> lo.
|
|
171
|
+
* For coherent streams only (each h near its answer, e.g. the previous result); on incoherent streams it can be slower than lowerBoundBranchless* (STATISTICAL for F64: ab.yml run 43145179770, two CPUs, n <= 65,536; this copy's type is unmeasured).
|
|
172
|
+
* h is an integer index or NaN (NaN -> lo; outside [lo, hi] clamped). A non-integer h is a caller error
|
|
173
|
+
* that may return a wrong index or never return.
|
|
174
|
+
*/
|
|
175
|
+
export function lowerBoundHintU16(a: Uint16Array, lo: number, hi: number, x: number, h: number): number;
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* segment: the last i in [lo, hi) with a[i] <= x (upper bound - 1); lo - 1 when x < a[lo], on a NaN x,
|
|
179
|
+
* on an empty range.
|
|
180
|
+
*/
|
|
181
|
+
export function segmentU16(a: Uint16Array, lo: number, hi: number, x: number): number;
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* nearest: the index in [lo, hi) closest to x. An exact match wins (the first i with a[i] === x); a tie
|
|
185
|
+
* between the last value below x and the first above goes to the LOWER index; within a run of equal
|
|
186
|
+
* values, the index next to x; past either end -> lo or hi - 1; an empty range -> lo; NaN x -> lo.
|
|
187
|
+
*/
|
|
188
|
+
export function nearestU16(a: Uint16Array, lo: number, hi: number, x: number): number;
|
|
189
|
+
|
|
190
|
+
// --- Int32Array -------------------------------------------------------------------------------
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* lower bound: the first i in [lo, hi) with a[i] >= x, else hi. NaN x -> lo.
|
|
194
|
+
* Branchless. Maglev does not inline it at its default 100 B cap; TurboFan inlines it (traced).
|
|
195
|
+
*/
|
|
196
|
+
export function lowerBoundBranchlessI32(a: Int32Array, lo: number, hi: number, x: number): number;
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* upper bound: the first i in [lo, hi) with a[i] > x, else hi. NaN x -> lo.
|
|
200
|
+
* Branchless. Maglev does not inline it at its default 100 B cap; TurboFan inlines it (traced).
|
|
201
|
+
*/
|
|
202
|
+
export function upperBoundBranchlessI32(a: Int32Array, lo: number, hi: number, x: number): number;
|
|
203
|
+
|
|
204
|
+
/** lower bound: the first i in [lo, hi) with a[i] >= x, else hi. NaN x -> lo. Branchy loop. */
|
|
205
|
+
export function lowerBoundBranchyI32(a: Int32Array, lo: number, hi: number, x: number): number;
|
|
206
|
+
|
|
207
|
+
/** upper bound: the first i in [lo, hi) with a[i] > x, else hi. NaN x -> lo. Branchy loop. */
|
|
208
|
+
export function upperBoundBranchyI32(a: Int32Array, lo: number, hi: number, x: number): number;
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* lower bound galloping out from the hint h: the first i in [lo, hi) with a[i] >= x, else hi. NaN x -> lo.
|
|
212
|
+
* For coherent streams only (each h near its answer, e.g. the previous result); on incoherent streams it can be slower than lowerBoundBranchless* (STATISTICAL for F64: ab.yml run 43145179770, two CPUs, n <= 65,536; this copy's type is unmeasured).
|
|
213
|
+
* h is an integer index or NaN (NaN -> lo; outside [lo, hi] clamped). A non-integer h is a caller error
|
|
214
|
+
* that may return a wrong index or never return.
|
|
215
|
+
*/
|
|
216
|
+
export function lowerBoundHintI32(a: Int32Array, lo: number, hi: number, x: number, h: number): number;
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* segment: the last i in [lo, hi) with a[i] <= x (upper bound - 1); lo - 1 when x < a[lo], on a NaN x,
|
|
220
|
+
* on an empty range.
|
|
221
|
+
*/
|
|
222
|
+
export function segmentI32(a: Int32Array, lo: number, hi: number, x: number): number;
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* nearest: the index in [lo, hi) closest to x. An exact match wins (the first i with a[i] === x); a tie
|
|
226
|
+
* between the last value below x and the first above goes to the LOWER index; within a run of equal
|
|
227
|
+
* values, the index next to x; past either end -> lo or hi - 1; an empty range -> lo; NaN x -> lo.
|
|
228
|
+
*/
|
|
229
|
+
export function nearestI32(a: Int32Array, lo: number, hi: number, x: number): number;
|
|
230
|
+
|
|
231
|
+
// --- cold checks: one copy each, called by no kernel ------------------------------------------
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Cold check: throws RangeError unless a.length is a number and a.length, lo, hi are safe integers,
|
|
235
|
+
* 0 <= lo <= hi <= a.length and hi - lo <= 2^30 - 1. Reads only a.length. Call it off the hot path.
|
|
236
|
+
*/
|
|
237
|
+
export function assertRange(a: BisectArray, lo: number, hi: number): void;
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Cold check: assertRange(a, lo, hi), then throws RangeError on a NaN in [lo, hi) or a descent
|
|
241
|
+
* (a[i] < a[i - 1]). Reads every element of the range. Call it off the hot path.
|
|
242
|
+
*/
|
|
243
|
+
export function assertSorted(a: BisectArray, lo: number, hi: number): void;
|