pub struct Grid {
    pub numbers: Vec<u8>,
    pub width: usize,
}
Expand description

A representation of a 2D grid of u8s.

Fields§

§numbers: Vec<u8>

Store the numbers in a 1D list…

§width: usize

…and use the width to determine the 1D offset as a 2D co-ordinate

Implementations§

source§

impl Grid

source

pub fn new<F>(width: usize, height: usize, init: F) -> Selfwhere F: Fn(usize, usize) -> u8,

Build a new grid with a mapping function to generate the date for each cell

source

pub fn from_string_with_mapping( input: &String, mapping: fn(_: char) -> u8 ) -> Self

source

pub fn iter(&self) -> GridCoords<'_>

Helper to abstract iterating over the whole grid

source

pub fn get(&self, y: usize, x: usize) -> Option<u8>

Return the value at the given co-ordinates

source

pub fn set(&mut self, y: usize, x: usize, val: u8) -> bool

Update the value in a given cell

source

pub fn pos_of(&self, (y, x): (usize, usize)) -> Option<usize>

Turn (y, x) coordinates into a position in the underlying array

source

pub fn height(&self) -> usize

Calculate the height from the

source

pub fn sum(&self) -> usize

Sum the cells in the grid

source

pub fn get_with_coords(&self, pos: usize) -> Option<((usize, usize), u8)>

source

pub fn get_orthogonal_surrounds( &self, (y, x): (usize, usize) ) -> Vec<((usize, usize), u8)>

source

pub fn get_relative( &self, y: usize, x: usize, dy: isize, dx: isize ) -> Option<((usize, usize), u8)>

source

pub fn print(&self) -> String

Dump the grid to stdout - useful for visualising the grid when debugging

source

pub fn print_with<F>(&self, cell_renderer: F) -> Stringwhere F: Fn(u8) -> String,

Trait Implementations§

source§

impl Clone for Grid

source§

fn clone(&self) -> Grid

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl Debug for Grid

source§

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

Formats the value using the given formatter. Read more
source§

impl From<String> for Grid

source§

fn from(string: String) -> Self

Turn the characters into digits and concatenate, caching the width

source§

impl PartialEq<Grid> for Grid

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Eq for Grid

source§

impl StructuralEq for Grid

source§

impl StructuralPartialEq for Grid

Auto Trait Implementations§

§

impl RefUnwindSafe for Grid

§

impl Send for Grid

§

impl Sync for Grid

§

impl Unpin for Grid

§

impl UnwindSafe for Grid

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. 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 Twhere 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> ToOwned for Twhere T: Clone,

§

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 Twhere U: Into<T>,

§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

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

§

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.