-
Notifications
You must be signed in to change notification settings - Fork 470
Expand file tree
/
Copy pathbisect.ts
More file actions
209 lines (190 loc) · 6.37 KB
/
bisect.ts
File metadata and controls
209 lines (190 loc) · 6.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// This file originally comes from https://github.com/3rd-Eden/node-bisection/, but has been
// imported and changed to fix some bugs.
/*
Copyright (c) 2010/2011 Arnout Kazemier,3rd-Eden
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
/**
* These functions are used on sorted arrays.
*
* You commonly use them in one of two cases:
*
* 1. When you want to insert a new element into a sorted array, at a position
* such that the array remains in sorted order. Or
* 2. When you have a sorted array of interval start positions, and you want
* to find out which interval includes a certain number.
*
* For case 1, you can use either bisectionRight or bisectionLeft. The difference
* only matters if you care about the positioning of two equal elements. For
* example:
* bisectionRight([1, 2, 3], 2) === 2
* bisectionLeft([1, 2, 3], 2) === 1
* i.e. the returned index is either to the right or to the left of the equal
* element. If no exactly matching element is present in the array, both functions
* return the same value, i.e. the index of the first element that's larger than
* the passed in element (which is the index at which that element would need to
* be inserted).
*
* ```js
* const insertionIndexR = bisectionRight(array, x);
* assert(array[insertionIndexR] > x);
* assert(array[insertionIndexR - 1] <= x);
*
* const insertionIndexL = bisectionLeft(array, x);
* assert(array[insertionIndexL] >= x);
* assert(array[insertionIndexL - 1] < x);
* ```
*
* For case 2, you'll have to use bisectionRight, and subtract 1 from the return
* value. For example, if you have the half-open intervals 2..4, 4..7, 7..Infinity,
* then your start position array will be [2, 4, 7] and bisectionRight() - 1 will
* be the index of the last interval whose start position is <= the checked element.
* bisectionRight([2, 4, 7], 1) - 1 === -1
* bisectionRight([2, 4, 7], 2) - 1 === 0
* bisectionRight([2, 4, 7], 3) - 1 === 0
* bisectionRight([2, 4, 7], 4) - 1 === 1
* bisectionRight([2, 4, 7], 5) - 1 === 1
* bisectionRight([2, 4, 7], 6) - 1 === 1
* bisectionRight([2, 4, 7], 7) - 1 === 2
* bisectionRight([2, 4, 7], 20) - 1 === 2
*
* Example code:
*
* ```js
* const intervalStarts = [2, 4, 7];
* const insertionIndex = bisectionRight(intervalStarts, x);
* if (insertionIndex === 0) {
* // x is before the first interval.
* return null;
* }
*
* const intervalIndex = insertionIndex - 1;
* assert(x >= intervalStarts[intervalIndex]);
*
* // If there can be gaps between your intervals, you also need to check the
* // interval end:
* if (x >= intervalEnds[intervalIndex]) {
* // x isn't actually inside this interval! It's in the gap between
* // intervalIndex and intervalIndex+1.
* return null;
* }
*
* // Now we know that x is inside the interval.
* assert(x >= intervalStarts[intervalIndex] && x < intervalEnds[intervalIndex]);
* return intervalIndex;
* ```
*/
type TypedArray =
| Int8Array
| Uint8Array
| Uint8ClampedArray
| Int16Array
| Uint16Array
| Int32Array
| Uint32Array
| Float32Array
| Float64Array
| BigInt64Array
| BigUint64Array;
export function bisectionRight(
array: number[] | TypedArray,
x: number,
low: number = 0,
high: number = array.length
): number {
if (low < 0 || low > array.length || high < 0 || high > array.length) {
throw new TypeError("low and high must lie within the array's range");
}
while (low < high) {
const mid = (low + high) >> 1;
if (x < array[mid]) {
high = mid;
} else {
low = mid + 1;
}
}
return low;
}
/**
* Like bisectionRight, but accepts a "key" function which maps an element of
* the array to a number "key". The array must be sorted by that key.
* The looked-up element `x` will be compared to the keys.
*/
export function bisectionRightByKey<T>(
array: T[],
x: number,
toKey: (arg: T) => number,
low: number = 0,
high: number = array.length
): number {
if (low < 0 || low > array.length || high < 0 || high > array.length) {
throw new TypeError("low and high must lie within the array's range");
}
while (low < high) {
const mid = (low + high) >> 1;
if (x < toKey(array[mid])) {
high = mid;
} else {
low = mid + 1;
}
}
return low;
}
/**
* This is the same as bisectionRightByKey but uses string as the key type.
* If you can find a way to make Flow accept a single function that handles
* both string and number keys, please remove this duplication.
*/
export function bisectionRightByStrKey<T>(
array: T[],
x: string,
toKey: (arg: T) => string,
low: number = 0,
high: number = array.length
): number {
if (low < 0 || low > array.length || high < 0 || high > array.length) {
throw new TypeError("low and high must lie within the array's range");
}
while (low < high) {
const mid = (low + high) >> 1;
if (x < toKey(array[mid])) {
high = mid;
} else {
low = mid + 1;
}
}
return low;
}
export function bisectionLeft(
array: number[] | TypedArray,
x: number,
low: number = 0,
high: number = array.length
): number {
if (low < 0 || low > array.length || high < 0 || high > array.length) {
throw new TypeError("low and high must lie within the array's range");
}
while (low < high) {
const mid = (low + high) >> 1;
if (x <= array[mid]) {
high = mid;
} else {
low = mid + 1;
}
}
return low;
}