[][src]Struct ingrid::ColumnMut

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

A mutable view onto a column of a grid

This structure is an mutable view into a column 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 columns effectively; see it as an equivalent to the slice primitive for grids.

Instead of accessing elements with coordinates, just an index is needed. Columns use the top to bottom direction, therefore, index zero corresponds to the element at the very top, also denoted the 'first' element of the column. Note that columns are indexable.

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

Unlike their immutable counter-part, there are additional operations you can do such as reversing the elements of the column, or rotate them to the top or the bottom.

Examples

Iterating over the elements of a column.

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

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

Indexing the column.

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

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

Fields

grid: &'a mut Grid<T>

A reference to its grid.

index: usize

The index of the column.

Methods

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

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

Returns the length of the column.

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

Examples

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

assert_eq!(grid.column_mut(0).length(), 3);
assert_eq!(grid.column_mut(1).length(), 3);
assert_eq!(grid.size().height, 3);

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

Returns a reference to an element of the column.

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

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

Arguments

  • index - Index of the element

Panics

It panics if the index is out of bounds.

Examples

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

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

pub fn value_mut(&mut self, index: usize) -> &mut T[src]

Returns a mutable reference to an element of the column.

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

Arguments

  • index - Index of the element

Panics

It panics if the index is out of bounds.

Examples

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

let mut column = grid.column_mut(1);
*column.value_mut(1) = 4;

assert_eq!(column.value(0), &2);
assert_eq!(column.value(1), &4);
assert_eq!(column.value(2), &6);

column.value(3); // It panics here !

pub fn set_value(&mut self, index: usize, value: T)[src]

Replace an element of the column.

This method replaces the value of an element of the column from its index and a new value, effectively dropping the previous value.

Arguments

  • index - Index of the element
  • value - New value of the element

Panics

It panics if the index is out of bounds.

Examples

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

let mut column = grid.column_mut(1);
column.set_value(1, 4);

assert_eq!(column.value(0), &2);
assert_eq!(column.value(1), &4);
assert_eq!(column.value(2), &6);

column.set_value(3, 42); // It panics here !

pub fn swap_value(&mut self, a: usize, b: usize)[src]

Swap two elements of the column.

This method swaps two elements of the column from their index.

Arguments

  • a - Index of one of the element to swap
  • b - Index of the other element to be swapped with

Panics

It panics if the indexes are out of bounds.

Examples

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

let mut column = grid.column_mut(1);
column.swap_value(0, 2);

assert_eq!(column.value(0), &2);
assert_eq!(column.value(1), &4);
assert_eq!(column.value(2), &6);

column.swap_value(1, 3); // It panics here !

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

Return the elements of the column.

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

Examples

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

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

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

Returns a reference to the first element of the column.

This method returns a reference to the first element of the column. 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 mut grid = Grid::from_rows(vec![vec![1, 2],
                                    vec![3, 4],
                                    vec![5, 6]]);

// The first element of the second column is 2.
let column = grid.column_mut(1);
assert_eq!(column.top(), &2);

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

Returns a mutable reference to the first element of the column.

This method returns a mutable reference to the first element of the column. 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 mut grid = Grid::from_rows(vec![vec![1, 0],
                                    vec![3, 4],
                                    vec![5, 6]]);

// The first element of the second column is 0 but we can change it to 2.
let mut column = grid.column_mut(1);
*column.top_mut() = 2;

assert_eq!(column.top(), &2);

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

Returns a reference to the last element of the column.

This method returns a reference to the last element of the column. 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 mut grid = Grid::from_rows(vec![vec![1, 2],
                                    vec![3, 4],
                                    vec![5, 6]]);

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

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

Returns a mutable reference to the last element of the column.

This method returns a mutable reference to the last element of the column. 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 mut grid = Grid::from_rows(vec![vec![1, 2],
                                    vec![3, 4],
                                    vec![5, 0]]);

// The last element of the second column is 0 but we can change it to 6.
let mut column = grid.column_mut(1);
*column.bottom_mut() = 6;

