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")];
extern crate std;
extern crate extra;
extern crate rand;
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`.
#[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)
}
/// Same as `cmp::min`.
#[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)
}

View File

@ -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<N> {
priv nrows: uint,
priv ncols: uint,
priv mij: ~[N]
priv mij: Vec<N>
}
double_dispatch_binop_decl_trait!(DMat, DMatMulRhs)
@ -37,7 +37,7 @@ impl<N> DMat<N> {
/// Creates an uninitialized matrix.
#[inline]
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);
DMat {
@ -96,7 +96,7 @@ impl<N: Clone> DMat<N> {
DMat {
nrows: nrows,
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 {
nrows: nrows,
ncols: ncols,
mij: vec.to_owned()
mij: Vec::from_slice(vec)
}
}
}
@ -141,7 +141,7 @@ impl<N> DMat<N> {
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<N> DMat<N> {
/// 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<N> {
self.mij
}
@ -168,18 +168,14 @@ impl<N> DMat<N> {
/// 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<N: Clone> DMat<N> {
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<N: Clone> DMat<N> {
/// 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<N, DVec<N>> for DVec<N> {
}
}
res.at[i] = acc;
*res.at.get_mut(i) = acc;
}
res
@ -312,7 +310,7 @@ DVecMulRhs<N, DVec<N>> for DMat<N> {
}
}
res.at[i] = acc;
*res.at.get_mut(i) = acc;
}
res
@ -366,8 +364,8 @@ Inv for DMat<N> {
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<N: Clone> Transpose for DMat<N> {
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);
}
}

View File

@ -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<N> {
/// Components of the vector. Contains as much elements as the vector dimension.
at: ~[N]
at: Vec<N>
}
double_dispatch_binop_decl_trait!(DVec, DVecMulRhs)
@ -52,7 +52,7 @@ impl<N: Zero + Clone> DVec<N> {
impl<N: Clone> DVec<N> {
/// 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<N> DVec<N> {
/// Creates an uninitialized vec.
#[inline]
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);
DVec {
@ -89,28 +89,24 @@ impl<N> DVec<N> {
#[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<N> {
self.at
}
}
@ -119,7 +115,7 @@ impl<N: Clone> DVec<N> {
/// Builds a vector filled with a constant.
#[inline]
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.
@ -130,7 +126,7 @@ impl<N: Clone> DVec<N> {
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<N> DVec<N> {
/// Builds a vector filled with the result of a function.
#[inline(always)]
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> {
#[inline]
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 {
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
/// 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<N>] {
let mut res : ~[DVec<N>] = ~[];
pub fn canonical_basis_with_dim(dim: uint) -> Vec<DVec<N>> {
let mut res : Vec<DVec<N>> = Vec::new();
for i in range(0u, 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);
}
@ -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
/// `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
// orthogonalization algorithm
let dim = self.at.len();
let mut res : ~[DVec<N>] = ~[];
let dim = self.at.len();
let mut res : Vec<DVec<N>> = Vec::new();
for i in range(0u, dim) {
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 {
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> {
#[inline]
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);
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

View File

@ -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};

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(
($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]
fn inf(a: &$t<N>, b: &$t<N>) -> $t<N> {
$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<N>, b: &$t<N>) -> $t<N> {
$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<N>, f: |$t<N>| -> bool) {
// compute the basis of the orthogonal subspace using Gram-Schmidt
// orthogonalization algorithm
let mut basis: ~[$t<N>] = ~[];
let mut basis: Vec<$t<N>> = Vec::new();
for i in range(0u, $dim) {
let mut basis_element : $t<N> = Zero::zero();

View File

@ -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));
}

View File

@ -91,7 +91,7 @@ pub trait RotationWithTranslation<LV: Neg<LV>, AV>: Rotation<AV> + Translation<L
/// * `point` - the center of rotation.
#[inline]
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_translation(center);
@ -108,7 +108,7 @@ pub trait RotationWithTranslation<LV: Neg<LV>, AV>: Rotation<AV> + Translation<L
/// * `center` - the new center of rotation
#[inline]
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_translation(center);
}

View File

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