Add wrapper function for Array::build

This commit is contained in:
Brendan Zabarauskas 2013-09-14 11:58:19 +10:00
parent 68e03cd392
commit 1a94e8d06e
4 changed files with 36 additions and 31 deletions

View file

@ -94,6 +94,11 @@ macro_rules! array(
)
)
#[inline]
pub fn build<T: Clone, Slice, A: Array<T, Slice>>(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)]);

View file

@ -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<S: Clone + Float> Neg<Mat2<S>> for Mat2<S> { #[inline] fn neg(&self) -> Mat2<S> { Array::build(|i| self.i(i).neg()) } }
impl<S: Clone + Float> Neg<Mat3<S>> for Mat3<S> { #[inline] fn neg(&self) -> Mat3<S> { Array::build(|i| self.i(i).neg()) } }
impl<S: Clone + Float> Neg<Mat4<S>> for Mat4<S> { #[inline] fn neg(&self) -> Mat4<S> { Array::build(|i| self.i(i).neg()) } }
impl<S: Clone + Float> Neg<Mat2<S>> for Mat2<S> { #[inline] fn neg(&self) -> Mat2<S> { build(|i| self.i(i).neg()) } }
impl<S: Clone + Float> Neg<Mat3<S>> for Mat3<S> { #[inline] fn neg(&self) -> Mat3<S> { build(|i| self.i(i).neg()) } }
impl<S: Clone + Float> Neg<Mat4<S>> for Mat4<S> { #[inline] fn neg(&self) -> Mat4<S> { build(|i| self.i(i).neg()) } }
impl<S: Clone + Float>
Matrix<S, [Vec2<S>, ..2], Vec2<S>, [S, ..2]>

View file

@ -62,12 +62,12 @@ pub trait Point
>
: Array<S, Slice>
{
#[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) } }

View file

@ -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<Self>
+ 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<S: Clone + Num + Ord> One for Vec2<S> { #[inline] fn one() -> Vec2<S> { Vec
impl<S: Clone + Num + Ord> One for Vec3<S> { #[inline] fn one() -> Vec3<S> { Vec3::ident() } }
impl<S: Clone + Num + Ord> One for Vec4<S> { #[inline] fn one() -> Vec4<S> { Vec4::ident() } }
impl<S: Clone + Num + Ord> Neg<Vec2<S>> for Vec2<S> { #[inline] fn neg(&self) -> Vec2<S> { Array::build(|i| self.i(i).neg()) } }
impl<S: Clone + Num + Ord> Neg<Vec3<S>> for Vec3<S> { #[inline] fn neg(&self) -> Vec3<S> { Array::build(|i| self.i(i).neg()) } }
impl<S: Clone + Num + Ord> Neg<Vec4<S>> for Vec4<S> { #[inline] fn neg(&self) -> Vec4<S> { Array::build(|i| self.i(i).neg()) } }
impl<S: Clone + Num + Ord> Neg<Vec2<S>> for Vec2<S> { #[inline] fn neg(&self) -> Vec2<S> { build(|i| self.i(i).neg()) } }
impl<S: Clone + Num + Ord> Neg<Vec3<S>> for Vec3<S> { #[inline] fn neg(&self) -> Vec3<S> { build(|i| self.i(i).neg()) } }
impl<S: Clone + Num + Ord> Neg<Vec4<S>> for Vec4<S> { #[inline] fn neg(&self) -> Vec4<S> { build(|i| self.i(i).neg()) } }
macro_rules! vector(
(impl $Self:ident <$S:ident> $Slice:ty { $x:ident, $($xs:ident),+ }) => (