[−][src]Struct gridly_grids::VecGrid
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]
dimensions: impl VectorLike,
gen: impl Fn() -> T
) -> Option<Self>
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]
dimensions: impl VectorLike,
gen: impl Fn(Location) -> T
) -> Option<Self>
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]
dimensions: impl VectorLike,
input: impl IntoIterator<Item = T>
) -> Option<Self>
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]
R: IntoIterator<Item = C>,
C: IntoIterator<Item = T>,
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]
dimensions: impl VectorLike,
input: impl IntoIterator<Item = T>
) -> Option<Self>
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
unsafe fn get_unchecked(&self, location: Location) -> &T
[src]
fn get(&self, location: impl LocationLike) -> Result<&Self::Item, BoundsError>
[src]
fn view<T>(&self) -> View<'_, Self, T> where
T: Component,
[src]
T: Component,
fn rows(&self) -> View<'_, Self, Row>
[src]
fn columns(&self) -> View<'_, Self, Column>
[src]
unsafe fn single_view_unchecked<T>(&self, index: T) -> SingleView<'_, Self, T> where
T: Component,
[src]
T: Component,
unsafe fn row_unchecked(&self, row: Row) -> SingleView<'_, Self, Row>
[src]
unsafe fn column_unchecked(
&self,
column: Column
) -> SingleView<'_, Self, Column>
[src]
&self,
column: Column
) -> SingleView<'_, Self, Column>
fn single_view<T>(
&self,
index: T
) -> Result<SingleView<'_, Self, T>, RangeError<T>> where
T: Component,
[src]
&self,
index: T
) -> Result<SingleView<'_, Self, T>, RangeError<T>> where
T: Component,
fn row(
&self,
row: impl Into<Row>
) -> Result<SingleView<'_, Self, Row>, RangeError<Row>>
[src]
&self,
row: impl Into<Row>
) -> Result<SingleView<'_, Self, Row>, RangeError<Row>>
fn column(
&self,
column: impl Into<Column>
) -> Result<SingleView<'_, Self, Column>, RangeError<Column>>
[src]
&self,
column: impl Into<Column>
) -> Result<SingleView<'_, Self, Column>, RangeError<Column>>
fn display_with<T, F>(&self, func: F) -> DisplayAdapter<&Self, F> where
F: Fn(&Self::Item) -> T,
T: Display,
[src]
F: Fn(&Self::Item) -> T,
T: Display,
impl<T> GridBounds for VecGrid<T>
[src]
fn dimensions(&self) -> Vector
[src]
fn root(&self) -> Location
[src]
#[must_use]fn outer_bound(&self) -> Location
[src]
#[must_use]fn num_rows(&self) -> Rows
[src]
#[must_use]fn num_columns(&self) -> Columns
[src]
#[must_use]fn dimension<C>(&self) -> C where
C: Component,
[src]
C: Component,
#[must_use]fn root_row(&self) -> Row
[src]
#[must_use]fn root_column(&self) -> Column
[src]
#[must_use]fn root_component<C>(&self) -> C where
C: Component,
[src]
C: Component,
#[must_use]fn row_range(&self) -> ComponentRange<Row>
[src]
#[must_use]fn column_range(&self) -> ComponentRange<Column>
[src]
#[must_use]fn range<C>(&self) -> ComponentRange<C> where
C: Component,
[src]
C: Component,
fn check_component<C>(&self, c: C) -> Result<C, RangeError<C>> where
C: Component,
[src]
C: Component,
fn check_row(&self, row: impl Into<Row>) -> Result<Row, RangeError<Row>>
[src]
fn check_column(
&self,
column: impl Into<Column>
) -> Result<Column, RangeError<Column>>
[src]
&self,
column: impl Into<Column>
) -> Result<Column, RangeError<Column>>
#[must_use]fn component_in_bounds<C>(&self, c: C) -> bool where
C: Component,
[src]
C: Component,
#[must_use]fn row_in_bounds(&self, row: impl Into<Row>) -> bool
[src]
#[must_use]fn column_in_bounds(&self, column: impl Into<Column>) -> bool
[src]
fn check_location(
&self,
location: impl LocationLike
) -> Result<Location, BoundsError>
[src]
&self,
location: impl LocationLike
) -> Result<Location, BoundsError>
#[must_use]fn location_in_bounds(&self, location: impl LocationLike) -> bool
[src]
impl<T> GridMut for VecGrid<T>
[src]
unsafe fn get_unchecked_mut(&mut self, location: Location) -> &mut T
[src]
fn get_mut(
&mut self,
location: impl LocationLike
) -> Result<&mut Self::Item, BoundsError>
[src]
&mut self,
location: impl LocationLike
) -> Result<&mut Self::Item, BoundsError>
impl<T> GridSetter for VecGrid<T>
[src]
unsafe fn replace_unchecked(&mut self, location: Location, value: T) -> T
[src]
unsafe fn set_unchecked(&mut self, location: Location, value: T)
[src]
fn replace(
&mut self,
location: impl LocationLike,
value: Self::Item
) -> Result<Self::Item, BoundsError>
[src]
&mut self,
location: impl LocationLike,
value: Self::Item
) -> Result<Self::Item, BoundsError>
fn set(
&mut self,
location: impl LocationLike,
value: Self::Item
) -> Result<(), BoundsError>
[src]
&mut self,
location: impl LocationLike,
value: Self::Item
) -> Result<(), BoundsError>
impl<T, L: LocationLike> Index<L> for VecGrid<T>
[src]
impl<T, L: LocationLike> IndexMut<L> for VecGrid<T>
[src]
Auto Trait Implementations
impl<T> RefUnwindSafe for VecGrid<T> where
T: RefUnwindSafe,
T: RefUnwindSafe,
impl<T> Send for VecGrid<T> where
T: Send,
T: Send,
impl<T> Sync for VecGrid<T> where
T: Sync,
T: Sync,
impl<T> Unpin for VecGrid<T> where
T: Unpin,
T: Unpin,
impl<T> UnwindSafe for VecGrid<T> where
T: UnwindSafe,
T: UnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
pub fn to_owned(&self) -> T
[src]
pub fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,