Struct necsim_impls_no_std::array2d::Array2D

source ·
pub struct Array2D<T, B: ArrayBackend<T> = Box<[T]>> { /* private fields */ }
Expand description

A fixed sized two-dimensional array.

Implementations§

source§

impl<T, B: ArrayBackend<T>> Array2D<T, B>

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.

§Errors

Returns an DimensionMismatch 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_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.

§Errors

Returns a DimensionMismatch error if elements.len() != num_rows * num_columns

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

§Errors

Returns a NotEnoughElements error if iterator.len() < num_rows * num_columns

§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 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_mut(&mut self, row: usize, column: usize) -> Option<&mut T>
where B: DerefMut,

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 row_iter( &self, row_index: usize ) -> Result<impl DoubleEndedIterator<Item = &T>, Error>

Returns an Iterator over references to all elements in the given row. Returns an error if the index is out of bounds.

§Errors

Returns a IndicesOutOfBounds(row_index, 0) error if row_index >= self.num_rows()

§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 rows_iter( &self ) -> impl DoubleEndedIterator<Item = impl DoubleEndedIterator<Item = &T>>

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 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 into_row_major(self) -> Vec<T>
where B: Into<Vec<T>>,

Converts 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.into_row_major(), vec![1, 2, 3, 4, 5, 6]);
source

pub fn into_row_major_backend(self) -> B

Converts the Array2D into its inner ArrayBackend B of elements in row major order.

§Examples
let rows = vec![vec![1, 2, 3], vec![4, 5, 6]];
let array = VecArray2D::from_rows(&rows)?;
assert_eq!(array.into_row_major_backend(), vec![1, 2, 3, 4, 5, 6]);
source

pub fn switch_backend<B2: ArrayBackend<T> + From<B>>(self) -> Array2D<T, B2>

Converts the Array2D into from its current into a new ArrayBackend.

§Examples
let rows = vec![vec![1, 2, 3], vec![4, 5, 6]];
let array = VecArray2D::from_rows(&rows)?;
let array: BoxArray2D<_> = array.switch_backend();
assert_eq!(array.into_row_major(), vec![1, 2, 3, 4, 5, 6]);
source

pub fn elements_row_major_iter(&self) -> impl DoubleEndedIterator<Item = &T>

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

Trait Implementations§

source§

impl<T, B: ArrayBackend<T> + Clone> Clone for Array2D<T, B>

source§

fn clone(&self) -> Self

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

source§

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

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

impl<T, B: ArrayBackend<T>> Index<(usize, usize)> for Array2D<T, B>

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, B: ArrayBackend<T> + DerefMut> IndexMut<(usize, usize)> for Array2D<T, B>

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, B: ArrayBackend<T> + PartialEq> PartialEq for Array2D<T, B>

source§

fn eq(&self, other: &Self) -> 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, B> RustToCuda for Array2D<T, B>
where T: PortableBitSemantics + TypeGraphLayout, B: RustToCuda + ArrayBackend<T>,

§

type CudaRepresentation = Array2DCudaRepresentation<T, B>

§

type CudaAllocation = CombinedCudaAlloc<<B as RustToCuda>::CudaAllocation, NoCudaAlloc>

source§

impl<T, B> RustToCudaAsync for Array2D<T, B>
where T: PortableBitSemantics + TypeGraphLayout, B: RustToCudaAsync + ArrayBackend<T>,

source§

impl<T, B: ArrayBackend<T> + Eq> Eq for Array2D<T, B>

Auto Trait Implementations§

§

impl<T, B> Freeze for Array2D<T, B>
where B: Freeze,

§

impl<T, B> RefUnwindSafe for Array2D<T, B>

§

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

§

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

§

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

§

impl<T, B> UnwindSafe for Array2D<T, B>
where B: UnwindSafe, 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
§

impl<T> Backup for T
where T: Clone,

§

default unsafe fn backup_unchecked(&self) -> T

§

default unsafe fn __contracts_impl_backup_unchecked(&self) -> T

§

fn backup(&self) -> BackedUp<Self>

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
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
§

impl<T> ExtractDiscriminant for T

§

type Discriminant = <T as ExtractDiscriminantSpec<<T as DiscriminantKind>::Discriminant>>::Ty

The type of the discriminant, which must satisfy the trait bounds required by core::mem::Discriminant. 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> LendToCuda for T
where T: RustToCuda,

source§

fn lend_to_cuda<O, E, F>(&self, inner: F) -> Result<O, E>

Lends an immutable borrow of &self to CUDA: Read more
source§

fn lend_to_cuda_mut<O, E, F>(&mut self, inner: F) -> Result<O, E>

Lends a mutable borrow of &mut self to CUDA iff Self is SafeMutableAliasing: Read more
source§

fn move_to_cuda<O, E, F>(self, inner: F) -> Result<O, E>

Moves self to CUDA iff Self is StackOnly. Read more
source§

impl<T> LendToCudaAsync for T
where T: RustToCudaAsync,

source§

fn lend_to_cuda_async<'stream, O, E, F>( &self, stream: Stream<'stream>, inner: F ) -> Result<O, E>
where E: From<CudaError>, F: FnOnce(Async<'_, 'stream, HostAndDeviceConstRef<'_, DeviceAccessible<<T as RustToCuda>::CudaRepresentation>>>) -> Result<O, E>, T: Sync,

Lends an immutable copy of &self to CUDA: Read more
source§

fn lend_to_cuda_mut_async<'a, 'stream, O, E, F, S>( this: OwningRefMut<'a, Box<S>, T>, stream: Stream<'stream>, inner: F ) -> Result<(Async<'a, 'stream, OwningRefMut<'a, Box<S>, T>, Box<dyn FnOnce(&mut T) -> Result<(), CudaError> + 'a>>, O), E>
where E: From<CudaError>, F: for<'b> FnOnce(Async<'b, 'stream, HostAndDeviceMutRef<'_, DeviceAccessible<<T as RustToCuda>::CudaRepresentation>>>) -> Result<O, E>, S: 'a, T: Sync + SafeMutableAliasing,

Lends a mutable borrow of &mut self to CUDA iff Self is SafeMutableAliasing: Read more
source§

fn move_to_cuda_async<'stream, O, E, F>( self, stream: Stream<'stream>, inner: F ) -> Result<O, E>

Moves self to CUDA iff self is StackOnly. Read more
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.
source§

impl<T> Data for T
where T: Send + Clone,

§

impl<T> Erased for T

source§

impl<T> PortableBitSemantics for T
where T: PortableBitSemantics + ?Sized,

source§

impl<T> StackOnly for T
where T: StackOnly,