Merge pull request #19 from bjz/index-operators

Implement index operators
This commit is contained in:
Sébastien Crozet 2014-09-19 23:43:14 +02:00
commit 1a83622a2d
8 changed files with 93 additions and 11 deletions

View File

@ -267,6 +267,17 @@ 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> IndexMut<uint, [N]> for DMat<N> {
fn index_mut(&mut self, i: &uint) -> &mut [N] { ... }
}
*/
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> {
assert!(left.ncols == right.nrows);

View File

@ -85,6 +85,18 @@ macro_rules! dvec_impl(
}
impl<N> Index<uint, N> for $dvec<N> {
fn index(&self, i: &uint) -> &N {
&self.as_slice()[*i]
}
}
impl<N> IndexMut<uint, N> for $dvec<N> {
fn index_mut(&mut self, i: &uint) -> &mut N {
&mut self.as_mut_slice()[*i]
}
}
impl<N: One + Zero + Clone> $dvec<N> {
/// Builds a vector filled with ones.
///

View File

@ -110,6 +110,7 @@ iterable_mut_impl!(Mat1, 1)
at_fast_impl!(Mat1, 1)
dim_impl!(Mat1, 1)
indexable_impl!(Mat1, 1)
index_impl!(Mat1, Vec1, 1)
mat_mul_mat_impl!(Mat1, Mat1MulRhs, 1)
mat_mul_vec_impl!(Mat1, Vec1, Mat1MulRhs, 1)
vec_mul_mat_impl!(Mat1, Vec1, Vec1MulRhs, 1)
@ -211,6 +212,7 @@ iterable_impl!(Mat2, 2)
iterable_mut_impl!(Mat2, 2)
dim_impl!(Mat2, 2)
indexable_impl!(Mat2, 2)
index_impl!(Mat2, Vec2, 2)
at_fast_impl!(Mat2, 2)
// (specialized) mul_impl!(Mat2, 2)
// (specialized) rmul_impl!(Mat2, Vec2, 2)
@ -327,6 +329,7 @@ iterable_impl!(Mat3, 3)
iterable_mut_impl!(Mat3, 3)
dim_impl!(Mat3, 3)
indexable_impl!(Mat3, 3)
index_impl!(Mat3, Vec3, 3)
at_fast_impl!(Mat3, 3)
// (specialized) mul_impl!(Mat3, 3)
// (specialized) rmul_impl!(Mat3, Vec3, 3)
@ -495,6 +498,7 @@ iterable_impl!(Mat4, 4)
iterable_mut_impl!(Mat4, 4)
dim_impl!(Mat4, 4)
indexable_impl!(Mat4, 4)
index_impl!(Mat4, Vec4, 4)
at_fast_impl!(Mat4, 4)
mat_mul_mat_impl!(Mat4, Mat4MulRhs, 4)
mat_mul_vec_impl!(Mat4, Vec4, Mat4MulRhs, 4)
@ -679,6 +683,7 @@ iterable_impl!(Mat5, 5)
iterable_mut_impl!(Mat5, 5)
dim_impl!(Mat5, 5)
indexable_impl!(Mat5, 5)
index_impl!(Mat5, Vec5, 5)
at_fast_impl!(Mat5, 5)
mat_mul_mat_impl!(Mat5, Mat5MulRhs, 5)
mat_mul_vec_impl!(Mat5, Vec5, Mat5MulRhs, 5)
@ -915,6 +920,7 @@ iterable_impl!(Mat6, 6)
iterable_mut_impl!(Mat6, 6)
dim_impl!(Mat6, 6)
indexable_impl!(Mat6, 6)
index_impl!(Mat6, Vec6, 6)
at_fast_impl!(Mat6, 6)
mat_mul_mat_impl!(Mat6, Mat6MulRhs, 6)
mat_mul_vec_impl!(Mat6, Vec6, Mat6MulRhs, 6)

View File

@ -225,6 +225,26 @@ 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> {
unsafe {
&mem::transmute::<&$t<N>, &[$tv<N>, ..$dim]>(self)[*i]
}
}
}
impl<N> IndexMut<uint, $tv<N>> for $t<N> {
fn index_mut(&mut self, i: &uint) -> &mut $tv<N> {
unsafe {
&mut mem::transmute::<&mut $t<N>, &mut [$tv<N>, ..$dim]>(self)[*i]
}
}
}
)
)
macro_rules! col_slice_impl(
($t: ident, $tv: ident, $slice: ident, $dim: expr) => (
impl<N: Clone + Zero> ColSlice<$slice<N>> for $t<N> {
@ -239,7 +259,7 @@ macro_rules! col_slice_impl(
macro_rules! row_impl(
($t: ident, $tv: ident, $dim: expr) => (
impl<N: Clone + Zero> Row<$tv<N>> for $t<N> {
impl<N: Clone> Row<$tv<N>> for $t<N> {
#[inline]
fn nrows(&self) -> uint {
Dim::dim(None::<$t<N>>)
@ -247,20 +267,12 @@ macro_rules! row_impl(
#[inline]
fn set_row(&mut self, row: uint, v: $tv<N>) {
for (i, e) in v.iter().enumerate() {
self.set((row, i), e.clone());
}
self[row] = v;
}
#[inline]
fn row(&self, row: uint) -> $tv<N> {
let mut res: $tv<N> = Zero::zero();
for (i, e) in res.mut_iter().enumerate() {
*e = self.at((row, i));
}
res
self[row].clone()
}
}
)

View File

@ -363,6 +363,7 @@ one_impl!(Rot2)
rotation_matrix_impl!(Rot2, Vec2, Vec1)
col_impl!(Rot2, Vec2)
row_impl!(Rot2, Vec2)
index_impl!(Rot2, Vec2)
absolute_impl!(Rot2, Mat2)
to_homogeneous_impl!(Rot2, Mat3)
inv_impl!(Rot2)
@ -382,6 +383,7 @@ one_impl!(Rot3)
rotation_matrix_impl!(Rot3, Vec3, Vec3)
col_impl!(Rot3, Vec3)
row_impl!(Rot3, Vec3)
index_impl!(Rot3, Vec3)
absolute_impl!(Rot3, Mat3)
to_homogeneous_impl!(Rot3, Mat4)
inv_impl!(Rot3)
@ -401,6 +403,7 @@ one_impl!(Rot4)
rotation_matrix_impl!(Rot4, Vec4, Vec4)
col_impl!(Rot4, Vec4)
row_impl!(Rot4, Vec4)
index_impl!(Rot4, Vec4)
absolute_impl!(Rot4, Mat4)
to_homogeneous_impl!(Rot4, Mat5)
inv_impl!(Rot4)

View File

@ -186,6 +186,22 @@ 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> {
&self.submat[*i]
}
}
impl<N> IndexMut<uint, $tv<N>> for $t<N> {
fn index_mut(&mut self, i: &uint) -> &mut $tv<N> {
&mut self.submat[*i]
}
}
)
)
macro_rules! to_homogeneous_impl(
($t: ident, $tm: ident) => (
impl<N: Num + Clone> ToHomogeneous<$tm<N>> for $t<N> {

View File

@ -53,6 +53,7 @@ ord_impl!(Vec1, x)
vec_axis_impl!(Vec1, x)
vec_cast_impl!(Vec1, Vec1Cast, x)
as_slice_impl!(Vec1, 1)
index_impl!(Vec1)
indexable_impl!(Vec1, 1)
at_fast_impl!(Vec1, 1)
new_repeat_impl!(Vec1, val, x)
@ -151,6 +152,7 @@ ord_impl!(Vec2, x, y)
vec_axis_impl!(Vec2, x, y)
vec_cast_impl!(Vec2, Vec2Cast, x, y)
as_slice_impl!(Vec2, 2)
index_impl!(Vec2)
indexable_impl!(Vec2, 2)
at_fast_impl!(Vec2, 2)
new_repeat_impl!(Vec2, val, x, y)
@ -251,6 +253,7 @@ ord_impl!(Vec3, x, y, z)
vec_axis_impl!(Vec3, x, y, z)
vec_cast_impl!(Vec3, Vec3Cast, x, y, z)
as_slice_impl!(Vec3, 3)
index_impl!(Vec3)
indexable_impl!(Vec3, 3)
at_fast_impl!(Vec3, 3)
new_repeat_impl!(Vec3, val, x, y, z)
@ -357,6 +360,7 @@ ord_impl!(Vec4, x, y, z, w)
vec_axis_impl!(Vec4, x, y, z, w)
vec_cast_impl!(Vec4, Vec4Cast, x, y, z, w)
as_slice_impl!(Vec4, 4)
index_impl!(Vec4)
indexable_impl!(Vec4, 4)
at_fast_impl!(Vec4, 4)
new_repeat_impl!(Vec4, val, x, y, z, w)
@ -461,6 +465,7 @@ ord_impl!(Vec5, x, y, z, w, a)
vec_axis_impl!(Vec5, x, y, z, w, a)
vec_cast_impl!(Vec5, Vec5Cast, x, y, z, w, a)
as_slice_impl!(Vec5, 5)
index_impl!(Vec5)
indexable_impl!(Vec5, 5)
at_fast_impl!(Vec5, 5)
new_repeat_impl!(Vec5, val, x, y, z, w, a)
@ -567,6 +572,7 @@ ord_impl!(Vec6, x, y, z, w, a, b)
vec_axis_impl!(Vec6, x, y, z, w, a, b)
vec_cast_impl!(Vec6, Vec6Cast, x, y, z, w, a, b)
as_slice_impl!(Vec6, 6)
index_impl!(Vec6)
indexable_impl!(Vec6, 6)
at_fast_impl!(Vec6, 6)
new_repeat_impl!(Vec6, val, x, y, z, w, a, b)

View File

@ -210,6 +210,22 @@ macro_rules! indexable_impl(
)
)
macro_rules! index_impl(
($t: ident) => (
impl<N> Index<uint, N> for $t<N> {
fn index(&self, i: &uint) -> &N {
&self.as_slice()[*i]
}
}
impl<N> IndexMut<uint, N> for $t<N> {
fn index_mut(&mut self, i: &uint) -> &mut N {
&mut self.as_mut_slice()[*i]
}
}
)
)
macro_rules! new_repeat_impl(
($t: ident, $param: ident, $comp0: ident $(,$compN: ident)*) => (
impl<N: Clone> $t<N> {