From 2b23446d54a9a24f8f23c620c5149e5aabfdbb1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Crozet?= Date: Sat, 25 Oct 2014 23:02:16 +0200 Subject: [PATCH] Matrix Index/IndexMut implementation: return an element instead of a column. There is no reason why indexing would prefer returning a column instead of a line. Instead, we return an element, and let the user use the `Col` and `Row` traits istead. --- src/structs/dmat.rs | 33 +++++++++++++++++++++++---------- src/structs/mat.rs | 12 ++++++------ src/structs/mat_macros.rs | 28 ++++++++++++++++++---------- src/structs/rot.rs | 6 +++--- src/structs/rot_macros.rs | 10 +++++----- 5 files changed, 55 insertions(+), 34 deletions(-) diff --git a/src/structs/dmat.rs b/src/structs/dmat.rs index af49f97a..8eda7f61 100644 --- a/src/structs/dmat.rs +++ b/src/structs/dmat.rs @@ -192,14 +192,14 @@ impl Eye for DMat { for i in range(0u, dim) { let _1: N = One::one(); - res.set((i, i), _1); + res[(i, i)] = _1; } res } } -impl DMat { +impl DMat { #[inline(always)] fn offset(&self, i: uint, j: uint) -> uint { i + j * self.nrows @@ -267,16 +267,29 @@ impl Indexable<(uint, uint), N> for DMat { } -// TODO: implement after DST lands -/* -impl Index for DMat { - fn index(&self, i: &uint) -> &[N] { ... } +impl Index<(uint, uint), N> for DMat { + fn index(&self, &(i, j): &(uint, uint)) -> &N { + assert!(i < self.nrows); + assert!(j < self.ncols); + + unsafe { + self.mij.as_slice().unsafe_get(self.offset(i, j)) + } + } } -impl IndexMut for DMat { - fn index_mut(&mut self, i: &uint) -> &mut [N] { ... } +impl IndexMut<(uint, uint), N> for DMat { + fn index_mut(&mut self, &(i, j): &(uint, uint)) -> &mut N { + assert!(i < self.nrows); + assert!(j < self.ncols); + + let offset = self.offset(i, j); + + unsafe { + self.mij.as_mut_slice().unsafe_mut(offset) + } + } } -*/ impl + Add + Zero> DMatMulRhs> for DMat { fn binop(left: &DMat, right: &DMat) -> DMat { @@ -620,7 +633,7 @@ impl Show for DMat { fn fmt(&self, form:&mut Formatter) -> Result { for i in range(0u, self.nrows()) { for j in range(0u, self.ncols()) { - let _ = write!(form, "{} ", self.at((i, j))); + let _ = write!(form, "{} ", self[(i, j)]); } let _ = write!(form, "\n"); } diff --git a/src/structs/mat.rs b/src/structs/mat.rs index 6c022823..9d6b78ba 100644 --- a/src/structs/mat.rs +++ b/src/structs/mat.rs @@ -111,7 +111,7 @@ iterable_mut_impl!(Mat1, 1) at_fast_impl!(Mat1, 1) dim_impl!(Mat1, 1) indexable_impl!(Mat1, 1) -index_impl!(Mat1, Vec1, 1) +index_impl!(Mat1, 1) mat_mul_mat_impl!(Mat1, Mat1MulRhs, 1) mat_mul_vec_impl!(Mat1, Vec1, Mat1MulRhs, 1, Zero::zero) vec_mul_mat_impl!(Mat1, Vec1, Vec1MulRhs, 1, Zero::zero) @@ -215,7 +215,7 @@ iterable_impl!(Mat2, 2) iterable_mut_impl!(Mat2, 2) dim_impl!(Mat2, 2) indexable_impl!(Mat2, 2) -index_impl!(Mat2, Vec2, 2) +index_impl!(Mat2, 2) at_fast_impl!(Mat2, 2) // (specialized) mul_impl!(Mat2, 2) // (specialized) rmul_impl!(Mat2, Vec2, 2) @@ -332,7 +332,7 @@ iterable_impl!(Mat3, 3) iterable_mut_impl!(Mat3, 3) dim_impl!(Mat3, 3) indexable_impl!(Mat3, 3) -index_impl!(Mat3, Vec3, 3) +index_impl!(Mat3, 3) at_fast_impl!(Mat3, 3) // (specialized) mul_impl!(Mat3, 3) // (specialized) rmul_impl!(Mat3, Vec3, 3) @@ -501,7 +501,7 @@ iterable_impl!(Mat4, 4) iterable_mut_impl!(Mat4, 4) dim_impl!(Mat4, 4) indexable_impl!(Mat4, 4) -index_impl!(Mat4, Vec4, 4) +index_impl!(Mat4, 4) at_fast_impl!(Mat4, 4) mat_mul_mat_impl!(Mat4, Mat4MulRhs, 4) mat_mul_vec_impl!(Mat4, Vec4, Mat4MulRhs, 4, Zero::zero) @@ -688,7 +688,7 @@ iterable_impl!(Mat5, 5) iterable_mut_impl!(Mat5, 5) dim_impl!(Mat5, 5) indexable_impl!(Mat5, 5) -index_impl!(Mat5, Vec5, 5) +index_impl!(Mat5, 5) at_fast_impl!(Mat5, 5) mat_mul_mat_impl!(Mat5, Mat5MulRhs, 5) mat_mul_vec_impl!(Mat5, Vec5, Mat5MulRhs, 5, Zero::zero) @@ -927,7 +927,7 @@ iterable_impl!(Mat6, 6) iterable_mut_impl!(Mat6, 6) dim_impl!(Mat6, 6) indexable_impl!(Mat6, 6) -index_impl!(Mat6, Vec6, 6) +index_impl!(Mat6, 6) at_fast_impl!(Mat6, 6) mat_mul_mat_impl!(Mat6, Mat6MulRhs, 6) mat_mul_vec_impl!(Mat6, Vec6, Mat6MulRhs, 6, Zero::zero) diff --git a/src/structs/mat_macros.rs b/src/structs/mat_macros.rs index 702bd3ae..5a5e40f9 100644 --- a/src/structs/mat_macros.rs +++ b/src/structs/mat_macros.rs @@ -226,19 +226,19 @@ macro_rules! indexable_impl( ) macro_rules! index_impl( - ($t: ident, $tv: ident, $dim: expr) => ( - impl Index> for $t { - fn index(&self, i: &uint) -> &$tv { + ($t: ident, $dim: expr) => ( + impl Index<(uint, uint), N> for $t { + fn index(&self, &(i, j): &(uint, uint)) -> &N { unsafe { - &mem::transmute::<&$t, &[$tv, ..$dim]>(self)[*i] + &mem::transmute::<&$t, &mut [N, ..$dim * $dim]>(self)[i + j * $dim] } } } - impl IndexMut> for $t { - fn index_mut(&mut self, i: &uint) -> &mut $tv { + impl IndexMut<(uint, uint), N> for $t { + fn index_mut(&mut self, &(i, j): &(uint, uint)) -> &mut N { unsafe { - &mut mem::transmute::<&mut $t, &mut [$tv, ..$dim]>(self)[*i] + &mut mem::transmute::<&mut $t, &mut [N, ..$dim * $dim]>(self)[i + j * $dim] } } } @@ -300,7 +300,7 @@ macro_rules! row_slice_impl( macro_rules! col_impl( ($t: ident, $tv: ident, $dim: expr) => ( - impl Col<$tv> for $t { + impl Col<$tv> for $t { #[inline] fn ncols(&self) -> uint { Dim::dim(None::<$t>) @@ -308,12 +308,20 @@ macro_rules! col_impl( #[inline] fn set_col(&mut self, col: uint, v: $tv) { - self[col] = v; + for (i, e) in v.iter().enumerate() { + self.set((i, col), e.clone()); + } } #[inline] fn col(&self, col: uint) -> $tv { - self[col].clone() + let mut res: $tv = Zero::zero(); + + for (i, e) in res.iter_mut().enumerate() { + *e = self.at((i, col)); + } + + res } } ) diff --git a/src/structs/rot.rs b/src/structs/rot.rs index 681ec6e2..3476b658 100644 --- a/src/structs/rot.rs +++ b/src/structs/rot.rs @@ -392,7 +392,7 @@ one_impl!(Rot2) rotation_matrix_impl!(Rot2, Vec2, Vec1) col_impl!(Rot2, Vec2) row_impl!(Rot2, Vec2) -index_impl!(Rot2, Vec2) +index_impl!(Rot2) absolute_impl!(Rot2, Mat2) to_homogeneous_impl!(Rot2, Mat3) inv_impl!(Rot2) @@ -414,7 +414,7 @@ one_impl!(Rot3) rotation_matrix_impl!(Rot3, Vec3, Vec3) col_impl!(Rot3, Vec3) row_impl!(Rot3, Vec3) -index_impl!(Rot3, Vec3) +index_impl!(Rot3) absolute_impl!(Rot3, Mat3) to_homogeneous_impl!(Rot3, Mat4) inv_impl!(Rot3) @@ -436,7 +436,7 @@ one_impl!(Rot4) rotation_matrix_impl!(Rot4, Vec4, Vec4) col_impl!(Rot4, Vec4) row_impl!(Rot4, Vec4) -index_impl!(Rot4, Vec4) +index_impl!(Rot4) absolute_impl!(Rot4, Mat4) to_homogeneous_impl!(Rot4, Mat5) inv_impl!(Rot4) diff --git a/src/structs/rot_macros.rs b/src/structs/rot_macros.rs index a184b4fe..3a4197f7 100644 --- a/src/structs/rot_macros.rs +++ b/src/structs/rot_macros.rs @@ -265,15 +265,15 @@ macro_rules! col_impl( ) macro_rules! index_impl( - ($t: ident, $tv: ident) => ( - impl Index> for $t { - fn index(&self, i: &uint) -> &$tv { + ($t: ident) => ( + impl Index<(uint, uint), N> for $t { + fn index(&self, i: &(uint, uint)) -> &N { &self.submat[*i] } } - impl IndexMut> for $t { - fn index_mut(&mut self, i: &uint) -> &mut $tv { + impl IndexMut<(uint, uint), N> for $t { + fn index_mut(&mut self, i: &(uint, uint)) -> &mut N { &mut self.submat[*i] } }