diff --git a/src/cgmath/array.rs b/src/cgmath/array.rs index 5494fa5..5ce96d3 100644 --- a/src/cgmath/array.rs +++ b/src/cgmath/array.rs @@ -50,6 +50,8 @@ pub trait Array V: Clone, SliceV, VV: Array>(&self, other: &UU, f: &fn(&T, &U) -> V) -> VV { Array::build(|i| f(self.i(i), other.i(i))) } + + fn zip(&self, f: &fn(&T, &T) -> T) -> T; } macro_rules! array( @@ -94,17 +96,27 @@ macro_rules! array( fn mut_iter<'a>(&'a mut self) -> ::std::vec::VecMutIterator<'a, $T> { self.as_mut_slice().mut_iter() } + + #[inline] + fn zip(&self, f: &fn(&$T, &$T) -> $T) -> $T { + gen_zip!($_n) + } } ) ) macro_rules! gen_builder( - (_1) => ([builder(0)]); (_2) => ([builder(0), builder(1)]); (_3) => ([builder(0), builder(1), builder(2)]); (_4) => ([builder(0), builder(1), builder(2), builder(3)]); ) +macro_rules! gen_zip( + (_2) => (f(self.i(0), self.i(1))); + (_3) => (f(&f(self.i(0), self.i(1)), self.i(2))); + (_4) => (f(&f(&f(self.i(0), self.i(1)), self.i(2)), self.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/vector.rs b/src/cgmath/vector.rs index 2f10772..8a436e4 100644 --- a/src/cgmath/vector.rs +++ b/src/cgmath/vector.rs @@ -130,10 +130,10 @@ pub trait Vector #[inline] fn rem_self_v(&mut self, other: &Self); /// The sum of each component of the vector. - #[inline] fn comp_add(&self) -> S; + #[inline] fn comp_add(&self) -> S { self.zip(|a, b| a.add(b)) } /// The product of each component of the vector. - #[inline] fn comp_mul(&self) -> S; + #[inline] fn comp_mul(&self) -> S { self.zip(|a, b| a.mul(b)) } /// Vector dot product. #[inline] fn dot(&self, other: &Self) -> S { self.mul_v(other).comp_add() } @@ -181,12 +181,6 @@ macro_rules! vector( #[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);)+ } - - /// The sum of each component of the vector. - #[inline] fn comp_add(&self) -> S { self.$x $(.add(&self.$xs))+ } - - /// The product of each component of the vector. - #[inline] fn comp_mul(&self) -> S { self.$x $(.mul(&self.$xs))+ } } ) )