Struct Board

Source
pub struct Board<T>(/* private fields */)
where
    T: Eq + Hash;
Expand description

A two-dimensional orthogonal grid map of live/dead cells.

The type parameter T is used as the type of the x- and y-coordinate values for each cell.

§Examples

use life_backend::{Board, Position};
let pattern = [Position(0, 0), Position(1, 0), Position(2, 0), Position(1, 1)];
let board: Board<i16> = pattern.iter().collect();
assert_eq!(board.contains(&Position(0, 0)), true);
assert_eq!(board.contains(&Position(0, 1)), false);
assert_eq!(board.iter().count(), 4);

Implementations§

Source§

impl<T> Board<T>
where T: Eq + Hash,

Source

pub fn new() -> Self

Creates an empty board.

§Examples
use life_backend::Board;
let board = Board::<i16>::new();
assert_eq!(board.iter().count(), 0);
Source

pub fn contains(&self, position: &Position<T>) -> bool

Returns true if the board contains the specified position.

§Examples
use life_backend::{Board, Position};
let board = Board::<i16>::new();
assert_eq!(board.contains(&Position(0, 0)), false);
Source

pub fn insert(&mut self, position: Position<T>) -> bool

Adds the specified position to the board.

Returns whether the position was newly inserted, like as insert() of HashSet.

§Examples
use life_backend::{Board, Position};
let mut board = Board::<i16>::new();
assert_eq!(board.insert(Position(0, 0)), true);
assert_eq!(board.contains(&Position(0, 0)), true);
Source

pub fn remove(&mut self, position: &Position<T>) -> bool

Removes the specified position from the board.

Returns whether the position was contained in the board, like as remove() of HashSet.

§Examples
use life_backend::{Board, Position};
let mut board = Board::<i16>::new();
assert_eq!(board.insert(Position(0, 0)), true);
assert_eq!(board.remove(&Position(0, 0)), true);
assert_eq!(board.contains(&Position(0, 0)), false);
Source

pub fn bounding_box(&self) -> BoardRange<T>
where T: Copy + PartialOrd + Zero + One,

Returns the minimum bounding box of all live cells on the board.

§Examples
use life_backend::{Board, Position};
let mut board = Board::new();
board.insert(Position(-1, 2));
board.insert(Position(3, -2));
let bbox = board.bounding_box();
assert_eq!(bbox.x(), &(-1..=3));
assert_eq!(bbox.y(), &(-2..=2));
Source

pub fn clear(&mut self)

Removes all live cells in the board.

§Examples
use life_backend::{Board, Position};
let mut board = Board::<i16>::new();
board.insert(Position(0, 0));
board.clear();
assert_eq!(board.contains(&Position(0, 0)), false);
Source

pub fn retain<F>(&mut self, pred: F)
where F: FnMut(&Position<T>) -> bool,

Retains only the live cell positions specified by the predicate, similar as retain() of HashSet.

§Examples
use life_backend::{Board, Position};
let mut board = Board::<i16>::new();
board.insert(Position(0, 0));
board.insert(Position(1, 0));
board.insert(Position(0, 1));
board.retain(|&pos| pos.0 == pos.1);
assert_eq!(board.contains(&Position(0, 0)), true);
assert_eq!(board.contains(&Position(1, 0)), false);
assert_eq!(board.contains(&Position(0, 1)), false);
Source§

impl<'a, T> Board<T>
where T: Eq + Hash,

Source

