Struct gridit::Grid[][src]

pub struct Grid<T> { /* fields omitted */ }

2D Grid, Position (0,0) is at the top left corner

Implementations

impl<T: Clone> Grid<T>[src]

pub fn new(width: usize, height: usize, default_value: T) -> Self[src]

Creates a new Grid with default_value as every value.

Example

let grid: Grid<u8> = Grid::new(2, 2, 10);
assert_eq!(grid.get((0, 0)), Some(&10));
assert_eq!(grid.get((1, 0)), Some(&10));
assert_eq!(grid.get((0, 1)), Some(&10));
assert_eq!(grid.get((1, 1)), Some(&10));

Panics

  • if width or height are zero

impl<T: Default> Grid<T>[src]

pub fn replace_default<P: Into<Position>>(&mut self, pos: P) -> Option<T>[src]

Returns the item at pos and leaves T::Default() in it’s place, or None if pos is out of bounds.

Example

let mut grid: Grid<usize> = Grid::new(2, 2, 10);
let old = grid.replace_default((1, 1));
assert_eq!(old, Some(10));
assert_eq!(grid.get((1, 1)), Some(&0));

pub fn move_to<P: Into<Position>>(&mut self, pos: P, to: P)[src]

Moves the item at pos to position to, overrides item at to in the process, and leaves the T::Default() in pos.

Example

let mut grid: Grid<usize> = Grid::from(vec![1, 2, 3, 4], 2, 2);
grid.move_to((0, 0), (1, 1));
assert_eq!(grid.get((0, 0)), Some(&0));
assert_eq!(grid.get((1, 1)), Some(&1));

Panics

  • if positions pos or to are out of bounds

impl<T> Grid<T>[src]

pub fn from(v: Vec<T>, width: usize, height: usize) -> Self[src]

Constructs a new Grid with items in Vector v

Example

let grid = Grid::from(vec![1, 2, 3, 4], 2, 2);

Panics

  • if width or height is zero
  • if v length is not equal width times height

pub fn is_bounds<P: Into<Position>>(&self, pos: P) -> bool[src]

Checks if position pos is in bounds of the grid.

Example

let grid = Grid::new(2, 2, 0usize);
assert_eq!(grid.is_bounds((1, 1)), true);
assert_eq!(grid.is_bounds((2, 2)), false);

pub fn size(&self) -> (usize, usize)[src]

Returns the width and height of the grid.

Example

let grid = Grid::from(vec![1, 2, 3, 4], 2, 2);
assert_eq!(grid.size(), (2, 2));

pub fn len(&self) -> usize[src]

Returns the full length of the grid

Example

let grid = Grid::from(vec![1, 2, 3, 4], 2, 2);
assert_eq!(grid.len(), 4);

pub fn get<P: Into<Position>>(&self, pos: P) -> Option<&T>[src]

Returns a reference to an element at position pos or None, if pos is out of bounds.

Example

let grid = Grid::from(vec![1, 2, 3, 4], 2, 2);
assert_eq!(grid.get((1, 0)), Some(&2));

pub fn get_mut<P: Into<Position>>(&mut self, pos: P) -> Option<&mut T>[src]

Returns a mutable reference to an element at position pos or None, if pos is out of bounds.

Example

let mut grid = Grid::from(vec![1, 2, 3, 4], 2, 2);
assert_eq!(grid.get_mut((0, 1)), Some(&mut 3));

pub fn get_unchecked<P: Into<Position>>(&self, pos: P) -> &T[src]

Returns a reference to an element at position pos without bound checks.

Example

let grid = Grid::from(vec![1, 2, 3, 4], 2, 2);
assert_eq!(grid.get_unchecked((1, 1)), &4);

Safety

Does not do any bound checks.
pos does not have to be in bounds as long pos.x*pos.y < grid.len()
for example on a grid size 3,3: get_unchecked(8,0) will return the last element

Panics

  • if pos.x times pos.y is greater than grid length.

pub fn get_mut_unchecked<P: Into<Position>>(&mut self, pos: P) -> &mut T[src]

Returns a reference to an element at position pos without bound checks.

Example

let mut grid = Grid::from(vec![1, 2, 3, 4], 2, 2);
assert_eq!(grid.get_mut_unchecked((1, 1)), &mut 4);

Safety

Does not do any bound checks.
pos does not have to be in bounds as long pos.x*pos.y < grid.len()
for example on a grid size 3,3: get_unchecked(8,0) will return the last element

Panics

  • if pos.x times pos.y is greater than grid length.

pub fn set<P: Into<Position>>(&mut self, pos: P, value: T) -> Option<()>[src]

