Trait array_math::ArrayNdOps
source · pub trait ArrayNdOps<const D: usize, T, const L: usize>: Array + ArrayNd<D, ItemNd = T> {
type Mapped<M>: ArrayNdOps<D, M, L>;
Show 13 methods
// Required methods
fn as_ptr_nd(&self) -> *const T;
fn as_mut_ptr_nd(&mut self) -> *mut T;
fn fill_nd<F>(fill: F) -> Self
where F: FnMut([usize; D]) -> T;
fn map_nd<M>(self, map: M) -> Self::Mapped<<M as FnOnce(T)>::Output>
where M: FnMut(T);
fn enumerate_nd(self) -> Self::Mapped<([usize; D], T)>;
fn flatten_nd_array(self) -> [T; L];
fn flatten_nd_array_ref(&self) -> &[T; L];
fn flatten_nd_array_mut(&mut self) -> &mut [T; L];
fn each_ref_nd(&self) -> Self::Mapped<&T>;
fn each_mut_nd(&mut self) -> Self::Mapped<&mut T>;
fn reduce_nd<R>(self, reduce: R) -> Option<T>
where R: FnMut(T, T) -> T;
fn get_nd(&self, i: [usize; D]) -> Option<&T>;
fn get_nd_mut(&mut self, i: [usize; D]) -> Option<&mut T>;
}
Expand description
A trait for N-dimensional arrays
Required Associated Types§
type Mapped<M>: ArrayNdOps<D, M, L>
Required Methods§
fn as_ptr_nd(&self) -> *const T
fn as_mut_ptr_nd(&mut self) -> *mut T
sourcefn fill_nd<F>(fill: F) -> Self
fn fill_nd<F>(fill: F) -> Self
Fills an N-dimensional array. Indices passed to fill-function are sorted from outermost to innermost.
Example
#![feature(generic_const_exprs)]
#![feature(const_trait_impl)]
#![feature(const_closures)]
#![feature(const_mut_refs)]
use array_trait::ArrayNdOps;
type T = u8;
let nd: [[T; 3]; 3] = ArrayNdOps::fill_nd(|[i, j]| 1 + 3*i as T + j as T);
assert_eq!(nd, [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]);
sourcefn map_nd<M>(self, map: M) -> Self::Mapped<<M as FnOnce(T)>::Output>where
M: FnMut(T),
fn map_nd<M>(self, map: M) -> Self::Mapped<<M as FnOnce(T)>::Output>where
M: FnMut(T),
Maps each element in the N-dimensional array.
Example
#![feature(generic_const_exprs)]
#![feature(const_trait_impl)]
#![feature(const_closures)]
#![feature(const_mut_refs)]
use array_trait::ArrayNdOps;
const ND: [[u8; 3]; 3] = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
let nd_mapped: [[i8; 3]; 3] = ND.map_nd(const |x: u8| -(x as i8));
assert_eq!(nd_mapped, [
[-1, -2, -3],
[-4, -5, -6],
[-7, -8, -9]
]);
sourcefn enumerate_nd(self) -> Self::Mapped<([usize; D], T)>
fn enumerate_nd(self) -> Self::Mapped<([usize; D], T)>
Enumerates each element of an N-dimensional array. Indices are sorted from outermost to innermost.
Example
#![feature(generic_const_exprs)]
#![feature(const_trait_impl)]
#![feature(const_closures)]
#![feature(const_mut_refs)]
#![feature(generic_arg_infer)]
use array_trait::ArrayNdOps;
type T = u8;
const ND: [[T; 3]; 3] = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
// For now, the compiler cannot infer the type, so type annotations are needed.
let nd_enum: [[([usize; 2], T); 3]; 3] = <[[T; 3]; 3] as ArrayNdOps<2, _, _>>::enumerate_nd(ND);
assert_eq!(nd_enum, [
[([0, 0], 1), ([0, 1], 2), ([0, 2], 3)],
[([1, 0], 4), ([1, 1], 5), ([1, 2], 6)],
[([2, 0], 7), ([2, 1], 8), ([2, 2], 9)]
]);
sourcefn flatten_nd_array(self) -> [T; L]
fn flatten_nd_array(self) -> [T; L]
Flattens one or multiple dimensions of an N-dimensional array.
Example
#![feature(generic_const_exprs)]
#![feature(const_trait_impl)]
use array_trait::ArrayNdOps;
type T = u8;
const ND: [[T; 3]; 3] = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
let flat: [T; 9] = ND.flatten_nd_array();
assert_eq!(flat, [1, 2, 3, 4, 5, 6, 7, 8, 9]);
sourcefn flatten_nd_array_ref(&self) -> &[T; L]
fn flatten_nd_array_ref(&self) -> &[T; L]
Flattens one or multiple dimensions of an N-dimensional array-slice.
Example
#![feature(generic_const_exprs)]
#![feature(const_trait_impl)]
use array_trait::ArrayNdOps;
type T = u8;
const ND: [[T; 3]; 3] = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
let flat: &[T; 9] = ND.flatten_nd_array_ref();
assert_eq!(flat, &[1, 2, 3, 4, 5, 6, 7, 8, 9]);
sourcefn flatten_nd_array_mut(&mut self) -> &mut [T; L]
fn flatten_nd_array_mut(&mut self) -> &mut [T; L]
Flattens one or multiple dimensions of an N-dimensional array-slice
Example
#![feature(generic_const_exprs)]
use array_trait::ArrayNdOps;
type T = u8;
let mut nd: [[T; 3]; 3] = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
let flat: &mut [T; 9] = nd.flatten_nd_array_mut();
for x in flat.into_iter()
{
*x = 10 - *x;
}
assert_eq!(nd, [
[9, 8, 7],
[6, 5, 4],
[3, 2, 1]
]);
fn each_ref_nd(&self) -> Self::Mapped<&T>
fn each_mut_nd(&mut self) -> Self::Mapped<&mut T>
sourcefn reduce_nd<R>(self, reduce: R) -> Option<T>where
R: FnMut(T, T) -> T,
fn reduce_nd<R>(self, reduce: R) -> Option<T>where
R: FnMut(T, T) -> T,
Reduces inner elements in N-dimensional array into one element, using a given operand
Example
#![feature(generic_const_exprs)]
use array_trait::ArrayNdOps;
const A: [[(u8, u8); 3]; 2] = [
[(0, 0), (0, 1), (0, 2)],
[(1, 0), (1, 1), (1, 2)]
];
let r: (u8, u8) = A.reduce_nd(|(a1, a2), (b1, b2)| (a1 + b1, a2 + b2)).unwrap();
assert_eq!(r, (3, 6));
sourcefn get_nd(&self, i: [usize; D]) -> Option<&T>
fn get_nd(&self, i: [usize; D]) -> Option<&T>
Retrieves the inner item using an array of indices, sorted from outermost to innermost, as a reference
Example
#![feature(const_closures)]
#![feature(const_option)]
#![feature(const_trait_impl)]
#![feature(generic_const_exprs)]
#![feature(const_mut_refs)]
use array_trait::ArrayNdOps;
const A: [[u8; 2]; 3] = [
[1, 2],
[3, 4],
[5, 6]
];
let b: [[u8; 2]; 3] = ArrayNdOps::fill_nd(|[i, j]| {
let item = *A.get_nd([i, j]).unwrap();
assert_eq!(item, A[i][j]);
item
});
assert_eq!(A, b);
sourcefn get_nd_mut(&mut self, i: [usize; D]) -> Option<&mut T>
fn get_nd_mut(&mut self, i: [usize; D]) -> Option<&mut T>
Retrieves the inner item using an array of indices, sorted from outermost to innermost, as a mutable reference
Example
#![feature(const_closures)]
#![feature(const_option)]
#![feature(const_trait_impl)]
#![feature(generic_const_exprs)]
#![feature(const_mut_refs)]
use array_trait::ArrayNdOps;
const A: [[u8; 2]; 3] = [
[1, 2],
[3, 4],
[5, 6]
];
let mut b: [[u8; 2]; 3] = [[0; 2]; 3];
let mut i = 0;
while i < 3
{
let mut j = 0;
while j < 2
{
let item = *A.get_nd([i, j]).unwrap();
assert_eq!(item, A[i][j]);
*b.get_nd_mut([i, j]).unwrap() = item;
j += 1;
}
i += 1;
}
assert_eq!(A, b);