pub struct VecGrid<T> { /* private fields */ }
Expand description
A grid that stores its elements in a Vec<T>
, in row-major order.
Implementations§
Source§impl<T> VecGrid<T>
impl<T> VecGrid<T>
Sourcepub fn new_fill_with(
dimensions: impl VectorLike,
gen: impl Fn() -> T,
) -> Option<Self>
pub fn new_fill_with( 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.
Sourcepub fn new_with(
dimensions: impl VectorLike,
gen: impl Fn(Location) -> T,
) -> Option<Self>
pub fn new_with( 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());
Sourcepub fn new_row_major(
dimensions: impl VectorLike,
input: impl IntoIterator<Item = T>,
) -> Option<Self>
pub fn new_row_major( 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
un-iterated.
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);
Sourcepub fn new_from_rows<R, C>(rows: R) -> Option<Self>where
R: IntoIterator<Item = C>,
C: IntoIterator<Item = T>,
pub fn new_from_rows<R, C>(rows: R) -> Option<Self>where
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));
§Mismatched 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());
Sourcepub fn fill_with(&mut self, gen: impl Fn() -> T)
pub fn fill_with(&mut self, gen: impl Fn() -> T)
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());
Sourcepub fn fill_row_major(&mut self, input: impl IntoIterator<Item = T>)
pub fn fill_row_major(&mut self, input: impl IntoIterator<Item = T>)
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 un-iterated. 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);
Source§impl<T: Default> VecGrid<T>
impl<T: Default> VecGrid<T>
Sourcepub fn new(dimensions: impl VectorLike) -> Option<Self>
pub fn new(dimensions: impl VectorLike) -> Option<Self>
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());
Sourcepub fn new_row_major_default(
dimensions: impl VectorLike,
input: impl IntoIterator<Item = T>,
) -> Option<Self>
pub fn new_row_major_default( 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 un-iterated.
§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);
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
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());
Source§impl<T: Clone> VecGrid<T>
impl<T: Clone> VecGrid<T>
Sourcepub fn new_fill(dimensions: impl VectorLike, value: &T) -> Option<Self>
pub fn new_fill(dimensions: impl VectorLike, value: &T) -> Option<Self>
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());
Sourcepub fn fill(&mut self, value: &T)
pub fn fill(&mut self, value: &T)
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());
Source§impl<T: Copy> VecGrid<T>
impl<T: Copy> VecGrid<T>
Sourcepub fn new_fill_copied(dimensions: impl VectorLike, value: T) -> Option<Self>
pub fn new_fill_copied(dimensions: impl VectorLike, value: T) -> Option<Self>
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§
Source§impl<T> Grid for VecGrid<T>
impl<T> Grid for VecGrid<T>
Source§unsafe fn get_unchecked(&self, location: Location) -> &T
unsafe fn get_unchecked(&self, location: Location) -> &T
get
operations on the underlying storage,
where relevant / possible. Read moreSource§fn get(&self, location: impl LocationLike) -> Result<&Self::Item, BoundsError>
fn get(&self, location: impl LocationLike) -> Result<&Self::Item, BoundsError>
Source§fn view<T>(&self) -> View<'_, Self, T>where
T: Component,
fn view<T>(&self) -> View<'_, Self, T>where
T: Component,
[View]
for details.Source§fn columns(&self) -> View<'_, Self, Column>
fn columns(&self) -> View<'_, Self, Column>
[View]
for details.Source§unsafe fn single_view_unchecked<T>(&self, index: T) -> SingleView<'_, Self, T>where
T: Component,
unsafe fn single_view_unchecked<T>(&self, index: T) -> SingleView<'_, Self, T>where
T: Component,
Source§unsafe fn row_unchecked(&self, row: Row) -> SingleView<'_, Self, Row>
unsafe fn row_unchecked(&self, row: Row) -> SingleView<'_, Self, Row>
Source§unsafe fn column_unchecked(
&self,
column: Column,
) -> SingleView<'_, Self, Column>
unsafe fn column_unchecked( &self, column: Column, ) -> SingleView<'_, Self, Column>
Source§fn single_view<T>(
&self,
index: T,
) -> Result<SingleView<'_, Self, T>, RangeError<T>>where
T: Component,
fn single_view<T>(
&self,
index: T,
) -> Result<SingleView<'_, Self, T>, RangeError<T>>where
T: Component,
Source§fn row(
&self,
row: impl Into<Row>,
) -> Result<SingleView<'_, Self, Row>, RangeError<Row>>
fn row( &self, row: impl Into<Row>, ) -> Result<SingleView<'_, Self, Row>, RangeError<Row>>
Source§fn column(
&self,
column: impl Into<Column>,
) -> Result<SingleView<'_, Self, Column>, RangeError<Column>>
fn column( &self, column: impl Into<Column>, ) -> Result<SingleView<'_, Self, Column>, RangeError<Column>>
Source§fn display_with<T, F>(&self, func: F) -> DisplayAdapter<&Self, F>
fn display_with<T, F>(&self, func: F) -> DisplayAdapter<&Self, F>
Source§impl<T> GridBounds for VecGrid<T>
impl<T> GridBounds for VecGrid<T>
Source§fn dimensions(&self) -> Vector
fn dimensions(&self) -> Vector
Vector
. The dimensions of
must be >= 0.Source§fn outer_bound(&self) -> Location
fn outer_bound(&self) -> Location
row < outer.row && column < outer.column
Source§fn num_columns(&self) -> Columns
fn num_columns(&self) -> Columns
Columns
.Source§fn root_row(&self) -> Row
fn root_row(&self) -> Row
Source§fn root_column(&self) -> Column
fn root_column(&self) -> Column
Source§fn root_component<C>(&self) -> Cwhere
C: Component,
fn root_component<C>(&self) -> Cwhere
C: Component,
Source§fn row_range(&self) -> ComponentRange<Row>
fn row_range(&self) -> ComponentRange<Row>
Row
indexes in this gridSource§fn column_range(&self) -> ComponentRange<Column>
fn column_range(&self) -> ComponentRange<Column>
Column
indexes in this gridSource§fn range<C>(&self) -> ComponentRange<C>where
C: Component,
fn range<C>(&self) -> ComponentRange<C>where
C: Component,
Source§fn check_component<C>(&self, c: C) -> Result<C, RangeError<C>>where
C: Component,
fn check_component<C>(&self, c: C) -> Result<C, RangeError<C>>where
C: Component,
Source§fn check_row(&self, row: impl Into<Row>) -> Result<Row, RangeError<Row>>
fn check_row(&self, row: impl Into<Row>) -> Result<Row, RangeError<Row>>
Row
is inside the bounds described by this grid.
Returns the component if it’s inside the bounds, or an error
describing the violated boundary if not. This function is intended
to help write more expressive code; ie,
grid.check_row(10).and_then(|row| ...)
.Source§fn check_column(
&self,
column: impl Into<Column>,
) -> Result<Column, RangeError<Column>>
fn check_column( &self, column: impl Into<Column>, ) -> Result<Column, RangeError<Column>>
Column
is inside the bounds described by this grid.
Returns the component if it’s inside the bounds, or an error
describing the violated boundary if not. This function is intended
to help write more expressive code; ie,
grid.check_column(10).and_then(|row| ...)
.Source§fn component_in_bounds<C>(&self, c: C) -> boolwhere
C: Component,
fn component_in_bounds<C>(&self, c: C) -> boolwhere
C: Component,
Source§fn row_in_bounds(&self, row: impl Into<Row>) -> bool
fn row_in_bounds(&self, row: impl Into<Row>) -> bool
Row
is inside the bounds described
by this grid.Source§fn column_in_bounds(&self, column: impl Into<Column>) -> bool
fn column_in_bounds(&self, column: impl Into<Column>) -> bool
Column
is inside the bounds described
by this grid.Source§fn check_location(
&self,
location: impl LocationLike,
) -> Result<Location, BoundsError>
fn check_location( &self, location: impl LocationLike, ) -> Result<Location, BoundsError>
Source§fn location_in_bounds(&self, location: impl LocationLike) -> bool
fn location_in_bounds(&self, location: impl LocationLike) -> bool
Source§impl<T> GridMut for VecGrid<T>
impl<T> GridMut for VecGrid<T>
Source§unsafe fn get_unchecked_mut(&mut self, location: Location) -> &mut T
unsafe fn get_unchecked_mut(&mut self, location: Location) -> &mut T
get_mut
operations
on the underlying storage, where relevant / possible. Read moreSource§fn get_mut(
&mut self,
location: impl LocationLike,
) -> Result<&mut Self::Item, BoundsError>
fn get_mut( &mut self, location: impl LocationLike, ) -> Result<&mut Self::Item, BoundsError>
Source§impl<T> GridSetter for VecGrid<T>
impl<T> GridSetter for VecGrid<T>
Source§unsafe fn replace_unchecked(&mut self, location: Location, value: T) -> T
unsafe fn replace_unchecked(&mut self, location: Location, value: T) -> T
location
with value
, without
bounds checking location
. Returns the previous value in the grid. Read moreSource§unsafe fn set_unchecked(&mut self, location: Location, value: T)
unsafe fn set_unchecked(&mut self, location: Location, value: T)
Source§fn replace(
&mut self,
location: impl LocationLike,
value: Self::Item,
) -> Result<Self::Item, BoundsError>
fn replace( &mut self, location: impl LocationLike, value: Self::Item, ) -> Result<Self::Item, BoundsError>
location
with value
. Returns the
previous value in the grid, or an error if the location was out of
bounds.Source§fn set(
&mut self,
location: impl LocationLike,
value: Self::Item,
) -> Result<(), BoundsError>
fn set( &mut self, location: impl LocationLike, value: Self::Item, ) -> Result<(), BoundsError>
location
in the grid. Returns an error
if the location was out of bounds.