Use Vec instead of ~[].

Version of rustc: 0.10-pre (fc7a112 2014-03-14 23:11:31 -0700)
This commit is contained in:
Sébastien Crozet 2014-03-15 12:23:54 +01:00
parent f2ec77e83c
commit 606ad947c9
9 changed files with 62 additions and 67 deletions

View File

@ -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")]; #[doc(html_root_url = "http://www.rust-ci.org/sebcrozet/nalgebra/doc")];
extern crate std; extern crate std;
extern crate extra;
extern crate rand; extern crate rand;
extern crate serialize; extern crate serialize;

View File

@ -70,13 +70,13 @@ pub fn clamp<T: Ord>(val: T, min: T, max: T) -> T {
/// Same as `cmp::max`. /// Same as `cmp::max`.
#[inline(always)] #[inline(always)]
pub fn max<T: Ord>(a: T, b: T) -> T { pub fn max<T: TotalOrd>(a: T, b: T) -> T {
cmp::max(a, b) cmp::max(a, b)
} }
/// Same as `cmp::min`. /// Same as `cmp::min`.
#[inline(always)] #[inline(always)]
pub fn min<T: Ord>(a: T, b: T) -> T { pub fn min<T: TotalOrd>(a: T, b: T) -> T {
cmp::min(a, b) cmp::min(a, b)
} }

View File

@ -5,7 +5,7 @@
use rand::Rand; use rand::Rand;
use rand; use rand;
use std::num::{One, Zero}; use std::num::{One, Zero};
use std::vec; use std::vec_ng::Vec;
use traits::operations::ApproxEq; use traits::operations::ApproxEq;
use std::mem; use std::mem;
use structs::dvec::{DVec, DVecMulRhs}; use structs::dvec::{DVec, DVecMulRhs};
@ -20,7 +20,7 @@ mod metal;
pub struct DMat<N> { pub struct DMat<N> {
priv nrows: uint, priv nrows: uint,
priv ncols: uint, priv ncols: uint,
priv mij: ~[N] priv mij: Vec<N>
} }
double_dispatch_binop_decl_trait!(DMat, DMatMulRhs) double_dispatch_binop_decl_trait!(DMat, DMatMulRhs)
@ -37,7 +37,7 @@ impl<N> DMat<N> {
/// Creates an uninitialized matrix. /// Creates an uninitialized matrix.
#[inline] #[inline]
pub unsafe fn new_uninitialized(nrows: uint, ncols: uint) -> DMat<N> { pub unsafe fn new_uninitialized(nrows: uint, ncols: uint) -> DMat<N> {
let mut vec = vec::with_capacity(nrows * ncols); let mut vec = Vec::with_capacity(nrows * ncols);
vec.set_len(nrows * ncols); vec.set_len(nrows * ncols);
DMat { DMat {
@ -96,7 +96,7 @@ impl<N: Clone> DMat<N> {
DMat { DMat {
nrows: nrows, nrows: nrows,
ncols: ncols, ncols: ncols,
mij: vec::from_elem(nrows * ncols, val) mij: Vec::from_elem(nrows * ncols, val)
} }
} }
@ -129,7 +129,7 @@ impl<N: Clone> DMat<N> {
DMat { DMat {
nrows: nrows, nrows: nrows,
ncols: ncols, ncols: ncols,
mij: vec.to_owned() mij: Vec::from_slice(vec)
} }
} }
} }
@ -141,7 +141,7 @@ impl<N> DMat<N> {
DMat { DMat {
nrows: nrows, nrows: nrows,
ncols: ncols, 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<N> DMat<N> {
/// Transforms this matrix into an array. This consumes the matrix and is O(1). /// 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. /// The returned vector contains the matrix data in column-major order.
#[inline] #[inline]
pub fn to_vec(self) -> ~[N] { pub fn to_vec(self) -> Vec<N> {
self.mij self.mij
} }
@ -168,18 +168,14 @@ impl<N> DMat<N> {
/// The returned vector contains the matrix data in column-major order. /// The returned vector contains the matrix data in column-major order.
#[inline] #[inline]
pub fn as_vec<'r>(&'r self) -> &'r [N] { pub fn as_vec<'r>(&'r self) -> &'r [N] {
let res: &'r [N] = self.mij; self.mij.as_slice()
res
} }
/// Gets a mutable reference to this matrix data. /// Gets a mutable reference to this matrix data.
/// The returned vector contains the matrix data in column-major order. /// The returned vector contains the matrix data in column-major order.
#[inline] #[inline]
pub fn as_mut_vec<'r>(&'r mut self) -> &'r mut [N] { pub fn as_mut_vec<'r>(&'r mut self) -> &'r mut [N] {
let res: &'r mut [N] = self.mij; self.mij.as_mut_slice()
res
} }
} }
@ -219,14 +215,16 @@ impl<N: Clone> DMat<N> {
pub fn set(&mut self, row: uint, col: uint, val: N) { pub fn set(&mut self, row: uint, col: uint, val: N) {
assert!(row < self.nrows); assert!(row < self.nrows);
assert!(col < self.ncols); 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. /// Just like `set` without bounds checking.
#[inline] #[inline]
pub unsafe fn set_fast(&mut self, row: uint, col: uint, val: N) { pub unsafe fn set_fast(&mut self, row: uint, col: uint, val: N) {
let off = self.offset(row, col); let offset = self.offset(row, col);
*self.mij.unsafe_mut_ref(off) = val *self.mij.as_mut_slice().unsafe_mut_ref(offset) = val
} }
/// Reads the value of a component of the matrix. /// Reads the value of a component of the matrix.
@ -244,7 +242,7 @@ impl<N: Clone> DMat<N> {
/// Just like `at` without bounds checking. /// Just like `at` without bounds checking.
#[inline] #[inline]
pub unsafe fn at_fast(&self, row: uint, col: uint) -> N { 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<N, DVec<N>> for DVec<N> {
} }
} }
res.at[i] = acc; *res.at.get_mut(i) = acc;
} }
res res
@ -312,7 +310,7 @@ DVecMulRhs<N, DVec<N>> for DMat<N> {
} }
} }
res.at[i] = acc; *res.at.get_mut(i) = acc;
} }
res res
@ -366,8 +364,8 @@ Inv for DMat<N> {
let off_n0_j = self.offset(n0, j); let off_n0_j = self.offset(n0, j);
let off_k_j = self.offset(k, j); let off_k_j = self.offset(k, j);
self.mij.swap(off_n0_j, off_k_j); self.mij.as_mut_slice().swap(off_n0_j, off_k_j);
res.mij.swap(off_n0_j, off_k_j); res.mij.as_mut_slice().swap(off_n0_j, off_k_j);
} }
} }
@ -441,7 +439,7 @@ impl<N: Clone> Transpose for DMat<N> {
let off_i_j = self.offset(i, j); let off_i_j = self.offset(i, j);
let off_j_i = self.offset(j, i); 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);
} }
} }

