From c404092879421ad1e7ed837bccc22a66557a7bba Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Mon, 2 Sep 2013 18:14:57 +1000 Subject: [PATCH] Use iterators instead of Array::{map_mut, bimap_mut, fold} --- src/cgmath/traits/alg/array.rs | 37 +++++++---------------------- src/cgmath/traits/alg/matrix.rs | 2 +- src/cgmath/traits/ext/vector_ext.rs | 28 ++++++++++++---------- 3 files changed, 24 insertions(+), 43 deletions(-) diff --git a/src/cgmath/traits/alg/array.rs b/src/cgmath/traits/alg/array.rs index e32a6b8..ca12807 100644 --- a/src/cgmath/traits/alg/array.rs +++ b/src/cgmath/traits/alg/array.rs @@ -15,10 +15,9 @@ #[macro_escape]; -use std::vec::VecIterator; +use std::vec::{VecIterator, VecMutIterator}; pub trait Array { - fn len(&self) -> uint; fn i<'a>(&'a self, i: uint) -> &'a T; fn mut_i<'a>(&'a mut self, i: uint) -> &'a mut T; fn as_slice<'a>(&'a self) -> &'a Slice; @@ -26,48 +25,23 @@ pub trait Array { fn from_slice(slice: Slice) -> Self; fn build(builder: &fn(i: uint) -> T) -> Self; fn iter<'a>(&'a self) -> VecIterator<'a, T>; + fn mut_iter<'a>(&'a mut self) -> VecMutIterator<'a, T>; #[inline] fn map>(&self, f: &fn(&T) -> U) -> UU { Array::build(|i| f(self.i(i))) } - #[inline] - fn map_mut(&mut self, f: &fn(&mut T)) { - for i in range(0, self.len()) { - f(self.mut_i(i)); - } - } - #[inline] fn bimap, V, SliceV, VV: Array>(&self, other: &UU, f: &fn(&T, &U) -> V) -> VV { Array::build(|i| f(self.i(i), other.i(i))) } - - #[inline] - fn bimap_mut>(&mut self, other: &UU, f: &fn(&mut T, &U)) { - for i in range(0, self.len()) { - f(self.mut_i(i), other.i(i)); - } - } - - #[inline] - fn fold(&self, init: U, f: &fn(acc: &U, x: &T) -> U) -> U { - let mut acc = init; - for i in range(0, self.len()) { - acc = f(&acc, self.i(i)); - } - acc - } } macro_rules! array( (impl<$S:ident> $Self:ty -> [$T:ty, ..$n:expr]) => ( impl<$S> Array<$T, [$T,..$n]> for $Self { - #[inline] - fn len(&self) -> uint { $n } - #[inline] fn i<'a>(&'a self, i: uint) -> &'a $T { &'a self.as_slice()[i] @@ -97,7 +71,7 @@ macro_rules! array( fn build(builder: &fn(i: uint) -> $T) -> $Self { use std::unstable::intrinsics; let mut s: [$T,..$n] = unsafe { intrinsics::uninit() }; - for i in range::(0, $n) { + for i in range(0u, $n) { s[i] = builder(i); } Array::from_slice(s) @@ -107,6 +81,11 @@ macro_rules! array( fn iter<'a>(&'a self) -> ::std::vec::VecIterator<'a, $T> { self.as_slice().iter() } + + #[inline] + fn mut_iter<'a>(&'a mut self) -> ::std::vec::VecMutIterator<'a, $T> { + self.as_mut_slice().mut_iter() + } } ) ) diff --git a/src/cgmath/traits/alg/matrix.rs b/src/cgmath/traits/alg/matrix.rs index e782c22..f5f77e3 100644 --- a/src/cgmath/traits/alg/matrix.rs +++ b/src/cgmath/traits/alg/matrix.rs @@ -46,7 +46,7 @@ pub trait Matrix #[inline] fn swap_r(&mut self, a: uint, b: uint) { - self.map_mut(|c| c.swap(a, b)); + for c in self.mut_iter() { c.swap(a, b) } } #[inline] diff --git a/src/cgmath/traits/ext/vector_ext.rs b/src/cgmath/traits/ext/vector_ext.rs index afeba38..0afc9fd 100644 --- a/src/cgmath/traits/ext/vector_ext.rs +++ b/src/cgmath/traits/ext/vector_ext.rs @@ -13,6 +13,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +use std::num::{zero, one}; + use traits::alg::Field; use traits::alg::VectorSpace; use traits::alg::Array; @@ -37,19 +39,19 @@ pub trait VectorExt #[inline] fn div_v(&self, other: &Self) -> Self { self.bimap(other, |a, b| a.div(b) ) } #[inline] fn rem_v(&self, other: &Self) -> Self { self.bimap(other, |a, b| a.rem(b) ) } - #[inline] fn neg_self(&mut self) { self.map_mut(|x| *x = -*x); } - #[inline] fn add_self_s(&mut self, s: S) { self.map_mut(|x| *x = x.add(&s)); } - #[inline] fn sub_self_s(&mut self, s: S) { self.map_mut(|x| *x = x.sub(&s)); } - #[inline] fn mul_self_s(&mut self, s: S) { self.map_mut(|x| *x = x.mul(&s)); } - #[inline] fn div_self_s(&mut self, s: S) { self.map_mut(|x| *x = x.div(&s)); } - #[inline] fn rem_self_s(&mut self, s: S) { self.map_mut(|x| *x = x.rem(&s)); } + #[inline] fn neg_self(&mut self) { for x in self.mut_iter() { *x = x.neg() } } + #[inline] fn add_self_s(&mut self, s: S) { for x in self.mut_iter() { *x = x.add(&s) } } + #[inline] fn sub_self_s(&mut self, s: S) { for x in self.mut_iter() { *x = x.sub(&s) } } + #[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 add_self_v(&mut self, other: &Self) { self.bimap_mut::(other, |a, b| *a = a.add(b)) } - #[inline] fn sub_self_v(&mut self, other: &Self) { self.bimap_mut::(other, |a, b| *a = a.sub(b)) } - #[inline] fn mul_self_v(&mut self, other: &Self) { self.bimap_mut::(other, |a, b| *a = a.mul(b)) } - #[inline] fn div_self_v(&mut self, other: &Self) { self.bimap_mut::(other, |a, b| *a = a.div(b)) } - #[inline] fn rem_self_v(&mut self, other: &Self) { self.bimap_mut::(other, |a, b| *a = a.rem(b)) } + #[inline] fn add_self_v(&mut self, other: &Self) { for (a, b) in self.mut_iter().zip(other.iter()) { *a = a.add(b) } } + #[inline] fn sub_self_v(&mut self, other: &Self) { for (a, b) in self.mut_iter().zip(other.iter()) { *a = a.sub(b) } } + #[inline] fn mul_self_v(&mut self, other: &Self) { for (a, b) in self.mut_iter().zip(other.iter()) { *a = a.mul(b) } } + #[inline] fn div_self_v(&mut self, other: &Self) { for (a, b) in self.mut_iter().zip(other.iter()) { *a = a.div(b) } } + #[inline] fn rem_self_v(&mut self, other: &Self) { for (a, b) in self.mut_iter().zip(other.iter()) { *a = a.rem(b) } } - #[inline] fn comp_add(&self) -> S { fail!() } - #[inline] fn comp_mul(&self) -> S { fail!() } + #[inline] fn comp_add(&self) -> S { self.iter().fold(zero::(), |a, b| a.add(b)) } + #[inline] fn comp_mul(&self) -> S { self.iter().fold(one::(), |a, b| a.mul(b)) } }