algorithm: remove index from bisect

This commit is contained in:
2024-06-25 18:40:20 +08:00
parent 910890f685
commit bfc8293304

View File

@@ -29,22 +29,19 @@ export async function autoBisect(
end: number,
check: (
begin: number,
end: number,
index: number
end: number
) => Promise<boolean>) {
// Try to find the first index where check() predicates to true.
// Assume check() is true at first, for range [begin, end)
let index = 0;
while (end - begin > 1) {
const mid = Math.floor((end - begin) / 2) + begin;
if (await check(begin, mid, index)) {
if (await check(begin, mid)) {
// Shrink half range.
end = mid;
} else {
begin = mid;
}
index++;
}
return begin;