| 384 | } |
| 385 | |
| 386 | const makeLookupTable = ( |
| 387 | size: number, |
| 388 | dir: "next" | "prev" |
| 389 | ): (values: ReadonlyArray<number>) => Array<number | undefined> => |
| 390 | (values) => { |
| 391 | const result = new Array(size).fill(undefined) |
| 392 | if (values.length === 0) { |
| 393 | return result |
| 394 | } |
| 395 | |
| 396 | let current: number | undefined = undefined |
| 397 | |
| 398 | if (dir === "next") { |
| 399 | let index = values.length - 1 |
| 400 | for (let i = size - 1; i >= 0; i--) { |
| 401 | while (index >= 0 && values[index] >= i) { |
| 402 | current = values[index--] |
| 403 | } |
| 404 | result[i] = current |
| 405 | } |
| 406 | } else { |
| 407 | let index = 0 |
| 408 | for (let i = 0; i < size; i++) { |
| 409 | while (index < values.length && values[index] <= i) { |
| 410 | current = values[index++] |
| 411 | } |
| 412 | result[i] = current |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | return result |
| 417 | } |
| 418 | |
| 419 | const lookup = { |
| 420 | prev: { |