Skip to main content

Module grid

Module grid 

Source
Expand description

Two-dimensional runs: rectangular blocks in a bitmap of rows.

One word per row, bit c of row r is cell (r, c). Finding a w × h block of set cells is separable: the halving chain of Bits::run_starts applied down the rows (rows r..r + h all set, ⌈log₂ h⌉ passes) and then along each row (⌈log₂ w⌉ steps). In morphology terms this is binary erosion by a w × h rectangle; in allocator terms it is first-fit of a rectangle over a tile bitmap, texture atlases, sparse-texture page tables, inventory grids, occupancy grids.

Composition carries over from one dimension: block_starts(block_starts(g, w₁, h₁), w₂, h₂) = block_starts(g, w₁ + w₂ − 1, h₁ + h₂ − 1).

Grids narrower than BITS columns must keep unused columns clear.

Functions§

block_starts
Writes into out[r] the columns c at which a w × h block of set cells starts at (r, c). out.len() == rows.len(); rows that cannot fit h more rows come out zero. w in 1..=BITS, h >= 1.
clear_block
Clears every cell of the w × h block at (row, col), as fill_block sets them.
fill_block
Sets every cell of the w × h block at (row, col). The block must lie inside the grid (debug-asserted).
find_block
First-fit (row-major) position (row, col) of a w × h block of set cells, using scratch (same length as rows) as workspace.