Sets the value at position pos. Returns None if pos is out of bounds, or () otherwise.

Example

let mut grid = Grid::from(vec![1, 2, 3, 4], 2, 2);
grid.set((0, 0), 10);
assert_eq!(grid.get((0, 0)), Some(&10));

pub fn set_unchecked<P: Into<Position>>(&mut self, pos: P, value: T)[src]

Sets the value at position pos, without bound checks.

Example

let mut grid = Grid::from(vec![1, 2, 3, 4], 2, 2);
grid.set_unchecked((0, 0), 10);
assert_eq!(grid.get((0, 0)), Some(&10));

Safety

Does not do any bound checks.
pos does not have to be in bounds as long pos.x*pos.y < grid.len()
for example on a grid size 3,3: get_unchecked(8,0) will return the last element

Panics

  • if pos.x times pos.y is greater than grid length.

pub fn replace<P: Into<Position>>(&mut self, pos: P, value: T) -> Option<T>[src]

Replace the value at position pos and returns the old value, or None if pos is out of bounds.

Example

let mut grid = Grid::from(vec![1, 2, 3, 4], 2, 2);
let old = grid.replace((0, 0), 10);
assert_eq!(old, Some(1));
assert_eq!(grid.get((0, 0)), Some(&10));

pub fn swap<P: Into<Position>>(&mut self, pos_a: P, pos_b: P)[src]

Swap the values of positions pos_a and pos_b.

Example

let mut grid = Grid::from(vec![1, 2, 3, 4], 2, 2);
grid.swap((0, 0), (1, 0));
assert_eq!(grid.get((0, 0)), Some(&2));
assert_eq!(grid.get((1, 0)), Some(&1));

Panics

  • if position pos is out of bounds.

pub fn move_and_leave<P: Into<Position>>(&mut self, pos: P, to: P, value: T)[src]

Move the value of position pos to position to and leaves value in it’s place.

Example

let mut grid = Grid::from(vec![1, 2, 3, 4], 2, 2);
grid.move_and_leave((0, 0), (1, 1), 42);
assert_eq!(grid.get((0, 0)), Some(&42));
assert_eq!(grid.get((1, 1)), Some(&1));

Panics

  • if position pos is out of bounds

pub fn positions(&self) -> PositionsIter

Notable traits for PositionsIter

impl Iterator for PositionsIter type Item = Position;
[src]

Creates an iterator which yields all positions of grid.

Example

let mut grid = Grid::new(2, 2, 0);
let mut positions = grid.positions();
assert_eq!(positions.next(), Some(Position::new(0, 0)));
assert_eq!(positions.next(), Some(Position::new(1, 0)));
assert_eq!(positions.next(), Some(Position::new(0, 1)));
assert_eq!(positions.next(), Some(Position::new(1, 1)));
assert_eq!(positions.next(), None);

pub fn iter(&self) -> GridIter<'_, T>

Notable traits for GridIter<'a, T>

impl<'a, T> Iterator for GridIter<'a, T> type Item = &'a T;
[src]

Creates an iterator which yields references of every element in grid.

Example

let mut grid = Grid::from(vec![1, 2, 3, 4], 2, 2);
let mut iter = grid.iter();
assert_eq!(iter.next(), Some(&1));
assert_eq!(iter.next(), Some(&2));
assert_eq!(iter.next(), Some(&3));
assert_eq!(iter.next(), Some(&4));
assert_eq!(iter.next(), None);

pub fn iter_mut(&mut self) -> GridIterMut<'_, T>

Notable traits for GridIterMut<'a, T>

impl<'a, T> Iterator for GridIterMut<'a, T> type Item = &'a mut T;
[src]

Creates an iterator which yields mutable references of every element in grid.

Example

let mut grid = Grid::from(vec![1, 2, 3, 4], 2, 2);
let mut iter = grid.iter_mut();
assert_eq!(iter.next(), Some(&mut 1));
assert_eq!(iter.next(), Some(&mut 2));
assert_eq!(iter.next(), Some(&mut 3));
assert_eq!(iter.next(), Some(&mut 4));
assert_eq!(iter.next(), None);

pub fn row(&self, y: usize) -> RowIter<'_, T>

Notable traits for RowIter<'a, T>

impl<'a, T> Iterator for RowIter<'a, T> type Item = &'a T;
[src]

Creates an iterator which yields references of every element in row y.

Example

let mut grid = Grid::from(vec![1, 2, 3, 4], 2, 2);
let mut row = grid.row(0);
assert_eq!(row.next(), Some(&1));
assert_eq!(row.next(), Some(&2));
assert_eq!(row.next(), None);

