From 5cbbc25bb28109d85afe4929daba68bdcbd7bf03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Crozet?= Date: Sun, 10 Jan 2016 14:49:37 +0100 Subject: [PATCH] Make vectors indexable the same way as slices. This includes range indexing. In addition, for unification, the methods `.as_slice` and `.as_mut_slice` of DVec have been renamed to `.as_ref` and `.as_mut`. --- benches/dmat.rs | 4 +-- src/structs/dvec_macros.rs | 52 +++++++++++++++++++------------------- src/structs/vec_macros.rs | 10 ++++---- tests/mat.rs | 22 ++++++++-------- 4 files changed, 44 insertions(+), 44 deletions(-) diff --git a/benches/dmat.rs b/benches/dmat.rs index 40d62c15..b41c8d4d 100644 --- a/benches/dmat.rs +++ b/benches/dmat.rs @@ -13,7 +13,7 @@ macro_rules! bench_mul_dmat( let a: DMat = DMat::new_random($nrows, $ncols); let mut b: DMat = DMat::new_random($nrows, $ncols); - for _ in (0usize .. 1000) { + for _ in 0usize .. 1000 { // XXX: the clone here is highly undesirable! b = a.clone() * b; } @@ -55,7 +55,7 @@ macro_rules! bench_mul_dmat_dvec( let m : DMat = DMat::new_random($nrows, $ncols); let mut v : DVec = DVec::new_random($ncols); - for _ in (0usize .. 1000) { + for _ in 0usize .. 1000 { // XXX: the clone here is highly undesirable! v = m.clone() * v } diff --git a/src/structs/dvec_macros.rs b/src/structs/dvec_macros.rs index 43f3357e..d741a986 100644 --- a/src/structs/dvec_macros.rs +++ b/src/structs/dvec_macros.rs @@ -15,20 +15,20 @@ macro_rules! dvec_impl( /// Tests if all components of the vector are zeroes. #[inline] pub fn is_zero(&self) -> bool { - self.as_slice().iter().all(|e| e.is_zero()) + self.as_ref().iter().all(|e| e.is_zero()) } } impl $dvec { /// Slices this vector. #[inline] - pub fn as_slice<'a>(&'a self) -> &'a [N] { + pub fn as_ref<'a>(&'a self) -> &'a [N] { &self.at[.. self.len()] } /// Mutably slices this vector. #[inline] - pub fn as_mut_slice<'a>(&'a mut self) -> &'a mut [N] { + pub fn as_mut<'a>(&'a mut self) -> &'a mut [N] { let len = self.len(); &mut self.at[.. len] } @@ -46,7 +46,7 @@ macro_rules! dvec_impl( fn swap(&mut self, i: usize, j: usize) { assert!(i < self.len()); assert!(j < self.len()); - self.as_mut_slice().swap(i, j); + self.as_mut().swap(i, j); } #[inline] @@ -61,17 +61,17 @@ macro_rules! dvec_impl( } - impl Index for $dvec { - type Output = N; + impl Index for $dvec where [N]: Index { + type Output = <[N] as Index>::Output; - fn index(&self, i: usize) -> &N { - &self.as_slice()[i] + fn index(&self, i: T) -> &<[N] as Index>::Output { + &self.as_ref()[i] } } - impl IndexMut for $dvec { - fn index_mut(&mut self, i: usize) -> &mut N { - &mut self.as_mut_slice()[i] + impl IndexMut for $dvec where [N]: IndexMut { + fn index_mut(&mut self, i: T) -> &mut <[N] as Index>::Output { + &mut self.as_mut()[i] } } @@ -97,14 +97,14 @@ macro_rules! dvec_impl( impl Iterable for $dvec { #[inline] fn iter<'l>(&'l self) -> Iter<'l, N> { - self.as_slice().iter() + self.as_ref().iter() } } impl IterableMut for $dvec { #[inline] fn iter_mut<'l>(&'l mut self) -> IterMut<'l, N> { - self.as_mut_slice().iter_mut() + self.as_mut().iter_mut() } } @@ -185,7 +185,7 @@ macro_rules! dvec_impl( let mut res = self; - for (left, right) in res.as_mut_slice().iter_mut().zip(right.as_slice().iter()) { + for (left, right) in res.as_mut().iter_mut().zip(right.as_ref().iter()) { *left = *left * *right } @@ -202,7 +202,7 @@ macro_rules! dvec_impl( let mut res = self; - for (left, right) in res.as_mut_slice().iter_mut().zip(right.as_slice().iter()) { + for (left, right) in res.as_mut().iter_mut().zip(right.as_ref().iter()) { *left = *left / *right } @@ -219,7 +219,7 @@ macro_rules! dvec_impl( let mut res = self; - for (left, right) in res.as_mut_slice().iter_mut().zip(right.as_slice().iter()) { + for (left, right) in res.as_mut().iter_mut().zip(right.as_ref().iter()) { *left = *left + *right } @@ -236,7 +236,7 @@ macro_rules! dvec_impl( let mut res = self; - for (left, right) in res.as_mut_slice().iter_mut().zip(right.as_slice().iter()) { + for (left, right) in res.as_mut().iter_mut().zip(right.as_ref().iter()) { *left = *left - *right } @@ -249,7 +249,7 @@ macro_rules! dvec_impl( #[inline] fn neg(self) -> $dvec { - FromIterator::from_iter(self.as_slice().iter().map(|a| -*a)) + FromIterator::from_iter(self.as_ref().iter().map(|a| -*a)) } } @@ -282,7 +282,7 @@ macro_rules! dvec_impl( fn normalize_mut(&mut self) -> N { let l = Norm::norm(self); - for n in self.as_mut_slice().iter_mut() { + for n in self.as_mut().iter_mut() { *n = *n / l; } @@ -303,13 +303,13 @@ macro_rules! dvec_impl( #[inline] fn approx_eq_eps(&self, other: &$dvec, epsilon: &N) -> bool { - let mut zip = self.as_slice().iter().zip(other.as_slice().iter()); + let mut zip = self.as_ref().iter().zip(other.as_ref().iter()); zip.all(|(a, b)| ApproxEq::approx_eq_eps(a, b, epsilon)) } #[inline] fn approx_eq_ulps(&self, other: &$dvec, ulps: u32) -> bool { - let mut zip = self.as_slice().iter().zip(other.as_slice().iter()); + let mut zip = self.as_ref().iter().zip(other.as_ref().iter()); zip.all(|(a, b)| ApproxEq::approx_eq_ulps(a, b, ulps)) } } @@ -321,7 +321,7 @@ macro_rules! dvec_impl( fn mul(self, right: N) -> $dvec { let mut res = self; - for e in res.as_mut_slice().iter_mut() { + for e in res.as_mut().iter_mut() { *e = *e * right } @@ -336,7 +336,7 @@ macro_rules! dvec_impl( fn div(self, right: N) -> $dvec { let mut res = self; - for e in res.as_mut_slice().iter_mut() { + for e in res.as_mut().iter_mut() { *e = *e / right } @@ -351,7 +351,7 @@ macro_rules! dvec_impl( fn add(self, right: N) -> $dvec { let mut res = self; - for e in res.as_mut_slice().iter_mut() { + for e in res.as_mut().iter_mut() { *e = *e + right } @@ -366,7 +366,7 @@ macro_rules! dvec_impl( fn sub(self, right: N) -> $dvec { let mut res = self; - for e in res.as_mut_slice().iter_mut() { + for e in res.as_mut().iter_mut() { *e = *e - right } @@ -392,7 +392,7 @@ macro_rules! small_dvec_impl ( return false; // FIXME: fail instead? } - for (a, b) in self.as_slice().iter().zip(other.as_slice().iter()) { + for (a, b) in self.as_ref().iter().zip(other.as_ref().iter()) { if *a != *b { return false; } diff --git a/src/structs/vec_macros.rs b/src/structs/vec_macros.rs index c42cf227..152db309 100644 --- a/src/structs/vec_macros.rs +++ b/src/structs/vec_macros.rs @@ -208,16 +208,16 @@ macro_rules! indexable_impl( macro_rules! index_impl( ($t: ident) => ( - impl Index for $t { - type Output = N; + impl Index for $t where [N]: Index { + type Output = <[N] as Index>::Output; - fn index(&self, i: usize) -> &N { + fn index(&self, i: T) -> &<[N] as Index>::Output { &self.as_ref()[i] } } - impl IndexMut for $t { - fn index_mut(&mut self, i: usize) -> &mut N { + impl IndexMut for $t where [N]: IndexMut { + fn index_mut(&mut self, i: T) -> &mut <[N] as Index>::Output { &mut self.as_mut()[i] } } diff --git a/tests/mat.rs b/tests/mat.rs index afae18d8..b79c548d 100644 --- a/tests/mat.rs +++ b/tests/mat.rs @@ -7,7 +7,7 @@ use na::{Vec1, Vec3, Mat1, Mat2, Mat3, Mat4, Mat5, Mat6, Rot2, Rot3, Persp3, Per macro_rules! test_inv_mat_impl( ($t: ty) => ( - for _ in (0usize .. 10000) { + for _ in 0usize .. 10000 { let randmat : $t = random(); match na::inv(&randmat) { @@ -20,7 +20,7 @@ macro_rules! test_inv_mat_impl( macro_rules! test_transpose_mat_impl( ($t: ty) => ( - for _ in (0usize .. 10000) { + for _ in 0usize .. 10000 { let randmat : $t = random(); assert!(na::transpose(&na::transpose(&randmat)) == randmat); @@ -30,7 +30,7 @@ macro_rules! test_transpose_mat_impl( macro_rules! test_qr_impl( ($t: ty) => ( - for _ in (0usize .. 10000) { + for _ in 0usize .. 10000 { let randmat : $t = random(); let (q, r) = na::qr(&randmat); @@ -43,7 +43,7 @@ macro_rules! test_qr_impl( macro_rules! test_cholesky_impl( ($t: ty) => ( - for _ in (0usize .. 10000) { + for _ in 0usize .. 10000 { // construct symmetric positive definite matrix let mut randmat : $t = random(); @@ -65,7 +65,7 @@ macro_rules! test_cholesky_impl( macro_rules! test_hessenberg_impl( ($t: ty) => ( - for _ in (0usize .. 10000) { + for _ in 0usize .. 10000 { let randmat : $t = random(); @@ -90,7 +90,7 @@ macro_rules! test_hessenberg_impl( macro_rules! test_eigen_qr_impl( ($t: ty) => { - for _ in (0usize .. 10000) { + for _ in 0usize .. 10000 { let randmat : $t = random(); // Make it symetric so that we can recompose the matrix to test at the end. let randmat = na::transpose(&randmat) * randmat; @@ -105,7 +105,7 @@ macro_rules! test_eigen_qr_impl( assert!(na::approx_eq_eps(&randmat, &recomp, &1.0e-2)); } - for _ in (0usize .. 10000) { + for _ in 0usize .. 10000 { let randmat : $t = random(); // Take only diagonal part let randmat: $t = Diag::from_diag(&randmat.diag()); @@ -184,7 +184,7 @@ fn test_inv_mat6() { #[test] fn test_rotation2() { - for _ in (0usize .. 10000) { + for _ in 0usize .. 10000 { let randmat: na::Rot2 = na::one(); let ang = Vec1::new(na::abs(&random::()) % ::pi()); @@ -201,7 +201,7 @@ fn test_index_mat2() { #[test] fn test_inv_rotation3() { - for _ in (0usize .. 10000) { + for _ in 0usize .. 10000 { let randmat: Rot3 = na::one(); let dir: Vec3 = random(); let ang = na::normalize(&dir) * (na::abs(&random::()) % ::pi()); @@ -524,7 +524,7 @@ fn test_dmat_subtraction() { /* FIXME: review qr decomposition to make it work with DMat. #[test] fn test_qr() { - for _ in (0usize .. 10) { + for _ in 0usize .. 10 { let dim1: usize = random(); let dim2: usize = random(); let rows = min(40, max(dim1, dim2)); @@ -809,6 +809,6 @@ fn test_transpose_square_mat() { let mut mat = DMat::from_col_vec(num_rows, num_cols, col_major_mat); mat.transpose_mut(); for i in 0..num_rows { - assert_eq!(&[0, 1, 2, 3], mat.row_slice(i, 0, num_cols).as_slice()); + assert_eq!(&[0, 1, 2, 3], &mat.row_slice(i, 0, num_cols)[..]); } }