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>
impl<T, B: ArrayBackend<T>> Array2D<T, B>
sourcepub fn from_rows(elements: &[Vec<T>]) -> Result<Self, Error>where
T: Clone,
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);
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.
§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]]);
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 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.
§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]]);
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_mut(&mut self, row: usize, column: usize) -> Option<&mut T>where
B: DerefMut,
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);
sourcepub fn row_iter(
&self,
row_index: usize
) -> Result<impl DoubleEndedIterator<Item = &T>, Error>
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);
sourcepub fn rows_iter(
&self
) -> impl DoubleEndedIterator<Item = impl DoubleEndedIterator<Item = &T>>
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());
sourcepub fn into_row_major(self) -> Vec<T>
pub fn into_row_major(self) -> 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]);
sourcepub fn into_row_major_backend(self) -> B
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]);
sourcepub fn switch_backend<B2: ArrayBackend<T> + From<B>>(self) -> Array2D<T, B2>
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]);
sourcepub fn elements_row_major_iter(&self) -> impl DoubleEndedIterator<Item = &T>
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>> Debug for Array2D<T, B>
impl<T, B: ArrayBackend<T>> Debug for Array2D<T, B>
source§impl<T, B: ArrayBackend<T>> Index<(usize, usize)> for Array2D<T, B>
impl<T, B: ArrayBackend<T>> Index<(usize, usize)> for Array2D<T, B>
source§impl<T, B: ArrayBackend<T> + DerefMut> IndexMut<(usize, usize)> for Array2D<T, B>
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
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>
impl<T, B: ArrayBackend<T> + PartialEq> PartialEq for Array2D<T, B>
source§impl<T, B> RustToCuda for Array2D<T, B>
impl<T, B> RustToCuda for Array2D<T, B>
type CudaRepresentation = Array2DCudaRepresentation<T, B>
type CudaAllocation = CombinedCudaAlloc<<B as RustToCuda>::CudaAllocation, NoCudaAlloc>
source§impl<T, B> RustToCudaAsync for Array2D<T, B>
impl<T, B> RustToCudaAsync for Array2D<T, B>
type CudaAllocationAsync = CombinedCudaAlloc<<B as RustToCudaAsync>::CudaAllocationAsync, NoCudaAlloc>
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>where
B: RefUnwindSafe,
T: RefUnwindSafe,
impl<T, B> Send for Array2D<T, B>
impl<T, B> Sync for Array2D<T, B>
impl<T, B> Unpin for Array2D<T, B>
impl<T, B> UnwindSafe for Array2D<T, B>where
B: UnwindSafe,
T: UnwindSafe,
Blanket Implementations§
§impl<T> Backup for Twhere
T: Clone,
impl<T> Backup for Twhere
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<T> ExtractDiscriminant for T
impl<T> ExtractDiscriminant for T
§type Discriminant = <T as ExtractDiscriminantSpec<<T as DiscriminantKind>::Discriminant>>::Ty
type Discriminant = <T as ExtractDiscriminantSpec<<T as DiscriminantKind>::Discriminant>>::Ty
core::mem::Discriminant
. Read moresource§impl<T> LendToCuda for Twhere
T: RustToCuda,
impl<T> LendToCuda for Twhere
T: RustToCuda,
source§fn lend_to_cuda<O, E, F>(&self, inner: F) -> Result<O, E>where
E: From<CudaError>,
F: FnOnce(HostAndDeviceConstRef<'_, DeviceAccessible<<T as RustToCuda>::CudaRepresentation>>) -> Result<O, E>,
T: Sync,
fn lend_to_cuda<O, E, F>(&self, inner: F) -> Result<O, E>where
E: From<CudaError>,
F: FnOnce(HostAndDeviceConstRef<'_, DeviceAccessible<<T as RustToCuda>::CudaRepresentation>>) -> Result<O, E>,
T: Sync,
&self
to CUDA: Read moresource§fn lend_to_cuda_mut<O, E, F>(&mut self, inner: F) -> Result<O, E>where
E: From<CudaError>,
F: FnOnce(HostAndDeviceMutRef<'_, DeviceAccessible<<T as RustToCuda>::CudaRepresentation>>) -> Result<O, E>,
T: Sync + SafeMutableAliasing,
fn lend_to_cuda_mut<O, E, F>(&mut self, inner: F) -> Result<O, E>where
E: From<CudaError>,
F: FnOnce(HostAndDeviceMutRef<'_, DeviceAccessible<<T as RustToCuda>::CudaRepresentation>>) -> Result<O, E>,
T: Sync + SafeMutableAliasing,
source§fn move_to_cuda<O, E, F>(self, inner: F) -> Result<O, E>where
E: From<CudaError>,
F: FnOnce(HostAndDeviceOwned<'_, DeviceAccessible<<T as RustToCuda>::CudaRepresentation>>) -> Result<O, E>,
T: Send + RustToCuda,
<T as RustToCuda>::CudaRepresentation: StackOnly,
<T as RustToCuda>::CudaAllocation: EmptyCudaAlloc,
fn move_to_cuda<O, E, F>(self, inner: F) -> Result<O, E>where
E: From<CudaError>,
F: FnOnce(HostAndDeviceOwned<'_, DeviceAccessible<<T as RustToCuda>::CudaRepresentation>>) -> Result<O, E>,
T: Send + RustToCuda,
<T as RustToCuda>::CudaRepresentation: StackOnly,
<T as RustToCuda>::CudaAllocation: EmptyCudaAlloc,
source§impl<T> LendToCudaAsync for Twhere
T: RustToCudaAsync,
impl<T> LendToCudaAsync for Twhere
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,
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,
&self
to CUDA: Read more