use std::fmt::{Debug, Display}; use std::ops::Range; use crate::linalg::basic::arrays::{ Array as BaseArray, Array1, ArrayView1, MutArray, MutArrayView1, }; use ndarray::{s, Array, ArrayBase, ArrayView, ArrayViewMut, Ix1, OwnedRepr}; impl BaseArray for ArrayBase, Ix1> { fn get(&self, i: usize) -> &T { &self[i] } fn shape(&self) -> usize { self.len() } fn is_empty(&self) -> bool { self.len() > 0 } fn iterator<'b>(&'b self, axis: u8) -> Box + 'b> { assert!(axis == 0, "For one dimensional array `axis` should == 0"); Box::new(self.iter()) } } impl MutArray for ArrayBase, Ix1> { fn set(&mut self, i: usize, x: T) { self[i] = x } fn iterator_mut<'b>(&'b mut self, axis: u8) -> Box + 'b> { assert!(axis == 0, "For one dimensional array `axis` should == 0"); Box::new(self.iter_mut()) } } impl ArrayView1 for ArrayBase, Ix1> {} impl MutArrayView1 for ArrayBase, Ix1> {} impl<'a, T: Debug + Display + Copy + Sized> BaseArray for ArrayView<'a, T, Ix1> { fn get(&self, i: usize) -> &T { &self[i] } fn shape(&self) -> usize { self.len() } fn is_empty(&self) -> bool { self.len() > 0 } fn iterator<'b>(&'b self, axis: u8) -> Box + 'b> { assert!(axis == 0, "For one dimensional array `axis` should == 0"); Box::new(self.iter()) } } impl<'a, T: Debug + Display + Copy + Sized> ArrayView1 for ArrayView<'a, T, Ix1> {} impl<'a, T: Debug + Display + Copy + Sized> BaseArray for ArrayViewMut<'a, T, Ix1> { fn get(&self, i: usize) -> &T { &self[i] } fn shape(&self) -> usize { self.len() } fn is_empty(&self) -> bool { self.len() > 0 } fn iterator<'b>(&'b self, axis: u8) -> Box + 'b> { assert!(axis == 0, "For one dimensional array `axis` should == 0"); Box::new(self.iter()) } } impl<'a, T: Debug + Display + Copy + Sized> MutArray for ArrayViewMut<'a, T, Ix1> { fn set(&mut self, i: usize, x: T) { self[i] = x; } fn iterator_mut<'b>(&'b mut self, axis: u8) -> Box + 'b> { assert!(axis == 0, "For one dimensional array `axis` should == 0"); Box::new(self.iter_mut()) } } impl<'a, T: Debug + Display + Copy + Sized> ArrayView1 for ArrayViewMut<'a, T, Ix1> {} impl<'a, T: Debug + Display + Copy + Sized> MutArrayView1 for ArrayViewMut<'a, T, Ix1> {} impl Array1 for ArrayBase, Ix1> { fn slice<'a>(&'a self, range: Range) -> Box + 'a> { assert!( range.end <= self.len(), "`range` should be <= {}", self.len() ); Box::new(self.slice(s![range])) } fn slice_mut<'b>(&'b mut self, range: Range) -> Box + 'b> { assert!( range.end <= self.len(), "`range` should be <= {}", self.len() ); Box::new(self.slice_mut(s![range])) } fn fill(len: usize, value: T) -> Self { Array::from_elem(len, value) } fn from_iterator>(iter: I, len: usize) -> Self where Self: Sized, { Array::from_iter(iter.take(len)) } fn from_vec_slice(slice: &[T]) -> Self { Array::from_iter(slice.iter().copied()) } fn from_slice(slice: &dyn ArrayView1) -> Self { Array::from_iter(slice.iterator(0).copied()) } } #[cfg(test)] mod tests { use super::*; use ndarray::arr1; #[test] fn test_get_set() { let mut a = arr1(&[1, 2, 3]); assert_eq!(*BaseArray::get(&a, 1), 2); a.set(1, 9); assert_eq!(a, arr1(&[1, 9, 3])); } #[test] fn test_iterator() { let a = arr1(&[1, 2, 3]); let v: Vec = a.iterator(0).copied().collect(); assert_eq!(v, vec!(1, 2, 3)); } #[test] fn test_mut_iterator() { let mut a = arr1(&[1, 2, 3]); a.iterator_mut(0).for_each(|v| *v = 1); assert_eq!(a, arr1(&[1, 1, 1])); } #[test] fn test_slice() { let x = arr1(&[1, 2, 3, 4, 5]); let x_slice = Array1::slice(&x, 2..3); assert_eq!(1, x_slice.shape()); assert_eq!(3, *x_slice.get(0)); } #[test] fn test_mut_slice() { let mut x = arr1(&[1, 2, 3, 4, 5]); let mut x_slice = Array1::slice_mut(&mut x, 2..4); x_slice.set(0, 9); assert_eq!(2, x_slice.shape()); assert_eq!(9, *x_slice.get(0)); assert_eq!(4, *x_slice.get(1)); } }