View File

@ -5,7 +5,7 @@
use std::num::{Zero, One, Float}; use std::num::{Zero, One, Float};
use rand::Rand; use rand::Rand;
use rand; use rand;
use std::vec; use std::vec_ng::Vec;
use std::vec::{Items, MutItems}; use std::vec::{Items, MutItems};
use traits::operations::ApproxEq; use traits::operations::ApproxEq;
use std::iter::FromIterator; use std::iter::FromIterator;
@ -19,7 +19,7 @@ mod metal;
#[deriving(Eq, Show, Clone)] #[deriving(Eq, Show, Clone)]
pub struct DVec<N> { pub struct DVec<N> {
/// Components of the vector. Contains as much elements as the vector dimension. /// Components of the vector. Contains as much elements as the vector dimension.
at: ~[N] at: Vec<N>
} }
double_dispatch_binop_decl_trait!(DVec, DVecMulRhs) double_dispatch_binop_decl_trait!(DVec, DVecMulRhs)
@ -52,7 +52,7 @@ impl<N: Zero + Clone> DVec<N> {
impl<N: Clone> DVec<N> { impl<N: Clone> DVec<N> {
/// Indexing without bounds checking. /// Indexing without bounds checking.
pub unsafe fn at_fast(&self, i: uint) -> N { 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<N> DVec<N> {
/// Creates an uninitialized vec. /// Creates an uninitialized vec.
#[inline] #[inline]
pub unsafe fn new_uninitialized(dim: uint) -> DVec<N> { pub unsafe fn new_uninitialized(dim: uint) -> DVec<N> {
let mut vec = vec::with_capacity(dim); let mut vec = Vec::with_capacity(dim);
vec.set_len(dim); vec.set_len(dim);
DVec { DVec {
@ -89,28 +89,24 @@ impl<N> DVec<N> {
#[inline] #[inline]
pub unsafe fn set_fast(&mut self, i: uint, val: N) { 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. /// Gets a reference to of this vector data.
#[inline] #[inline]
pub fn as_vec<'r>(&'r self) -> &'r [N] { pub fn as_vec<'r>(&'r self) -> &'r [N] {
let data: &'r [N] = self.at; self.at.as_slice()
data
} }
/// Gets a mutable reference to of this vector data. /// Gets a mutable reference to of this vector data.
#[inline] #[inline]
pub fn as_mut_vec<'r>(&'r mut self) -> &'r mut [N] { pub fn as_mut_vec<'r>(&'r mut self) -> &'r mut [N] {
let data: &'r mut [N] = self.at; self.at.as_mut_slice()
data
} }
/// Extracts this vector data. /// Extracts this vector data.
#[inline] #[inline]
pub fn to_vec(self) -> ~[N] { pub fn to_vec(self) -> Vec<N> {
self.at self.at
} }
} }
@ -119,7 +115,7 @@ impl<N: Clone> DVec<N> {
/// Builds a vector filled with a constant. /// Builds a vector filled with a constant.
#[inline] #[inline]
pub fn from_elem(dim: uint, elem: N) -> DVec<N> { pub fn from_elem(dim: uint, elem: N) -> DVec<N> {
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. /// Builds a vector filled with the components provided by a vector.
@ -130,7 +126,7 @@ impl<N: Clone> DVec<N> {
assert!(dim <= vec.len()); assert!(dim <= vec.len());
DVec { DVec {
at: vec.slice_to(dim).to_owned() at: Vec::from_slice(vec.slice_to(dim))
} }
} }
} }
@ -139,7 +135,7 @@ impl<N> DVec<N> {
/// Builds a vector filled with the result of a function. /// Builds a vector filled with the result of a function.
#[inline(always)] #[inline(always)]
pub fn from_fn(dim: uint, f: |uint| -> N) -> DVec<N> { pub fn from_fn(dim: uint, f: |uint| -> N) -> DVec<N> {
DVec { at: vec::from_fn(dim, |i| f(i)) } DVec { at: Vec::from_fn(dim, |i| f(i)) }
} }
} }
@ -167,7 +163,7 @@ impl<N> IterableMut<N> for DVec<N> {
impl<N> FromIterator<N> for DVec<N> { impl<N> FromIterator<N> for DVec<N> {
#[inline] #[inline]
fn from_iterator<I: Iterator<N>>(mut param: &mut I) -> DVec<N> { fn from_iterator<I: Iterator<N>>(mut param: &mut I) -> DVec<N> {
let mut res = DVec { at: ~[] }; let mut res = DVec { at: Vec::new() };
for e in param { for e in param {
res.at.push(e) res.at.push(e)
@ -181,13 +177,13 @@ impl<N: Clone + Num + Float + ApproxEq<N> + DVecMulRhs<N, DVec<N>>> DVec<N> {
/// Computes the canonical basis for the given dimension. A canonical basis is a set of /// 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 /// vectors, mutually orthogonal, with all its component equal to 0.0 except one which is equal
/// to 1.0. /// to 1.0.
pub fn canonical_basis_with_dim(dim: uint) -> ~[DVec<N>] { pub fn canonical_basis_with_dim(dim: uint) -> Vec<DVec<N>> {
let mut res : ~[DVec<N>] = ~[]; let mut res : Vec<DVec<N>> = Vec::new();
for i in range(0u, dim) { for i in range(0u, dim) {
let mut basis_element : DVec<N> = DVec::new_zeros(dim); let mut basis_element : DVec<N> = DVec::new_zeros(dim);
basis_element.at[i] = One::one(); *basis_element.at.get_mut(i) = One::one();
res.push(basis_element); res.push(basis_element);
} }
@ -197,16 +193,16 @@ impl<N: Clone + Num + Float + ApproxEq<N> + DVecMulRhs<N, DVec<N>>> DVec<N> {
/// Computes a basis of the space orthogonal to the vector. If the input vector is of dimension /// Computes a basis of the space orthogonal to the vector. If the input vector is of dimension
/// `n`, this will return `n - 1` vectors. /// `n`, this will return `n - 1` vectors.
pub fn orthogonal_subspace_basis(&self) -> ~[DVec<N>] { pub fn orthogonal_subspace_basis(&self) -> Vec<DVec<N>> {
// compute the basis of the orthogonal subspace using Gram-Schmidt // compute the basis of the orthogonal subspace using Gram-Schmidt
// orthogonalization algorithm // orthogonalization algorithm
let dim = self.at.len(); let dim = self.at.len();
let mut res : ~[DVec<N>] = ~[]; let mut res : Vec<DVec<N>> = Vec::new();
for i in range(0u, dim) { for i in range(0u, dim) {
let mut basis_element : DVec<N> = DVec::new_zeros(self.at.len()); let mut basis_element : DVec<N> = 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 { if res.len() == dim - 1 {
break; break;
@ -254,7 +250,7 @@ impl<N: Sub<N, N>> DVecSubRhs<N, DVec<N>> for DVec<N> {
impl<N: Neg<N>> Neg<DVec<N>> for DVec<N> { impl<N: Neg<N>> Neg<DVec<N>> for DVec<N> {
#[inline] #[inline]
fn neg(&self) -> DVec<N> { fn neg(&self) -> DVec<N> {
DVec { at: self.at.iter().map(|a| -a).collect() } DVec { at: self.at.iter().map(|a| -*a).collect() }
} }
} }
@ -309,7 +305,7 @@ impl<N: Num + Float + Clone> Norm<N> for DVec<N> {
let l = Norm::norm(self); let l = Norm::norm(self);
for i in range(0u, self.at.len()) { 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 l

View File

@ -3,8 +3,8 @@
#[allow(missing_doc)]; // we allow missing to avoid having to document the vector components. #[allow(missing_doc)]; // we allow missing to avoid having to document the vector components.
use std::cast; use std::cast;
use std::cmp;
use std::num::{Zero, One, Float, Bounded}; use std::num::{Zero, One, Float, Bounded};
use std::vec_ng::Vec;
use std::vec::{Items, MutItems}; use std::vec::{Items, MutItems};
use std::iter::{Iterator, FromIterator}; use std::iter::{Iterator, FromIterator};
use traits::operations::{ApproxEq, PartialOrd, PartialOrdering, Less, Equal, Greater, NotComparable}; use traits::operations::{ApproxEq, PartialOrd, PartialOrdering, Less, Equal, Greater, NotComparable};

View File

@ -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( macro_rules! ord_impl(
($t: ident, $comp0: ident $(,$compN: ident)*) => ( ($t: ident, $comp0: ident $(,$compN: ident)*) => (
impl<N: Ord + Eq + Clone> PartialOrd for $t<N> { impl<N: Float + Eq + Clone> PartialOrd for $t<N> {
#[inline] #[inline]
fn inf(a: &$t<N>, b: &$t<N>) -> $t<N> { fn inf(a: &$t<N>, b: &$t<N>) -> $t<N> {
$t::new(cmp::min(a.$comp0.clone(), b.$comp0.clone()) $t::new(a.$comp0.min(b.$comp0.clone())
$(, cmp::min(a.$compN.clone(), b.$compN.clone()))*) $(, a.$compN.min(b.$compN))*)
} }
#[inline] #[inline]
fn sup(a: &$t<N>, b: &$t<N>) -> $t<N> { fn sup(a: &$t<N>, b: &$t<N>) -> $t<N> {
$t::new(cmp::max(a.$comp0.clone(), b.$comp0.clone()) $t::new(a.$comp0.max(b.$comp0.clone())
$(, cmp::max(a.$compN.clone(), b.$compN.clone()))*) $(, a.$compN.max(b.$compN.clone()))*)
} }
#[inline] #[inline]
@ -266,7 +268,7 @@ macro_rules! basis_impl(
fn orthonormal_subspace_basis(n: &$t<N>, f: |$t<N>| -> bool) { fn orthonormal_subspace_basis(n: &$t<N>, f: |$t<N>| -> bool) {
// compute the basis of the orthogonal subspace using Gram-Schmidt // compute the basis of the orthogonal subspace using Gram-Schmidt
// orthogonalization algorithm // orthogonalization algorithm
let mut basis: ~[$t<N>] = ~[]; let mut basis: Vec<$t<N>> = Vec::new();
for i in range(0u, $dim) { for i in range(0u, $dim) {
let mut basis_element : $t<N> = Zero::zero(); let mut basis_element : $t<N> = Zero::zero();

View File

@ -303,16 +303,16 @@ fn test_ord_vec3() {
#[test] #[test]
fn test_min_max_vec3() { 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::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, 2, 3), &Vec3::new(3, 2, 1)), Vec3::new(1, 2, 1)); 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] #[test]
fn test_outer_vec3() { fn test_outer_vec3() {
assert_eq!( 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( Mat3::new(
4, 5, 6, 4.0, 5.0, 6.0,
8, 10, 12, 8.0, 10.0, 12.0,
12, 15, 18)); 12.0, 15.0, 18.0));
} }

View File

@ -91,7 +91,7 @@ pub trait RotationWithTranslation<LV: Neg<LV>, AV>: Rotation<AV> + Translation<L
/// * `point` - the center of rotation. /// * `point` - the center of rotation.
#[inline] #[inline]
fn append_rotation_wrt_point_cpy(t: &Self, amount: &AV, center: &LV) -> Self { fn append_rotation_wrt_point_cpy(t: &Self, amount: &AV, center: &LV) -> 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_rotation(amount);
res.append_translation(center); res.append_translation(center);
@ -108,7 +108,7 @@ pub trait RotationWithTranslation<LV: Neg<LV>, AV>: Rotation<AV> + Translation<L
/// * `center` - the new center of rotation /// * `center` - the new center of rotation
#[inline] #[inline]
fn append_rotation_wrt_point(&mut self, amount: &AV, center: &LV) { fn append_rotation_wrt_point(&mut self, amount: &AV, center: &LV) {
self.append_translation(&-center); self.append_translation(&-*center);
self.append_rotation(amount); self.append_rotation(amount);
self.append_translation(center); self.append_translation(center);
} }

View File

@ -280,7 +280,7 @@ pub trait RMul<V> {
impl<M: Mul<T, T>, T> RMul<T> for M { impl<M: Mul<T, T>, T> RMul<T> for M {
fn rmul(&self, v: &T) -> T { fn rmul(&self, v: &T) -> T {
self * *v *self * *v
} }
} }
@ -292,7 +292,7 @@ pub trait LMul<V> {
impl<T: Mul<M, T>, M> LMul<T> for M { impl<T: Mul<M, T>, M> LMul<T> for M {
fn lmul(&self, v: &T) -> T { fn lmul(&self, v: &T) -> T {
v * *self *v * *self
} }
} }