pub fn iter(&'a self) -> Iter<'a, Position<T>>

Creates a non-owning iterator over the series of immutable live cell positions on the board in arbitrary order.

§Examples
use std::collections::HashSet;
use life_backend::{Board, Position};
let mut board = Board::<i16>::new();
board.insert(Position(1, 0));
board.insert(Position(0, 1));
let result: HashSet<_> = board.iter().collect();
assert_eq!(result.len(), 2);
assert!(result.contains(&Position(1, 0)));
assert!(result.contains(&Position(0, 1)));

Trait Implementations§

Source§

impl<T> Clone for Board<T>
where T: Eq + Hash + Clone,

Source§

fn clone(&self) -> Board<T>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T> Debug for Board<T>
where T: Eq + Hash + Debug,

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<T> Default for Board<T>
where T: Eq + Hash,

Source§

fn default() -> Self

Returns the default value of the type, same as the return value of new().

Source§

impl<T> Display for Board<T>
where T: Eq + Hash + Copy + PartialOrd + Zero + One + ToPrimitive,

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<'a, T> Extend<&'a Position<T>> for Board<T>
where T: Eq + Hash + Copy + 'a,

Source§

fn extend<U>(&mut self, iter: U)
where U: IntoIterator<Item = &'a Position<T>>,

Extends the board with the contents of the specified non-owning iterator over the series of &Position<T>. Each item in the series represents an immutable reference of a live cell position.

§Examples
use life_backend::{Board, Position};
let mut board = Board::<i16>::new();
let pattern = [Position(1, 0), Position(0, 1)];
board.extend(pattern.iter());
assert_eq!(board.contains(&Position(0, 0)), false);
assert_eq!(board.contains(&Position(1, 0)), true);
assert_eq!(board.contains(&Position(0, 1)), true);
assert_eq!(board.contains(&Position(1, 1)), false);
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<T> Extend<Position<T>> for Board<T>
where T: Eq + Hash,

Source§

fn extend<U>(&mut self, iter: U)
where U: IntoIterator<Item = Position<T>>,

Extends the board with the contents of the specified owning iterator over the series of Position<T>. Each item in the series represents a moved live cell position.

§Examples
use life_backend::{Board, Position};
let mut board = Board::<i16>::new();
let pattern = [Position(1, 0), Position(0, 1)];
board.extend(pattern.into_iter());
assert_eq!(board.contains(&Position(0, 0)), false);
assert_eq!(board.contains(&Position(1, 0)), true);
assert_eq!(board.contains(&Position(0, 1)), true);
assert_eq!(board.contains(&Position(1, 1)), false);
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<'a, T> FromIterator<&'a Position<T>> for Board<T>
where T: Eq + Hash + Copy + 'a,

Source§

fn from_iter<U>(iter: U) -> Self
where U: IntoIterator<Item = &'a Position<T>>,

Creates a value from a non-owning iterator over a series of &Position<T>. Each item in the series represents an immutable reference of a live cell position.

§Examples
use life_backend::{Board, Position};
let pattern = [Position(1, 0), Position(0, 1)];
let board: Board<i16> = pattern.iter().collect();
assert_eq!(board.contains(&Position(0, 0)), false);
assert_eq!(board.contains(&Position(1, 0)), true);
assert_eq!(board.contains(&Position(0, 1)), true);
assert_eq!(board.contains(&Position(1, 1)), false);
Source§

impl<T> FromIterator<Position<T>> for Board<T>
where T: Eq + Hash,

Source§

fn from_iter<U>(iter: U) -> Self
where U: IntoIterator<Item = Position<T>>,

Creates a value from an owning iterator over a series of Position<T>. Each item in the series represents a moved live cell position.

§Examples
use life_backend::{Board, Position};
let mut pattern = [Position(1, 0), Position(0, 1)];
let board: Board<i16> = pattern.into_iter().collect();
assert_eq!(board.contains(&Position(0, 0)), false);
assert_eq!(board.contains(&Position(1, 0)), true);
assert_eq!(board.contains(&Position(0, 1)), true);
assert_eq!(board.contains(&Position(1, 1)), false);
Source§

impl<'a, T> IntoIterator for &'a Board<T>
where T: Eq + Hash,

Source§

fn into_iter(self) -> Self::IntoIter

Creates a non-owning iterator over the series of immutable live cell positions on the board in arbitrary order.

§Examples
use std::collections::HashSet;
use life_backend::{Board, Position};
let pattern = [Position(1, 0), Position(0, 1)];
let board: Board<i16> = pattern.iter().collect();
let result: HashSet<_> = (&board).into_iter().collect();
let expected: HashSet<_> = pattern.iter().collect();
assert_eq!(result, expected);
Source§

type Item = &'a Position<T>

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, Position<T>>

Which kind of iterator are we turning this into?
Source§

impl<T> IntoIterator for Board<T>
where T: Eq + Hash,

Source§

fn into_iter(self) -> Self::IntoIter

Creates an owning iterator over the series of moved live cell positions on the board in arbitrary order.

§Examples
use std::collections::HashSet;
use life_backend::{Board, Position};
let pattern = [Position(1, 0), Position(0, 1)];
let board: Board<i16> = pattern.iter().collect();
let result: HashSet<_> = board.into_iter().collect();
let expected: HashSet<_> = pattern.iter().copied().collect();
assert_eq!(result, expected);
Source§

type Item = Position<T>

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<<Board<T> as IntoIterator>::Item>

Which kind of iterator are we turning this into?
Source§

impl<T> PartialEq for Board<T>
where T: Eq + Hash + PartialEq,

Source§

fn eq(&self, other: &Board<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T> Eq for Board<T>
where T: Eq + Hash + Eq,

Source§

impl<T> StructuralPartialEq for Board<T>
where T: Eq + Hash,

Auto Trait Implementations§

§

impl<T> Freeze for Board<T>

§

impl<T> RefUnwindSafe for Board<T>
where T: RefUnwindSafe,

§

impl<T> Send for Board<T>
where T: Send,

§

impl<T> Sync for Board<T>
where T: Sync,

§

impl<T> Unpin for Board<T>
where T: Unpin,

§

impl<T> UnwindSafe for Board<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

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

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.