Struct simple_grid::Grid [−][src]
#[non_exhaustive]pub struct Grid<T> { /* fields omitted */ }Expand description
A two-dimensional array, indexed with x-and-y-coordinates (columns and rows).
Implementations
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]));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
widthorheight(but not both) are 0. If both are 0, the resulting subgrid will be an empty (0 by 0)Grid. - If
column_startis out of bounds. - If
row_startis 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]));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));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());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);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());Attempts to get a mutable reference to the element at idx
Returns None if idx is out of bounds.
Return an iterator over the cells in the grid.
Iterates from left to right (starting with row 0, then row 1 etc.).
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]);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.heightis 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 dAdd 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 xReplace the contents in a row.
Returns the old elements of the row.
Panics
- If
row >= self.height - If
data.len() != self.width
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 bRemove 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()));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.widthis 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 dAdd 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 xReplace the contents in a column.
Returns the old elements of the column.
Panics
- If
column >= self.width - If
data.len() != self.height
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
// cRemove 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()));Replace the contents in a cell.
Returns the old element of the cell.
Panics
- If
idxis out of bounds.
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 cRotate 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 bFlip 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 cFlip 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 bTranspose 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 fReturn 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]);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]);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)));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)]);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 100Trait Implementations
Auto Trait Implementations
impl<T> RefUnwindSafe for Grid<T> where
T: RefUnwindSafe,
impl<T> UnwindSafe for Grid<T> where
T: UnwindSafe,
Blanket Implementations
Mutably borrows from an owned value. Read more
