Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
inokawa committed Apr 15, 2023
1 parent 253e2b4 commit bcc05ab
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions src/core/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,27 +50,23 @@ const findIndex = (cache: Cache, i: number, distance: number): number => {
if (distance >= 0) {
// search forward
while (i < cache._length - 1) {
const h = getItemSize(cache, i++);
sum += h;
if (sum >= distance) {
if (sum - h / 2 >= distance) {
i--;
}
if ((sum += getItemSize(cache, i++)) >= distance) {
break;
}
}
if (sum - getItemSize(cache, i) / 2 >= distance) {
i--;
}
} else {
// search backward
while (i > 0) {
const h = getItemSize(cache, --i);
sum -= h;
if (sum <= distance) {
if (sum + h / 2 < distance) {
i++;
}
if ((sum -= getItemSize(cache, --i)) <= distance) {
break;
}
}
if (sum + getItemSize(cache, i) / 2 < distance) {
i++;
}
}

return min(max(i, 0), cache._length - 1);
Expand Down

0 comments on commit bcc05ab

Please sign in to comment.