Struct simple_grid::Grid [−][src]
#[non_exhaustive]pub struct Grid<T> { /* fields omitted */ }
A two-dimensional array, indexed with x-and-y-coordinates (columns and rows).
Implementations
impl<T> Grid<T>
[src]
impl<T> Grid<T>
[src]pub fn new(width: usize, height: usize, data: Vec<T>) -> Self
[src]
Construct a new Grid.
Panics
- If
width * height != data.len()
- If one of (but not both)
width
andheight
is zero.
Example
// construct a 2x3 (width x height) grid of chars let grid = Grid::new(2, 3, "abcdef".chars().collect()); println!("{}", grid); // prints: // a b // c d // e f
pub fn width(&self) -> usize
[src]
Returns the width (number of columns) of the Grid
.
pub fn height(&self) -> usize
[src]
Returns the height (number of rows) of the Grid
.
pub fn subgrid(
self,
column_start: usize,
row_start: usize,
width: usize,
height: usize
) -> Grid<T>
[src]
self,
column_start: usize,
row_start: usize,
width: usize,
height: usize
) -> Grid<T>
pub fn dimensions(&self) -> (usize, usize)
[src]
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));
pub fn is_square(&self) -> bool
[src]
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());
pub fn area(&self) -> usize
[src]
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);
pub fn get<I>(&self, idx: I) -> Option<&T> where
GridIndex: From<I>,
[src]
GridIndex: From<I>,
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());
pub fn get_mut<I>(&mut self, idx: I) -> Option<&mut T> where
GridIndex: From<I>,
[src]
GridIndex: From<I>,
Attempts to get a mutable reference to the element at idx
Returns None
if idx
is out of bounds.
pub fn cell_iter(&self) -> impl DoubleEndedIterator<Item = &T>
[src]
Return an iterator over the cells in the grid.
Goes from left->right, top->bottom.
pub fn row_iter(&self, row: usize) -> impl DoubleEndedIterator<Item = &T>
[src]
Return an iterator over the columns in the row with index row
.
Panics
- If
row >= self.height
Example
let grid = Grid::new(10, 10, (1..=100).collect()); let items_in_row_2: Vec<u32> = grid.row_iter(2).cloned().collect(); assert_eq!(items_in_row_2, vec![21, 22, 23, 24, 25, 26, 27, 28, 29, 30]);
pub fn column_iter(&self, column: usize) -> impl DoubleEndedIterator<Item = &T>
[src]
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]);
pub fn insert_row(&mut self, row: usize, row_contents: Vec<T>)
[src]
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
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); // prints: // a b // x x // c d
pub fn replace_row(&mut self, row: usize, data: Vec<T>) -> Vec<T>
[src]
Replace the contents in a row.
Returns the old elements of the row.
Panics
- If
row >= self.height
- If
data.len() != self.width
pub fn remove_row(&mut self, row: usize) -> Vec<T>
[src]
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); // prints: // a b
pub fn swap_rows(&mut self, row1: usize, row2: usize)
[src]
pub fn insert_column(&mut self, column: usize, column_contents: Vec<T>)
[src]
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
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); // prints: // a x b // c x d
pub fn replace_column(&mut self, column: usize, data: Vec<T>) -> Vec<T>
[src]
Replace the contents in a column.
Returns the old elements of the column.
Panics
- If
column >= self.width
- If
data.len() != self.height
pub fn swap_columns(&mut self, column1: usize, column2: usize)
[src]
pub fn remove_column(&mut self, column: usize) -> Vec<T>
[src]
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); // prints: // a // c
pub fn swap_cells<I>(&mut self, a: I, b: I) where
GridIndex: From<I>,
[src]
GridIndex: From<I>,
Swap the values in two cells in the grid.
Panics
- If either index is out of bounds.
Example
let mut grid = Grid::new(2, 3, vec![1, 2, 3, 4, 5, 6]); grid.swap_cells((1, 1), (0, 2)); assert_eq!(grid, Grid::new(2, 3, vec![1, 2, 3, 5, 4, 6]));
pub fn replace_cell<I>(&mut self, idx: I, elem: T) -> T where
GridIndex: From<I>,
[src]
GridIndex: From<I>,
Replace the contents in a cell.
Returns the old element of the cell.
Panics
- If
idx
is out of bounds.
pub fn rotate_ccw(&mut self)
[src]
Rotate the grid counter-clockwise 90 degrees.
Example
let mut grid = Grid::new(2, 2, "abcd".chars().collect()); println!("{}", grid); // prints: // a b // c d grid.rotate_ccw(); assert_eq!(grid, Grid::new(2, 2, "bdac".chars().collect())); println!("{}", grid); // prints: // b d // a c
pub fn rotate_cw(&mut self)
[src]
Rotate the grid clockwise 90 degrees.
Example
let mut grid = Grid::new(2, 2, "abcd".chars().collect()); println!("{}", grid); // prints: // a b // c d grid.rotate_cw(); assert_eq!(grid, Grid::new(2, 2, "cadb".chars().collect())); println!("{}", grid); // prints: // c a // d b
pub fn flip_horizontally(&mut self)
[src]
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); // prints: // a b // c d grid.flip_horizontally(); assert_eq!(grid, Grid::new(2, 2, "badc".chars().collect())); println!("{}", grid); // prints: // b a // d c
pub fn flip_vertically(&mut self)
[src]
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); // prints: // a b // c d grid.flip_vertically(); assert_eq!(grid, Grid::new(2, 2, "cdab".chars().collect())); println!("{}", grid); // prints: // c d // a b
pub fn transpose(&mut self)
[src]
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); // prints: // a b // c d // e f grid.transpose(); assert_eq!(grid, Grid::new(3, 2, "acebdf".chars().collect())); println!("{}", grid); // prints: // a c e // b d f
pub fn rows(&self) -> impl DoubleEndedIterator<Item = usize>
[src]
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]);
pub fn columns(&self) -> impl DoubleEndedIterator<Item = usize>
[src]
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]);
impl<T> Grid<T> where
T: Default,
[src]
impl<T> Grid<T> where
T: Default,
[src]pub fn new_default(width: usize, height: usize) -> Grid<T>
[src]
Create a grid filled with default values.
Trait Implementations
impl<T> IntoIterator for Grid<T>
[src]
impl<T> IntoIterator for Grid<T>
[src]impl<T> StructuralEq for Grid<T>
[src]
impl<T> StructuralEq for Grid<T>
[src]impl<T> StructuralPartialEq for Grid<T>
[src]
impl<T> StructuralPartialEq for Grid<T>
[src]Auto Trait Implementations
impl<T> RefUnwindSafe for Grid<T> where
T: RefUnwindSafe,
impl<T> RefUnwindSafe for Grid<T> where
T: RefUnwindSafe,
impl<T> UnwindSafe for Grid<T> where
T: UnwindSafe,
impl<T> UnwindSafe for Grid<T> where
T: UnwindSafe,