From 68e03cd3922d16b9370e2da6be5341ad38de31ea Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Sat, 14 Sep 2013 11:53:12 +1000 Subject: [PATCH] Remove Array::{map, bimap} functions --- src/cgmath/array.rs | 11 ----------- src/cgmath/matrix.rs | 16 ++++++++-------- src/cgmath/point.rs | 10 +++++----- src/cgmath/vector.rs | 40 +++++++++++++++------------------------- 4 files changed, 28 insertions(+), 49 deletions(-) diff --git a/src/cgmath/array.rs b/src/cgmath/array.rs index 5ce96d3..a3517ee 100644 --- a/src/cgmath/array.rs +++ b/src/cgmath/array.rs @@ -40,17 +40,6 @@ pub trait Array *self.mut_i(b) = tmp; } - #[inline] - fn map>(&self, f: &fn(&T) -> U) -> UU { - Array::build(|i| f(self.i(i))) - } - - #[inline] - fn bimap, - 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; } diff --git a/src/cgmath/matrix.rs b/src/cgmath/matrix.rs index af27ea4..73d3e0f 100644 --- a/src/cgmath/matrix.rs +++ b/src/cgmath/matrix.rs @@ -224,9 +224,9 @@ pub trait Matrix #[inline] fn neg_self(&mut self) { for c in self.mut_iter() { *c = c.neg() } } - #[inline] fn mul_s(&self, s: S) -> Self { self.map(|c| c.mul_s(s.clone())) } - #[inline] fn div_s(&self, s: S) -> Self { self.map(|c| c.div_s(s.clone())) } - #[inline] fn rem_s(&self, s: S) -> Self { self.map(|c| c.rem_s(s.clone())) } + #[inline] fn mul_s(&self, s: S) -> Self { Array::build(|i| self.i(i).mul_s(s.clone())) } + #[inline] fn div_s(&self, s: S) -> Self { Array::build(|i| self.i(i).div_s(s.clone())) } + #[inline] fn rem_s(&self, s: S) -> Self { Array::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()) } } @@ -234,8 +234,8 @@ pub trait Matrix #[inline] fn mul_v(&self, v: &V) -> V { Array::build(|i| self.r(i).dot(v)) } - #[inline] fn add_m(&self, other: &Self) -> Self { self.bimap(other, |a, b| a.add_v(b) ) } - #[inline] fn sub_m(&self, other: &Self) -> Self { self.bimap(other, |a, b| a.sub_v(b) ) } + #[inline] fn add_m(&self, other: &Self) -> Self { Array::build(|i| self.i(i).add_v(other.i(i))) } + #[inline] fn sub_m(&self, other: &Self) -> Self { Array::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) } } @@ -278,9 +278,9 @@ pub trait Matrix fn is_symmetric(&self) -> bool; } -impl Neg> for Mat2 { #[inline] fn neg(&self) -> Mat2 { self.map(|c| c.neg()) } } -impl Neg> for Mat3 { #[inline] fn neg(&self) -> Mat3 { self.map(|c| c.neg()) } } -impl Neg> for Mat4 { #[inline] fn neg(&self) -> Mat4 { self.map(|c| c.neg()) } } +impl Neg> for Mat2 { #[inline] fn neg(&self) -> Mat2 { Array::build(|i| self.i(i).neg()) } } +impl Neg> for Mat3 { #[inline] fn neg(&self) -> Mat3 { Array::build(|i| self.i(i).neg()) } } +impl Neg> for Mat4 { #[inline] fn neg(&self) -> Mat4 { Array::build(|i| self.i(i).neg()) } } impl Matrix, ..2], Vec2, [S, ..2]> diff --git a/src/cgmath/point.rs b/src/cgmath/point.rs index df9f1d7..d225388 100644 --- a/src/cgmath/point.rs +++ b/src/cgmath/point.rs @@ -62,12 +62,12 @@ pub trait Point > : Array { - #[inline] fn mul_s(&self, s: S) -> Self { self.map(|x| x.mul(&s)) } - #[inline] fn div_s(&self, s: S) -> Self { self.map(|x| x.div(&s)) } - #[inline] fn rem_s(&self, s: S) -> Self { self.map(|x| x.rem(&s)) } + #[inline] fn mul_s(&self, s: S) -> Self { Array::build(|i| self.i(i).mul(&s)) } + #[inline] fn div_s(&self, s: S) -> Self { Array::build(|i| self.i(i).div(&s)) } + #[inline] fn rem_s(&self, s: S) -> Self { Array::build(|i| self.i(i).rem(&s)) } - #[inline] fn add_v(&self, other: &V) -> Self { self.bimap(other, |a, b| a.add(b) ) } - #[inline] fn sub_p(&self, other: &Self) -> V { self.bimap(other, |a, b| a.sub(b) ) } + #[inline] fn add_v(&self, other: &V) -> Self { Array::build(|i| self.i(i).add(other.i(i))) } + #[inline] fn sub_p(&self, other: &Self) -> V { Array::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) } } diff --git a/src/cgmath/vector.rs b/src/cgmath/vector.rs index 8a436e4..9e18257 100644 --- a/src/cgmath/vector.rs +++ b/src/cgmath/vector.rs @@ -103,17 +103,17 @@ pub trait Vector + Neg + Zero + One { - #[inline] fn add_s(&self, s: S) -> Self; - #[inline] fn sub_s(&self, s: S) -> Self; - #[inline] fn mul_s(&self, s: S) -> Self; - #[inline] fn div_s(&self, s: S) -> Self; - #[inline] fn rem_s(&self, s: S) -> Self; + #[inline] fn add_s(&self, s: S) -> Self { Array::build(|i| self.i(i).add(&s)) } + #[inline] fn sub_s(&self, s: S) -> Self { Array::build(|i| self.i(i).sub(&s)) } + #[inline] fn mul_s(&self, s: S) -> Self { Array::build(|i| self.i(i).mul(&s)) } + #[inline] fn div_s(&self, s: S) -> Self { Array::build(|i| self.i(i).div(&s)) } + #[inline] fn rem_s(&self, s: S) -> Self { Array::build(|i| self.i(i).rem(&s)) } - #[inline] fn add_v(&self, other: &Self) -> Self; - #[inline] fn sub_v(&self, other: &Self) -> Self; - #[inline] fn mul_v(&self, other: &Self) -> Self; - #[inline] fn div_v(&self, other: &Self) -> Self; - #[inline] fn rem_v(&self, other: &Self) -> Self; + #[inline] fn add_v(&self, other: &Self) -> Self { Array::build(|i| self.i(i).add(other.i(i))) } + #[inline] fn sub_v(&self, other: &Self) -> Self { Array::build(|i| self.i(i).sub(other.i(i))) } + #[inline] fn mul_v(&self, other: &Self) -> Self { Array::build(|i| self.i(i).mul(other.i(i))) } + #[inline] fn div_v(&self, other: &Self) -> Self { Array::build(|i| self.i(i).div(other.i(i))) } + #[inline] fn rem_v(&self, other: &Self) -> Self { Array::build(|i| self.i(i).rem(other.i(i))) } #[inline] fn neg_self(&mut self); @@ -145,29 +145,19 @@ pub trait Vector #[inline] fn comp_max(&self) -> S { self.iter().max().unwrap().clone() } } +#[inline] fn dot>(a: V, b: V) -> S { a.dot(&b) } + impl One for Vec2 { #[inline] fn one() -> Vec2 { Vec2::ident() } } impl One for Vec3 { #[inline] fn one() -> Vec3 { Vec3::ident() } } impl One for Vec4 { #[inline] fn one() -> Vec4 { Vec4::ident() } } -impl Neg> for Vec2 { #[inline] fn neg(&self) -> Vec2 { Vec2::new(-self.x, -self.y) } } -impl Neg> for Vec3 { #[inline] fn neg(&self) -> Vec3 { Vec3::new(-self.x, -self.y, -self.z) } } -impl Neg> for Vec4 { #[inline] fn neg(&self) -> Vec4 { Vec4::new(-self.x, -self.y, -self.z, -self.w) } } +impl Neg> for Vec2 { #[inline] fn neg(&self) -> Vec2 { Array::build(|i| self.i(i).neg()) } } +impl Neg> for Vec3 { #[inline] fn neg(&self) -> Vec3 { Array::build(|i| self.i(i).neg()) } } +impl Neg> for Vec4 { #[inline] fn neg(&self) -> Vec4 { Array::build(|i| self.i(i).neg()) } } 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 add_s(&self, s: S) -> $Self<$S> { $Self::new(self.$x.add(&s), $(self.$xs.add(&s)),+) } - #[inline] fn sub_s(&self, s: S) -> $Self<$S> { $Self::new(self.$x.sub(&s), $(self.$xs.sub(&s)),+) } - #[inline] fn mul_s(&self, s: S) -> $Self<$S> { $Self::new(self.$x.mul(&s), $(self.$xs.mul(&s)),+) } - #[inline] fn div_s(&self, s: S) -> $Self<$S> { $Self::new(self.$x.div(&s), $(self.$xs.div(&s)),+) } - #[inline] fn rem_s(&self, s: S) -> $Self<$S> { $Self::new(self.$x.rem(&s), $(self.$xs.rem(&s)),+) } - - #[inline] fn add_v(&self, other: &$Self<$S>) -> $Self<$S> { $Self::new(self.$x.add(&other.$x), $(self.$xs.add(&other.$xs)),+) } - #[inline] fn sub_v(&self, other: &$Self<$S>) -> $Self<$S> { $Self::new(self.$x.sub(&other.$x), $(self.$xs.sub(&other.$xs)),+) } - #[inline] fn mul_v(&self, other: &$Self<$S>) -> $Self<$S> { $Self::new(self.$x.mul(&other.$x), $(self.$xs.mul(&other.$xs)),+) } - #[inline] fn div_v(&self, other: &$Self<$S>) -> $Self<$S> { $Self::new(self.$x.div(&other.$x), $(self.$xs.div(&other.$xs)),+) } - #[inline] fn rem_v(&self, other: &$Self<$S>) -> $Self<$S> { $Self::new(self.$x.rem(&other.$x), $(self.$xs.rem(&other.$xs)),+) } - #[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);)+ }