[][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.

Implementations

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 Fn(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 new_row_major(
    dimensions: impl VectorLike,
    input: impl IntoIterator<Item = T>
) -> Option<Self>
[src]

Create a new VecGrid with the given dimensions. Fills the grid in row-major order (that is, by filling the first row, then the next row, etc) by evaluating the input iterator. Returns None if the iterator was too short, or if the dimensions were invalid. If the iterator is longer than required, the remaining elements are left uniterated.

This method will return prematurely if the iterator reports (via size_hint) that it is definitely too short.

Example:

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

let data = [1, 2, 3, 4, 5];
let mut data_iter = data[..].iter().copied();

let grid = VecGrid::new_row_major(
    (Rows(2), Columns(2)),
    &mut data_iter
).unwrap();

assert_eq!(grid[(0, 0)], 1);
assert_eq!(grid[(0, 1)], 2);
assert_eq!(grid[(1, 0)], 3);
assert_eq!(grid[(1, 1)], 4);

// excess elements in the iterator are still available
assert_eq!(data_iter.next(), Some(5));
assert_eq!(data_iter.next(), None);

pub fn new_from_rows<R, C>(rows: R) -> Option<Self> where
    R: IntoIterator<Item = C>,
    C: IntoIterator<Item = T>, 
[src]

Create a new VecGrid from an iterator of rows. Each row should be an iterator of items in the row. The dimensions are deduced automatically from the number of rows and columns. Returns None If the number of columns is mismatched between any two rows. If the rows iterator is empty, the returned dimensions are (0, 0).

Examples

Basic example

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

let rows = vec![
    vec![1, 2, 3],
    vec![4, 5, 6],
    vec![7, 8, 9],
];

let grid = VecGrid::new_from_rows(&rows).unwrap();

assert_eq!(grid[(0, 0)], &1);
assert_eq!(grid[(1, 1)], &5);
assert_eq!(grid[(2, 2)], &9);

Empty grid example

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

// Note that, even though the column width here is 5, no rows
// are ever iterated, so VecGrid is forced to assume (0, 0)
// dimensions.
let rows: [[isize; 5]; 0] = [];

let grid = VecGrid::new_from_rows(&rows).unwrap();

assert_eq!(grid.dimensions(), (0, 0));

Mistmatched row length example

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

let rows = vec![
    vec![1, 2, 3],
    vec![4, 5],
];

let grid = VecGrid::new_from_rows(&rows);

assert!(grid.is_none());

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());

pub fn fill_row_major(&mut self, input: impl IntoIterator<Item = T>)[src]

Fill the grid in row-major order with values from an iterator. That is, fill the first row, then the next row, etc.

If the iterator is longer than the volume of the grid, the remaining elements are left uniterated. If the iterator is shorter than the volume of the grid, the remaining existing elements in the grid are left unaltered.

Example

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

let mut grid = VecGrid::new_fill((Rows(2), Columns(2)), &10).unwrap();
let values = [1, 2, 3];
let values_iter = values[..].iter().copied();

grid.fill_row_major(values_iter);

assert_eq!(grid[(0, 0)], 1);
assert_eq!(grid[(0, 1)], 2);
assert_eq!(grid[(1, 0)], 3);
assert_eq!(grid[(1, 1)], 10);

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 new_row_major_default(
    dimensions: impl VectorLike,
    input: impl IntoIterator<Item = T>
) -> Option<Self>
[src]

Create a new VecGrid with the given dimensions. Fills the grid in row-major order (that is, by filling the first row, then the next row, etc) by evaluating the input iterator. Returns None if the dimensions were invalid. If the iterator is shorter than required to fill the grid, the remaining elements are filled with T::default. If the iterator is longer than required, the remaining elements are left uniterated.

Example:

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

let data = [1, 2, 3];
let data_iter = data[..].iter().copied();

let grid = VecGrid::new_row_major_default(
    (Rows(2), Columns(2)),
    data_iter
).unwrap();

assert_eq!(grid[(0, 0)], 1);
assert_eq!(grid[(0, 1)], 2);
assert_eq!(grid[(1, 0)], 3);
assert_eq!(grid[(1, 1)], 0);

pub fn clear(&mut self)[src]

Replace all the cells in the grid with the default value. Doesn't change the dimensions of the grid.

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());

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

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

Create a new VecGrid filled with copies of value.

Example

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

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

Trait Implementations

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

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

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

type Item = T

The item type stored in the grid

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

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

impl<T> GridSetter 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]

Auto Trait Implementations

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

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

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

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

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

Blanket Implementations

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

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

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

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

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

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

type Owned = T

The resulting type after obtaining ownership.

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.