Struct simple_grid::GridIndex

source ·
pub struct GridIndex(/* private fields */);
Expand description

A struct used for indexing into a grid.

Implementations§

source§

impl GridIndex

source

pub fn new(column: usize, row: usize) -> Self

Construct a new GridIndex.

source

pub fn column(&self) -> usize

Get the column (x) index.

source

pub fn row(&self) -> usize

Get the row (y) index.

source

pub fn neighbors(self) -> impl Iterator<Item = Self>

Returns an iterator over the cardinal and ordinal neighbors of self.

Returns the neighbors in clockwise order: [up, up_right, right, down_right, down, down_left, left, up_left].

Example
let idx = GridIndex::new(0, 1);
let neighbors: Vec<_> = idx.neighbors().collect();
assert_eq!(neighbors, vec![
    (0, 0).into(), // up
    (1, 0).into(), // up_right
    (1, 1).into(), // right
    (1, 2).into(), // down_right
    (0, 2).into(), // down
                   // nothing to the left since `idx` has column=0
]);
source

pub fn cardinal_neighbors(self) -> impl Iterator<Item = Self>

Returns an iterator over the cardinal neighbors of self.

Returns the neighbors in clockwise order: [up, right, down, left].

Example
let idx = GridIndex::new(0, 1);
let neighbors: Vec<_> = idx.cardinal_neighbors().collect();
assert_eq!(neighbors, vec![
    (0, 0).into(), // up
    (1, 1).into(), // right
    (0, 2).into(), // down
                   // nothing to the left since `idx` has column=0
]);
source

pub fn up(&self) -> Option<Self>

Get the GridIndex above, if it exists.

Example
let row_5 = GridIndex::new(17, 5);
assert_eq!(row_5.up(), Some(GridIndex::new(17, 4)));
let row_0 = GridIndex::new(38, 0);
assert_eq!(row_0.up(), None);
source

pub fn right(&self) -> Option<Self>

Get the GridIndex to the right, if it exists.

Example
let column_17 = GridIndex::new(17, 11);
assert_eq!(column_17.right(), Some(GridIndex::new(18, 11)));
source

pub fn down(&self) -> Option<Self>

Get the GridIndex below, if it exists.

Example
let row_15 = GridIndex::new(3, 15);
assert_eq!(row_15.down(), Some(GridIndex::new(3, 16)));
source

pub fn left(&self) -> Option<Self>

Get the GridIndex to the left, if it exists. 0).

Example
let column_9 = GridIndex::new(9, 10);
assert_eq!(column_9.left(), Some(GridIndex::new(8, 10)));
let column_0 = GridIndex::new(0, 10);
assert_eq!(column_0.left(), None);
source

pub fn up_left(&self) -> Option<Self>

Get the GridIndex above and to the left, if it exists.

Example
let column_5_row_4 = GridIndex::new(5, 4);
assert_eq!(column_5_row_4.up_left(), Some(GridIndex::new(4, 3)));
let column_0_row_4 = GridIndex::new(0, 4);
assert_eq!(column_0_row_4.up_left(), None);
source

pub fn up_right(&self) -> Option<Self>

Get the GridIndex above and to the right, if it exists.

Example
let column_5_row_4 = GridIndex::new(5, 4);
assert_eq!(column_5_row_4.up_right(), Some(GridIndex::new(6, 3)));
let column_5_row_0 = GridIndex::new(5, 0);
assert_eq!(column_5_row_0.up_right(), None);
source

pub fn down_right(&self) -> Option<Self>

Get the GridIndex below and to the right, if it exists.

Example
let column_5_row_4 = GridIndex::new(5, 4);
assert_eq!(column_5_row_4.down_right(), Some(GridIndex::new(6, 5)));
source

pub fn down_left(&self) -> Option<Self>

Get the GridIndex below and to the left, if it exists.

Example
let column_5_row_4 = GridIndex::new(5, 4);
assert_eq!(column_5_row_4.down_left(), Some(GridIndex::new(4, 5)));
let column_0_row_0 = GridIndex::new(0, 0);
assert_eq!(column_0_row_0.down_left(), None);

Trait Implementations§

source§

impl Clone for GridIndex

source§

fn clone(&self) -> GridIndex

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 Debug for GridIndex

source§

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

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

impl Display for GridIndex

source§

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

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

impl From<(usize, usize)> for GridIndex

source§

fn from((column, row): (usize, usize)) -> Self

Converts to this type from the input type.
source§

impl Hash for GridIndex

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

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

impl PartialEq for GridIndex

source§

fn eq(&self, other: &GridIndex) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Copy for GridIndex

source§

impl Eq for GridIndex

source§

impl StructuralEq for GridIndex

source§

impl StructuralPartialEq for GridIndex

Auto Trait Implementations§

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> 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,

§

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§

default 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>,

§

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>,

§

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.