pub trait ArrayIndexing<T: ArrayElement>where
    Self: Sized + Clone,{
    // Required methods
    fn index_at(&self, coords: &[usize]) -> Result<usize, ArrayError>;
    fn index_to_coord(&self, idx: usize) -> Result<Vec<usize>, ArrayError>;
    fn at(&self, coords: &[usize]) -> Result<T, ArrayError>;
    fn slice(&self, range: Range<usize>) -> Result<Array<T>, ArrayError>;
    fn indices_at(&self, indices: &[usize]) -> Result<Array<T>, ArrayError>;
}
Expand description

ArrayTrait - Array Indexing functions

Required Methods§

source

fn index_at(&self, coords: &[usize]) -> Result<usize, ArrayError>

Return an index of element at the given coordinates

Arguments
  • coords - vector representing the coordinates of the element in array
Examples
use arr_rs::prelude::*;

let arr: Array<i32> = Array::new(vec![1,2,3,4,5,6,7,8], vec![2, 2, 2]).unwrap();

let idx_1 = arr.index_at(&[0, 0, 0]).unwrap();
assert_eq!(0, idx_1);

let idx_2 = arr.index_at(&[1, 0, 1]).unwrap();
assert_eq!(5, idx_2);

let idx_3 = arr.index_at(&[1, 1, 1]).unwrap();
assert_eq!(7, idx_3);
source

fn index_to_coord(&self, idx: usize) -> Result<Vec<usize>, ArrayError>

Return coordinates at the given index of element

Arguments
  • index - index of element in flattened array
Examples
use arr_rs::prelude::*;

let arr: Array<i32> = Array::new(vec![1,2,3,4,5,6,7,8], vec![2, 2, 2]).unwrap();

let coord_1 = arr.index_to_coord(0).unwrap();
assert_eq!(vec![0, 0, 0], coord_1);

let coord_2 = arr.index_to_coord(5).unwrap();
assert_eq!(vec![1, 0, 1], coord_2);

let coord_3 = arr.index_to_coord(7).unwrap();
assert_eq!(vec![1, 1, 1], coord_3);
source

fn at(&self, coords: &[usize]) -> Result<T, ArrayError>

Return an index of element at the given coordinates

Arguments
  • coords - vector representing the coordinates of the element in array
Examples
use arr_rs::prelude::*;

let arr: Array<i32> = Array::new(vec![1,2,3,4,5,6,7,8], vec![2, 2, 2]).unwrap();

let at_1 = arr.at(&[0, 0, 0]).unwrap();
assert_eq!(1, at_1);

let at_2 = arr.at(&[1, 0, 1]).unwrap();
assert_eq!(6, at_2);

let at_3 = arr.at(&[1, 1, 1]).unwrap();
assert_eq!(8, at_3);
source

fn slice(&self, range: Range<usize>) -> Result<Array<T>, ArrayError>

Return a subarray of provided range

Arguments
  • range - starting and ending indices of elements to include in the subarray
Examples
use arr_rs::prelude::*;

let arr = Array::<i32>::flat(vec![1, 2, 3, 4, 5, 6, 7, 8]).unwrap();
let expected = Array::<i32>::flat(vec![1, 2, 3, 4]).unwrap();
assert_eq!(expected, arr.slice(0..4).unwrap());

let arr = Array::<i32>::new(vec![1, 2, 3, 4, 5, 6, 7, 8], vec![2, 4]).unwrap();
let expected = Array::<i32>::flat(vec![1, 2, 3, 4]).unwrap();
assert_eq!(expected, arr.slice(0..1).unwrap());
source

fn indices_at(&self, indices: &[usize]) -> Result<Array<T>, ArrayError>

Return a subarray consisting on values on given indices.

Arguments
  • indices - indices which should be included in resulting array
Examples
use arr_rs::prelude::*;

let arr = Array::<i32>::flat(vec![1, 2, 3, 4, 5, 6, 7, 8]).unwrap();

let expected = Array::<i32>::flat(vec![3, 5, 7]).unwrap();
let slice_1 = arr.indices_at(&[2, 4, 6]).unwrap();
assert_eq!(format!("{expected}"), format!("{slice_1}"));

let expected = Array::<i32>::flat(vec![4, 5, 3, 8, 6, 7, 1, 2]).unwrap();
let slice_1 = arr.indices_at(&[3, 4, 2, 7, 5, 6, 0, 1]).unwrap();
assert_eq!(format!("{expected}"), format!("{slice_1}"));

Implementations on Foreign Types§

source§

impl<T: ArrayElement> ArrayIndexing<T> for Result<Array<T>, ArrayError>

source§

fn index_at(&self, coords: &[usize]) -> Result<usize, ArrayError>

source§

fn index_to_coord(&self, idx: usize) -> Result<Vec<usize>, ArrayError>

source§

fn at(&self, coords: &[usize]) -> Result<T, ArrayError>

source§

fn slice(&self, range: Range<usize>) -> Result<Array<T>, ArrayError>

source§

fn indices_at(&self, indices: &[usize]) -> Result<Array<T>, ArrayError>

Implementors§