[][src]Struct gridly_grids::VecGrid

pub struct VecGrid<T> { /* fields omitted */ }

A grid that stores its elements in a Vec<T>, in row-major order.

Methods

impl<T> VecGrid<T>[src]

pub fn new_fill_with(
    dimensions: impl VectorLike,
    gen: impl Fn() -> T
) -> Option<Self>
[src]

Create a new VecGrid, filled with elements by repeatedly calling a function. The function is called once per cell in an unspecified order; use [new_with] if you want to have per-cell initialization logic.

Returns the grid, or None if the dimensions were invalid.

Example:

use gridly_grids::VecGrid;
use gridly::prelude::*;

let grid = VecGrid::new_fill_with((Rows(2), Columns(2)), || "Hello, World!".to_string()).unwrap();
assert_eq!(grid[(1, 0)], "Hello, World!")

See also [new] for filling a grid with a type's [default] value, and [new_fill] for filling a grid with a clone of a value.

pub fn new_with(
    dimensions: impl VectorLike,
    gen: impl FnMut(&Location) -> T
) -> Option<Self>
[src]

Create a new VecGrid by calling a function with the location of each cell in the grid, storing the return value of that function in that cell.

The function is called once per cell in an unspecified order; users should not rely on it being called in row-major order.

Returns the grid, or None if the dimensions were invalid.

Example:

use gridly_grids::VecGrid;
use gridly::prelude::*;

let grid = VecGrid::new_with((Rows(2), Columns(2)), |loc| loc.row.0 + loc.column.0).unwrap();
assert_eq!(grid.get((0, 0)), Ok(&0));
assert_eq!(grid.get((0, 1)), Ok(&1));
assert_eq!(grid.get((1, 0)), Ok(&1));
assert_eq!(grid.get((1, 1)), Ok(&2));
assert!(grid.get((1, 2)).is_err());

pub fn fill_with(&mut self, gen: impl Fn() -> T)[src]

Fill every cell in the grid with the values produced by repeatedly calling gen. Called in an unspecified order.

Example

use gridly_grids::VecGrid;
use gridly::prelude::*;

let mut grid: VecGrid<isize> = VecGrid::new((Rows(2), Columns(2))).unwrap();

grid.fill_with(|| 3);
assert_eq!(grid.get((0, 0)), Ok(&3));
assert_eq!(grid.get((0, 1)), Ok(&3));
assert_eq!(grid.get((1, 0)), Ok(&3));
assert_eq!(grid.get((1, 1)), Ok(&3));
assert!(grid.get((1, 2)).is_err());

impl<T: Default> VecGrid<T>[src]

pub fn new(dimensions: impl VectorLike) -> Option<Self>[src]

Create a new VecGrid filled with the default value of T in each cell

Example

use gridly_grids::VecGrid;
use gridly::prelude::*;

let grid: VecGrid<isize> = VecGrid::new((Rows(2), Columns(2))).unwrap();
assert_eq!(grid.get((0, 0)), Ok(&0));
assert_eq!(grid.get((0, 1)), Ok(&0));
assert_eq!(grid.get((1, 0)), Ok(&0));
assert_eq!(grid.get((1, 1)), Ok(&0));
assert!(grid.get((1, 2)).is_err());

pub fn clear(&mut self)[src]

Replace all the cells in the grid with the default value

Example

use gridly_grids::VecGrid;
use gridly::prelude::*;

let mut grid = VecGrid::new_fill((Rows(2), Columns(2)), &5).unwrap();
grid.clear();
assert_eq!(grid.get((0, 0)), Ok(&0));
assert_eq!(grid.get((0, 1)), Ok(&0));
assert_eq!(grid.get((1, 0)), Ok(&0));
assert_eq!(grid.get((1, 1)), Ok(&0));
assert!(grid.get((1, 2)).is_err());

impl<T: Clone> VecGrid<T>[src]

pub fn new_fill(dimensions: impl VectorLike, value: &T) -> Option<Self>[src]

Create a new VecGrid filled with clones of value

Example

use gridly_grids::VecGrid;
use gridly::prelude::*;

let grid = VecGrid::new_fill((Rows(2), Columns(2)), &"Hello").unwrap();
assert_eq!(grid.get((0, 0)), Ok(&"Hello"));
assert_eq!(grid.get((0, 1)), Ok(&"Hello"));
assert_eq!(grid.get((1, 0)), Ok(&"Hello"));
assert_eq!(grid.get((1, 1)), Ok(&"Hello"));
assert!(grid.get((1, 2)).is_err());

pub fn fill(&mut self, value: &T)[src]

Fill every element in the grid with clones of value

Example

use gridly_grids::VecGrid;
use gridly::prelude::*;

let mut grid = VecGrid::new((Rows(2), Columns(2))).unwrap();

grid.fill(&"Hello");
assert_eq!(grid.get((0, 0)), Ok(&"Hello"));
assert_eq!(grid.get((0, 1)), Ok(&"Hello"));
assert_eq!(grid.get((1, 0)), Ok(&"Hello"));
assert_eq!(grid.get((1, 1)), Ok(&"Hello"));
assert!(grid.get((1, 2)).is_err());

Trait Implementations

impl<T: Clone> Clone for VecGrid<T>[src]

impl<T, L: LocationLike> Index<L> for VecGrid<T>[src]

type Output = T

The returned type after indexing.

impl<T, L: LocationLike> IndexMut<L> for VecGrid<T>[src]

impl<T: Debug> Debug for VecGrid<T>[src]

impl<T> BaseGridBounds for VecGrid<T>[src]

impl<T> BaseGridSetter for VecGrid<T>[src]

impl<T> BaseGrid for VecGrid<T>[src]

type Item = T

impl<T> BaseGridMut for VecGrid<T>[src]

Auto Trait Implementations

impl<T> Send for VecGrid<T> where
    T: Send

impl<T> Unpin for VecGrid<T> where
    T: Unpin

impl<T> Sync for VecGrid<T> where
    T: Sync

impl<T> UnwindSafe for VecGrid<T> where
    T: UnwindSafe

impl<T> RefUnwindSafe for VecGrid<T> where
    T: RefUnwindSafe

Blanket Implementations

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> From<T> for T[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<G> GridBounds for G where
    G: BaseGridBounds
[src]

impl<G> GridSetter for G where
    G: BaseGridSetter,
    <G as BaseGrid>::Item: Sized
[src]

impl<G> Grid for G where
    G: BaseGrid
[src]

impl<G> GridMut for G where
    G: BaseGridMut
[src]