| 205 | } |
| 206 | |
| 207 | const lookupTable = ( |
| 208 | values: ReadonlyArray<number>, |
| 209 | size: number, |
| 210 | dir: "next" | "prev" |
| 211 | ): Array<number | undefined> => { |
| 212 | const result = new Array(size).fill(undefined) |
| 213 | if (values.length === 0) { |
| 214 | return result |
| 215 | } |
| 216 | |
| 217 | let current: number | undefined = undefined |
| 218 | |
| 219 | if (dir === "next") { |
| 220 | let index = values.length - 1 |
| 221 | for (let i = size - 1; i >= 0; i--) { |
| 222 | while (index >= 0 && values[index] >= i) { |
| 223 | current = values[index--] |
| 224 | } |
| 225 | result[i] = current |
| 226 | } |
| 227 | } else { |
| 228 | let index = 0 |
| 229 | for (let i = 0; i < size; i++) { |
| 230 | while (index < values.length && values[index] <= i) { |
| 231 | current = values[index++] |
| 232 | } |
| 233 | result[i] = current |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | return result |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * @since 2.0.0 |