Struct array2d::Array2D

source ·
pub struct Array2D<T> { /* private fields */ }
Expand description

A fixed sized two-dimensional array.

Implementations§

source§

impl<T> Array2D<T>

source

pub fn from_rows(elements: &[Vec<T>]) -> Result<Self, Error>
where T: Clone,

Creates a new Array2D from a slice of rows, each of which is a Vec of elements.

Returns an error 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);
source

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]]);
source

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]]);
source

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]]);
source

pub fn filled_with(element: T, num_rows: usize, num_columns: usize) -> Self
where T: Clone,

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]]);
source

pub fn fill_with(element: T, num_rows: usize, num_columns: usize) -> Self
where T: Clone,

👎Deprecated since 0.2.0: Renamed to filled_with

Renamed to filled_with.

source

pub fn filled_by_row_major<F>( 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]]);
source

pub fn filled_by_column_major<F>( generator: F, num_rows: usize, num_columns: usize ) -> Self
where F: FnMut() -> T, T: Clone,

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]]);
source

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]]);
source

pub fn from_iter_column_major<I>( iterator: I, num_rows: usize, num_columns: usize ) -> Result<Self, Error>
where I: Iterator<Item = T>, T: Clone,

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]]);
source

pub fn num_rows(&self) -> usize

The number of rows.

source

pub fn num_columns(&self) -> usize

The number of columns.

source

pub fn num_elements(&self) -> usize

The total number of elements, i.e. the product of num_rows and num_columns.

source

pub fn row_len(&self) -> usize

The number of elements in each row, i.e. the number of columns.

source

pub fn column_len(&self) -> usize

The number of elements in each column, i.e. the number of rows.

source

pub fn get(&self, row: usize, column: usize) -> Option<&T>

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);
source

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);
source

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);
source

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);
source

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);
source

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);
source

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)));
source

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)));
source

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)));
source

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);
source

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);
source

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);
source

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);
source

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());
source

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());
source

pub fn as_rows(&self) -> Vec<Vec<T>>
where T: Clone,

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);
source

pub fn as_columns(&self) -> Vec<Vec<T>>
where T: Clone,

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);
source

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]);
source

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]);
source

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)]
);
source

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)]
);
source

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)
    ]
);
source

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: Clone> Clone for Array2D<T>

source§

fn clone(&self) -> Array2D<T>

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<T: Debug> Debug for Array2D<T>

source§

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

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

impl<T: Hash> Hash for Array2D<T>

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<T> Index<(usize, usize)> for Array2D<T>

source§

fn index(&self, (row, column): (usize, usize)) -> &Self::Output

Returns the element at the given indices, given as (row, column).

Examples
let array = Array2D::filled_with(42, 2, 3);
assert_eq!(array[(0, 0)], 42);
Panics

Panics if the indices are out of bounds.

let array = Array2D::filled_with(42, 2, 3);
let element = array[(10, 10)];
§

type Output = T

The returned type after indexing.
source§

impl<T> IndexMut<(usize, usize)> for Array2D<T>

source§

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;
source§

impl<T: PartialEq> PartialEq for Array2D<T>

source§

fn eq(&self, other: &Array2D<T>) -> 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<T: Eq> Eq for Array2D<T>

source§

impl<T> StructuralEq for Array2D<T>

source§

impl<T> StructuralPartialEq for Array2D<T>

Auto Trait Implementations§

§

impl<T> RefUnwindSafe for Array2D<T>
where T: RefUnwindSafe,

§

impl<T> Send for Array2D<T>
where T: Send,

§

impl<T> Sync for Array2D<T>
where T: Sync,

§

impl<T> Unpin for Array2D<T>
where T: Unpin,

§

impl<T> UnwindSafe for Array2D<T>
where T: UnwindSafe,

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