[−][src]Struct simple_grid::Grid
A two-dimensional array, indexed with x-and-y-coordinates (columns and rows).
Implementations
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()
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 area(&self) -> usize
[src]
Returns the area (number of columns * number of rows) of the grid.
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.
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 remove_row(&mut self, row: usize)
[src]
Remove row at row
, shifting all rows with higher indices "upward" (row n
becomes row n-1
).
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 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 remove_column(&mut self, column: usize)
[src]
Remove column at column
, shifting all columns with higher indices "left" (column n
becomes column n-1
).
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
impl<T> Grid<T> where
T: Clone,
[src]
T: Clone,
pub fn rotate_cw(&self) -> Self
[src]
Rotate the grid clockwise 90 degrees, cloning the data into a new grid.
Example
let grid = Grid::new(2, 2, "abcd".chars().collect()); println!("{}", grid); // prints: // a b // c d let cw = grid.rotate_cw(); assert_eq!(cw, Grid::new(2, 2, "cadb".chars().collect())); println!("{}", cw); // prints: // c a // d b
pub fn rotate_ccw(&self) -> Self
[src]
Rotate the grid counter-clockwise 90 degrees, cloning the data into a new grid.
Example
let grid = Grid::new(2, 2, "abcd".chars().collect()); println!("{}", grid); // prints: // a b // c d let ccw = grid.rotate_ccw(); assert_eq!(ccw, Grid::new(2, 2, "bdac".chars().collect())); println!("{}", ccw); // prints: // b d // a c
pub fn flip_horizontally(&self) -> Self
[src]
Flip the grid horizontally, so that the first column becomes the last.
Example
let grid = Grid::new(2, 2, "abcd".chars().collect()); println!("{}", grid); // prints: // a b // c d let hori = grid.flip_horizontally(); assert_eq!(hori, Grid::new(2, 2, "badc".chars().collect())); println!("{}", hori); // prints: // b a // d c
pub fn flip_vertically(&self) -> Self
[src]
Flip the grid vertically, so that the first row becomes the last.
Example
let grid = Grid::new(2, 2, "abcd".chars().collect()); println!("{}", grid); // prints: // a b // c d let vert = grid.flip_vertically(); assert_eq!(vert, Grid::new(2, 2, "cdab".chars().collect())); println!("{}", vert); // prints: // c d // a b
pub fn transpose(&self) -> Self
[src]
Transpose the grid along the diagonal, so that cells at index (x, y) end up at index (y, x).
Example
let grid = Grid::new(2, 3, "abcdef".chars().collect()); println!("{}", grid); // prints: // a b // c d // e f let transposed = grid.transpose(); assert_eq!(transposed, Grid::new(3, 2, "acebdf".chars().collect())); println!("{}", transposed); // prints: // a c e // b d f
impl<T> Grid<T> where
T: Default,
[src]
T: Default,
pub fn new_default(width: usize, height: usize) -> Grid<T>
[src]
Create a grid filled with default values.
impl<T> Grid<T> where
T: PartialEq,
[src]
T: PartialEq,
pub fn contains(&self, value: &T) -> bool
[src]
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'));
Trait Implementations
impl<T: Clone> Clone for Grid<T>
[src]
impl<T: Debug> Debug for Grid<T>
[src]
impl<T> Display for Grid<T> where
T: Display,
[src]
T: Display,
impl<T: Eq> Eq for Grid<T>
[src]
impl<T: Hash> Hash for Grid<T>
[src]
pub fn hash<__H: Hasher>(&self, state: &mut __H)
[src]
pub fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher,
1.3.0[src]
H: Hasher,
impl<T, I> Index<I> for Grid<T> where
GridIndex: From<I>,
[src]
GridIndex: From<I>,
type Output = T
The returned type after indexing.
pub fn index(&self, index: I) -> &Self::Output
[src]
impl<T, I> IndexMut<I> for Grid<T> where
GridIndex: From<I>,
[src]
GridIndex: From<I>,
impl<T> IntoIterator for Grid<T>
[src]
type Item = T
The type of the elements being iterated over.
type IntoIter = IntoIter<Self::Item>
Which kind of iterator are we turning this into?
pub fn into_iter(self) -> Self::IntoIter
[src]
impl<T: Ord> Ord for Grid<T>
[src]
pub fn cmp(&self, other: &Grid<T>) -> Ordering
[src]
#[must_use]pub fn max(self, other: Self) -> Self
1.21.0[src]
#[must_use]pub fn min(self, other: Self) -> Self
1.21.0[src]
#[must_use]pub fn clamp(self, min: Self, max: Self) -> Self
1.50.0[src]
impl<T: PartialEq> PartialEq<Grid<T>> for Grid<T>
[src]
impl<T: PartialOrd> PartialOrd<Grid<T>> for Grid<T>
[src]
pub fn partial_cmp(&self, other: &Grid<T>) -> Option<Ordering>
[src]
pub fn lt(&self, other: &Grid<T>) -> bool
[src]
pub fn le(&self, other: &Grid<T>) -> bool
[src]
pub fn gt(&self, other: &Grid<T>) -> bool
[src]
pub fn ge(&self, other: &Grid<T>) -> bool
[src]
impl<T> StructuralEq for Grid<T>
[src]
impl<T> StructuralPartialEq for Grid<T>
[src]
Auto Trait Implementations
impl<T> RefUnwindSafe for Grid<T> where
T: RefUnwindSafe,
[src]
T: RefUnwindSafe,
impl<T> Send for Grid<T> where
T: Send,
[src]
T: Send,
impl<T> Sync for Grid<T> where
T: Sync,
[src]
T: Sync,
impl<T> Unpin for Grid<T> where
T: Unpin,
[src]
T: Unpin,
impl<T> UnwindSafe for Grid<T> where
T: UnwindSafe,
[src]
T: UnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
pub fn to_owned(&self) -> T
[src]
pub fn clone_into(&self, target: &mut T)
[src]
impl<T> ToString for T where
T: Display + ?Sized,
[src]
T: Display + ?Sized,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,