pub struct Array2D<T> { /* private fields */ }
Expand description
A fixed sized two-dimensional array.
Implementations§
Source§impl<T> Array2D<T>
impl<T> Array2D<T>
Sourcepub fn from_columns(elements: &[Vec<T>]) -> Result<Self, Error>where
T: Clone,
pub fn from_columns(elements: &[Vec<T>]) -> Result<Self, Error>where
T: Clone,
Creates a new Array2D
from a slice of columns, each of which
contains a Vec
of elements.
Returns an error 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]]);
Sourcepub fn from_row_major(
elements: &[T],
num_rows: usize,
num_columns: usize,
) -> Result<Self, Error>where
T: Clone,
pub fn from_row_major(
elements: &[T],
num_rows: usize,
num_columns: usize,
) -> Result<Self, Error>where
T: Clone,
Creates a new Array2D
from the given flat slice in row major
order.
Returns an error 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]]);
Sourcepub fn from_column_major(
elements: &[T],
num_rows: usize,
num_columns: usize,
) -> Result<Self, Error>where
T: Clone,
pub fn from_column_major(
elements: &[T],
num_rows: usize,
num_columns: usize,
) -> Result<Self, Error>where
T: Clone,
Creates a new Array2D
from the given flat slice in column major
order.
Return an error 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]]);
Sourcepub fn filled_with(element: T, num_rows: usize, num_columns: usize) -> Selfwhere
T: Clone,
pub fn filled_with(element: T, num_rows: usize, num_columns: usize) -> Selfwhere
T: Clone,
Sourcepub fn fill_with(element: T, num_rows: usize, num_columns: usize) -> Selfwhere
T: Clone,
👎Deprecated since 0.2.0: Renamed to filled_with
pub fn fill_with(element: T, num_rows: usize, num_columns: usize) -> Selfwhere
T: Clone,
Renamed to filled_with.
Sourcepub fn filled_by_row_major<F>(
generator: F,
num_rows: usize,
num_columns: usize,
) -> Selfwhere
F: FnMut() -> T,
pub fn filled_by_row_major<F>(
generator: F,
num_rows: usize,
num_columns: usize,
) -> Selfwhere
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]]);
Sourcepub fn filled_by_column_major<F>(
generator: F,
num_rows: usize,
num_columns: usize,
) -> Self
pub fn filled_by_column_major<F>( generator: F, num_rows: usize, num_columns: usize, ) -> Self
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]]);
Sourcepub fn from_iter_row_major<I>(
iterator: I,
num_rows: usize,
num_columns: usize,
) -> Result<Self, Error>where
I: Iterator<Item = T>,
pub fn from_iter_row_major<I>(
iterator: I,
num_rows: usize,
num_columns: usize,
) -> Result<Self, Error>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. Returns an error 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]]);
Sourcepub fn from_iter_column_major<I>(
iterator: I,
num_rows: usize,
num_columns: usize,
) -> Result<Self, Error>
pub fn from_iter_column_major<I>( iterator: I, num_rows: usize, num_columns: usize, ) -> Result<Self, Error>
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. Returns an error 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]]);
Sourcepub fn num_columns(&self) -> usize
pub fn num_columns(&self) -> usize
The number of columns.
Sourcepub fn num_elements(&self) -> usize
pub fn num_elements(&self) -> usize
The total number of elements, i.e. the product of num_rows
and
num_columns
.
Sourcepub fn column_len(&self) -> usize
pub fn column_len(&self) -> usize
The number of elements in each column, i.e. the number of rows.
Sourcepub fn get_row_major(&self, index: usize) -> Option<&T>
pub fn get_row_major(&self, index: usize) -> Option<&T>
Returns a reference to the element at the given index in row major
order. Returns None
if the index is out of bounds.
§Examples
let rows = vec![vec![1, 2, 3], vec![4, 5, 6]];
let array = Array2D::from_rows(&rows)?;
assert_eq!(array.get_row_major(2), Some(&3));
assert_eq!(array.get_row_major(4), Some(&5));
assert_eq!(array.get_row_major(10), None);
Sourcepub fn get_column_major(&self, index: usize) -> Option<&T>
pub fn get_column_major(&self, index: usize) -> Option<&T>
Returns a reference to the element at the given index in column major
order. Returns None
if the index is out of bounds.
§Examples
let rows = vec![vec![1, 2, 3], vec![4, 5, 6]];
let array = Array2D::from_rows(&rows)?;
assert_eq!(array.get_column_major(2), Some(&2));
assert_eq!(array.get_column_major(4), Some(&3));
assert_eq!(array.get_column_major(10), None);
Sourcepub fn get_mut(&mut self, row: usize, column: usize) -> Option<&mut T>
pub fn get_mut(&mut self, row: usize, column: usize) -> Option<&mut T>
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);
Sourcepub fn get_mut_row_major(&mut self, index: usize) -> Option<&mut T>
pub fn get_mut_row_major(&mut self, index: usize) -> Option<&mut T>
Returns a mutable reference to the element at the given index in row
major order. Returns None
if the index is out of bounds.
§Examples
let rows = vec![vec![1, 2, 3], vec![4, 5, 6]];
let mut array = Array2D::from_rows(&rows)?;
assert_eq!(array.get_mut_row_major(1), Some(&mut 2));
assert_eq!(array.get_mut_row_major(10), None);
array.get_mut_row_major(3).map(|x| *x = 100);
assert_eq!(array.get(1, 0), Some(&100));
array.get_mut_row_major(10).map(|x| *x = 200);
assert_eq!(array.get(10, 10), None);
Sourcepub fn get_mut_column_major(&mut self, index: usize) -> Option<&mut T>
pub fn get_mut_column_major(&mut self, index: usize) -> Option<&mut T>
Returns a mutable reference to the element at the given index in row
major order. Returns None
if the index is out of bounds.
§Examples
let rows = vec![vec![1, 2, 3], vec![4, 5, 6]];
let mut array = Array2D::from_rows(&rows)?;
assert_eq!(array.get_mut_column_major(1), Some(&mut 4));
assert_eq!(array.get_mut_column_major(10), None);
array.get_mut_column_major(4).map(|x| *x = 100);
assert_eq!(array.get(0, 2), Some(&100));
array.get_mut_column_major(10).map(|x| *x = 200);
assert_eq!(array.get(10, 10), None);
Sourcepub fn set(
&mut self,
row: usize,
column: usize,
element: T,
) -> Result<(), Error>
pub fn set( &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
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::IndicesOutOfBounds(10, 20)));
Sourcepub fn set_row_major(&mut self, index: usize, element: T) -> Result<(), Error>
pub fn set_row_major(&mut self, index: usize, element: T) -> Result<(), Error>
Changes the element at the given index
to element
, in row major
order. Returns Ok(())
if the index is in bounds and returns an
Err
otherwise.
§Examples
let mut array = Array2D::filled_with(42, 2, 3);
let result = array.set_row_major(4, 100);
assert_eq!(result, Ok(()));
assert_eq!(array.get(1, 1), Some(&100));
let result = array.set_row_major(10, 200);
assert_eq!(result, Err(Error::IndexOutOfBounds(10)));
Sourcepub fn set_column_major(
&mut self,
index: usize,
element: T,
) -> Result<(), Error>
pub fn set_column_major( &mut self, index: usize, element: T, ) -> Result<(), Error>
Changes the element at the given index
to element
, in column major
order. Returns Ok(())
if the index is in bounds and returns an
Err
otherwise.
§Examples
let mut array = Array2D::filled_with(42, 2, 3);
let result = array.set_column_major(4, 100);
assert_eq!(result, Ok(()));
assert_eq!(array.get(0, 2), Some(&100));
let result = array.set_column_major(10, 200);
assert_eq!(result, Err(Error::IndexOutOfBounds(10)));
Sourcepub fn elements_row_major_iter(
&self,
) -> impl DoubleEndedIterator<Item = &T> + Clone
pub fn elements_row_major_iter( &self, ) -> impl DoubleEndedIterator<Item = &T> + Clone
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);
Sourcepub fn elements_column_major_iter(
&self,
) -> impl DoubleEndedIterator<Item = &T> + Clone
pub fn elements_column_major_iter( &self, ) -> impl DoubleEndedIterator<Item = &T> + Clone
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);
Sourcepub fn row_iter(
&self,
row_index: usize,
) -> Result<impl DoubleEndedIterator<Item = &T> + Clone, Error>
pub fn row_iter( &self, row_index: usize, ) -> Result<impl DoubleEndedIterator<Item = &T> + Clone, Error>
Returns an Iterator
over references to all elements in the given
row. Returns an error if the index is out of bounds.
§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);
Sourcepub fn column_iter(
&self,
column_index: usize,
) -> Result<impl DoubleEndedIterator<Item = &T> + Clone, Error>
pub fn column_iter( &self, column_index: usize, ) -> Result<impl DoubleEndedIterator<Item = &T> + Clone, Error>
Returns an Iterator
over references to all elements in the given
column. Returns an error if the index is out of bounds.
§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);
Sourcepub fn rows_iter(
&self,
) -> impl DoubleEndedIterator<Item = impl DoubleEndedIterator<Item = &T> + Clone> + Clone
pub fn rows_iter( &self, ) -> impl DoubleEndedIterator<Item = impl DoubleEndedIterator<Item = &T> + Clone> + Clone
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());
Sourcepub fn columns_iter(
&self,
) -> impl DoubleEndedIterator<Item = impl DoubleEndedIterator<Item = &T> + Clone> + Clone
pub fn columns_iter( &self, ) -> impl DoubleEndedIterator<Item = impl DoubleEndedIterator<Item = &T> + Clone> + Clone
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());
Sourcepub fn as_columns(&self) -> Vec<Vec<T>>where
T: Clone,
pub fn as_columns(&self) -> Vec<Vec<T>>where
T: Clone,
Sourcepub fn as_row_major(&self) -> Vec<T>where
T: Clone,
pub fn as_row_major(&self) -> Vec<T>where
T: Clone,
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]);
Sourcepub fn as_column_major(&self) -> Vec<T>where
T: Clone,
pub fn as_column_major(&self) -> Vec<T>where
T: Clone,
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]);
Sourcepub fn indices_row_major(
&self,
) -> impl DoubleEndedIterator<Item = (usize, usize)> + Clone
pub fn indices_row_major( &self, ) -> impl DoubleEndedIterator<Item = (usize, usize)> + Clone
Returns the indices of the array in row major order. Each index is a tuple of usize
.
§Examples
let rows = vec![vec![1, 2, 3], vec![4, 5, 6]];
let array = Array2D::from_rows(&rows)?;
let indices_row_major = array.indices_row_major().collect::<Vec<_>>();
assert_eq!(
indices_row_major,
vec![(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2)]
);
Sourcepub fn indices_column_major(
&self,
) -> impl DoubleEndedIterator<Item = (usize, usize)> + Clone
pub fn indices_column_major( &self, ) -> impl DoubleEndedIterator<Item = (usize, usize)> + Clone
Returns the indices of the array in column major order. Each index is a tuple of usize
.
§Examples
let rows = vec![vec![1, 2, 3], vec![4, 5, 6]];
let array = Array2D::from_rows(&rows)?;
let indices_column_major = array.indices_column_major().collect::<Vec<_>>();
assert_eq!(
indices_column_major,
vec![(0, 0), (1, 0), (0, 1), (1, 1), (0, 2), (1, 2)]
);
Sourcepub fn enumerate_row_major(
&self,
) -> impl DoubleEndedIterator<Item = ((usize, usize), &T)> + Clone
pub fn enumerate_row_major( &self, ) -> impl DoubleEndedIterator<Item = ((usize, usize), &T)> + Clone
Iterate through the array in row major order along with the corresponding indices. Each
index is a tuple of usize
.
§Examples
let rows = vec![vec![1, 2, 3], vec![4, 5, 6]];
let array = Array2D::from_rows(&rows)?;
let enumerate_row_major = array.enumerate_row_major().collect::<Vec<_>>();
assert_eq!(
enumerate_row_major,
vec![
((0, 0), &1),
((0, 1), &2),
((0, 2), &3),
((1, 0), &4),
((1, 1), &5),
((1, 2), &6)
]
);
Sourcepub fn enumerate_column_major(
&self,
) -> impl DoubleEndedIterator<Item = ((usize, usize), &T)> + Clone
pub fn enumerate_column_major( &self, ) -> impl DoubleEndedIterator<Item = ((usize, usize), &T)> + Clone
Iterate through the array in column major order along with the corresponding indices. Each
index is a tuple of usize
.
§Examples
let rows = vec![vec![1, 2, 3], vec![4, 5, 6]];
let array = Array2D::from_rows(&rows)?;
let enumerate_column_major = array.enumerate_column_major().collect::<Vec<_>>();
assert_eq!(
enumerate_column_major,
vec![
((0, 0), &1),
((1, 0), &4),
((0, 1), &2),
((1, 1), &5),
((0, 2), &3),
((1, 2), &6)
]
);
Trait Implementations§
Source§impl<T> Index<(usize, usize)> for Array2D<T>
impl<T> Index<(usize, usize)> for Array2D<T>
Source§impl<T> IndexMut<(usize, usize)> for Array2D<T>
impl<T> IndexMut<(usize, usize)> for Array2D<T>
Source§fn index_mut(&mut self, (row, column): (usize, usize)) -> &mut Self::Output
fn index_mut(&mut self, (row, column): (usize, usize)) -> &mut Self::Output
Returns a mutable version of the element at the given indices, given as
(row, column)
.
§Examples
let mut array = Array2D::filled_with(42, 2, 3);
array[(0, 0)] = 100;
assert_eq!(array[(0, 0)], 100);
§Panics
Panics if the indices are out of bounds.
let mut array = Array2D::filled_with(42, 2, 3);
array[(10, 10)] = 7;