Use higher order functions to reduce code repetition in some methods

This commit is contained in:
Brendan Zabarauskas 2013-06-12 15:49:54 +10:00
parent b727dd2bf5
commit e82ac511a5
7 changed files with 52 additions and 147 deletions

View file

@ -218,8 +218,7 @@ impl<T:Copy + Num> Mat2<T> {
#[inline] #[inline]
pub fn mul_t(&self, value: T) -> Mat2<T> { pub fn mul_t(&self, value: T) -> Mat2<T> {
Mat2::from_cols(self.col(0).mul_t(value), self.map(|&c| c.mul_t(value))
self.col(1).mul_t(value))
} }
#[inline] #[inline]
@ -248,8 +247,7 @@ impl<T:Copy + Num> Mat2<T> {
#[inline] #[inline]
pub fn mul_self_t(&mut self, value: T) { pub fn mul_self_t(&mut self, value: T) {
self.x.mul_self_t(value); self.map_mut(|x| x.mul_self_t(value))
self.y.mul_self_t(value);
} }
#[inline] #[inline]
@ -337,7 +335,7 @@ impl<T:Copy + Num> ToMat4<T> for Mat2<T> {
impl<T:Copy + Num> Neg<Mat2<T>> for Mat2<T> { impl<T:Copy + Num> Neg<Mat2<T>> for Mat2<T> {
#[inline] #[inline]
pub fn neg(&self) -> Mat2<T> { pub fn neg(&self) -> Mat2<T> {
Mat2::from_cols(-self.col(0), -self.col(1)) self.map(|&x| -x)
} }
} }

View file

@ -259,9 +259,7 @@ impl<T:Copy + Num> Mat3<T> {
#[inline] #[inline]
pub fn mul_t(&self, value: T) -> Mat3<T> { pub fn mul_t(&self, value: T) -> Mat3<T> {
Mat3::from_cols(self.col(0).mul_t(value), self.map(|&c| c.mul_t(value))
self.col(1).mul_t(value),
self.col(2).mul_t(value))
} }
#[inline] #[inline]
@ -302,9 +300,7 @@ impl<T:Copy + Num> Mat3<T> {
#[inline] #[inline]
pub fn mul_self_t(&mut self, value: T) { pub fn mul_self_t(&mut self, value: T) {
self.col_mut(0).mul_self_t(value); self.map_mut(|x| x.mul_self_t(value))
self.col_mut(1).mul_self_t(value);
self.col_mut(2).mul_self_t(value);
} }
#[inline] #[inline]
@ -372,7 +368,7 @@ impl<T:Copy + Num> ToMat4<T> for Mat3<T> {
impl<T:Copy + Num> Neg<Mat3<T>> for Mat3<T> { impl<T:Copy + Num> Neg<Mat3<T>> for Mat3<T> {
#[inline] #[inline]
pub fn neg(&self) -> Mat3<T> { pub fn neg(&self) -> Mat3<T> {
Mat3::from_cols(-self.col(0), -self.col(1), -self.col(2)) self.map(|&x| -x)
} }
} }

View file

@ -301,10 +301,7 @@ impl<T:Copy + Num> Mat4<T> {
#[inline] #[inline]
pub fn mul_t(&self, value: T) -> Mat4<T> { pub fn mul_t(&self, value: T) -> Mat4<T> {
Mat4::from_cols(self.col(0).mul_t(value), self.map(|&c| c.mul_t(value))
self.col(1).mul_t(value),
self.col(2).mul_t(value),
self.col(3).mul_t(value))
} }
#[inline] #[inline]
@ -356,10 +353,7 @@ impl<T:Copy + Num> Mat4<T> {
#[inline] #[inline]
pub fn mul_self_t(&mut self, value: T) { pub fn mul_self_t(&mut self, value: T) {
self.col_mut(0).mul_self_t(value); self.map_mut(|x| x.mul_self_t(value))
self.col_mut(1).mul_self_t(value);
self.col_mut(2).mul_self_t(value);
self.col_mut(3).mul_self_t(value);
} }
#[inline] #[inline]
@ -423,7 +417,7 @@ impl<T:Copy + Num> Mat4<T> {
impl<T:Copy + Num> Neg<Mat4<T>> for Mat4<T> { impl<T:Copy + Num> Neg<Mat4<T>> for Mat4<T> {
#[inline] #[inline]
pub fn neg(&self) -> Mat4<T> { pub fn neg(&self) -> Mat4<T> {
Mat4::from_cols(-self.col(0), -self.col(1), -self.col(2), -self.col(3)) self.map(|&x| -x)
} }
} }

View file

@ -128,10 +128,7 @@ impl<T:Copy + Real> Quat<T> {
/// The multiplicative identity, ie: `q = 1 + 0i + 0j + 0i` /// The multiplicative identity, ie: `q = 1 + 0i + 0j + 0i`
#[inline] #[inline]
pub fn identity() -> Quat<T> { pub fn identity() -> Quat<T> {
Quat::new(One::one(), Quat::from_sv(One::one(), Vec3::zero())
Zero::zero(),
Zero::zero(),
Zero::zero())
} }
/// The additive identity, ie: `q = 0 + 0i + 0j + 0i` /// The additive identity, ie: `q = 0 + 0i + 0j + 0i`
@ -191,19 +188,13 @@ impl<T:Copy + Real> Quat<T> {
/// The result of multiplying the quaternion a scalar /// The result of multiplying the quaternion a scalar
#[inline] #[inline]
pub fn mul_t(&self, value: T) -> Quat<T> { pub fn mul_t(&self, value: T) -> Quat<T> {
Quat::new(*self.index(0) * value, self.map(|&x| x * value)
*self.index(1) * value,
*self.index(2) * value,
*self.index(3) * value)
} }
/// The result of dividing the quaternion a scalar /// The result of dividing the quaternion a scalar
#[inline] #[inline]
pub fn div_t(&self, value: T) -> Quat<T> { pub fn div_t(&self, value: T) -> Quat<T> {
Quat::new(*self.index(0) / value, self.map(|&x| x / value)
*self.index(1) / value,
*self.index(2) / value,
*self.index(3) / value)
} }
/// The result of multiplying the quaternion by a vector /// The result of multiplying the quaternion by a vector
@ -323,10 +314,7 @@ impl<T:Copy + Num> ToMat3<T> for Quat<T> {
impl<T:Copy + Float> Neg<Quat<T>> for Quat<T> { impl<T:Copy + Float> Neg<Quat<T>> for Quat<T> {
#[inline] #[inline]
pub fn neg(&self) -> Quat<T> { pub fn neg(&self) -> Quat<T> {
Quat::new(-*self.index(0), self.map(|&x| -x)
-*self.index(1),
-*self.index(2),
-*self.index(3))
} }
} }

View file

@ -62,8 +62,6 @@ impl<T> Dimensional<T,[T,..2]> for Vec2<T> {
pub fn map_mut(&mut self, f: &fn(&mut T)) { pub fn map_mut(&mut self, f: &fn(&mut T)) {
f(self.index_mut(0)); f(self.index_mut(0));
f(self.index_mut(1)); f(self.index_mut(1));
f(self.index_mut(2));
f(self.index_mut(3));
} }
} }
@ -110,32 +108,27 @@ impl<T:Copy + Num> Vec2<T> {
#[inline] #[inline]
pub fn add_t(&self, value: T) -> Vec2<T> { pub fn add_t(&self, value: T) -> Vec2<T> {
Vec2::new(*self.index(0) + value, self.map(|&x| x + value)
*self.index(1) + value)
} }
#[inline] #[inline]
pub fn sub_t(&self, value: T) -> Vec2<T> { pub fn sub_t(&self, value: T) -> Vec2<T> {
Vec2::new(*self.index(0) - value, self.map(|&x| x - value)
*self.index(1) - value)
} }
#[inline] #[inline]
pub fn mul_t(&self, value: T) -> Vec2<T> { pub fn mul_t(&self, value: T) -> Vec2<T> {
Vec2::new(*self.index(0) * value, self.map(|&x| x * value)
*self.index(1) * value)
} }
#[inline] #[inline]
pub fn div_t(&self, value: T) -> Vec2<T> { pub fn div_t(&self, value: T) -> Vec2<T> {
Vec2::new(*self.index(0) / value, self.map(|&x| x / value)
*self.index(1) / value)
} }
#[inline] #[inline]
pub fn rem_t(&self, value: T) -> Vec2<T> { pub fn rem_t(&self, value: T) -> Vec2<T> {
Vec2::new(*self.index(0) % value, self.map(|&x| x % value)
*self.index(1) % value)
} }
#[inline] #[inline]
@ -170,38 +163,32 @@ impl<T:Copy + Num> Vec2<T> {
#[inline] #[inline]
pub fn neg_self(&mut self) { pub fn neg_self(&mut self) {
*self.index_mut(0) = -*self.index(0); self.map_mut(|x| *x = -*x)
*self.index_mut(1) = -*self.index(1);
} }
#[inline] #[inline]
pub fn add_self_t(&mut self, value: T) { pub fn add_self_t(&mut self, value: T) {
*self.index_mut(0) += value; self.map_mut(|x| *x += value)
*self.index_mut(1) += value;
} }
#[inline] #[inline]
pub fn sub_self_t(&mut self, value: T) { pub fn sub_self_t(&mut self, value: T) {
*self.index_mut(0) -= value; self.map_mut(|x| *x -= value)
*self.index_mut(1) -= value;
} }
#[inline] #[inline]
pub fn mul_self_t(&mut self, value: T) { pub fn mul_self_t(&mut self, value: T) {
*self.index_mut(0) *= value; self.map_mut(|x| *x *= value)
*self.index_mut(1) *= value;
} }
#[inline] #[inline]
pub fn div_self_t(&mut self, value: T) { pub fn div_self_t(&mut self, value: T) {
*self.index_mut(0) /= value; self.map_mut(|x| *x /= value)
*self.index_mut(1) /= value;
} }
#[inline] #[inline]
pub fn rem_self_t(&mut self, value: T) { pub fn rem_self_t(&mut self, value: T) {
*self.index_mut(0) %= value; self.map_mut(|x| *x %= value)
*self.index_mut(1) %= value;
} }
#[inline] #[inline]
@ -255,7 +242,7 @@ impl<T:Copy + Num> Vec2<T> {
impl<T:Copy + Num> Neg<Vec2<T>> for Vec2<T> { impl<T:Copy + Num> Neg<Vec2<T>> for Vec2<T> {
#[inline] #[inline]
pub fn neg(&self) -> Vec2<T> { pub fn neg(&self) -> Vec2<T> {
Vec2::new(-self.index(0), -self.index(1)) self.map(|&x| -x)
} }
} }
@ -425,7 +412,7 @@ impl Vec2<bool> {
#[inline] #[inline]
pub fn not(&self) -> Vec2<bool> { pub fn not(&self) -> Vec2<bool> {
Vec2::new(!*self.index(0), !*self.index(1)) self.map(|&x| !x)
} }
} }

View file

@ -63,7 +63,6 @@ impl<T> Dimensional<T,[T,..3]> for Vec3<T> {
f(self.index_mut(0)); f(self.index_mut(0));
f(self.index_mut(1)); f(self.index_mut(1));
f(self.index_mut(2)); f(self.index_mut(2));
f(self.index_mut(3));
} }
} }
@ -116,37 +115,27 @@ impl<T:Copy + Num> Vec3<T> {
#[inline] #[inline]
pub fn add_t(&self, value: T) -> Vec3<T> { pub fn add_t(&self, value: T) -> Vec3<T> {
Vec3::new(*self.index(0) + value, self.map(|&x| x + value)
*self.index(1) + value,
*self.index(2) + value)
} }
#[inline] #[inline]
pub fn sub_t(&self, value: T) -> Vec3<T> { pub fn sub_t(&self, value: T) -> Vec3<T> {
Vec3::new(*self.index(0) - value, self.map(|&x| x - value)
*self.index(1) - value,
*self.index(2) - value)
} }
#[inline] #[inline]
pub fn mul_t(&self, value: T) -> Vec3<T> { pub fn mul_t(&self, value: T) -> Vec3<T> {
Vec3::new(*self.index(0) * value, self.map(|&x| x * value)
*self.index(1) * value,
*self.index(2) * value)
} }
#[inline] #[inline]
pub fn div_t(&self, value: T) -> Vec3<T> { pub fn div_t(&self, value: T) -> Vec3<T> {
Vec3::new(*self.index(0) / value, self.map(|&x| x / value)
*self.index(1) / value,
*self.index(2) / value)
} }
#[inline] #[inline]
pub fn rem_t(&self, value: T) -> Vec3<T> { pub fn rem_t(&self, value: T) -> Vec3<T> {
Vec3::new(*self.index(0) % value, self.map(|&x| x % value)
*self.index(1) % value,
*self.index(2) % value)
} }
#[inline] #[inline]
@ -186,44 +175,32 @@ impl<T:Copy + Num> Vec3<T> {
#[inline] #[inline]
pub fn neg_self(&mut self) { pub fn neg_self(&mut self) {
*self.index_mut(0) = -*self.index(0); self.map_mut(|x| *x = -*x)
*self.index_mut(1) = -*self.index(1);
*self.index_mut(2) = -*self.index(2);
} }
#[inline] #[inline]
pub fn add_self_t(&mut self, value: T) { pub fn add_self_t(&mut self, value: T) {
*self.index_mut(0) += value; self.map_mut(|x| *x += value)
*self.index_mut(1) += value;
*self.index_mut(2) += value;
} }
#[inline] #[inline]
pub fn sub_self_t(&mut self, value: T) { pub fn sub_self_t(&mut self, value: T) {
*self.index_mut(0) -= value; self.map_mut(|x| *x -= value)
*self.index_mut(1) -= value;
*self.index_mut(2) -= value;
} }
#[inline] #[inline]
pub fn mul_self_t(&mut self, value: T) { pub fn mul_self_t(&mut self, value: T) {
*self.index_mut(0) *= value; self.map_mut(|x| *x *= value)
*self.index_mut(1) *= value;
*self.index_mut(2) *= value;
} }
#[inline] #[inline]
pub fn div_self_t(&mut self, value: T) { pub fn div_self_t(&mut self, value: T) {
*self.index_mut(0) /= value; self.map_mut(|x| *x /= value)
*self.index_mut(1) /= value;
*self.index_mut(2) /= value;
} }
#[inline] #[inline]
pub fn rem_self_t(&mut self, value: T) { pub fn rem_self_t(&mut self, value: T) {
*self.index_mut(0) %= value; self.map_mut(|x| *x %= value)
*self.index_mut(1) %= value;
*self.index_mut(2) %= value;
} }
#[inline] #[inline]
@ -289,7 +266,7 @@ impl<T:Copy + Num> Vec3<T> {
impl<T:Copy + Num> Neg<Vec3<T>> for Vec3<T> { impl<T:Copy + Num> Neg<Vec3<T>> for Vec3<T> {
#[inline] #[inline]
pub fn neg(&self) -> Vec3<T> { pub fn neg(&self) -> Vec3<T> {
Vec3::new(-self.index(0), -self.index(1), -self.index(2)) self.map(|&x| -x)
} }
} }
@ -472,7 +449,7 @@ impl Vec3<bool> {
#[inline] #[inline]
pub fn not(&self) -> Vec3<bool> { pub fn not(&self) -> Vec3<bool> {
Vec3::new(!*self.index(0), !*self.index(1), !*self.index(2)) self.map(|&x| !x)
} }
} }

View file

@ -122,42 +122,27 @@ impl<T:Copy + Num> Vec4<T> {
#[inline] #[inline]
pub fn add_t(&self, value: T) -> Vec4<T> { pub fn add_t(&self, value: T) -> Vec4<T> {
Vec4::new(*self.index(0) + value, self.map(|&x| x + value)
*self.index(1) + value,
*self.index(2) + value,
*self.index(3) + value)
} }
#[inline] #[inline]
pub fn sub_t(&self, value: T) -> Vec4<T> { pub fn sub_t(&self, value: T) -> Vec4<T> {
Vec4::new(*self.index(0) - value, self.map(|&x| x - value)
*self.index(1) - value,
*self.index(2) - value,
*self.index(3) - value)
} }
#[inline] #[inline]
pub fn mul_t(&self, value: T) -> Vec4<T> { pub fn mul_t(&self, value: T) -> Vec4<T> {
Vec4::new(*self.index(0) * value, self.map(|&x| x * value)
*self.index(1) * value,
*self.index(2) * value,
*self.index(3) * value)
} }
#[inline] #[inline]
pub fn div_t(&self, value: T) -> Vec4<T> { pub fn div_t(&self, value: T) -> Vec4<T> {
Vec4::new(*self.index(0) / value, self.map(|&x| x / value)
*self.index(1) / value,
*self.index(2) / value,
*self.index(3) / value)
} }
#[inline] #[inline]
pub fn rem_t(&self, value: T) -> Vec4<T> { pub fn rem_t(&self, value: T) -> Vec4<T> {
Vec4::new(*self.index(0) % value, self.map(|&x| x % value)
*self.index(1) % value,
*self.index(2) % value,
*self.index(3) % value)
} }
#[inline] #[inline]
@ -202,50 +187,32 @@ impl<T:Copy + Num> Vec4<T> {
#[inline] #[inline]
pub fn neg_self(&mut self) { pub fn neg_self(&mut self) {
*self.index_mut(0) = -*self.index(0); self.map_mut(|x| *x = -*x)
*self.index_mut(1) = -*self.index(1);
*self.index_mut(2) = -*self.index(2);
*self.index_mut(3) = -*self.index(3);
} }
#[inline] #[inline]
pub fn add_self_t(&mut self, value: T) { pub fn add_self_t(&mut self, value: T) {
*self.index_mut(0) += value; self.map_mut(|x| *x += value)
*self.index_mut(1) += value;
*self.index_mut(2) += value;
*self.index_mut(3) += value;
} }
#[inline] #[inline]
pub fn sub_self_t(&mut self, value: T) { pub fn sub_self_t(&mut self, value: T) {
*self.index_mut(0) -= value; self.map_mut(|x| *x -= value)
*self.index_mut(1) -= value;
*self.index_mut(2) -= value;
*self.index_mut(3) -= value;
} }
#[inline] #[inline]
pub fn mul_self_t(&mut self, value: T) { pub fn mul_self_t(&mut self, value: T) {
*self.index_mut(0) *= value; self.map_mut(|x| *x *= value)
*self.index_mut(1) *= value;
*self.index_mut(2) *= value;
*self.index_mut(3) *= value;
} }
#[inline] #[inline]
pub fn div_self_t(&mut self, value: T) { pub fn div_self_t(&mut self, value: T) {
*self.index_mut(0) /= value; self.map_mut(|x| *x /= value)
*self.index_mut(1) /= value;
*self.index_mut(2) /= value;
*self.index_mut(3) /= value;
} }
#[inline] #[inline]
pub fn rem_self_t(&mut self, value: T) { pub fn rem_self_t(&mut self, value: T) {
*self.index_mut(0) %= value; self.map_mut(|x| *x %= value)
*self.index_mut(1) %= value;
*self.index_mut(2) %= value;
*self.index_mut(3) %= value;
} }
#[inline] #[inline]
@ -300,7 +267,7 @@ impl<T:Copy + Num> Vec4<T> {
impl<T:Copy + Num> Neg<Vec4<T>> for Vec4<T> { impl<T:Copy + Num> Neg<Vec4<T>> for Vec4<T> {
#[inline] #[inline]
pub fn neg(&self) -> Vec4<T> { pub fn neg(&self) -> Vec4<T> {
Vec4::new(-self.index(0), -self.index(1), -self.index(2), -self.index(3)) self.map(|&x| -x)
} }
} }
@ -495,9 +462,7 @@ impl Vec4<bool> {
} }
#[inline] #[inline]
pub fn not(&self) -> Vec4<bool> { pub fn not(&self) -> Vec4<bool> { self.map(|&x| !x) }
Vec4::new(!*self.index(0), !*self.index(1), !*self.index(2), !*self.index(3))
}
} }
#[cfg(test)] #[cfg(test)]