pub struct Grid<T> { /* private fields */ }Expand description
A dense width × height field of T, stored row-major (CORE-R33).
Implementations§
Source§impl<T: Clone> Grid<T>
impl<T: Clone> Grid<T>
Sourcepub fn resize(&mut self, size: Size, fill: T)
pub fn resize(&mut self, size: Size, fill: T)
Change the dimensions, keeping every cell whose (x, y) exists in both the
old and the new size; new cells become fill (CORE-R34).
Works in place: surviving cells are moved to their new row-major slot by
swap, which needs neither Copy nor extra clones. The buffer only grows
(never shrinks), so a shrink-then-grow on terminal resize does not allocate.
Source§impl<T> Grid<T>
impl<T> Grid<T>
Sourcepub fn size(&self) -> Size
pub fn size(&self) -> Size
The grid’s dimensions (CORE-R33).
A Size cannot be 0×0 (CORE-R1), so for the empty Grid::default this
reports 1×1; use width/height or
is_empty when the grid may be empty.
pub fn is_empty(&self) -> bool
Sourcepub fn get(&self, x: i32, y: i32) -> Option<&T>
pub fn get(&self, x: i32, y: i32) -> Option<&T>
The cell at (x, y), or None off-grid; never panics (CORE-R33).
Sourcepub fn get_mut(&mut self, x: i32, y: i32) -> Option<&mut T>
pub fn get_mut(&mut self, x: i32, y: i32) -> Option<&mut T>
Mutable access to the cell at (x, y), or None off-grid (CORE-R33).
Sourcepub fn set(&mut self, x: i32, y: i32, v: T) -> bool
pub fn set(&mut self, x: i32, y: i32, v: T) -> bool
Store v at (x, y); returns false (dropping v) off-grid (CORE-R33).
Sourcepub fn iter(&self) -> Iter<'_, T> ⓘ
pub fn iter(&self) -> Iter<'_, T> ⓘ
Every cell with its coordinates, in row-major order (CORE-R33).
Sourcepub fn as_slice(&self) -> &[T]
pub fn as_slice(&self) -> &[T]
The row-major buffer: cell (x, y) is at y * width + x (CORE-R33).
Sourcepub fn as_mut_slice(&mut self) -> &mut [T]
pub fn as_mut_slice(&mut self) -> &mut [T]
Mutable row-major buffer, for bulk per-cell updates that need no coordinates.
Sourcepub fn neighbors(&self, x: i32, y: i32, hood: Hood, edges: Edges) -> Neighbors ⓘ
pub fn neighbors(&self, x: i32, y: i32, hood: Hood, edges: Edges) -> Neighbors ⓘ
In-grid neighbour coordinates of (x, y), excluding (x, y) itself
(CORE-R35). Yields nothing if (x, y) is off-grid.
Order follows the offset table (row-major around the cell). With
Edges::Wrap on a grid narrower or shorter than 3, several offsets can
land on the same cell or on the cell itself; those are yielded once / skipped.
Allocation-free: a fixed offset table and an 8-slot inline “seen” list.
Sourcepub fn step(
&mut self,
scratch: &mut Grid<T>,
rule: impl FnMut(&Grid<T>, i32, i32) -> T,
)
pub fn step( &mut self, scratch: &mut Grid<T>, rule: impl FnMut(&Grid<T>, i32, i32) -> T, )
One synchronous update: every cell of the next generation is
rule(&old, x, y), computed from the old grid only, then the buffers are
swapped (CORE-R36). Afterwards scratch holds the previous generation.
scratch is rebuilt to this grid’s size by clear-and-push, which keeps its
capacity, so after the first call the two buffers just ping-pong with no
allocation. This also means T needs no Clone/Default for the scratch.
Trait Implementations§
Source§impl<T> Default for Grid<T>
The 0×0 grid (CORE-R33). Does not allocate, so it is the natural initial value
for a scene field or a Grid::step scratch buffer before the first resize.
impl<T> Default for Grid<T>
The 0×0 grid (CORE-R33). Does not allocate, so it is the natural initial value
for a scene field or a Grid::step scratch buffer before the first resize.