Make vectors indexable the same way as slices.

This includes range indexing.
In addition, for unification, the methods `.as_slice` and `.as_mut_slice` of DVec have been renamed
to `.as_ref` and `.as_mut`.
This commit is contained in:
Sébastien Crozet 2016-01-10 14:49:37 +01:00
parent 22edb3a157
commit 5cbbc25bb2
4 changed files with 44 additions and 44 deletions

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 (0usize .. 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 (0usize .. 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

@ -15,20 +15,20 @@ macro_rules! dvec_impl(
/// Tests if all components of the vector are zeroes. /// Tests if all components of the vector are zeroes.
#[inline] #[inline]
pub fn is_zero(&self) -> bool { pub fn is_zero(&self) -> bool {
self.as_slice().iter().all(|e| e.is_zero()) self.as_ref().iter().all(|e| e.is_zero())
} }
} }
impl<N> $dvec<N> { impl<N> $dvec<N> {
/// Slices this vector. /// Slices this vector.
#[inline] #[inline]
pub fn as_slice<'a>(&'a self) -> &'a [N] { pub fn as_ref<'a>(&'a self) -> &'a [N] {
&self.at[.. self.len()] &self.at[.. self.len()]
} }
/// Mutably slices this vector. /// Mutably slices this vector.
#[inline] #[inline]
pub fn as_mut_slice<'a>(&'a mut self) -> &'a mut [N] { pub fn as_mut<'a>(&'a mut self) -> &'a mut [N] {
let len = self.len(); let len = self.len();
&mut self.at[.. len] &mut self.at[.. len]
} }
@ -46,7 +46,7 @@ macro_rules! dvec_impl(
fn swap(&mut self, i: usize, j: usize) { fn swap(&mut self, i: usize, j: usize) {
assert!(i < self.len()); assert!(i < self.len());
assert!(j < self.len()); assert!(j < self.len());
self.as_mut_slice().swap(i, j); self.as_mut().swap(i, j);
} }
#[inline] #[inline]
@ -61,17 +61,17 @@ macro_rules! dvec_impl(
} }
impl<N> Index<usize> for $dvec<N> { impl<N, T> Index<T> for $dvec<N> where [N]: Index<T> {
type Output = N; type Output = <[N] as Index<T>>::Output;
fn index(&self, i: usize) -> &N { fn index(&self, i: T) -> &<[N] as Index<T>>::Output {
&self.as_slice()[i] &self.as_ref()[i]
} }
} }
impl<N> IndexMut<usize> for $dvec<N> { impl<N, T> IndexMut<T> for $dvec<N> where [N]: IndexMut<T> {
fn index_mut(&mut self, i: usize) -> &mut N { fn index_mut(&mut self, i: T) -> &mut <[N] as Index<T>>::Output {
&mut self.as_mut_slice()[i] &mut self.as_mut()[i]
} }
} }
@ -97,14 +97,14 @@ macro_rules! dvec_impl(
impl<N> Iterable<N> for $dvec<N> { impl<N> Iterable<N> for $dvec<N> {
#[inline] #[inline]
fn iter<'l>(&'l self) -> Iter<'l, N> { fn iter<'l>(&'l self) -> Iter<'l, N> {
self.as_slice().iter() self.as_ref().iter()
} }
} }
impl<N> IterableMut<N> for $dvec<N> { impl<N> IterableMut<N> for $dvec<N> {
#[inline] #[inline]
fn iter_mut<'l>(&'l mut self) -> IterMut<'l, N> { fn iter_mut<'l>(&'l mut self) -> IterMut<'l, N> {
self.as_mut_slice().iter_mut() self.as_mut().iter_mut()
} }
} }
@ -185,7 +185,7 @@ macro_rules! dvec_impl(
let mut res = self; let mut res = self;
for (left, right) in res.as_mut_slice().iter_mut().zip(right.as_slice().iter()) { for (left, right) in res.as_mut().iter_mut().zip(right.as_ref().iter()) {
*left = *left * *right *left = *left * *right
} }
@ -202,7 +202,7 @@ macro_rules! dvec_impl(
let mut res = self; let mut res = self;
for (left, right) in res.as_mut_slice().iter_mut().zip(right.as_slice().iter()) { for (left, right) in res.as_mut().iter_mut().zip(right.as_ref().iter()) {
*left = *left / *right *left = *left / *right
} }
@ -219,7 +219,7 @@ macro_rules! dvec_impl(
let mut res = self; let mut res = self;
for (left, right) in res.as_mut_slice().iter_mut().zip(right.as_slice().iter()) { for (left, right) in res.as_mut().iter_mut().zip(right.as_ref().iter()) {
*left = *left + *right *left = *left + *right
} }
@ -236,7 +236,7 @@ macro_rules! dvec_impl(
let mut res = self; let mut res = self;
for (left, right) in res.as_mut_slice().iter_mut().zip(right.as_slice().iter()) { for (left, right) in res.as_mut().iter_mut().zip(right.as_ref().iter()) {
*left = *left - *right *left = *left - *right
} }
@ -249,7 +249,7 @@ macro_rules! dvec_impl(
#[inline] #[inline]
fn neg(self) -> $dvec<N> { fn neg(self) -> $dvec<N> {
FromIterator::from_iter(self.as_slice().iter().map(|a| -*a)) FromIterator::from_iter(self.as_ref().iter().map(|a| -*a))
} }
} }
@ -282,7 +282,7 @@ macro_rules! dvec_impl(
fn normalize_mut(&mut self) -> N { fn normalize_mut(&mut self) -> N {
let l = Norm::norm(self); let l = Norm::norm(self);
for n in self.as_mut_slice().iter_mut() { for n in self.as_mut().iter_mut() {
*n = *n / l; *n = *n / l;
} }
@ -303,13 +303,13 @@ macro_rules! dvec_impl(
#[inline] #[inline]
fn approx_eq_eps(&self, other: &$dvec<N>, epsilon: &N) -> bool { fn approx_eq_eps(&self, other: &$dvec<N>, epsilon: &N) -> bool {
let mut zip = self.as_slice().iter().zip(other.as_slice().iter()); let mut zip = self.as_ref().iter().zip(other.as_ref().iter());
zip.all(|(a, b)| ApproxEq::approx_eq_eps(a, b, epsilon)) zip.all(|(a, b)| ApproxEq::approx_eq_eps(a, b, epsilon))
} }
#[inline] #[inline]
fn approx_eq_ulps(&self, other: &$dvec<N>, ulps: u32) -> bool { fn approx_eq_ulps(&self, other: &$dvec<N>, ulps: u32) -> bool {
let mut zip = self.as_slice().iter().zip(other.as_slice().iter()); let mut zip = self.as_ref().iter().zip(other.as_ref().iter());
zip.all(|(a, b)| ApproxEq::approx_eq_ulps(a, b, ulps)) zip.all(|(a, b)| ApproxEq::approx_eq_ulps(a, b, ulps))
} }
} }
@ -321,7 +321,7 @@ macro_rules! dvec_impl(
fn mul(self, right: N) -> $dvec<N> { fn mul(self, right: N) -> $dvec<N> {
let mut res = self; let mut res = self;
for e in res.as_mut_slice().iter_mut() { for e in res.as_mut().iter_mut() {
*e = *e * right *e = *e * right
} }
@ -336,7 +336,7 @@ macro_rules! dvec_impl(
fn div(self, right: N) -> $dvec<N> { fn div(self, right: N) -> $dvec<N> {
let mut res = self; let mut res = self;
for e in res.as_mut_slice().iter_mut() { for e in res.as_mut().iter_mut() {
*e = *e / right *e = *e / right
} }
@ -351,7 +351,7 @@ macro_rules! dvec_impl(
fn add(self, right: N) -> $dvec<N> { fn add(self, right: N) -> $dvec<N> {
let mut res = self; let mut res = self;
for e in res.as_mut_slice().iter_mut() { for e in res.as_mut().iter_mut() {
*e = *e + right *e = *e + right
} }
@ -366,7 +366,7 @@ macro_rules! dvec_impl(
fn sub(self, right: N) -> $dvec<N> { fn sub(self, right: N) -> $dvec<N> {
let mut res = self; let mut res = self;
for e in res.as_mut_slice().iter_mut() { for e in res.as_mut().iter_mut() {
*e = *e - right *e = *e - right
} }
@ -392,7 +392,7 @@ macro_rules! small_dvec_impl (
return false; // FIXME: fail instead? return false; // FIXME: fail instead?
} }
for (a, b) in self.as_slice().iter().zip(other.as_slice().iter()) { for (a, b) in self.as_ref().iter().zip(other.as_ref().iter()) {
if *a != *b { if *a != *b {
return false; return false;
} }

View File

@ -208,16 +208,16 @@ macro_rules! indexable_impl(
macro_rules! index_impl( macro_rules! index_impl(
($t: ident) => ( ($t: ident) => (
impl<N> Index<usize> for $t<N> { impl<N, T> Index<T> for $t<N> where [N]: Index<T> {
type Output = N; type Output = <[N] as Index<T>>::Output;
fn index(&self, i: usize) -> &N { fn index(&self, i: T) -> &<[N] as Index<T>>::Output {
&self.as_ref()[i] &self.as_ref()[i]
} }
} }
impl<N> IndexMut<usize> for $t<N> { impl<N, T> IndexMut<T> for $t<N> where [N]: IndexMut<T> {
fn index_mut(&mut self, i: usize) -> &mut N { fn index_mut(&mut self, i: T) -> &mut <[N] as Index<T>>::Output {
&mut self.as_mut()[i] &mut self.as_mut()[i]
} }
} }

View File

@ -7,7 +7,7 @@ use na::{Vec1, Vec3, Mat1, Mat2, Mat3, Mat4, Mat5, Mat6, Rot2, Rot3, Persp3, Per
macro_rules! test_inv_mat_impl( macro_rules! test_inv_mat_impl(
($t: ty) => ( ($t: ty) => (
for _ in (0usize .. 10000) { for _ in 0usize .. 10000 {
let randmat : $t = random(); let randmat : $t = random();
match na::inv(&randmat) { match na::inv(&randmat) {
@ -20,7 +20,7 @@ macro_rules! test_inv_mat_impl(
macro_rules! test_transpose_mat_impl( macro_rules! test_transpose_mat_impl(
($t: ty) => ( ($t: ty) => (
for _ in (0usize .. 10000) { for _ in 0usize .. 10000 {
let randmat : $t = random(); let randmat : $t = random();
assert!(na::transpose(&na::transpose(&randmat)) == randmat); assert!(na::transpose(&na::transpose(&randmat)) == randmat);
@ -30,7 +30,7 @@ macro_rules! test_transpose_mat_impl(
macro_rules! test_qr_impl( macro_rules! test_qr_impl(
($t: ty) => ( ($t: ty) => (
for _ in (0usize .. 10000) { for _ in 0usize .. 10000 {
let randmat : $t = random(); let randmat : $t = random();
let (q, r) = na::qr(&randmat); let (q, r) = na::qr(&randmat);
@ -43,7 +43,7 @@ macro_rules! test_qr_impl(
macro_rules! test_cholesky_impl( macro_rules! test_cholesky_impl(
($t: ty) => ( ($t: ty) => (
for _ in (0usize .. 10000) { for _ in 0usize .. 10000 {
// construct symmetric positive definite matrix // construct symmetric positive definite matrix
let mut randmat : $t = random(); let mut randmat : $t = random();
@ -65,7 +65,7 @@ macro_rules! test_cholesky_impl(
macro_rules! test_hessenberg_impl( macro_rules! test_hessenberg_impl(
($t: ty) => ( ($t: ty) => (
for _ in (0usize .. 10000) { for _ in 0usize .. 10000 {
let randmat : $t = random(); let randmat : $t = random();
@ -90,7 +90,7 @@ macro_rules! test_hessenberg_impl(
macro_rules! test_eigen_qr_impl( macro_rules! test_eigen_qr_impl(
($t: ty) => { ($t: ty) => {
for _ in (0usize .. 10000) { for _ in 0usize .. 10000 {
let randmat : $t = random(); let randmat : $t = random();
// Make it symetric so that we can recompose the matrix to test at the end. // Make it symetric so that we can recompose the matrix to test at the end.
let randmat = na::transpose(&randmat) * randmat; let randmat = na::transpose(&randmat) * randmat;
@ -105,7 +105,7 @@ macro_rules! test_eigen_qr_impl(
assert!(na::approx_eq_eps(&randmat, &recomp, &1.0e-2)); assert!(na::approx_eq_eps(&randmat, &recomp, &1.0e-2));
} }
for _ in (0usize .. 10000) { for _ in 0usize .. 10000 {
let randmat : $t = random(); let randmat : $t = random();
// Take only diagonal part // Take only diagonal part
let randmat: $t = Diag::from_diag(&randmat.diag()); let randmat: $t = Diag::from_diag(&randmat.diag());
@ -184,7 +184,7 @@ fn test_inv_mat6() {
#[test] #[test]
fn test_rotation2() { fn test_rotation2() {
for _ in (0usize .. 10000) { for _ in 0usize .. 10000 {
let randmat: na::Rot2<f64> = na::one(); let randmat: na::Rot2<f64> = na::one();
let ang = Vec1::new(na::abs(&random::<f64>()) % <f64 as BaseFloat>::pi()); let ang = Vec1::new(na::abs(&random::<f64>()) % <f64 as BaseFloat>::pi());
@ -201,7 +201,7 @@ fn test_index_mat2() {
#[test] #[test]
fn test_inv_rotation3() { fn test_inv_rotation3() {
for _ in (0usize .. 10000) { for _ in 0usize .. 10000 {
let randmat: Rot3<f64> = na::one(); let randmat: Rot3<f64> = na::one();
let dir: Vec3<f64> = random(); let dir: Vec3<f64> = random();
let ang = na::normalize(&dir) * (na::abs(&random::<f64>()) % <f64 as BaseFloat>::pi()); let ang = na::normalize(&dir) * (na::abs(&random::<f64>()) % <f64 as BaseFloat>::pi());
@ -524,7 +524,7 @@ fn test_dmat_subtraction() {
/* FIXME: review qr decomposition to make it work with DMat. /* FIXME: review qr decomposition to make it work with DMat.
#[test] #[test]
fn test_qr() { fn test_qr() {
for _ in (0usize .. 10) { for _ in 0usize .. 10 {
let dim1: usize = random(); let dim1: usize = random();
let dim2: usize = random(); let dim2: usize = random();
let rows = min(40, max(dim1, dim2)); let rows = min(40, max(dim1, dim2));
@ -809,6 +809,6 @@ fn test_transpose_square_mat() {
let mut mat = DMat::from_col_vec(num_rows, num_cols, col_major_mat); let mut mat = DMat::from_col_vec(num_rows, num_cols, col_major_mat);
mat.transpose_mut(); mat.transpose_mut();
for i in 0..num_rows { for i in 0..num_rows {
assert_eq!(&[0, 1, 2, 3], mat.row_slice(i, 0, num_cols).as_slice()); assert_eq!(&[0, 1, 2, 3], &mat.row_slice(i, 0, num_cols)[..]);
} }
} }