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 new Grid.

Panics
  • If width * height != data.len()
  • If one of (but not both) width and height is zero.
Example
// construct a 2x3 (width x height) grid of chars
let grid = Grid::new(2, 3, "abcdef".chars().collect());
println!("{}", grid.to_pretty_string());
// prints:
// a b
// c d
// e f

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]));

Returns the width (number of columns) of the Grid.

Returns the height (number of rows) of the Grid.

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 or height (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]));

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 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]);

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 that row == self.height is allowed, to add a row at the bottom of the Grid)
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

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

Replace 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 b

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()));

Swap two rows in the grid.

Panics
  • If either of the row indices are out of bounds.

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 that column == self.width is allowed, to add a column at the right of the Grid)
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

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

Replace the contents in a column.

Returns the old elements of the column.

Panics
  • If column >= self.width
  • If data.len() != self.height

Swap two columns in the grid.

Panics
  • If either of the column indices are out of bounds.

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

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()));

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]));

Replace the contents in a cell.

Returns the old element of the cell.

Panics
  • If idx is 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 c

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

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

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

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

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]);

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)]);

Create a grid filled with default values.

Example
let grid: Grid<bool> = Grid::new_default(2, 2);
assert_eq!(grid, Grid::new(2, 2, vec![false, false, false, false]));

Returns true if the grid contains some element equal to value.

Example
let grid = Grid::new(2, 2, "abcd".chars().collect());
assert!(grid.contains(&'a'));
assert!(!grid.contains(&'e'));

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

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Feeds this value into the given Hasher. Read more

Feeds a slice of this type into the given Hasher. Read more

The returned type after indexing.

Performs the indexing (container[index]) operation. Read more

Performs the mutable indexing (container[index]) operation. Read more

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.