[][src]Struct ingrid::Row

pub struct Row<'a, T> {
    pub grid: &'a Grid<T>,
    pub index: usize,
}

A view onto a row of a grid

This structure is an immutable view into a row of a grid and its lifetime is bound to the lifetime of the grid. It's a lightweight construct that allows to operate on individual rows effectively; see it as an equivalent to the slice primitive for grids.

Instead of accessing elements with coordinates, just an index is needed. Rows use the left to right direction, therefore, index zero corresponds to the element at the very left, also denoted the 'first' element of the row. Note that rows are indexable.

With a row, you can easily retrieve the left and right elements of the row, with the left() and right() methods, but also retrieve the row above or below, with the top() and bottom() methods. You can also conveniently iterate over its elements with the iterator() method which returns an efficient iterator.

Because this view is immutable, it's limited in terms of what it can do; check out the mutable counter-part for more operations over the rows.

Examples

Iterating over the elements of a row.

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

let row = grid.row(0);
for (coordinate, value) in row.iterator().enumerate_coordinate() {
    println!("Element at {:?} has value {}.", coordinate, *value);
}

Indexing the row.

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

println!("First element of first row is {}", grid.row(0)[0]);
println!("Last element of last row is {}", grid.row(1)[2]);

Fields

grid: &'a Grid<T>

A reference to its grid.

index: usize

The index of the row.

Methods

impl<'a, T: Clone> Row<'a, T>[src]

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

Returns the length of the row.

This method returns the length of the row which is the number of elements. It's equivalent to the width of the grid.

Examples

let grid = Grid::with_size(size!(3, 2), 42);

assert_eq!(grid.row(0).length(), 3);
assert_eq!(grid.row(1).length(), 3);
assert_eq!(grid.size().width, 3);

pub fn value(&self, index: usize) -> &'a T[src]

Returns a reference to an element of the row.

This method returns a reference to an element of the row from its index.

Note that index zero corresponds to the element at the very left (the first element of the row). If you're looking to get the first or the last elements of the row, check out the left() and right() methods.

Arguments

  • index - Index of the element

Panics

It panics if the index is out of bounds.

Examples

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

let row = grid.row(1);
assert_eq!(row.value(0), &4);
assert_eq!(row.value(1), &5);
assert_eq!(row.value(2), &6);
row.value(3); // It panics here !

pub fn values(&self) -> Vec<&T>[src]

Return the elements of the row.

This method returns the elements of the row as a vector of reference.

Examples

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

assert_eq!(grid.row(0).values(), vec![&1, &2]);
assert_eq!(grid.row(1).values(), vec![&3, &4]);

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

Returns a reference to the first element of the row.

This method returns a reference to the first element of the row. It's equivalent to retrieving the element with index 0.

Note that there is always a first element or the grid would have no size.

Examples

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

// The first element of the second row is 4.
let row = grid.row(1);
assert_eq!(row.left(), &4);

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

Returns a reference to the last element of the row.

This method returns a reference to the last element of the row. It's equivalent to retrieving the element with index length() -1.

Note that there is always a last element or the grid would have no size.

Examples

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

// The last element of the second row is 6.
let row = grid.row(1);
assert_eq!(row.right(), &6);

Important traits for IteratorRow<'a, T>
pub fn iterator(&self) -> IteratorRow<'a, T>[src]

Returns an iterator over the row.

This method returns an iterator over the row.

Examples

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

// Check if all elements of the row have value 42.
assert_eq!(grid.row(0).iterator().all(|item| *item == 42), false);
assert_eq!(grid.row(1).iterator().all(|item| *item == 42), true);

pub fn top(&self) -> Option<Row<'a, T>>[src]

Returns the row above.

This method returns the row above this row, or None if this is already the row at the very top of the grid.

Examples

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

let second_row = grid.row(1);
let first_row = second_row.top().unwrap();
assert!(first_row.top().is_none()); // There is no row above.

pub fn bottom(&self) -> Option<Row<'a, T>>[src]

Returns the row below.

This method returns the row below this row, or None if this is already the row at the very bottom of the grid.

Examples

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

let first_row = grid.row(0);
let second_row = first_row.bottom().unwrap();
assert!(second_row.bottom().is_none()); // There is no row below.

Trait Implementations

impl<'a, T: Clone> Clone for Row<'a, T>[src]

impl<'a, T: Debug> Debug for Row<'a, T>[src]

impl<'a, T: Eq> Eq for Row<'a, T>[src]

impl<'a, T: Clone> Index<usize> for Row<'a, T>[src]

type Output = T

The returned type after indexing.

impl<'a, T: PartialEq> PartialEq<Row<'a, T>> for Row<'a, T>[src]

impl<'a, T> StructuralEq for Row<'a, T>[src]

impl<'a, T> StructuralPartialEq for Row<'a, T>[src]

Auto Trait Implementations

impl<'a, T> RefUnwindSafe for Row<'a, T> where
    T: RefUnwindSafe

impl<'a, T> Send for Row<'a, T> where
    T: Sync

impl<'a, T> Sync for Row<'a, T> where
    T: Sync

impl<'a, T> Unpin for Row<'a, T>

impl<'a, T> UnwindSafe for Row<'a, T> where
    T: RefUnwindSafe

Blanket Implementations

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

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

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

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

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

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

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.

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.