Type Alias necsim_impls_no_std::array2d::ArcArray2D

source ·
pub type ArcArray2D<T> = Array2D<T, Arc<[T]>>;

Aliased Type§

struct ArcArray2D<T> { /* private fields */ }

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>