diff --git a/src/cgmath/array.rs b/src/cgmath/array.rs index eeb3684..6357471 100644 --- a/src/cgmath/array.rs +++ b/src/cgmath/array.rs @@ -41,6 +41,7 @@ pub trait Array } fn fold(&self, f: &fn(&T, &T) -> T) -> T; + fn each_mut(&mut self, f: &fn(i: uint, x: &mut T)); } macro_rules! array( @@ -90,6 +91,11 @@ macro_rules! array( fn fold(&self, f: &fn(&$T, &$T) -> $T) -> $T { gen_fold!($_n) } + + #[inline] + fn each_mut(&mut self, f: &fn(i: uint, x: &mut $T)) { + gen_each_mut!($_n) + } } ) ) @@ -111,6 +117,12 @@ macro_rules! gen_fold( (_4) => (f(&f(&f(self.i(0), self.i(1)), self.i(2)), self.i(3))); ) +macro_rules! gen_each_mut( + (_2) => (f(0, self.mut_i(0)); f(1, self.mut_i(1));); + (_3) => (f(0, self.mut_i(0)); f(1, self.mut_i(1)); f(2, self.mut_i(2));); + (_4) => (f(0, self.mut_i(0)); f(1, self.mut_i(1)); f(2, self.mut_i(2)); f(3, self.mut_i(3));); +) + macro_rules! approx_eq( (impl<$S:ident> $Self:ty) => ( impl<$S: Clone + ApproxEq<$S>> ApproxEq<$S> for $Self { diff --git a/src/cgmath/matrix.rs b/src/cgmath/matrix.rs index d26193e..5f8cebd 100644 --- a/src/cgmath/matrix.rs +++ b/src/cgmath/matrix.rs @@ -196,7 +196,7 @@ pub trait Matrix #[inline] fn swap_r(&mut self, a: uint, b: uint) { - for c in self.mut_iter() { c.swap(a, b) } + self.each_mut(|_, c| c.swap(a, b)) } #[inline] @@ -222,26 +222,26 @@ pub trait Matrix // *self.mut_cr(cb, rb) = tmp; // } - #[inline] fn neg_self(&mut self) { for c in self.mut_iter() { *c = c.neg() } } + #[inline] fn neg_self(&mut self) { self.each_mut(|_, x| *x = x.neg()) } #[inline] fn mul_s(&self, s: S) -> Self { build(|i| self.i(i).mul_s(s.clone())) } #[inline] fn div_s(&self, s: S) -> Self { build(|i| self.i(i).div_s(s.clone())) } #[inline] fn rem_s(&self, s: S) -> Self { build(|i| self.i(i).rem_s(s.clone())) } - #[inline] fn mul_self_s(&mut self, s: S) { for c in self.mut_iter() { *c = c.mul_s(s.clone()) } } - #[inline] fn div_self_s(&mut self, s: S) { for c in self.mut_iter() { *c = c.div_s(s.clone()) } } - #[inline] fn rem_self_s(&mut self, s: S) { for c in self.mut_iter() { *c = c.rem_s(s.clone()) } } - - #[inline] fn mul_v(&self, v: &V) -> V { build(|i| self.r(i).dot(v)) } - #[inline] fn add_m(&self, other: &Self) -> Self { build(|i| self.i(i).add_v(other.i(i))) } #[inline] fn sub_m(&self, other: &Self) -> Self { build(|i| self.i(i).sub_v(other.i(i))) } - #[inline] fn add_self_m(&mut self, other: &Self) { for (a, b) in self.mut_iter().zip(other.iter()) { *a = a.add_v(b) } } - #[inline] fn sub_self_m(&mut self, other: &Self) { for (a, b) in self.mut_iter().zip(other.iter()) { *a = a.sub_v(b) } } + #[inline] fn mul_v(&self, v: &V) -> V { build(|i| self.r(i).dot(v)) } fn mul_m(&self, other: &Self) -> Self; + #[inline] fn mul_self_s(&mut self, s: S) { self.each_mut(|_, c| *c = c.mul_s(s.clone())) } + #[inline] fn div_self_s(&mut self, s: S) { self.each_mut(|_, c| *c = c.div_s(s.clone())) } + #[inline] fn rem_self_s(&mut self, s: S) { self.each_mut(|_, c| *c = c.rem_s(s.clone())) } + + #[inline] fn add_self_m(&mut self, other: &Self) { self.each_mut(|i, c| *c = c.add_v(other.c(i))) } + #[inline] fn sub_self_m(&mut self, other: &Self) { self.each_mut(|i, c| *c = c.sub_v(other.c(i))) } + fn transpose(&self) -> Self; fn transpose_self(&mut self); fn determinant(&self) -> S; diff --git a/src/cgmath/point.rs b/src/cgmath/point.rs index 650f7f0..7de5392 100644 --- a/src/cgmath/point.rs +++ b/src/cgmath/point.rs @@ -69,11 +69,11 @@ pub trait Point #[inline] fn add_v(&self, other: &V) -> Self { build(|i| self.i(i).add(other.i(i))) } #[inline] fn sub_p(&self, other: &Self) -> V { build(|i| self.i(i).sub(other.i(i))) } - #[inline] fn mul_self_s(&mut self, s: S) { for x in self.mut_iter() { *x = x.mul(&s) } } - #[inline] fn div_self_s(&mut self, s: S) { for x in self.mut_iter() { *x = x.div(&s) } } - #[inline] fn rem_self_s(&mut self, s: S) { for x in self.mut_iter() { *x = x.rem(&s) } } + #[inline] fn mul_self_s(&mut self, s: S) { self.each_mut(|_, x| *x = x.mul(&s)) } + #[inline] fn div_self_s(&mut self, s: S) { self.each_mut(|_, x| *x = x.div(&s)) } + #[inline] fn rem_self_s(&mut self, s: S) { self.each_mut(|_, x| *x = x.rem(&s)) } - #[inline] fn add_self_v(&mut self, other: &V) { for (a, b) in self.mut_iter().zip(other.iter()) { *a = a.add(b) } } + #[inline] fn add_self_v(&mut self, other: &V) { self.each_mut(|i, x| *x = x.add(other.i(i))) } } array!(impl Point2 -> [S, ..2] _2) diff --git a/src/cgmath/vector.rs b/src/cgmath/vector.rs index 5769b09..99d5118 100644 --- a/src/cgmath/vector.rs +++ b/src/cgmath/vector.rs @@ -115,19 +115,19 @@ pub trait Vector #[inline] fn div_v(&self, other: &Self) -> Self { build(|i| self.i(i).div(other.i(i))) } #[inline] fn rem_v(&self, other: &Self) -> Self { build(|i| self.i(i).rem(other.i(i))) } - #[inline] fn neg_self(&mut self); + #[inline] fn neg_self(&mut self) { self.each_mut(|_, x| *x = x.neg()) } - #[inline] fn add_self_s(&mut self, s: S); - #[inline] fn sub_self_s(&mut self, s: S); - #[inline] fn mul_self_s(&mut self, s: S); - #[inline] fn div_self_s(&mut self, s: S); - #[inline] fn rem_self_s(&mut self, s: S); + #[inline] fn add_self_s(&mut self, s: S) { self.each_mut(|_, x| *x = x.add(&s)) } + #[inline] fn sub_self_s(&mut self, s: S) { self.each_mut(|_, x| *x = x.sub(&s)) } + #[inline] fn mul_self_s(&mut self, s: S) { self.each_mut(|_, x| *x = x.mul(&s)) } + #[inline] fn div_self_s(&mut self, s: S) { self.each_mut(|_, x| *x = x.div(&s)) } + #[inline] fn rem_self_s(&mut self, s: S) { self.each_mut(|_, x| *x = x.rem(&s)) } - #[inline] fn add_self_v(&mut self, other: &Self); - #[inline] fn sub_self_v(&mut self, other: &Self); - #[inline] fn mul_self_v(&mut self, other: &Self); - #[inline] fn div_self_v(&mut self, other: &Self); - #[inline] fn rem_self_v(&mut self, other: &Self); + #[inline] fn add_self_v(&mut self, other: &Self) { self.each_mut(|i, x| *x = x.add(other.i(i))) } + #[inline] fn sub_self_v(&mut self, other: &Self) { self.each_mut(|i, x| *x = x.sub(other.i(i))) } + #[inline] fn mul_self_v(&mut self, other: &Self) { self.each_mut(|i, x| *x = x.mul(other.i(i))) } + #[inline] fn div_self_v(&mut self, other: &Self) { self.each_mut(|i, x| *x = x.div(other.i(i))) } + #[inline] fn rem_self_v(&mut self, other: &Self) { self.each_mut(|i, x| *x = x.rem(other.i(i))) } /// The sum of each component of the vector. #[inline] fn comp_add(&self) -> S { self.fold(|a, b| a.add(b)) } @@ -157,21 +157,7 @@ impl Neg> for Vec4 { #[inline] fn neg(&self) -> macro_rules! vector( (impl $Self:ident <$S:ident> $Slice:ty { $x:ident, $($xs:ident),+ }) => ( - impl<$S: Clone + Num + Ord> Vector<$S, $Slice> for $Self<$S> { - #[inline] fn neg_self(&mut self) { self.$x = -self.$x; $(self.$xs = -self.$xs;)+ } - - #[inline] fn add_self_s(&mut self, s: S) { self.$x = self.$x.add(&s); $(self.$xs = self.$x.add(&s);)+ } - #[inline] fn sub_self_s(&mut self, s: S) { self.$x = self.$x.sub(&s); $(self.$xs = self.$x.sub(&s);)+ } - #[inline] fn mul_self_s(&mut self, s: S) { self.$x = self.$x.mul(&s); $(self.$xs = self.$x.mul(&s);)+ } - #[inline] fn div_self_s(&mut self, s: S) { self.$x = self.$x.div(&s); $(self.$xs = self.$x.div(&s);)+ } - #[inline] fn rem_self_s(&mut self, s: S) { self.$x = self.$x.rem(&s); $(self.$xs = self.$x.rem(&s);)+ } - - #[inline] fn add_self_v(&mut self, other: &$Self<$S>) { self.$x = self.$x.add(&other.$x); $(self.$xs = self.$xs.add(&other.$xs);)+ } - #[inline] fn sub_self_v(&mut self, other: &$Self<$S>) { self.$x = self.$x.sub(&other.$x); $(self.$xs = self.$xs.sub(&other.$xs);)+ } - #[inline] fn mul_self_v(&mut self, other: &$Self<$S>) { self.$x = self.$x.mul(&other.$x); $(self.$xs = self.$xs.mul(&other.$xs);)+ } - #[inline] fn div_self_v(&mut self, other: &$Self<$S>) { self.$x = self.$x.div(&other.$x); $(self.$xs = self.$xs.div(&other.$xs);)+ } - #[inline] fn rem_self_v(&mut self, other: &$Self<$S>) { self.$x = self.$x.rem(&other.$x); $(self.$xs = self.$xs.rem(&other.$xs);)+ } - } + impl<$S: Clone + Num + Ord> Vector<$S, $Slice> for $Self<$S>; ) )