diff --git a/src/cgmath/traits/alg/array.rs b/src/cgmath/traits/alg/array.rs index 83f2c15..e32a6b8 100644 --- a/src/cgmath/traits/alg/array.rs +++ b/src/cgmath/traits/alg/array.rs @@ -15,6 +15,8 @@ #[macro_escape]; +use std::vec::VecIterator; + pub trait Array { fn len(&self) -> uint; fn i<'a>(&'a self, i: uint) -> &'a T; @@ -23,6 +25,7 @@ pub trait Array { fn as_mut_slice<'a>(&'a mut self) -> &'a mut Slice; fn from_slice(slice: Slice) -> Self; fn build(builder: &fn(i: uint) -> T) -> Self; + fn iter<'a>(&'a self) -> VecIterator<'a, T>; #[inline] fn map>(&self, f: &fn(&T) -> U) -> UU { @@ -99,6 +102,11 @@ macro_rules! array( } Array::from_slice(s) } + + #[inline] + fn iter<'a>(&'a self) -> ::std::vec::VecIterator<'a, $T> { + self.as_slice().iter() + } } ) ) diff --git a/src/cgmath/types/vector.rs b/src/cgmath/types/vector.rs index 3454006..6ef37ed 100644 --- a/src/cgmath/types/vector.rs +++ b/src/cgmath/types/vector.rs @@ -13,7 +13,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -use std::num; use std::num::{Zero, zero}; use std::num::{sqrt, atan2}; @@ -121,17 +120,21 @@ impl Vec3 { macro_rules! impl_vec_inner_product( ($Self:ident <$S:ident>) => ( impl<$S:Real + Field + ApproxEq<$S>> InnerProductSpace<$S> for $Self<$S> { + #[inline] fn norm(&self) -> $S { - num::sqrt(self.inner(self)) + sqrt(self.inner(self)) } + #[inline] fn inner(&self, other: &$Self<$S>) -> $S { - let comp_sum: $Self<$S> = self.bimap(other, |a, b| a.mul(b)); - comp_sum.fold(num::zero::<$S>(), |a, b| a.add(b)) + self.iter().zip(other.iter()) + .map(|(a, b)| a.mul(b)) + .fold(zero::<$S>(), |a, b| a.add(&b)) } + #[inline] fn is_orthogonal(&self, other: &$Self<$S>) -> bool { - self.inner(other).approx_eq(&num::zero()) + self.inner(other).approx_eq(&zero()) } } )