From 606ad947c9c3d0f051adf3b38b70543750d6a967 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Crozet?= Date: Sat, 15 Mar 2014 12:23:54 +0100 Subject: [PATCH] Use Vec instead of ~[]. Version of rustc: 0.10-pre (fc7a112 2014-03-14 23:11:31 -0700) --- src/lib.rs | 1 - src/na.rs | 4 ++-- src/structs/dmat.rs | 42 +++++++++++++++++------------------ src/structs/dvec.rs | 46 ++++++++++++++++++--------------------- src/structs/vec.rs | 2 +- src/structs/vec_macros.rs | 14 +++++++----- src/tests/vec.rs | 12 +++++----- src/traits/geometry.rs | 4 ++-- src/traits/operations.rs | 4 ++-- 9 files changed, 62 insertions(+), 67 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 7976270f..35f60bb5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -110,7 +110,6 @@ Feel free to add your project to this list if you happen to use **nalgebra**! #[doc(html_root_url = "http://www.rust-ci.org/sebcrozet/nalgebra/doc")]; extern crate std; -extern crate extra; extern crate rand; extern crate serialize; diff --git a/src/na.rs b/src/na.rs index 311a3938..984ca22b 100644 --- a/src/na.rs +++ b/src/na.rs @@ -70,13 +70,13 @@ pub fn clamp(val: T, min: T, max: T) -> T { /// Same as `cmp::max`. #[inline(always)] -pub fn max(a: T, b: T) -> T { +pub fn max(a: T, b: T) -> T { cmp::max(a, b) } /// Same as `cmp::min`. #[inline(always)] -pub fn min(a: T, b: T) -> T { +pub fn min(a: T, b: T) -> T { cmp::min(a, b) } diff --git a/src/structs/dmat.rs b/src/structs/dmat.rs index 5386d6c1..05e1d22e 100644 --- a/src/structs/dmat.rs +++ b/src/structs/dmat.rs @@ -5,7 +5,7 @@ use rand::Rand; use rand; use std::num::{One, Zero}; -use std::vec; +use std::vec_ng::Vec; use traits::operations::ApproxEq; use std::mem; use structs::dvec::{DVec, DVecMulRhs}; @@ -20,7 +20,7 @@ mod metal; pub struct DMat { priv nrows: uint, priv ncols: uint, - priv mij: ~[N] + priv mij: Vec } double_dispatch_binop_decl_trait!(DMat, DMatMulRhs) @@ -37,7 +37,7 @@ impl DMat { /// Creates an uninitialized matrix. #[inline] pub unsafe fn new_uninitialized(nrows: uint, ncols: uint) -> DMat { - let mut vec = vec::with_capacity(nrows * ncols); + let mut vec = Vec::with_capacity(nrows * ncols); vec.set_len(nrows * ncols); DMat { @@ -96,7 +96,7 @@ impl DMat { DMat { nrows: nrows, ncols: ncols, - mij: vec::from_elem(nrows * ncols, val) + mij: Vec::from_elem(nrows * ncols, val) } } @@ -129,7 +129,7 @@ impl DMat { DMat { nrows: nrows, ncols: ncols, - mij: vec.to_owned() + mij: Vec::from_slice(vec) } } } @@ -141,7 +141,7 @@ impl DMat { DMat { nrows: nrows, ncols: ncols, - mij: vec::from_fn(nrows * ncols, |i| { let m = i % ncols; f(m, m - i * ncols) }) + mij: Vec::from_fn(nrows * ncols, |i| { let m = i % ncols; f(m, m - i * ncols) }) } } @@ -160,7 +160,7 @@ impl DMat { /// Transforms this matrix into an array. This consumes the matrix and is O(1). /// The returned vector contains the matrix data in column-major order. #[inline] - pub fn to_vec(self) -> ~[N] { + pub fn to_vec(self) -> Vec { self.mij } @@ -168,18 +168,14 @@ impl DMat { /// The returned vector contains the matrix data in column-major order. #[inline] pub fn as_vec<'r>(&'r self) -> &'r [N] { - let res: &'r [N] = self.mij; - - res + self.mij.as_slice() } /// Gets a mutable reference to this matrix data. /// The returned vector contains the matrix data in column-major order. #[inline] pub fn as_mut_vec<'r>(&'r mut self) -> &'r mut [N] { - let res: &'r mut [N] = self.mij; - - res + self.mij.as_mut_slice() } } @@ -219,14 +215,16 @@ impl DMat { pub fn set(&mut self, row: uint, col: uint, val: N) { assert!(row < self.nrows); assert!(col < self.ncols); - self.mij[self.offset(row, col)] = val + + let offset = self.offset(row, col); + *self.mij.get_mut(offset) = val } /// Just like `set` without bounds checking. #[inline] pub unsafe fn set_fast(&mut self, row: uint, col: uint, val: N) { - let off = self.offset(row, col); - *self.mij.unsafe_mut_ref(off) = val + let offset = self.offset(row, col); + *self.mij.as_mut_slice().unsafe_mut_ref(offset) = val } /// Reads the value of a component of the matrix. @@ -244,7 +242,7 @@ impl DMat { /// Just like `at` without bounds checking. #[inline] pub unsafe fn at_fast(&self, row: uint, col: uint) -> N { - (*self.mij.unsafe_ref(self.offset(row, col))).clone() + (*self.mij.as_slice().unsafe_ref(self.offset(row, col))).clone() } } @@ -288,7 +286,7 @@ DMatMulRhs> for DVec { } } - res.at[i] = acc; + *res.at.get_mut(i) = acc; } res @@ -312,7 +310,7 @@ DVecMulRhs> for DMat { } } - res.at[i] = acc; + *res.at.get_mut(i) = acc; } res @@ -366,8 +364,8 @@ Inv for DMat { let off_n0_j = self.offset(n0, j); let off_k_j = self.offset(k, j); - self.mij.swap(off_n0_j, off_k_j); - res.mij.swap(off_n0_j, off_k_j); + self.mij.as_mut_slice().swap(off_n0_j, off_k_j); + res.mij.as_mut_slice().swap(off_n0_j, off_k_j); } } @@ -441,7 +439,7 @@ impl Transpose for DMat { let off_i_j = self.offset(i, j); let off_j_i = self.offset(j, i); - self.mij.swap(off_i_j, off_j_i); + self.mij.as_mut_slice().swap(off_i_j, off_j_i); } } diff --git a/src/structs/dvec.rs b/src/structs/dvec.rs index 0e585b22..2cceb869 100644 --- a/src/structs/dvec.rs +++ b/src/structs/dvec.rs @@ -5,7 +5,7 @@ use std::num::{Zero, One, Float}; use rand::Rand; use rand; -use std::vec; +use std::vec_ng::Vec; use std::vec::{Items, MutItems}; use traits::operations::ApproxEq; use std::iter::FromIterator; @@ -19,7 +19,7 @@ mod metal; #[deriving(Eq, Show, Clone)] pub struct DVec { /// Components of the vector. Contains as much elements as the vector dimension. - at: ~[N] + at: Vec } double_dispatch_binop_decl_trait!(DVec, DVecMulRhs) @@ -52,7 +52,7 @@ impl DVec { impl DVec { /// Indexing without bounds checking. pub unsafe fn at_fast(&self, i: uint) -> N { - (*self.at.unsafe_ref(i)).clone() + (*self.at.as_slice().unsafe_ref(i)).clone() } } @@ -79,7 +79,7 @@ impl DVec { /// Creates an uninitialized vec. #[inline] pub unsafe fn new_uninitialized(dim: uint) -> DVec { - let mut vec = vec::with_capacity(dim); + let mut vec = Vec::with_capacity(dim); vec.set_len(dim); DVec { @@ -89,28 +89,24 @@ impl DVec { #[inline] pub unsafe fn set_fast(&mut self, i: uint, val: N) { - *self.at.unsafe_mut_ref(i) = val + *self.at.as_mut_slice().unsafe_mut_ref(i) = val } /// Gets a reference to of this vector data. #[inline] pub fn as_vec<'r>(&'r self) -> &'r [N] { - let data: &'r [N] = self.at; - - data + self.at.as_slice() } /// Gets a mutable reference to of this vector data. #[inline] pub fn as_mut_vec<'r>(&'r mut self) -> &'r mut [N] { - let data: &'r mut [N] = self.at; - - data + self.at.as_mut_slice() } /// Extracts this vector data. #[inline] - pub fn to_vec(self) -> ~[N] { + pub fn to_vec(self) -> Vec { self.at } } @@ -119,7 +115,7 @@ impl DVec { /// Builds a vector filled with a constant. #[inline] pub fn from_elem(dim: uint, elem: N) -> DVec { - DVec { at: vec::from_elem(dim, elem) } + DVec { at: Vec::from_elem(dim, elem) } } /// Builds a vector filled with the components provided by a vector. @@ -130,7 +126,7 @@ impl DVec { assert!(dim <= vec.len()); DVec { - at: vec.slice_to(dim).to_owned() + at: Vec::from_slice(vec.slice_to(dim)) } } } @@ -139,7 +135,7 @@ impl DVec { /// Builds a vector filled with the result of a function. #[inline(always)] pub fn from_fn(dim: uint, f: |uint| -> N) -> DVec { - DVec { at: vec::from_fn(dim, |i| f(i)) } + DVec { at: Vec::from_fn(dim, |i| f(i)) } } } @@ -167,7 +163,7 @@ impl IterableMut for DVec { impl FromIterator for DVec { #[inline] fn from_iterator>(mut param: &mut I) -> DVec { - let mut res = DVec { at: ~[] }; + let mut res = DVec { at: Vec::new() }; for e in param { res.at.push(e) @@ -181,13 +177,13 @@ impl + DVecMulRhs>> DVec { /// Computes the canonical basis for the given dimension. A canonical basis is a set of /// vectors, mutually orthogonal, with all its component equal to 0.0 except one which is equal /// to 1.0. - pub fn canonical_basis_with_dim(dim: uint) -> ~[DVec] { - let mut res : ~[DVec] = ~[]; + pub fn canonical_basis_with_dim(dim: uint) -> Vec> { + let mut res : Vec> = Vec::new(); for i in range(0u, dim) { let mut basis_element : DVec = DVec::new_zeros(dim); - basis_element.at[i] = One::one(); + *basis_element.at.get_mut(i) = One::one(); res.push(basis_element); } @@ -197,16 +193,16 @@ impl + DVecMulRhs>> DVec { /// Computes a basis of the space orthogonal to the vector. If the input vector is of dimension /// `n`, this will return `n - 1` vectors. - pub fn orthogonal_subspace_basis(&self) -> ~[DVec] { + pub fn orthogonal_subspace_basis(&self) -> Vec> { // compute the basis of the orthogonal subspace using Gram-Schmidt // orthogonalization algorithm - let dim = self.at.len(); - let mut res : ~[DVec] = ~[]; + let dim = self.at.len(); + let mut res : Vec> = Vec::new(); for i in range(0u, dim) { let mut basis_element : DVec = DVec::new_zeros(self.at.len()); - basis_element.at[i] = One::one(); + *basis_element.at.get_mut(i) = One::one(); if res.len() == dim - 1 { break; @@ -254,7 +250,7 @@ impl> DVecSubRhs> for DVec { impl> Neg> for DVec { #[inline] fn neg(&self) -> DVec { - DVec { at: self.at.iter().map(|a| -a).collect() } + DVec { at: self.at.iter().map(|a| -*a).collect() } } } @@ -309,7 +305,7 @@ impl Norm for DVec { let l = Norm::norm(self); for i in range(0u, self.at.len()) { - self.at[i] = self.at[i] / l; + *self.at.get_mut(i) = *self.at.get(i) / l; } l diff --git a/src/structs/vec.rs b/src/structs/vec.rs index 5b5d60a4..d73f070f 100644 --- a/src/structs/vec.rs +++ b/src/structs/vec.rs @@ -3,8 +3,8 @@ #[allow(missing_doc)]; // we allow missing to avoid having to document the vector components. use std::cast; -use std::cmp; use std::num::{Zero, One, Float, Bounded}; +use std::vec_ng::Vec; use std::vec::{Items, MutItems}; use std::iter::{Iterator, FromIterator}; use traits::operations::{ApproxEq, PartialOrd, PartialOrdering, Less, Equal, Greater, NotComparable}; diff --git a/src/structs/vec_macros.rs b/src/structs/vec_macros.rs index 27dbca56..f3764276 100644 --- a/src/structs/vec_macros.rs +++ b/src/structs/vec_macros.rs @@ -34,19 +34,21 @@ macro_rules! at_fast_impl( ) ) +// FIXME: N should be bounded by TotalOrd instead of Float… +// However, f32/f64 does not implement TotalOrd… macro_rules! ord_impl( ($t: ident, $comp0: ident $(,$compN: ident)*) => ( - impl PartialOrd for $t { + impl PartialOrd for $t { #[inline] fn inf(a: &$t, b: &$t) -> $t { - $t::new(cmp::min(a.$comp0.clone(), b.$comp0.clone()) - $(, cmp::min(a.$compN.clone(), b.$compN.clone()))*) + $t::new(a.$comp0.min(b.$comp0.clone()) + $(, a.$compN.min(b.$compN))*) } #[inline] fn sup(a: &$t, b: &$t) -> $t { - $t::new(cmp::max(a.$comp0.clone(), b.$comp0.clone()) - $(, cmp::max(a.$compN.clone(), b.$compN.clone()))*) + $t::new(a.$comp0.max(b.$comp0.clone()) + $(, a.$compN.max(b.$compN.clone()))*) } #[inline] @@ -266,7 +268,7 @@ macro_rules! basis_impl( fn orthonormal_subspace_basis(n: &$t, f: |$t| -> bool) { // compute the basis of the orthogonal subspace using Gram-Schmidt // orthogonalization algorithm - let mut basis: ~[$t] = ~[]; + let mut basis: Vec<$t> = Vec::new(); for i in range(0u, $dim) { let mut basis_element : $t = Zero::zero(); diff --git a/src/tests/vec.rs b/src/tests/vec.rs index d88e85e2..c7728d50 100644 --- a/src/tests/vec.rs +++ b/src/tests/vec.rs @@ -303,16 +303,16 @@ fn test_ord_vec3() { #[test] fn test_min_max_vec3() { - assert_eq!(na::sup(&Vec3::new(1, 2, 3), &Vec3::new(3, 2, 1)), Vec3::new(3, 2, 3)); - assert_eq!(na::inf(&Vec3::new(1, 2, 3), &Vec3::new(3, 2, 1)), Vec3::new(1, 2, 1)); + assert_eq!(na::sup(&Vec3::new(1.0, 2.0, 3.0), &Vec3::new(3.0, 2.0, 1.0)), Vec3::new(3.0, 2.0, 3.0)); + assert_eq!(na::inf(&Vec3::new(1.0, 2.0, 3.0), &Vec3::new(3.0, 2.0, 1.0)), Vec3::new(1.0, 2.0, 1.0)); } #[test] fn test_outer_vec3() { assert_eq!( - na::outer(&Vec3::new(1, 2, 3), &Vec3::new(4, 5, 6)), + na::outer(&Vec3::new(1.0, 2.0, 3.0), &Vec3::new(4.0, 5.0, 6.0)), Mat3::new( - 4, 5, 6, - 8, 10, 12, - 12, 15, 18)); + 4.0, 5.0, 6.0, + 8.0, 10.0, 12.0, + 12.0, 15.0, 18.0)); } diff --git a/src/traits/geometry.rs b/src/traits/geometry.rs index 14b4df58..cd65382b 100644 --- a/src/traits/geometry.rs +++ b/src/traits/geometry.rs @@ -91,7 +91,7 @@ pub trait RotationWithTranslation, AV>: Rotation + Translation Self { - let mut res = Translation::append_translation_cpy(t, &-center); + let mut res = Translation::append_translation_cpy(t, &-*center); res.append_rotation(amount); res.append_translation(center); @@ -108,7 +108,7 @@ pub trait RotationWithTranslation, AV>: Rotation + Translation { impl, T> RMul for M { fn rmul(&self, v: &T) -> T { - self * *v + *self * *v } } @@ -292,7 +292,7 @@ pub trait LMul { impl, M> LMul for M { fn lmul(&self, v: &T) -> T { - v * *self + *v * *self } }