algorithm: remove index from bisect
This commit is contained in:
@@ -29,22 +29,19 @@ export async function autoBisect(
|
|||||||
end: number,
|
end: number,
|
||||||
check: (
|
check: (
|
||||||
begin: number,
|
begin: number,
|
||||||
end: number,
|
end: number
|
||||||
index: number
|
|
||||||
) => Promise<boolean>) {
|
) => Promise<boolean>) {
|
||||||
|
|
||||||
// Try to find the first index where check() predicates to true.
|
// Try to find the first index where check() predicates to true.
|
||||||
// Assume check() is true at first, for range [begin, end)
|
// Assume check() is true at first, for range [begin, end)
|
||||||
let index = 0;
|
|
||||||
while (end - begin > 1) {
|
while (end - begin > 1) {
|
||||||
const mid = Math.floor((end - begin) / 2) + begin;
|
const mid = Math.floor((end - begin) / 2) + begin;
|
||||||
if (await check(begin, mid, index)) {
|
if (await check(begin, mid)) {
|
||||||
// Shrink half range.
|
// Shrink half range.
|
||||||
end = mid;
|
end = mid;
|
||||||
} else {
|
} else {
|
||||||
begin = mid;
|
begin = mid;
|
||||||
}
|
}
|
||||||
index++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return begin;
|
return begin;
|
||||||
|
|||||||
Reference in New Issue
Block a user