assert_eq!(column.bottom_mut(), &6);

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

Returns an iterator over the column.

This method returns an iterator over the column.

Examples

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

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

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

Returns the column on the left.

This method returns the column on the left of this column, or None if this is already the column at the very left of the grid.

Examples

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

let second_column = grid.column_mut(1);
let first_column = second_column.left().unwrap();
assert!(first_column.left().is_none()); // There is no column on the left.

pub fn left_mut(&'a mut self) -> Option<ColumnMut<'a, T>>[src]

Returns the mutable column on the left.

This method returns the mutable column on the left of this row, or None if this is already the column at the very left of the grid.

Examples

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

let mut second_column = grid.column_mut(1);
let mut first_column = second_column.left_mut().unwrap();
assert!(first_column.left_mut().is_none()); // There is no column on the left.

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

Returns the column on the right.

This method returns the column on the right of this column, or None if this is already the column at the very right of the grid.

Examples

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

let mut first_column = grid.column_mut(0);
let second_column = first_column.right().unwrap();
assert!(second_column.right().is_none()); // There is no column on the right.

pub fn right_mut(&'a mut self) -> Option<ColumnMut<'a, T>>[src]

Returns the mutable column on the right.

This method returns the mutable column on the right of this column, or None if this is already the column at the very right of the grid.

Examples

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

let mut first_column = grid.column_mut(0);
let mut second_column = first_column.right_mut().unwrap();
assert!(second_column.right_mut().is_none()); // There is no column on the right.

pub fn reverse(&mut self)[src]

Reverse the order of the elements.

This method reverses the order of the elements in the column, in place.

Note that it's similar to the reverse() method of the slice primitive type.

Examples

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

let mut column = grid.column_mut(1);
column.reverse();

assert_eq!(column.value(0), &2);
assert_eq!(column.value(1), &4);
assert_eq!(column.value(2), &6);

pub fn rotate_top(&mut self, number: usize)[src]

Rotate elements to the top.

This method rotates the column in-place such that the elements are moved a given number of times to the top. The elements that goes out of the column are added back to the bottom of the column.

Note that it's similar to the rotate_left() method of the slice primitive type.

Arguments

  • number - The number of times elements are rotated

Panics

This function will panic if number is greater than the length of the column.

Examples

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

let mut column = grid.column_mut(1);
column.rotate_top(1);

assert_eq!(column.value(0), &4);
assert_eq!(column.value(1), &6);
assert_eq!(column.value(2), &2);

pub fn rotate_bottom(&mut self, number: usize)[src]

Rotate elements to the bottom.

This method rotates the column in-place such that the elements are moved a given number of times to the bottom. The elements that goes out of the column are added back to the top of the column.

Note that it's similar to the rotate_right() method of the slice primitive type.

Arguments

  • number - The number of times elements are rotated

Panics

This function will panic if number is greater than the length of the column.

Examples

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

let mut column = grid.column_mut(1);
column.rotate_bottom(1);

assert_eq!(column.value(0), &6);
assert_eq!(column.value(1), &2);
assert_eq!(column.value(2), &4);

pub fn swap(&mut self, a: usize, b: usize)[src]

Swap two elements in the column.

This method swaps two elements in the column.

Note that it's similar to the swap() method of the slice primitive type.

Arguments

  • a - The index of the first element
  • b - The index of the second element

Panics

It panics if a or b are out of bounds.

Examples

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

let mut column = grid.column_mut(1);
column.swap(0, 2);

assert_eq!(column.value(0), &2);
assert_eq!(column.value(1), &4);
assert_eq!(column.value(2), &6);

Trait Implementations

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

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

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

type Output = T

The returned type after indexing.

impl<'a, T: Clone> IndexMut<usize> for ColumnMut<'a, T>[src]

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

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

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

Auto Trait Implementations

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

impl<'a, T> Send for ColumnMut<'a, T> where
    T: Send

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

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

impl<'a, T> !UnwindSafe for ColumnMut<'a, T>

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