Skip to main content

Grid

Struct Grid 

Source
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>

Source

pub fn new(size: Size, fill: T) -> Self

A size grid with every cell set to fill (CORE-R33).

Source

pub fn fill(&mut self, v: T)

Set every cell to v (CORE-R33).

Source

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>

Source

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.

Source

pub fn width(&self) -> u16

Number of columns; 0 only for the empty grid (CORE-R33).

Source

pub fn height(&self) -> u16

Number of rows; 0 only for the empty grid (CORE-R33).

Source

pub fn len(&self) -> usize

Number of cells, width * height.

Source

pub fn is_empty(&self) -> bool

Source

pub fn get(&self, x: i32, y: i32) -> Option<&T>

The cell at (x, y), or None off-grid; never panics (CORE-R33).

Source

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).

Source

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).

Source

pub fn iter(&self) -> Iter<'_, T> ⓘ

Every cell with its coordinates, in row-major order (CORE-R33).

Source

pub fn as_slice(&self) -> &[T]

The row-major buffer: cell (x, y) is at y * width + x (CORE-R33).

Source

pub fn as_mut_slice(&mut self) -> &mut [T]

Mutable row-major buffer, for bulk per-cell updates that need no coordinates.

Source

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.

Source

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: Clone> Clone for Grid<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for Grid<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
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.

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T: Eq> Eq for Grid<T>

Source§

impl<T: PartialEq> PartialEq for Grid<T>

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl<T: PartialEq> StructuralPartialEq for Grid<T>

Auto Trait Implementations§

§

impl<T> Forget for Grid<T>

§

impl<T> Freeze for Grid<T>
where Vec<T>: Freeze,

§

impl<T> Move for Grid<T>

§

impl<T> RefUnwindSafe for Grid<T>
where Vec<T>: RefUnwindSafe,

§

impl<T> Send for Grid<T>
where Vec<T>: Send,

§

impl<T> Sync for Grid<T>
where Vec<T>: Sync,

§

impl<T> Unpin for Grid<T>
where Vec<T>: Unpin,

§

impl<T> UnsafeUnpin for Grid<T>
where Vec<T>: UnsafeUnpin,

§

impl<T> UnwindSafe for Grid<T>
where Vec<T>: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V