Use the `Zero` and `One` traits from the `num` crate.

Remove our own definitions of those traits.
Also fixes test-related attributes to make them compile again.
This commit is contained in:
Sébastien Crozet 2015-04-18 14:19:43 +02:00
parent 87ded7fa1e
commit 313ca1b8ae
17 changed files with 38 additions and 87 deletions

View File

@ -8,8 +8,8 @@ macro_rules! bench_binop(
let mut rng = IsaacRng::new_unseeded(); let mut rng = IsaacRng::new_unseeded();
let elems1: Vec<$t1> = (0us .. LEN).map(|_| rng.gen::<$t1>()).collect(); let elems1: Vec<$t1> = (0usize .. LEN).map(|_| rng.gen::<$t1>()).collect();
let elems2: Vec<$t2> = (0us .. LEN).map(|_| rng.gen::<$t2>()).collect(); let elems2: Vec<$t2> = (0usize .. LEN).map(|_| rng.gen::<$t2>()).collect();
let mut i = 0; let mut i = 0;
bh.iter(|| { bh.iter(|| {
@ -31,8 +31,8 @@ macro_rules! bench_binop_na(
let mut rng = IsaacRng::new_unseeded(); let mut rng = IsaacRng::new_unseeded();
let elems1: Vec<$t1> = (0us .. LEN).map(|_| rng.gen::<$t1>()).collect(); let elems1: Vec<$t1> = (0usize .. LEN).map(|_| rng.gen::<$t1>()).collect();
let elems2: Vec<$t2> = (0us .. LEN).map(|_| rng.gen::<$t2>()).collect(); let elems2: Vec<$t2> = (0usize .. LEN).map(|_| rng.gen::<$t2>()).collect();
let mut i = 0; let mut i = 0;
bh.iter(|| { bh.iter(|| {
@ -54,7 +54,7 @@ macro_rules! bench_unop(
let mut rng = IsaacRng::new_unseeded(); let mut rng = IsaacRng::new_unseeded();
let elems: Vec<$t> = (0us .. LEN).map(|_| rng.gen::<$t>()).collect(); let elems: Vec<$t> = (0usize .. LEN).map(|_| rng.gen::<$t>()).collect();
let mut i = 0; let mut i = 0;
bh.iter(|| { bh.iter(|| {
@ -76,7 +76,7 @@ macro_rules! bench_unop_self(
let mut rng = IsaacRng::new_unseeded(); let mut rng = IsaacRng::new_unseeded();
let mut elems: Vec<$t> = (0us .. LEN).map(|_| rng.gen::<$t>()).collect(); let mut elems: Vec<$t> = (0usize .. LEN).map(|_| rng.gen::<$t>()).collect();
let mut i = 0; let mut i = 0;
bh.iter(|| { bh.iter(|| {
@ -98,7 +98,7 @@ macro_rules! bench_construction(
let mut rng = IsaacRng::new_unseeded(); let mut rng = IsaacRng::new_unseeded();
$(let $args: Vec<$types> = (0us .. LEN).map(|_| rng.gen::<$types>()).collect();)* $(let $args: Vec<$types> = (0usize .. LEN).map(|_| rng.gen::<$types>()).collect();)*
let mut i = 0; let mut i = 0;
bh.iter(|| { bh.iter(|| {

View File

@ -13,7 +13,7 @@ macro_rules! bench_mul_dmat(
let a: DMat<f64> = DMat::new_random($nrows, $ncols); let a: DMat<f64> = DMat::new_random($nrows, $ncols);
let mut b: DMat<f64> = DMat::new_random($nrows, $ncols); let mut b: DMat<f64> = DMat::new_random($nrows, $ncols);
for _ in (0us .. 1000) { for _ in (0usize .. 1000) {
// XXX: the clone here is highly undesirable! // XXX: the clone here is highly undesirable!
b = a.clone() * b; b = a.clone() * b;
} }
@ -55,7 +55,7 @@ macro_rules! bench_mul_dmat_dvec(
let m : DMat<f64> = DMat::new_random($nrows, $ncols); let m : DMat<f64> = DMat::new_random($nrows, $ncols);
let mut v : DVec<f64> = DVec::new_random($ncols); let mut v : DVec<f64> = DVec::new_random($ncols);
for _ in (0us .. 1000) { for _ in (0usize .. 1000) {
// XXX: the clone here is highly undesirable! // XXX: the clone here is highly undesirable!
v = m.clone() * v v = m.clone() * v
} }

View File

@ -82,7 +82,6 @@ Feel free to add your project to this list if you happen to use **nalgebra**!
#![deny(unused_qualifications)] #![deny(unused_qualifications)]
#![deny(unused_results)] #![deny(unused_results)]
#![warn(missing_docs)] #![warn(missing_docs)]
#![cfg_attr(test, feature(test))]
#![doc(html_root_url = "http://nalgebra.org/doc")] #![doc(html_root_url = "http://nalgebra.org/doc")]
extern crate rustc_serialize; extern crate rustc_serialize;
@ -92,11 +91,9 @@ extern crate num;
#[cfg(feature="arbitrary")] #[cfg(feature="arbitrary")]
extern crate quickcheck; extern crate quickcheck;
#[cfg(test)]
extern crate test;
use std::cmp; use std::cmp;
use std::ops::{Neg, Mul}; use std::ops::{Neg, Mul};
use num::{Zero, One};
pub use traits::{ pub use traits::{
Absolute, Absolute,
AbsoluteRotate, AbsoluteRotate,
@ -131,7 +128,6 @@ pub use traits::{
Norm, Norm,
NumPnt, NumPnt,
NumVec, NumVec,
One,
Orig, Orig,
Outer, Outer,
POrd, POrd,
@ -150,7 +146,6 @@ pub use traits::{
Transpose, Transpose,
UniformSphereSample, UniformSphereSample,
VecAsPnt, VecAsPnt,
Zero
}; };
pub use structs::{ pub use structs::{

View File

@ -7,9 +7,10 @@ use std::iter::repeat;
use std::ops::{Add, Sub, Mul, Div, Index, IndexMut}; use std::ops::{Add, Sub, Mul, Div, Index, IndexMut};
use std::fmt::{Debug, Display, Formatter, Result}; use std::fmt::{Debug, Display, Formatter, Result};
use rand::{self, Rand}; use rand::{self, Rand};
use num::{Zero, One};
use structs::dvec::DVec; use structs::dvec::DVec;
use traits::operations::{ApproxEq, Inv, Transpose, Mean, Cov}; use traits::operations::{ApproxEq, Inv, Transpose, Mean, Cov};
use traits::structure::{Cast, ColSlice, RowSlice, Diag, Eye, Indexable, Shape, Zero, One, BaseNum}; use traits::structure::{Cast, ColSlice, RowSlice, Diag, Eye, Indexable, Shape, BaseNum};
#[cfg(feature="arbitrary")] #[cfg(feature="arbitrary")]
use quickcheck::{Arbitrary, Gen}; use quickcheck::{Arbitrary, Gen};

View File

@ -7,9 +7,10 @@ use std::iter::{FromIterator, IntoIterator};
use std::iter::repeat; use std::iter::repeat;
use std::ops::{Add, Sub, Mul, Div, Neg, Index, IndexMut}; use std::ops::{Add, Sub, Mul, Div, Neg, Index, IndexMut};
use rand::{self, Rand}; use rand::{self, Rand};
use num::{Zero, One};
use traits::operations::{ApproxEq, Axpy}; use traits::operations::{ApproxEq, Axpy};
use traits::geometry::{Dot, Norm}; use traits::geometry::{Dot, Norm};
use traits::structure::{Iterable, IterableMut, Indexable, Shape, BaseFloat, BaseNum, Zero, One}; use traits::structure::{Iterable, IterableMut, Indexable, Shape, BaseFloat, BaseNum};
#[cfg(feature="arbitrary")] #[cfg(feature="arbitrary")]
use quickcheck::{Arbitrary, Gen}; use quickcheck::{Arbitrary, Gen};

View File

@ -5,8 +5,9 @@
use std::ops::{Add, Sub, Mul, Neg}; use std::ops::{Add, Sub, Mul, Neg};
use rand::{Rand, Rng}; use rand::{Rand, Rng};
use num::One;
use structs::mat::{Mat3, Mat4, Mat5}; use structs::mat::{Mat3, Mat4, Mat5};
use traits::structure::{Cast, Dim, Col, BaseFloat, BaseNum, One}; use traits::structure::{Cast, Dim, Col, BaseFloat, BaseNum};
use traits::operations::{Inv, ApproxEq}; use traits::operations::{Inv, ApproxEq};
use traits::geometry::{RotationMatrix, Rotation, Rotate, AbsoluteRotate, Transform, Transformation, use traits::geometry::{RotationMatrix, Rotation, Rotate, AbsoluteRotate, Transform, Transformation,
Translate, Translation, ToHomogeneous}; Translate, Translation, ToHomogeneous};

View File

@ -6,13 +6,14 @@ use std::ops::{Add, Sub, Mul, Div, Index, IndexMut};
use std::mem; use std::mem;
use std::slice::{Iter, IterMut}; use std::slice::{Iter, IterMut};
use rand::{Rand, Rng}; use rand::{Rand, Rng};
use num::{Zero, One};
use traits::operations::ApproxEq; use traits::operations::ApproxEq;
use structs::vec::{Vec1, Vec2, Vec3, Vec4, Vec5, Vec6}; use structs::vec::{Vec1, Vec2, Vec3, Vec4, Vec5, Vec6};
use structs::pnt::{Pnt1, Pnt4, Pnt5, Pnt6}; use structs::pnt::{Pnt1, Pnt4, Pnt5, Pnt6};
use structs::dvec::{DVec1, DVec2, DVec3, DVec4, DVec5, DVec6}; use structs::dvec::{DVec1, DVec2, DVec3, DVec4, DVec5, DVec6};
use traits::structure::{Cast, Row, Col, Iterable, IterableMut, Dim, Indexable, use traits::structure::{Cast, Row, Col, Iterable, IterableMut, Dim, Indexable,
Eye, ColSlice, RowSlice, Diag, Shape, BaseFloat, BaseNum, Zero, One}; Eye, ColSlice, RowSlice, Diag, Shape, BaseFloat, BaseNum};
use traits::operations::{Absolute, Transpose, Inv, Outer, EigenQR}; use traits::operations::{Absolute, Transpose, Inv, Outer, EigenQR};
use traits::geometry::{ToHomogeneous, FromHomogeneous, Orig}; use traits::geometry::{ToHomogeneous, FromHomogeneous, Orig};
use linalg; use linalg;

View File

@ -8,10 +8,11 @@ use std::slice::{Iter, IterMut};
use std::iter::{Iterator, FromIterator, IntoIterator}; use std::iter::{Iterator, FromIterator, IntoIterator};
use std::ops::{Add, Sub, Mul, Div, Neg, Index, IndexMut}; use std::ops::{Add, Sub, Mul, Div, Neg, Index, IndexMut};
use rand::{Rand, Rng}; use rand::{Rand, Rng};
use num::{Zero, One};
use traits::operations::{ApproxEq, POrd, POrdering, Axpy, ScalarAdd, ScalarSub, ScalarMul, use traits::operations::{ApproxEq, POrd, POrdering, Axpy, ScalarAdd, ScalarSub, ScalarMul,
ScalarDiv}; ScalarDiv};
use traits::structure::{Cast, Dim, Indexable, Iterable, IterableMut, PntAsVec, Shape, use traits::structure::{Cast, Dim, Indexable, Iterable, IterableMut, PntAsVec, Shape,
NumPnt, FloatPnt, BaseFloat, BaseNum, Zero, One, Bounded}; NumPnt, FloatPnt, BaseFloat, BaseNum, Bounded};
use traits::geometry::{Orig, FromHomogeneous, ToHomogeneous}; use traits::geometry::{Orig, FromHomogeneous, ToHomogeneous};
use structs::vec::{Vec1, Vec2, Vec3, Vec4, Vec5, Vec6}; use structs::vec::{Vec1, Vec2, Vec3, Vec4, Vec5, Vec6};
#[cfg(feature="arbitrary")] #[cfg(feature="arbitrary")]

View File

@ -7,11 +7,12 @@ use std::slice::{Iter, IterMut};
use std::ops::{Add, Sub, Mul, Div, Neg, Index, IndexMut}; use std::ops::{Add, Sub, Mul, Div, Neg, Index, IndexMut};
use std::iter::{FromIterator, IntoIterator}; use std::iter::{FromIterator, IntoIterator};
use rand::{Rand, Rng}; use rand::{Rand, Rng};
use num::{Zero, One};
use structs::{Vec3, Pnt3, Rot3, Mat3}; use structs::{Vec3, Pnt3, Rot3, Mat3};
use traits::operations::{ApproxEq, Inv, POrd, POrdering, Axpy, ScalarAdd, ScalarSub, ScalarMul, use traits::operations::{ApproxEq, Inv, POrd, POrdering, Axpy, ScalarAdd, ScalarSub, ScalarMul,
ScalarDiv}; ScalarDiv};
use traits::structure::{Cast, Indexable, Iterable, IterableMut, Dim, Shape, BaseFloat, BaseNum, Zero, use traits::structure::{Cast, Indexable, Iterable, IterableMut, Dim, Shape, BaseFloat, BaseNum,
One, Bounded}; Bounded};
use traits::geometry::{Norm, Rotation, Rotate, Transform}; use traits::geometry::{Norm, Rotation, Rotate, Transform};
#[cfg(feature="arbitrary")] #[cfg(feature="arbitrary")]

View File

@ -4,9 +4,10 @@
use std::ops::{Mul, Neg, Index}; use std::ops::{Mul, Neg, Index};
use rand::{Rand, Rng}; use rand::{Rand, Rng};
use num::{Zero, One};
use traits::geometry::{Rotate, Rotation, AbsoluteRotate, RotationMatrix, Transform, ToHomogeneous, use traits::geometry::{Rotate, Rotation, AbsoluteRotate, RotationMatrix, Transform, ToHomogeneous,
Norm, Cross}; Norm, Cross};
use traits::structure::{Cast, Dim, Row, Col, BaseFloat, BaseNum, Zero, One}; use traits::structure::{Cast, Dim, Row, Col, BaseFloat, BaseNum, };
use traits::operations::{Absolute, Inv, Transpose, ApproxEq}; use traits::operations::{Absolute, Inv, Transpose, ApproxEq};
use structs::vec::{Vec1, Vec2, Vec3, Vec4}; use structs::vec::{Vec1, Vec2, Vec3, Vec4};
use structs::pnt::{Pnt2, Pnt3, Pnt4}; use structs::pnt::{Pnt2, Pnt3, Pnt4};

View File

@ -1,7 +1,7 @@
use std::ops::Mul; use std::ops::Mul;
use num::{Zero, One};
use structs::mat; use structs::mat;
use traits::operations::{Inv, Transpose}; use traits::operations::{Inv, Transpose};
use traits::structure::{Zero, One};
use traits::geometry::{Translation, Translate, Rotation, Rotate, Transformation, Transform, use traits::geometry::{Translation, Translate, Rotation, Rotate, Transformation, Transform,
AbsoluteRotate}; AbsoluteRotate};

View File

@ -1,5 +1,6 @@
use std::ops::{Sub, Mul, Neg}; use std::ops::{Sub, Mul, Neg};
use traits::structure::{Cast, Row, Basis, BaseFloat, Zero, One}; use num::{Zero, One};
use traits::structure::{Cast, Row, Basis, BaseFloat};
use traits::geometry::{Norm, Cross, CrossMatrix, UniformSphereSample}; use traits::geometry::{Norm, Cross, CrossMatrix, UniformSphereSample};
use structs::vec::{Vec1, Vec2, Vec3, Vec4}; use structs::vec::{Vec1, Vec2, Vec3, Vec4};
use structs::mat::Mat3; use structs::mat::Mat3;
@ -228,7 +229,8 @@ static SAMPLES_3_F64: [Vec3<f64>; 42] = [
Vec3 { x: 0.162456 , y: 0.499995 , z: 0.850654 } Vec3 { x: 0.162456 , y: 0.499995 , z: 0.850654 }
]; ];
impl<N: One + Copy> UniformSphereSample for Vec1<N> { impl<N> UniformSphereSample for Vec1<N>
where Vec1<N>: One {
#[inline(always)] #[inline(always)]
fn sample<F: FnMut(Vec1<N>)>(mut f: F) { fn sample<F: FnMut(Vec1<N>)>(mut f: F) {
f(::one()) f(::one())

View File

@ -4,9 +4,9 @@ use std::mem;
use std::slice::{Iter, IterMut}; use std::slice::{Iter, IterMut};
use std::iter::{FromIterator, IntoIterator}; use std::iter::{FromIterator, IntoIterator};
use rand::{Rand, Rng}; use rand::{Rand, Rng};
use num::{Zero, One};
use traits::operations::ApproxEq; use traits::operations::ApproxEq;
use traits::structure::{Iterable, IterableMut, Indexable, Basis, Dim, Shape, BaseFloat, BaseNum, use traits::structure::{Iterable, IterableMut, Indexable, Basis, Dim, Shape, BaseFloat, BaseNum, Bounded};
Zero, One, Bounded};
use traits::geometry::{Translation, Dot, Norm}; use traits::geometry::{Translation, Dot, Norm};
use structs::vec; use structs::vec;

View File

@ -8,12 +8,13 @@ use std::mem;
use std::slice::{Iter, IterMut}; use std::slice::{Iter, IterMut};
use std::iter::{Iterator, FromIterator, IntoIterator}; use std::iter::{Iterator, FromIterator, IntoIterator};
use rand::{Rand, Rng}; use rand::{Rand, Rng};
use num::{Zero, One};
use traits::operations::{ApproxEq, POrd, POrdering, Axpy, ScalarAdd, ScalarSub, ScalarMul, use traits::operations::{ApproxEq, POrd, POrdering, Axpy, ScalarAdd, ScalarSub, ScalarMul,
ScalarDiv, Absolute}; ScalarDiv, Absolute};
use traits::geometry::{Transform, Rotate, FromHomogeneous, ToHomogeneous, Dot, Norm, use traits::geometry::{Transform, Rotate, FromHomogeneous, ToHomogeneous, Dot, Norm,
Translation, Translate}; Translation, Translate};
use traits::structure::{Basis, Cast, Dim, Indexable, Iterable, IterableMut, VecAsPnt, Shape, use traits::structure::{Basis, Cast, Dim, Indexable, Iterable, IterableMut, VecAsPnt, Shape,
NumVec, FloatVec, BaseFloat, BaseNum, Zero, One, Bounded}; NumVec, FloatVec, BaseFloat, BaseNum, Bounded};
use structs::pnt::{Pnt1, Pnt2, Pnt3, Pnt4, Pnt5, Pnt6}; use structs::pnt::{Pnt1, Pnt2, Pnt3, Pnt4, Pnt5, Pnt6};
#[cfg(feature="arbitrary")] #[cfg(feature="arbitrary")]

View File

@ -651,7 +651,8 @@ macro_rules! approx_eq_impl(
macro_rules! zero_one_impl( macro_rules! zero_one_impl(
($t: ident, $($compN: ident),+) => ( ($t: ident, $($compN: ident),+) => (
impl<N: One> One for $t<N> { impl<N> One for $t<N>
where N: Copy + One + Sub<N, Output = N> + Add<N, Output = N> {
#[inline] #[inline]
fn one() -> $t<N> { fn one() -> $t<N> {
$t { $t {

View File

@ -6,8 +6,7 @@ pub use traits::geometry::{AbsoluteRotate, Cross, CrossMatrix, Dot, FromHomogene
pub use traits::structure::{FloatVec, FloatPnt, Basis, Cast, Col, Dim, Indexable, Iterable, pub use traits::structure::{FloatVec, FloatPnt, Basis, Cast, Col, Dim, Indexable, Iterable,
IterableMut, Mat, SquareMat, Row, NumVec, NumPnt, PntAsVec, VecAsPnt, IterableMut, Mat, SquareMat, Row, NumVec, NumPnt, PntAsVec, VecAsPnt,
ColSlice, RowSlice, Diag, Eye, Shape, BaseFloat, BaseNum, Zero, One, ColSlice, RowSlice, Diag, Eye, Shape, BaseFloat, BaseNum, Bounded};
Bounded};
pub use traits::operations::{Absolute, ApproxEq, Axpy, Cov, Det, Inv, LMul, Mean, Outer, POrd, pub use traits::operations::{Absolute, ApproxEq, Axpy, Cov, Det, Inv, LMul, Mean, Outer, POrd,
RMul, ScalarAdd, ScalarSub, ScalarMul, ScalarDiv, Transpose, EigenQR}; RMul, ScalarAdd, ScalarSub, ScalarMul, ScalarDiv, Transpose, EigenQR};

View File

@ -3,7 +3,7 @@
use std::{f32, f64, i8, i16, i32, i64, u8, u16, u32, u64, isize, usize}; use std::{f32, f64, i8, i16, i32, i64, u8, u16, u32, u64, isize, usize};
use std::slice::{Iter, IterMut}; use std::slice::{Iter, IterMut};
use std::ops::{Add, Sub, Mul, Div, Rem, Index, IndexMut}; use std::ops::{Add, Sub, Mul, Div, Rem, Index, IndexMut};
use num::Float; use num::{Float, Zero, One};
use traits::operations::{RMul, LMul, Axpy, Transpose, Inv, Absolute}; use traits::operations::{RMul, LMul, Axpy, Transpose, Inv, Absolute};
use traits::geometry::{Dot, Norm, Orig}; use traits::geometry::{Dot, Norm, Orig};
@ -80,20 +80,6 @@ pub trait Eye {
fn new_identity(dim: usize) -> Self; fn new_identity(dim: usize) -> Self;
} }
/// Additive identity.
pub trait Zero {
/// Returns the additive identity.
fn zero() -> Self;
/// Tests if `self` is exactly zero.
fn is_zero(&self) -> bool;
}
/// Multiplicative identity.
pub trait One {
/// Returns the multiplicative identity.
fn one() -> Self;
}
/// Types that have maximum and minimum value. /// Types that have maximum and minimum value.
pub trait Bounded { pub trait Bounded {
/// The minimum value. /// The minimum value.
@ -303,46 +289,6 @@ pub trait FloatPnt<N: BaseFloat, V: Norm<N>>: NumPnt<N, V> + Sized {
* *
* *
*/ */
// Zero and One
macro_rules! impl_zero_one(
($n: ty, $zero: expr, $one: expr) => {
impl Zero for $n {
#[inline]
fn zero() -> $n {
$zero
}
#[inline]
fn is_zero(&self) -> bool {
*self == $zero
}
}
impl One for $n {
fn one() -> $n {
$one
}
}
}
);
impl_zero_one!(f32, 0.0, 1.0);
impl_zero_one!(f64, 0.0, 1.0);
impl_zero_one!(i8, 0, 1);
impl_zero_one!(i16, 0, 1);
impl_zero_one!(i32, 0, 1);
impl_zero_one!(i64, 0, 1);
impl_zero_one!(isize, 0, 1);
impl_zero_one!(u8, 0, 1);
impl_zero_one!(u16, 0, 1);
impl_zero_one!(u32, 0, 1);
impl_zero_one!(u64, 0, 1);
impl_zero_one!(usize, 0, 1);
// Bounded // Bounded
macro_rules! impl_bounded( macro_rules! impl_bounded(
($n: ty, $min: expr, $max: expr) => { ($n: ty, $min: expr, $max: expr) => {