forked from M-Labs/nalgebra
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.
This commit is contained in:
parent
eb24c4063f
commit
2b23446d54
@ -192,14 +192,14 @@ impl<N: One + Zero + Clone> Eye for DMat<N> {
|
||||
|
||||
for i in range(0u, dim) {
|
||||
let _1: N = One::one();
|
||||
res.set((i, i), _1);
|
||||
res[(i, i)] = _1;
|
||||
}
|
||||
|
||||
res
|
||||
}
|
||||
}
|
||||
|
||||
impl<N: Clone> DMat<N> {
|
||||
impl<N> DMat<N> {
|
||||
#[inline(always)]
|
||||
fn offset(&self, i: uint, j: uint) -> uint {
|
||||
i + j * self.nrows
|
||||
@ -267,16 +267,29 @@ impl<N: Clone> Indexable<(uint, uint), N> for DMat<N> {
|
||||
|
||||
}
|
||||
|
||||
// TODO: implement after DST lands
|
||||
/*
|
||||
impl<N> Index<uint, [N]> for DMat<N> {
|
||||
fn index(&self, i: &uint) -> &[N] { ... }
|
||||
impl<N> Index<(uint, uint), N> for DMat<N> {
|
||||
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<N> IndexMut<uint, [N]> for DMat<N> {
|
||||
fn index_mut(&mut self, i: &uint) -> &mut [N] { ... }
|
||||
impl<N> IndexMut<(uint, uint), N> for DMat<N> {
|
||||
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<N: Clone + Mul<N, N> + Add<N, N> + Zero> DMatMulRhs<N, DMat<N>> for DMat<N> {
|
||||
fn binop(left: &DMat<N>, right: &DMat<N>) -> DMat<N> {
|
||||
@ -620,7 +633,7 @@ impl<N: Show + Clone> Show for DMat<N> {
|
||||
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");
|
||||
}
|
||||
|
@ -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)
|
||||
|
@ -226,19 +226,19 @@ macro_rules! indexable_impl(
|
||||
)
|
||||
|
||||
macro_rules! index_impl(
|
||||
($t: ident, $tv: ident, $dim: expr) => (
|
||||
impl<N> Index<uint, $tv<N>> for $t<N> {
|
||||
fn index(&self, i: &uint) -> &$tv<N> {
|
||||
($t: ident, $dim: expr) => (
|
||||
impl<N> Index<(uint, uint), N> for $t<N> {
|
||||
fn index(&self, &(i, j): &(uint, uint)) -> &N {
|
||||
unsafe {
|
||||
&mem::transmute::<&$t<N>, &[$tv<N>, ..$dim]>(self)[*i]
|
||||
&mem::transmute::<&$t<N>, &mut [N, ..$dim * $dim]>(self)[i + j * $dim]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<N> IndexMut<uint, $tv<N>> for $t<N> {
|
||||
fn index_mut(&mut self, i: &uint) -> &mut $tv<N> {
|
||||
impl<N> IndexMut<(uint, uint), N> for $t<N> {
|
||||
fn index_mut(&mut self, &(i, j): &(uint, uint)) -> &mut N {
|
||||
unsafe {
|
||||
&mut mem::transmute::<&mut $t<N>, &mut [$tv<N>, ..$dim]>(self)[*i]
|
||||
&mut mem::transmute::<&mut $t<N>, &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<N: Clone> Col<$tv<N>> for $t<N> {
|
||||
impl<N: Clone + Zero> Col<$tv<N>> for $t<N> {
|
||||
#[inline]
|
||||
fn ncols(&self) -> uint {
|
||||
Dim::dim(None::<$t<N>>)
|
||||
@ -308,12 +308,20 @@ macro_rules! col_impl(
|
||||
|
||||
#[inline]
|
||||
fn set_col(&mut self, col: uint, v: $tv<N>) {
|
||||
self[col] = v;
|
||||
for (i, e) in v.iter().enumerate() {
|
||||
self.set((i, col), e.clone());
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn col(&self, col: uint) -> $tv<N> {
|
||||
self[col].clone()
|
||||
let mut res: $tv<N> = Zero::zero();
|
||||
|
||||
for (i, e) in res.iter_mut().enumerate() {
|
||||
*e = self.at((i, col));
|
||||
}
|
||||
|
||||
res
|
||||
}
|
||||
}
|
||||
)
|
||||
|
@ -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)
|
||||
|
@ -265,15 +265,15 @@ macro_rules! col_impl(
|
||||
)
|
||||
|
||||
macro_rules! index_impl(
|
||||
($t: ident, $tv: ident) => (
|
||||
impl<N> Index<uint, $tv<N>> for $t<N> {
|
||||
fn index(&self, i: &uint) -> &$tv<N> {
|
||||
($t: ident) => (
|
||||
impl<N> Index<(uint, uint), N> for $t<N> {
|
||||
fn index(&self, i: &(uint, uint)) -> &N {
|
||||
&self.submat[*i]
|
||||
}
|
||||
}
|
||||
|
||||
impl<N> IndexMut<uint, $tv<N>> for $t<N> {
|
||||
fn index_mut(&mut self, i: &uint) -> &mut $tv<N> {
|
||||
impl<N> IndexMut<(uint, uint), N> for $t<N> {
|
||||
fn index_mut(&mut self, i: &(uint, uint)) -> &mut N {
|
||||
&mut self.submat[*i]
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user