[−][src]Struct array2d::Array2D
A fixed sized two-dimensional array.
Methods
impl<T: Clone> Array2D<T>
[src]
pub fn from_rows(elements: &[Vec<T>]) -> Self
[src]
Creates a new Array2D
from a Vec
of rows, each of which is a
Vec
of elements.
Panics
Panics if the rows are not all the same size.
Examples
let rows = vec![vec![1, 2, 3], vec![4, 5, 6]]; let array = Array2D::from_rows(&rows); assert_eq!(array[(1, 2)], 6); assert_eq!(array.as_rows(), rows);
pub fn from_columns(elements: &[Vec<T>]) -> Self
[src]
Creates a new Array2D
from a Vec
of columns, each of which
contains a Vec
of elements.
Panics
Panics if the columns are not all the same size.
Examples
let columns = vec![vec![1, 4], vec![2, 5], vec![3, 6]]; let array = Array2D::from_columns(&columns); assert_eq!(array[(1, 2)], 6); assert_eq!(array.as_rows(), vec![vec![1, 2, 3], vec![4, 5, 6]]);
pub fn from_row_major(
elements: &[T],
num_rows: usize,
num_columns: usize
) -> Self
[src]
elements: &[T],
num_rows: usize,
num_columns: usize
) -> Self
Creates a new Array2D
from the given flat Vec
in row major
order.
Panics
Panics if the number of elements in elements
is not the product of
num_rows
and num_columns
, i.e. the dimensions do not match.
Examples
let row_major = vec![1, 2, 3, 4, 5, 6]; let array = Array2D::from_row_major(&row_major, 2, 3); assert_eq!(array[(1, 2)], 6); assert_eq!(array.as_rows(), vec![vec![1, 2, 3], vec![4, 5, 6]]);
pub fn from_column_major(
elements: &[T],
num_rows: usize,
num_columns: usize
) -> Self
[src]
elements: &[T],
num_rows: usize,
num_columns: usize
) -> Self
Creates a new Array2D
from the given flat Vec
in column major
order.
Panics
Panics if the number of elements in elements
is not the product of
num_rows
and num_columns
, i.e. the dimensions do not match.
Examples
let column_major = vec![1, 4, 2, 5, 3, 6]; let array = Array2D::from_column_major(&column_major, 2, 3); assert_eq!(array[(1, 2)], 6); assert_eq!(array.as_rows(), vec![vec![1, 2, 3], vec![4, 5, 6]]);
pub fn filled_with(element: T, num_rows: usize, num_columns: usize) -> Self
[src]
Creates a new Array2D
with the specified number of rows and columns
that contains element
in every location.
Examples
let array = Array2D::filled_with(42, 2, 3); assert_eq!(array.as_rows(), vec![vec![42, 42, 42], vec![42, 42, 42]]);
pub fn fill_with(element: T, num_rows: usize, num_columns: usize) -> Self
[src]
Renamed to filled_with
Renamed to filled_with.
pub fn filled_by_row_major<F>(
generator: F,
num_rows: usize,
num_columns: usize
) -> Self where
F: FnMut() -> T,
[src]
generator: F,
num_rows: usize,
num_columns: usize
) -> Self where
F: FnMut() -> T,
Creates a new Array2D
with the specified number of rows and columns
and fills each element with the result of calling the given
function. The function is called once for every location going in
row major order.
Examples
let mut counter = 1; let increment = || { let tmp = counter; counter += 1; tmp }; let array = Array2D::filled_by_row_major(increment, 2, 3); assert_eq!(array.as_rows(), vec![vec![1, 2, 3], vec![4, 5, 6]]);
pub fn filled_by_column_major<F>(
generator: F,
num_rows: usize,
num_columns: usize
) -> Self where
F: FnMut() -> T,
[src]
generator: F,
num_rows: usize,
num_columns: usize
) -> Self where
F: FnMut() -> T,
Creates a new Array2D
with the specified number of rows and columns
and fills each element with the result of calling the given
function. The function is called once for every location going in
column major order.
Examples
let mut counter = 1; let increment = || { let tmp = counter; counter += 1; tmp }; let array = Array2D::filled_by_column_major(increment, 2, 3); assert_eq!(array.as_columns(), vec![vec![1, 2], vec![3, 4], vec![5, 6]]);
pub fn from_iter_row_major<I>(
iterator: I,
num_rows: usize,
num_columns: usize
) -> Self where
I: Iterator<Item = T>,
[src]
iterator: I,
num_rows: usize,
num_columns: usize
) -> Self where
I: Iterator<Item = T>,
Creates a new Array2D
with the specified number of rows and columns
and fills each element with the elements produced from the provided
iterator. If the iterator produces more than enough elements, the
remaining are unused. Panics if the iterator does not produce enough
elements.
The elements are inserted into the array in row major order.
Examples
let iterator = (1..); let array = Array2D::from_iter_row_major(iterator, 2, 3); assert_eq!(array.as_rows(), vec![vec![1, 2, 3], vec![4, 5, 6]]);
Panics
If the iterator provided does not produce enough elements.
pub fn from_iter_column_major<I>(
iterator: I,
num_rows: usize,
num_columns: usize
) -> Self where
I: Iterator<Item = T>,
[src]
iterator: I,
num_rows: usize,
num_columns: usize
) -> Self where
I: Iterator<Item = T>,
Creates a new Array2D
with the specified number of rows and columns
and fills each element with the elements produced from the provided
iterator. If the iterator produces more than enough elements, the
remaining are unused. Panics if the iterator does not produce enough
elements.
The elements are inserted into the array in column major order.
Examples
let iterator = (1..); let array = Array2D::from_iter_column_major(iterator, 2, 3); assert_eq!(array.as_rows(), vec![vec![1, 3, 5], vec![2, 4, 6]]);
Panics
If the iterator provided does not produce enough elements.
pub fn num_rows(&self) -> usize
[src]
The number of rows.
pub fn num_columns(&self) -> usize
[src]
The number of columns.
pub fn num_elements(&self) -> usize
[src]
The total number of elements, i.e. the product of num_rows
and
num_columns
pub fn row_len(&self) -> usize
[src]
The number of elements in each row, i.e. the number of columns.
pub fn column_len(&self) -> usize
[src]
The number of elements in each column, i.e. the number of rows.
pub fn get(&self, row: usize, column: usize) -> Option<&T>
[src]
Returns a reference to the element at the given row
and column
if the
index is in bounds (wrapped in Some
). Returns None
if the index
is out of bounds.
Examples
let array = Array2D::filled_with(42, 2, 3); assert_eq!(array.get(0, 0), Some(&42)); assert_eq!(array.get(10, 10), None);
pub fn get_mut(&mut self, row: usize, column: usize) -> Option<&mut T>
[src]
Returns a mutable reference to the element at the given row
and
column
if the index is in bounds (wrapped in Some
). Returns
None
if the index is out of bounds.
Examples
let mut array = Array2D::filled_with(42, 2, 3); assert_eq!(array.get_mut(0, 0), Some(&mut 42)); assert_eq!(array.get_mut(10, 10), None); array.get_mut(0, 0).map(|x| *x = 100); assert_eq!(array.get(0, 0), Some(&100)); array.get_mut(10, 10).map(|x| *x = 200); assert_eq!(array.get(10, 10), None);
pub fn set(
&mut self,
row: usize,
column: usize,
element: T
) -> Result<(), Error>
[src]
&mut self,
row: usize,
column: usize,
element: T
) -> Result<(), Error>
Changes the element at given row
and column
to element
. Returns
Ok(())
if the indices were in bounds and returns an
Err
(
Error
)
otherwise.
Examples
let mut array = Array2D::filled_with(42, 2, 3); let result = array.set(0, 0, 100); assert_eq!(result, Ok(())); assert_eq!(array.get(0, 0), Some(&100)); let result = array.set(10, 20, 200); assert_eq!(result, Err(Error::IndexOutOfBounds(10, 20)));
pub fn elements_row_major_iter(&self) -> impl Iterator<Item = &T>
[src]
Returns an Iterator
over references to all elements in row major
order.
Examples
let rows = vec![vec![1, 2, 3], vec![4, 5, 6]]; let elements = vec![1, 2, 3, 4, 5, 6]; let array = Array2D::from_rows(&rows); let row_major = array.elements_row_major_iter(); assert_eq!(row_major.cloned().collect::<Vec<_>>(), elements);
pub fn elements_column_major_iter(&self) -> impl Iterator<Item = &T>
[src]
Returns an Iterator
over references to all elements in column major
order.
Examples
let rows = vec![vec![1, 2, 3], vec![4, 5, 6]]; let elements = vec![1, 4, 2, 5, 3, 6]; let array = Array2D::from_rows(&rows); let column_major = array.elements_column_major_iter(); assert_eq!(column_major.cloned().collect::<Vec<_>>(), elements);
pub fn row_iter(&self, row_index: usize) -> impl Iterator<Item = &T>
[src]
Returns an Iterator
over references to all elements in the given
row.
Examples
let rows = vec![vec![1, 2, 3], vec![4, 5, 6]]; let array = Array2D::from_rows(&rows); let mut row_iter = array.row_iter(1); assert_eq!(row_iter.next(), Some(&4)); assert_eq!(row_iter.next(), Some(&5)); assert_eq!(row_iter.next(), Some(&6)); assert_eq!(row_iter.next(), None);
Panics
Panics if row_index
is out of bounds.
pub fn column_iter(&self, column_index: usize) -> impl Iterator<Item = &T>
[src]
Returns an Iterator
over references to all elements in the given
column.
Examples
let rows = vec![vec![1, 2, 3], vec![4, 5, 6]]; let array = Array2D::from_rows(&rows); let mut column_iter = array.column_iter(1); assert_eq!(column_iter.next(), Some(&2)); assert_eq!(column_iter.next(), Some(&5)); assert_eq!(column_iter.next(), None);
Panics
Panics if column_index
is out of bounds.
pub fn rows_iter(&self) -> impl Iterator<Item = impl Iterator<Item = &T>>
[src]
Returns an Iterator
over all rows. Each Item
is itself another
Iterator
over references to the elements in that row.
Examples
let rows = vec![vec![1, 2, 3], vec![4, 5, 6]]; let array = Array2D::from_rows(&rows); for row_iter in array.rows_iter() { for element in row_iter { print!("{} ", element); } println!(); } let mut rows_iter = array.rows_iter(); let mut first_row_iter = rows_iter.next().unwrap(); assert_eq!(first_row_iter.next(), Some(&1)); assert_eq!(first_row_iter.next(), Some(&2)); assert_eq!(first_row_iter.next(), Some(&3)); assert_eq!(first_row_iter.next(), None); let mut second_row_iter = rows_iter.next().unwrap(); assert_eq!(second_row_iter.next(), Some(&4)); assert_eq!(second_row_iter.next(), Some(&5)); assert_eq!(second_row_iter.next(), Some(&6)); assert_eq!(second_row_iter.next(), None); assert!(rows_iter.next().is_none());
pub fn columns_iter(&self) -> impl Iterator<Item = impl Iterator<Item = &T>>
[src]
Returns an Iterator
over all columns. Each Item
is itself
another Iterator
over references to the elements in that column.
Examples
let rows = vec![vec![1, 2, 3], vec![4, 5, 6]]; let array = Array2D::from_rows(&rows); for column_iter in array.columns_iter() { for element in column_iter { print!("{} ", element); } println!(); } let mut columns_iter = array.columns_iter(); let mut first_column_iter = columns_iter.next().unwrap(); assert_eq!(first_column_iter.next(), Some(&1)); assert_eq!(first_column_iter.next(), Some(&4)); assert_eq!(first_column_iter.next(), None); let mut second_column_iter = columns_iter.next().unwrap(); assert_eq!(second_column_iter.next(), Some(&2)); assert_eq!(second_column_iter.next(), Some(&5)); assert_eq!(second_column_iter.next(), None); let mut third_column_iter = columns_iter.next().unwrap(); assert_eq!(third_column_iter.next(), Some(&3)); assert_eq!(third_column_iter.next(), Some(&6)); assert_eq!(third_column_iter.next(), None); assert!(columns_iter.next().is_none());
pub fn as_rows(&self) -> Vec<Vec<T>>
[src]
Collects the Array2D
into a Vec
of rows, each of which contains
a Vec
of elements.
Examples
let rows = vec![vec![1, 2, 3], vec![4, 5, 6]]; let array = Array2D::from_rows(&rows); assert_eq!(array.as_rows(), rows);
pub fn as_columns(&self) -> Vec<Vec<T>>
[src]
Collects the Array2D
into a Vec
of columns, each of which
contains a Vec
of elements.
Examples
let columns = vec![vec![1, 4], vec![2, 5], vec![3, 6]]; let array = Array2D::from_columns(&columns); assert_eq!(array.as_columns(), columns);
pub fn as_row_major(&self) -> Vec<T>
[src]
Collects the Array2D
into a Vec
of elements in row major
order.
Examples
let rows = vec![vec![1, 2, 3], vec![4, 5, 6]]; let array = Array2D::from_rows(&rows); assert_eq!(array.as_row_major(), vec![1, 2, 3, 4, 5, 6]);
pub fn as_column_major(&self) -> Vec<T>
[src]
Collects the Array2D
into a Vec
of elements in column major
order.
Examples
let rows = vec![vec![1, 2, 3], vec![4, 5, 6]]; let array = Array2D::from_rows(&rows); assert_eq!(array.as_column_major(), vec![1, 4, 2, 5, 3, 6]);
Trait Implementations
impl<T: Eq + Clone> Eq for Array2D<T>
[src]
impl<T: PartialEq + Clone> PartialEq<Array2D<T>> for Array2D<T>
[src]
impl<T: Clone + Clone> Clone for Array2D<T>
[src]
fn clone(&self) -> Array2D<T>
[src]
fn clone_from(&mut self, source: &Self)
1.0.0[src]
Performs copy-assignment from source
. Read more
impl<T: Debug + Clone> Debug for Array2D<T>
[src]
impl<T: Clone> Index<(usize, usize)> for Array2D<T>
[src]
type Output = T
The returned type after indexing.
fn index(&self, indices: (usize, usize)) -> &Self::Output
[src]
impl<T: Clone> IndexMut<(usize, usize)> for Array2D<T>
[src]
Auto Trait Implementations
Blanket Implementations
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> From<T> for T
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,