Add implementation of Bounded.

This commit is contained in:
Sébastien Crozet 2013-06-15 22:11:33 +00:00
parent caee7eb423
commit 854dda73bf
4 changed files with 48 additions and 5 deletions

View File

@ -1,4 +1,4 @@
use std::num::{Zero, One, Algebraic}; use std::num::{Zero, One, Algebraic, Bounded};
use std::rand::{Rand, Rng, RngUtil}; use std::rand::{Rand, Rng, RngUtil};
use std::cmp::ApproxEq; use std::cmp::ApproxEq;
use traits::basis::Basis; use traits::basis::Basis;
@ -14,7 +14,6 @@ use traits::scalar_op::{ScalarMul, ScalarDiv, ScalarAdd, ScalarSub};
pub struct Vec1<N> pub struct Vec1<N>
{ x : N } { x : N }
impl<N: Copy> Vec1<N> impl<N: Copy> Vec1<N>
{ {
#[inline(always)] #[inline(always)]
@ -219,3 +218,14 @@ impl<N: Copy> Flatten<N> for Vec1<N>
fn flatten_to(&self, l: &mut [N], off: uint) fn flatten_to(&self, l: &mut [N], off: uint)
{ l[off] = self.x } { l[off] = self.x }
} }
impl<N: Bounded + Copy> Bounded for Vec1<N>
{
#[inline(always)]
fn max_value() -> Vec1<N>
{ Vec1::new(Bounded::max_value()) }
#[inline(always)]
fn min_value() -> Vec1<N>
{ Vec1::new(Bounded::min_value()) }
}

View File

@ -1,4 +1,4 @@
use std::num::{Zero, One, Algebraic}; use std::num::{Zero, One, Algebraic, Bounded};
use std::rand::{Rand, Rng, RngUtil}; use std::rand::{Rand, Rng, RngUtil};
use dim1::vec1::Vec1; use dim1::vec1::Vec1;
use std::cmp::ApproxEq; use std::cmp::ApproxEq;
@ -257,3 +257,14 @@ impl<N: Copy> Flatten<N> for Vec2<N>
l[off + 1] = self.y; l[off + 1] = self.y;
} }
} }
impl<N: Bounded + Copy> Bounded for Vec2<N>
{
#[inline(always)]
fn max_value() -> Vec2<N>
{ Vec2::new(Bounded::max_value(), Bounded::max_value()) }
#[inline(always)]
fn min_value() -> Vec2<N>
{ Vec2::new(Bounded::min_value(), Bounded::min_value()) }
}

View File

@ -1,4 +1,4 @@
use std::num::{Zero, One, Algebraic, abs}; use std::num::{Zero, One, Algebraic, abs, Bounded};
use std::rand::{Rand, Rng, RngUtil}; use std::rand::{Rand, Rng, RngUtil};
use std::cmp::ApproxEq; use std::cmp::ApproxEq;
use traits::basis::Basis; use traits::basis::Basis;
@ -287,3 +287,14 @@ impl<N: Copy> Flatten<N> for Vec3<N>
l[off + 2] = self.z; l[off + 2] = self.z;
} }
} }
impl<N: Bounded + Copy> Bounded for Vec3<N>
{
#[inline(always)]
fn max_value() -> Vec3<N>
{ Vec3::new(Bounded::max_value(), Bounded::max_value(), Bounded::max_value()) }
#[inline(always)]
fn min_value() -> Vec3<N>
{ Vec3::new(Bounded::min_value(), Bounded::min_value(), Bounded::min_value()) }
}

View File

@ -1,5 +1,5 @@
use std::uint::iterate; use std::uint::iterate;
use std::num::{Zero, Algebraic}; use std::num::{Zero, Algebraic, Bounded};
use std::rand::{Rand, Rng, RngUtil}; use std::rand::{Rand, Rng, RngUtil};
use std::vec::{map}; use std::vec::{map};
use std::cmp::ApproxEq; use std::cmp::ApproxEq;
@ -265,3 +265,14 @@ impl<D: Dim, N: Zero + Copy> Flatten<N> for NVec<D, N>
{ l[off + i] = self.at.at[i] } { l[off + i] = self.at.at[i] }
} }
} }
impl<D: Dim, N: Bounded + Zero + Add<N, N> + Copy> Bounded for NVec<D, N>
{
#[inline(always)]
fn max_value() -> NVec<D, N>
{ Zero::zero::<NVec<D, N>>().scalar_add(&Bounded::max_value()) }
#[inline(always)]
fn min_value() -> NVec<D, N>
{ Zero::zero::<NVec<D, N>>().scalar_add(&Bounded::min_value()) }
}