Panics

  • if the row is out of bounds.

pub fn row_mut(&mut self, y: usize) -> RowIterMut<'_, T>

Notable traits for RowIterMut<'a, T>

impl<'a, T> Iterator for RowIterMut<'a, T> type Item = &'a mut T;
[src]

Creates an iterator which yields mutable references of every element in row y.

Example

let mut grid = Grid::from(vec![1, 2, 3, 4], 2, 2);
let mut row = grid.row_mut(1);
assert_eq!(row.next(), Some(&mut 3));
assert_eq!(row.next(), Some(&mut 4));
assert_eq!(row.next(), None);

Panics

  • if the row is out of bounds.

pub fn column(&self, x: usize) -> ColumnIter<'_, T>

Notable traits for ColumnIter<'a, T>

impl<'a, T> Iterator for ColumnIter<'a, T> type Item = &'a T;
[src]

Creates an iterator which yields references of every element in column x.

Example

let mut grid = Grid::from(vec![1, 2, 3, 4], 2, 2);
let mut column = grid.column(0);
assert_eq!(column.next(), Some(&1));
assert_eq!(column.next(), Some(&3));
assert_eq!(column.next(), None);

Panics

  • if the column is out of bounds.

pub fn column_mut(&mut self, x: usize) -> ColumnIterMut<'_, T>

Notable traits for ColumnIterMut<'a, T>

impl<'a, T> Iterator for ColumnIterMut<'a, T> type Item = &'a mut T;
[src]

Creates an iterator which yields mutable references of every element in column x.

Example

let mut grid = Grid::from(vec![1, 2, 3, 4], 2, 2);
let mut column = grid.column_mut(1);
assert_eq!(column.next(), Some(&mut 2));
assert_eq!(column.next(), Some(&mut 4));
assert_eq!(column.next(), None);

Panics

  • if the column is out of bounds.

pub fn neighbors<P: Into<Position>>(&self, pos: P) -> NeighborIter<'_, T>

Notable traits for NeighborIter<'a, T>

impl<'a, T> Iterator for NeighborIter<'a, T> type Item = &'a T;
[src]

Creates an iterator which yields references of every neighbor element of position pos.

Example

let mut grid = Grid::from(vec![1, 2, 3, 4], 2, 2);
let mut neighbors = grid.neighbors((0, 1));
assert_eq!(neighbors.next(), Some(&1));
assert_eq!(neighbors.next(), Some(&2));
assert_eq!(neighbors.next(), Some(&4));
assert_eq!(neighbors.next(), None);

Panics

  • if x or y is out of bounds.

pub fn pattern<P, Pat>(&self, pos: P, pattern: Pat) -> PatternIter<'_, T>

Notable traits for PatternIter<'a, T>

impl<'a, T> Iterator for PatternIter<'a, T> type Item = &'a T;
where
    P: Into<Position>,
    Pat: Pattern + 'static, 
[src]

Creates an iterator which yields references of every element of pattern starting at position pos.
See Pattern more details.

Example

let mut grid = Grid::from(vec![1, 2, 3, 4], 2, 2);
let pattern = StepsPattern::new(vec![(1,0), (-1, 0), (1, 0)]);
let mut iter = grid.pattern((0, 0), pattern);
assert_eq!(iter.next(), Some(&2));
assert_eq!(iter.next(), Some(&1));
assert_eq!(iter.next(), Some(&2));
assert_eq!(iter.next(), None);

Trait Implementations

impl<T: Debug> Debug for Grid<T>[src]

fn fmt(&self, f: &mut Formatter<'_>) -> Result[src]

Formats the value using the given formatter. Read more

impl<T: PartialEq> PartialEq<Grid<T>> for Grid<T>[src]

fn eq(&self, other: &Grid<T>) -> bool[src]

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

fn ne(&self, other: &Grid<T>) -> bool[src]

This method tests for !=.

impl<T> StructuralPartialEq for Grid<T>[src]

Auto Trait Implementations

impl<T> RefUnwindSafe for Grid<T> where
    T: RefUnwindSafe

impl<T> Send for Grid<T> where
    T: Send

impl<T> Sync for Grid<T> where
    T: Sync

impl<T> Unpin for Grid<T> where
    T: Unpin

impl<T> UnwindSafe for Grid<T> where
    T: UnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

pub fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

pub fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

impl<T> From<T> for T[src]

pub fn from(t: T) -> T[src]

Performs the conversion.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

pub fn into(self) -> U[src]

Performs the conversion.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

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]

Performs the conversion.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>[src]

Performs the conversion.