Visible rows in a virtualised list
Your task
You are windowing a virtualised list. Given the pixel height of every row, the viewport height, the current scroll offset in pixels, and an overscan count of extra rows to render on each side, return the half-open index range [start, end) of rows to mount. A row is visible when any part of it intersects the viewport, that is when its top is strictly less than scrollTop + viewport and its bottom is strictly greater than scrollTop. Then widen the range by overscan rows on each side and clamp to the list bounds. Return [0, 0] for an empty list. Rows may have different heights and the list may be very long, so a linear scan per scroll event is not acceptable.
Examples
visible_range(heights: list[int], viewport: int, scroll_top: int, overscan: int) -> list[int]Example 1
Input
visible_range([100, 100, 100, 100, 100], 250, 0, 0)Output
[0, 3]Example 2
Input
visible_range([100, 100, 100, 100, 100], 250, 120, 1)Output
[0, 5]