Struct simple_grid::Grid
source · #[non_exhaustive]pub struct Grid<T> { /* private fields */ }
Expand description
A two-dimensional array, indexed with x-and-y-coordinates (columns and rows).
Implementations§
source§impl<T> Grid<T>
impl<T> Grid<T>
sourcepub fn from_grid<U>(other: Grid<U>) -> Grid<T>where
T: From<U>,
pub fn from_grid<U>(other: Grid<U>) -> Grid<T>where
T: From<U>,
Construct a Grid
from another, by converting each element.
Example
let u32_grid: Grid<u32> = Grid::new(2, 2, vec![1, 2, 3, 4]);
let f64_grid: Grid<f64> = Grid::from_grid(u32_grid);
assert_eq!(f64_grid, Grid::new(2, 2, vec![1.0, 2.0, 3.0, 4.0]));
sourcepub fn subgrid(
self,
column_start: usize,
row_start: usize,
width: usize,
height: usize
) -> Grid<T>
pub fn subgrid( self, column_start: usize, row_start: usize, width: usize, height: usize ) -> Grid<T>
Consumes the Grid
, creating a new one from a subset of the original.
Arguments
column_start
- Left bound for the subgrid.row_start
- Upper bound for the subgrid.width
- Number of columns in the subgrid.height
- Number of rows in the subgrid.
Panics
- If
width
orheight
(but not both) are 0. If both are 0, the resulting subgrid will be an empty (0 by 0)Grid
. - If
column_start
is out of bounds. - If
row_start
is out of bounds.
Example
let original: Grid<u32> = Grid::new(3, 3, (1..=9).collect());
let subgrid = original.subgrid(1, 0, 2, 2);
assert_eq!(subgrid, Grid::new(2, 2, vec![2, 3, 5, 6]));
sourcepub fn dimensions(&self) -> (usize, usize)
pub fn dimensions(&self) -> (usize, usize)
Returns a tuple containing the (width, height) of the grid.
Example
let grid = Grid::new(2, 3, vec![1, 2, 3, 4, 5, 6]);
assert_eq!(grid.dimensions(), (2, 3));
sourcepub fn is_square(&self) -> bool
pub fn is_square(&self) -> bool
Checks if the Grid is square (number of columns and rows is equal).
Note: an empty Grid is not square (even though columns and rows is 0).
Example
let grid = Grid::new(2, 2, vec![1, 2, 3, 4]);
assert!(grid.is_square());
let grid = Grid::new(2, 3, vec![1, 2, 3, 4, 5, 6]);
assert!(!grid.is_square());
sourcepub fn area(&self) -> usize
pub fn area(&self) -> usize
Returns the area (number of columns * number of rows) of the grid.
Example
let grid = Grid::new(2, 3, vec![2, 4, 8, 16, 32, 64]);
assert_eq!(grid.area(), 6);
sourcepub fn get<I>(&self, idx: I) -> Option<&T>
pub fn get<I>(&self, idx: I) -> Option<&T>
Attempts to get a reference to the element at idx
.
Returns None
if idx
is out of bounds.
Example
let grid = Grid::new(2, 3, vec![2, 4, 8, 16, 32, 64]);
assert_eq!(grid.get((1, 1)), Some(&16));
assert!(grid.get((10, 15)).is_none());
sourcepub fn get_mut<I>(&mut self, idx: I) -> Option<&mut T>
pub fn get_mut<I>(&mut self, idx: I) -> Option<&mut T>
Attempts to get a mutable reference to the element at idx
Returns None
if idx
is out of bounds.
sourcepub fn cell_iter(&self) -> impl DoubleEndedIterator<Item = &T>
pub fn cell_iter(&self) -> impl DoubleEndedIterator<Item = &T>
Return an iterator over the cells in the grid.
Iterates from left to right (starting with row 0, then row 1 etc.).
sourcepub fn row_iter(&self, row: usize) -> impl DoubleEndedIterator<Item = &T>
pub fn row_iter(&self, row: usize) -> impl DoubleEndedIterator<Item = &T>
sourcepub fn column_iter(&self, column: usize) -> impl DoubleEndedIterator<Item = &T>
pub fn column_iter(&self, column: usize) -> impl DoubleEndedIterator<Item = &T>
Return an iterator over the rows in the column with index column
.
Panics
- If
column >= self.width
Example
let grid = Grid::new(10, 10, (1..=100).collect());
let items_in_column_2: Vec<u32> = grid.column_iter(2).cloned().collect();
assert_eq!(items_in_column_2, vec![3, 13, 23, 33, 43, 53, 63, 73, 83, 93]);
sourcepub fn insert_row(&mut self, row: usize, row_contents: Vec<T>)
pub fn insert_row(&mut self, row: usize, row_contents: Vec<T>)
Insert a row at index row
, shifting all other rows downward (row n
becomes row n+1
and so on).
Panics
- If
row_contents.is_empty()
- If
row_contents.len() != self.width
- If
row > self.height
(note thatrow == self.height
is allowed, to add a row at the bottom of theGrid
)
Example
let mut grid = Grid::new(2, 2, "abcd".chars().collect());
grid.insert_row(1, "xx".chars().collect());
assert_eq!(grid, Grid::new(2, 3, "abxxcd".chars().collect()));
println!("{}", grid.to_pretty_string());
// prints:
// a b
// x x
// c d
sourcepub fn push_row(&mut self, row_contents: Vec<T>)
pub fn push_row(&mut self, row_contents: Vec<T>)
Add a row to the bottom of the Grid
.
Example
let mut grid = Grid::new(2, 2, "abcd".chars().collect());
grid.push_row(vec!['x', 'x']);
assert_eq!(grid, Grid::new(2, 3, "abcdxx".chars().collect()));
println!("{}", grid.to_pretty_string());
// prints
// a b
// c d
// x x
sourcepub fn replace_row(&mut self, row: usize, data: Vec<T>) -> Vec<T>
pub fn replace_row(&mut self, row: usize, data: Vec<T>) -> Vec<T>
Replace the contents in a row.
Returns the old elements of the row.
Panics
- If
row >= self.height
- If
data.len() != self.width
sourcepub fn remove_row(&mut self, row: usize) -> Vec<T>
pub fn remove_row(&mut self, row: usize) -> Vec<T>
Remove row at row
, shifting all rows with higher indices “upward” (row n
becomes row n-1
).
Returns the row that was removed.
Panics
- If
row >= self.height
Example
let mut grid = Grid::new(2, 2, "abcd".chars().collect());
grid.remove_row(1);
assert_eq!(grid, Grid::new(2, 1, "ab".chars().collect()));
println!("{}", grid.to_pretty_string());
// prints:
// a b
sourcepub fn pop_row(&mut self) -> Option<Vec<T>>
pub fn pop_row(&mut self) -> Option<Vec<T>>
Remove the bottom row, returning it (if it exists).
Returns None
if the height of the Grid
is zero.
Example
let mut grid = Grid::new(2, 2, "abcd".chars().collect());
let bottom_row = grid.pop_row();
assert_eq!(bottom_row, Some(vec!['c', 'd']));
assert_eq!(grid, Grid::new(2, 1, "ab".chars().collect()));
sourcepub fn insert_column(&mut self, column: usize, column_contents: Vec<T>)
pub fn insert_column(&mut self, column: usize, column_contents: Vec<T>)
Insert a column at index column
, shifting all other columns to the right (column n
becomes column n+1
and so on).
Panics
- If
column_contents.is_empty()
- If
column_contents.len() != self.height
- If
column > self.width
(note thatcolumn == self.width
is allowed, to add a column at the right of theGrid
)
Example
let mut grid = Grid::new(2, 2, "abcd".chars().collect());
grid.insert_column(1, "xx".chars().collect());
assert_eq!(grid, Grid::new(3, 2, "axbcxd".chars().collect()));
println!("{}", grid.to_pretty_string());
// prints:
// a x b
// c x d
sourcepub fn push_column(&mut self, column_contents: Vec<T>)
pub fn push_column(&mut self, column_contents: Vec<T>)
Add a column to the right of the Grid
.
Example
let mut grid = Grid::new(2, 2, "abcd".chars().collect());
grid.push_column(vec!['x', 'x']);
assert_eq!(grid, Grid::new(3, 2, "abxcdx".chars().collect()));
println!("{}", grid.to_pretty_string());
// prints
// a b x
// c d x
sourcepub fn replace_column(&mut self, column: usize, data: Vec<T>) -> Vec<T>
pub fn replace_column(&mut self, column: usize, data: Vec<T>) -> Vec<T>
Replace the contents in a column.
Returns the old elements of the column.
Panics
- If
column >= self.width
- If
data.len() != self.height
sourcepub fn swap_columns(&mut self, column1: usize, column2: usize)
pub fn swap_columns(&mut self, column1: usize, column2: usize)
sourcepub fn remove_column(&mut self, column: usize) -> Vec<T>
pub fn remove_column(&mut self, column: usize) -> Vec<T>
Remove column at column
, shifting all columns with higher indices “left” (column n
becomes column n-1
).
Returns the column that was removed.
Panics
- If
column >= self.width
Example
let mut grid = Grid::new(2, 2, "abcd".chars().collect());
grid.remove_column(1);
assert_eq!(grid, Grid::new(1, 2, "ac".chars().collect()));
println!("{}", grid.to_pretty_string());
// prints:
// a
// c
sourcepub fn pop_column(&mut self) -> Option<Vec<T>>
pub fn pop_column(&mut self) -> Option<Vec<T>>
Remove the rightmost column, returning it (if it exists).
Returns None
if the width of the Grid
is zero.
Example
let mut grid = Grid::new(2, 2, "abcd".chars().collect());
let rightmost_column = grid.pop_column();
assert_eq!(rightmost_column, Some(vec!['b', 'd']));
assert_eq!(grid, Grid::new(1, 2, "ac".chars().collect()));
sourcepub fn swap_cells<I>(&mut self, a: I, b: I)
pub fn swap_cells<I>(&mut self, a: I, b: I)
sourcepub fn replace_cell<I>(&mut self, idx: I, elem: T) -> T
pub fn replace_cell<I>(&mut self, idx: I, elem: T) -> T
Replace the contents in a cell.
Returns the old element of the cell.
Panics
- If
idx
is out of bounds.
sourcepub fn rotate_ccw(&mut self)
pub fn rotate_ccw(&mut self)
Rotate the grid counter-clockwise 90 degrees.
Example
let mut grid = Grid::new(2, 2, "abcd".chars().collect());
println!("{}", grid.to_pretty_string());
// prints:
// a b
// c d
grid.rotate_ccw();
assert_eq!(grid, Grid::new(2, 2, "bdac".chars().collect()));
println!("{}", grid.to_pretty_string());
// prints:
// b d
// a c
sourcepub fn rotate_cw(&mut self)
pub fn rotate_cw(&mut self)
Rotate the grid clockwise 90 degrees.
Example
let mut grid = Grid::new(2, 2, "abcd".chars().collect());
println!("{}", grid.to_pretty_string());
// prints:
// a b
// c d
grid.rotate_cw();
assert_eq!(grid, Grid::new(2, 2, "cadb".chars().collect()));
println!("{}", grid.to_pretty_string());
// prints:
// c a
// d b
sourcepub fn flip_horizontally(&mut self)
pub fn flip_horizontally(&mut self)
Flip the grid horizontally, so that the first column becomes the last.
Example
let mut grid = Grid::new(2, 2, "abcd".chars().collect());
println!("{}", grid.to_pretty_string());
// prints:
// a b
// c d
grid.flip_horizontally();
assert_eq!(grid, Grid::new(2, 2, "badc".chars().collect()));
println!("{}", grid.to_pretty_string());
// prints:
// b a
// d c
sourcepub fn flip_vertically(&mut self)
pub fn flip_vertically(&mut self)
Flip the grid vertically, so that the first row becomes the last.
Example
let mut grid = Grid::new(2, 2, "abcd".chars().collect());
println!("{}", grid.to_pretty_string());
// prints:
// a b
// c d
grid.flip_vertically();
assert_eq!(grid, Grid::new(2, 2, "cdab".chars().collect()));
println!("{}", grid.to_pretty_string());
// prints:
// c d
// a b
sourcepub fn transpose(&mut self)
pub fn transpose(&mut self)
Transpose the grid along the diagonal, so that cells at index (x, y) end up at index (y, x).
Example
let mut grid = Grid::new(2, 3, "abcdef".chars().collect());
println!("{}", grid.to_pretty_string());
// prints:
// a b
// c d
// e f
grid.transpose();
assert_eq!(grid, Grid::new(3, 2, "acebdf".chars().collect()));
println!("{}", grid.to_pretty_string());
// prints:
// a c e
// b d f
sourcepub fn rows(&self) -> impl DoubleEndedIterator<Item = usize>
pub fn rows(&self) -> impl DoubleEndedIterator<Item = usize>
Return an iterator over the row indices in this grid. Allows you to write for row in grid.rows()
instead of for row in 0..grid.height()
.
Example
let grid: Grid<u32> = Grid::new_default(3, 5);
let rows: Vec<usize> = grid.rows().collect();
assert_eq!(rows, vec![0, 1, 2, 3, 4]);
sourcepub fn columns(&self) -> impl DoubleEndedIterator<Item = usize>
pub fn columns(&self) -> impl DoubleEndedIterator<Item = usize>
Return an iterator over the column indices in this grid. Allows you to write for column in grid.columns()
instead of for column in 0..grid.width()
.
Example
let grid: Grid<u32> = Grid::new_default(2, 5);
let rows: Vec<usize> = grid.columns().collect();
assert_eq!(rows, vec![0, 1]);
sourcepub fn position<P>(&self, predicate: P) -> Option<GridIndex>
pub fn position<P>(&self, predicate: P) -> Option<GridIndex>
Searches for an element in the Grid
matching a predicate, returning its index.
Iterates from left to right (looks through row 0 followed by row 1 etc.).
Returns the index of the first element that matches the predicate.
Example
let grid = Grid::new(2, 3, vec![1, 2, 3, 4, 5, 6]);
let position_of_4 = grid.position(|&e| e == 4);
assert_eq!(position_of_4, Some(GridIndex::new(1, 1)));
sourcepub fn indices(&self) -> impl DoubleEndedIterator<Item = GridIndex>
pub fn indices(&self) -> impl DoubleEndedIterator<Item = GridIndex>
Return an iterator over the cell indices in this grid. Iterates from top to bottom, left to right.
Example
let two_by_three: Grid<u32> = Grid::new(2, 3, vec![1, 2, 3, 4, 5, 6]);
let indices: Vec<GridIndex> = two_by_three.indices().collect();
assert_eq!(indices, vec![GridIndex::new(0, 0), GridIndex::new(1, 0), GridIndex::new(0, 1), GridIndex::new(1, 1), GridIndex::new(0, 2), GridIndex::new(1, 2)]);
sourcepub fn cells_with_indices_iter(
&self
) -> impl DoubleEndedIterator<Item = (GridIndex, &T)>
pub fn cells_with_indices_iter( &self ) -> impl DoubleEndedIterator<Item = (GridIndex, &T)>
Return an iterator over the cells in the grid, together with their indices.
Example
let grid = Grid::new(2, 2, "abcd".chars().collect());
// a b
// c d
assert_eq!(grid.cells_with_indices_iter().collect::<Vec<_>>(), vec![(GridIndex::new(0, 0), &'a'), (GridIndex::new(1, 0), &'b'), (GridIndex::new(0, 1), &'c'), (GridIndex::new(1, 1), &'d')]);
sourcepub fn contains_index(&self, idx: GridIndex) -> bool
pub fn contains_index(&self, idx: GridIndex) -> bool
Returns true
if idx
is within the bounds of this Grid
, false
otherwise.
Example
let two_by_two = Grid::new(2, 2, "abcd".chars().collect());
// a b
// c d
assert!(two_by_two.contains_index(GridIndex::new(1, 1)));
assert!(!two_by_two.contains_index(GridIndex::new(2, 1)));
sourcepub fn neighbor_indices_of<I>(
&self,
idx: I
) -> impl Iterator<Item = GridIndex> + '_
pub fn neighbor_indices_of<I>( &self, idx: I ) -> impl Iterator<Item = GridIndex> + '_
Returns an iterator over the indices of the cardinal and ordinal neighbors of the cell at idx
.
Returns the neighbors in clockwise order: [up, up_right, right, down_right, down, down_left, left, up_left]
.
Example
let three_by_three = Grid::new(3, 3, "abcdefghi".chars().collect());
// a b c
// d e f
// g h i
let neighbors: Vec<_> = three_by_three.neighbor_indices_of((1, 1)).collect();
assert_eq!(neighbors, vec![
(1, 0).into(), // up
(2, 0).into(), // up_right
(2, 1).into(), // right
(2, 2).into(), // down_right
(1, 2).into(), // down
(0, 2).into(), // down_left
(0, 1).into(), // left
(0, 0).into(), // up_left
]);
sourcepub fn neighbor_cells_of<I>(&self, idx: I) -> impl Iterator<Item = &T>
pub fn neighbor_cells_of<I>(&self, idx: I) -> impl Iterator<Item = &T>
Returns an iterator over the contents of the cardinal and ordinal neighbors of the cell at idx
.
Returns the neighbors in clockwise order: [up, up_right, right, down_right, down, down_left, left, up_left]
.
Example
let three_by_three = Grid::new(3, 3, "abcdefghi".chars().collect());
// a b c
// d e f
// g h i
let neighbors: Vec<_> = three_by_three.neighbor_cells_of((1, 1)).collect();
assert_eq!(neighbors, vec![
&'b', // up
&'c', // up_right
&'f', // right
&'i', // down_right
&'h', // down
&'g', // down_left
&'d', // left
&'a', // up_left
]);
sourcepub fn cardinal_neighbor_indices_of<I>(
&self,
idx: I
) -> impl Iterator<Item = GridIndex> + '_
pub fn cardinal_neighbor_indices_of<I>( &self, idx: I ) -> impl Iterator<Item = GridIndex> + '_
Returns an iterator over the indices of the cardinal neighbors of the cell at idx
.
Returns the neighbors in clockwise order: [up, right, down, left]
.
Example
let two_by_two = Grid::new(2, 2, "abcd".chars().collect());
// a b
// c d
let neighbors: Vec<_> = two_by_two.cardinal_neighbor_indices_of((1, 1)).collect();
assert_eq!(
neighbors,
vec![GridIndex::new(1, 0), GridIndex::new(0, 1)]
);
sourcepub fn cardinal_neighbor_cells_of<I>(&self, idx: I) -> impl Iterator<Item = &T>
pub fn cardinal_neighbor_cells_of<I>(&self, idx: I) -> impl Iterator<Item = &T>
Returns an iterator over the contents of the cardinal neighbors of the cell at idx
.
Returns the neighbors in clockwise order: [up, right, down, left]
.
Example
let two_by_two = Grid::new(2, 2, "abcd".chars().collect());
// a b
// c d
let neighbors: Vec<_> = two_by_two.cardinal_neighbor_cells_of((1, 1)).collect();
assert_eq!(neighbors, vec![&'b', &'c']);
sourcepub fn up_index<I>(&self, idx: I) -> Option<GridIndex>
pub fn up_index<I>(&self, idx: I) -> Option<GridIndex>
Returns the GridIndex
above idx
, if it exists.
Example
let two_by_two = Grid::new(2, 2, "abcd".chars().collect());
// a b
// c d
assert_eq!(two_by_two.up_index((1, 1)), Some(GridIndex::new(1, 0)));
assert_eq!(two_by_two.up_index((1, 0)), None);
sourcepub fn up_cell<I>(&self, idx: I) -> Option<&T>
pub fn up_cell<I>(&self, idx: I) -> Option<&T>
Returns a reference to the contents of the cell above idx
, if it exists in this Grid
.
Example
let two_by_two = Grid::new(2, 2, "abcd".chars().collect());
// a b
// c d
assert_eq!(two_by_two.up_cell((1, 1)), Some(&'b'));
assert_eq!(two_by_two.up_cell((1, 0)), None);
sourcepub fn up_right_index<I>(&self, idx: I) -> Option<GridIndex>
pub fn up_right_index<I>(&self, idx: I) -> Option<GridIndex>
Returns the GridIndex
above and to the right of idx
, if it exists in this Grid
.
Example
let two_by_two = Grid::new(2, 2, "abcd".chars().collect());
// a b
// c d
assert_eq!(two_by_two.up_right_index((0, 1)), Some(GridIndex::new(1, 0)));
assert_eq!(two_by_two.up_right_index((1, 0)), None);
sourcepub fn up_right_cell<I>(&self, idx: I) -> Option<&T>
pub fn up_right_cell<I>(&self, idx: I) -> Option<&T>
Returns a reference to the contents of the cell above and to the right of idx
, if it exists in this Grid
.
Example
let two_by_two = Grid::new(2, 2, "abcd".chars().collect());
// a b
// c d
assert_eq!(two_by_two.up_cell((1, 1)), Some(&'b'));
assert_eq!(two_by_two.up_cell((1, 0)), None);
sourcepub fn right_index<I>(&self, idx: I) -> Option<GridIndex>
pub fn right_index<I>(&self, idx: I) -> Option<GridIndex>
Returns the GridIndex
to the right of idx
, if it exists in this Grid
.
Example
let two_by_two = Grid::new(2, 2, "abcd".chars().collect());
// a b
// c d
assert_eq!(two_by_two.right_index((0, 0)), Some(GridIndex::new(1, 0)));
assert_eq!(two_by_two.right_index((1, 0)), None);
sourcepub fn right_cell<I>(&self, idx: I) -> Option<&T>
pub fn right_cell<I>(&self, idx: I) -> Option<&T>
Returns a reference to the contents of the cell to the right of idx
, if it exists in this Grid
.
Example
let two_by_two = Grid::new(2, 2, "abcd".chars().collect());
// a b
// c d
assert_eq!(two_by_two.right_cell((0, 1)), Some(&'d'));
assert_eq!(two_by_two.right_cell((1, 0)), None);
sourcepub fn down_right_index<I>(&self, idx: I) -> Option<GridIndex>
pub fn down_right_index<I>(&self, idx: I) -> Option<GridIndex>
Returns the GridIndex
below and to the right of idx
, if it exists in this Grid
.
Example
let two_by_two = Grid::new(2, 2, "abcd".chars().collect());
// a b
// c d
assert_eq!(two_by_two.down_right_index((0, 0)), Some(GridIndex::new(1,1)));
assert_eq!(two_by_two.down_right_index((1, 0)), None);
sourcepub fn down_right_cell<I>(&self, idx: I) -> Option<&T>
pub fn down_right_cell<I>(&self, idx: I) -> Option<&T>
Returns a reference to the contents of the cell below and to the right of idx
, if it exists in this Grid
.
Example
let two_by_two = Grid::new(2, 2, "abcd".chars().collect());
// a b
// c d
assert_eq!(two_by_two.down_right_cell((0, 0)), Some(&'d'));
assert_eq!(two_by_two.down_right_cell((1, 0)), None);
sourcepub fn down_index<I>(&self, idx: I) -> Option<GridIndex>
pub fn down_index<I>(&self, idx: I) -> Option<GridIndex>
Returns the GridIndex
below idx
, if it exists in this Grid
.
Example
let two_by_two = Grid::new(2, 2, "abcd".chars().collect());
// a b
// c d
assert_eq!(two_by_two.down_index((0, 0)), Some(GridIndex::new(0, 1)));
assert_eq!(two_by_two.down_index((0, 1)), None);
sourcepub fn down_cell<I>(&self, idx: I) -> Option<&T>
pub fn down_cell<I>(&self, idx: I) -> Option<&T>
Returns a reference to the contents of the cell below idx
, if it exists in this Grid
.
Example
let two_by_two = Grid::new(2, 2, "abcd".chars().collect());
// a b
// c d
assert_eq!(two_by_two.down_cell((0, 0)), Some(&'c'));
assert_eq!(two_by_two.down_cell((0, 1)), None);
sourcepub fn down_left_index<I>(&self, idx: I) -> Option<GridIndex>
pub fn down_left_index<I>(&self, idx: I) -> Option<GridIndex>
Returns the GridIndex
below and to the left of idx
, if it exists in this Grid
.
Example
let two_by_two = Grid::new(2, 2, "abcd".chars().collect());
// a b
// c d
assert_eq!(two_by_two.down_left_index((1, 0)), Some(GridIndex::new(0,1)));
assert_eq!(two_by_two.down_left_index((0, 0)), None);
sourcepub fn down_left_cell<I>(&self, idx: I) -> Option<&T>
pub fn down_left_cell<I>(&self, idx: I) -> Option<&T>
Returns a reference to the contents of the cell below and to the left of idx
, if it exists in this Grid
.
Example
let two_by_two = Grid::new(2, 2, "abcd".chars().collect());
// a b
// c d
assert_eq!(two_by_two.down_left_cell((1, 0)), Some(&'c'));
assert_eq!(two_by_two.down_left_cell((0, 0)), None);
sourcepub fn left_index<I>(&self, idx: I) -> Option<GridIndex>
pub fn left_index<I>(&self, idx: I) -> Option<GridIndex>
Returns the GridIndex
to the left of idx
, if it exists in this Grid
.
Example
let two_by_two = Grid::new(2, 2, "abcd".chars().collect());
// a b
// c d
assert_eq!(two_by_two.left_index((1, 0)), Some(GridIndex::new(0, 0)));
assert_eq!(two_by_two.left_index((0, 0)), None);
sourcepub fn left_cell<I>(&self, idx: I) -> Option<&T>
pub fn left_cell<I>(&self, idx: I) -> Option<&T>
Returns a reference to the contents of the cell to the left of idx
, if it exists in this Grid
.
Example
let two_by_two = Grid::new(2, 2, "abcd".chars().collect());
// a b
// c d
assert_eq!(two_by_two.left_cell((1, 0)), Some(&'a'));
assert_eq!(two_by_two.left_cell((0, 0)), None);
sourcepub fn up_left_index<I>(&self, idx: I) -> Option<GridIndex>
pub fn up_left_index<I>(&self, idx: I) -> Option<GridIndex>
Returns the GridIndex
above and to the left of idx
, if it exists in this Grid
.
Example
let two_by_two = Grid::new(2, 2, "abcd".chars().collect());
// a b
// c d
assert_eq!(two_by_two.up_left_index((1, 1)), Some(GridIndex::new(0, 0)));
assert_eq!(two_by_two.up_left_index((0, 0)), None);
sourcepub fn up_left_cell<I>(&self, idx: I) -> Option<&T>
pub fn up_left_cell<I>(&self, idx: I) -> Option<&T>
Returns a reference to the contents of the cell above and to the left of idx
, if it exists in this Grid
.
Example
let two_by_two = Grid::new(2, 2, "abcd".chars().collect());
// a b
// c d
assert_eq!(two_by_two.up_left_cell((1, 1)), Some(&'a'));
assert_eq!(two_by_two.up_left_cell((0, 0)), None);
source§impl<T> Grid<T>where
T: Display,
impl<T> Grid<T>where
T: Display,
sourcepub fn to_pretty_string(&self) -> String
pub fn to_pretty_string(&self) -> String
Format this Grid
as a string. This can look weird when the Display
output of T
has varying length.
Example
let grid = Grid::new(10, 10, (1..=100).collect::<Vec<u32>>());
assert_eq!(grid.get((5, 2)).unwrap(), &26);
println!("{}", grid.to_pretty_string());
// prints:
// 1 2 3 4 5 6 7 8 9 10
// 11 12 13 14 15 16 17 18 19 20
// 21 22 23 24 25 26 27 28 29 30
// 31 32 33 34 35 36 37 38 39 40
// 41 42 43 44 45 46 47 48 49 50
// 51 52 53 54 55 56 57 58 59 60
// 61 62 63 64 65 66 67 68 69 70
// 71 72 73 74 75 76 77 78 79 80
// 81 82 83 84 85 86 87 88 89 90
// 91 92 93 94 95 96 97 98 99 100