From 1a94e8d06e07070ea408d8fa94b7dbb4f2089c4e Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Sat, 14 Sep 2013 11:58:19 +1000 Subject: [PATCH] Add wrapper function for Array::build --- src/cgmath/array.rs | 5 +++++ src/cgmath/matrix.rs | 24 ++++++++++++------------ src/cgmath/point.rs | 10 +++++----- src/cgmath/vector.rs | 28 ++++++++++++++-------------- 4 files changed, 36 insertions(+), 31 deletions(-) diff --git a/src/cgmath/array.rs b/src/cgmath/array.rs index a3517ee..25b2223 100644 --- a/src/cgmath/array.rs +++ b/src/cgmath/array.rs @@ -94,6 +94,11 @@ macro_rules! array( ) ) +#[inline] +pub fn build>(builder: &fn(i: uint) -> T) -> A { + Array::build(builder) +} + macro_rules! gen_builder( (_2) => ([builder(0), builder(1)]); (_3) => ([builder(0), builder(1), builder(2)]); diff --git a/src/cgmath/matrix.rs b/src/cgmath/matrix.rs index 73d3e0f..d26193e 100644 --- a/src/cgmath/matrix.rs +++ b/src/cgmath/matrix.rs @@ -18,7 +18,7 @@ use std::num::{Zero, zero, One, one, cast, sqrt}; use angle::{Rad, sin, cos}; -use array::Array; +use array::{Array, build}; use quaternion::{Quat, ToQuat}; use vector::*; @@ -191,7 +191,7 @@ pub trait Matrix #[inline] fn r(&self, r: uint) -> V { - Array::build(|i| self.i(i).i(r).clone()) + build(|i| self.i(i).i(r).clone()) } #[inline] @@ -224,18 +224,18 @@ 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 { 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_s(&self, s: S) -> Self { build(|i| self.i(i).mul_s(s.clone())) } + #[inline] fn div_s(&self, s: S) -> Self { build(|i| self.i(i).div_s(s.clone())) } + #[inline] fn rem_s(&self, s: S) -> Self { 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()) } } #[inline] fn rem_self_s(&mut self, s: S) { for c in self.mut_iter() { *c = c.rem_s(s.clone()) } } - #[inline] fn mul_v(&self, v: &V) -> V { Array::build(|i| self.r(i).dot(v)) } + #[inline] fn mul_v(&self, v: &V) -> V { build(|i| self.r(i).dot(v)) } - #[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_m(&self, other: &Self) -> Self { build(|i| self.i(i).add_v(other.i(i))) } + #[inline] fn sub_m(&self, other: &Self) -> Self { 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) } } @@ -247,7 +247,7 @@ pub trait Matrix fn determinant(&self) -> S; #[inline] - fn diagonal(&self) -> V { Array::build(|i| self.cr(i, i).clone()) } + fn diagonal(&self) -> V { build(|i| self.cr(i, i).clone()) } #[inline] fn trace(&self) -> S { self.diagonal().comp_add() } @@ -278,9 +278,9 @@ pub trait Matrix fn is_symmetric(&self) -> bool; } -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 Neg> for Mat2 { #[inline] fn neg(&self) -> Mat2 { build(|i| self.i(i).neg()) } } +impl Neg> for Mat3 { #[inline] fn neg(&self) -> Mat3 { build(|i| self.i(i).neg()) } } +impl Neg> for Mat4 { #[inline] fn neg(&self) -> Mat4 { 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 d225388..650f7f0 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 { 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 mul_s(&self, s: S) -> Self { build(|i| self.i(i).mul(&s)) } + #[inline] fn div_s(&self, s: S) -> Self { build(|i| self.i(i).div(&s)) } + #[inline] fn rem_s(&self, s: S) -> Self { build(|i| self.i(i).rem(&s)) } - #[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 add_v(&self, other: &V) -> Self { build(|i| self.i(i).add(other.i(i))) } + #[inline] fn sub_p(&self, other: &Self) -> V { 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 9e18257..53f2c84 100644 --- a/src/cgmath/vector.rs +++ b/src/cgmath/vector.rs @@ -16,7 +16,7 @@ use std::num::{Zero, zero, One, one, sqrt}; use angle::{Rad, atan2, acos}; -use array::Array; +use array::{Array, build}; /// A 2-dimensional vector. #[deriving(Eq, Clone, Zero)] @@ -103,17 +103,17 @@ pub trait Vector + Neg + Zero + One { - #[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_s(&self, s: S) -> Self { build(|i| self.i(i).add(&s)) } + #[inline] fn sub_s(&self, s: S) -> Self { build(|i| self.i(i).sub(&s)) } + #[inline] fn mul_s(&self, s: S) -> Self { build(|i| self.i(i).mul(&s)) } + #[inline] fn div_s(&self, s: S) -> Self { build(|i| self.i(i).div(&s)) } + #[inline] fn rem_s(&self, s: S) -> Self { build(|i| self.i(i).rem(&s)) } - #[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 add_v(&self, other: &Self) -> Self { build(|i| self.i(i).add(other.i(i))) } + #[inline] fn sub_v(&self, other: &Self) -> Self { build(|i| self.i(i).sub(other.i(i))) } + #[inline] fn mul_v(&self, other: &Self) -> Self { build(|i| self.i(i).mul(other.i(i))) } + #[inline] fn div_v(&self, other: &Self) -> Self { build(|i| self.i(i).div(other.i(i))) } + #[inline] fn rem_v(&self, other: &Self) -> Self { build(|i| self.i(i).rem(other.i(i))) } #[inline] fn neg_self(&mut self); @@ -151,9 +151,9 @@ impl One for Vec2 { #[inline] fn one() -> Vec2 { Vec 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 { 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()) } } +impl Neg> for Vec2 { #[inline] fn neg(&self) -> Vec2 { build(|i| self.i(i).neg()) } } +impl Neg> for Vec3 { #[inline] fn neg(&self) -> Vec3 { build(|i| self.i(i).neg()) } } +impl Neg> for Vec4 { #[inline] fn neg(&self) -> Vec4 { build(|i| self.i(i).neg()) } } macro_rules! vector( (impl $Self:ident <$S:ident> $Slice:ty { $x:ident, $($xs:ident),+ }) => (