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:
parent
22edb3a157
commit
5cbbc25bb2
|
@ -13,7 +13,7 @@ macro_rules! bench_mul_dmat(
|
|||
let a: 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!
|
||||
b = a.clone() * b;
|
||||
}
|
||||
|
@ -55,7 +55,7 @@ macro_rules! bench_mul_dmat_dvec(
|
|||
let m : DMat<f64> = DMat::new_random($nrows, $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!
|
||||
v = m.clone() * v
|
||||
}
|
||||
|
|
|
@ -15,20 +15,20 @@ macro_rules! dvec_impl(
|
|||
/// Tests if all components of the vector are zeroes.
|
||||
#[inline]
|
||||
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> {
|
||||
/// Slices this vector.
|
||||
#[inline]
|
||||
pub fn as_slice<'a>(&'a self) -> &'a [N] {
|
||||
pub fn as_ref<'a>(&'a self) -> &'a [N] {
|
||||
&self.at[.. self.len()]
|
||||
}
|
||||
|
||||
/// Mutably slices this vector.
|
||||
#[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();
|
||||
&mut self.at[.. len]
|
||||
}
|
||||
|
@ -46,7 +46,7 @@ macro_rules! dvec_impl(
|
|||
fn swap(&mut self, i: usize, j: usize) {
|
||||
assert!(i < self.len());
|
||||
assert!(j < self.len());
|
||||
self.as_mut_slice().swap(i, j);
|
||||
self.as_mut().swap(i, j);
|
||||
}
|
||||
|
||||
#[inline]
|
||||
|
@ -61,17 +61,17 @@ macro_rules! dvec_impl(
|
|||
|
||||
}
|
||||
|
||||
impl<N> Index<usize> for $dvec<N> {
|
||||
type Output = N;
|
||||
impl<N, T> Index<T> for $dvec<N> where [N]: Index<T> {
|
||||
type Output = <[N] as Index<T>>::Output;
|
||||
|
||||
fn index(&self, i: usize) -> &N {
|
||||
&self.as_slice()[i]
|
||||
fn index(&self, i: T) -> &<[N] as Index<T>>::Output {
|
||||
&self.as_ref()[i]
|
||||
}
|
||||
}
|
||||
|
||||
impl<N> IndexMut<usize> for $dvec<N> {
|
||||
fn index_mut(&mut self, i: usize) -> &mut N {
|
||||
&mut self.as_mut_slice()[i]
|
||||
impl<N, T> IndexMut<T> for $dvec<N> where [N]: IndexMut<T> {
|
||||
fn index_mut(&mut self, i: T) -> &mut <[N] as Index<T>>::Output {
|
||||
&mut self.as_mut()[i]
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -97,14 +97,14 @@ macro_rules! dvec_impl(
|
|||
impl<N> Iterable<N> for $dvec<N> {
|
||||
#[inline]
|
||||
fn iter<'l>(&'l self) -> Iter<'l, N> {
|
||||
self.as_slice().iter()
|
||||
self.as_ref().iter()
|
||||
}
|
||||
}
|
||||
|
||||
impl<N> IterableMut<N> for $dvec<N> {
|
||||
#[inline]
|
||||
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;
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
|
@ -202,7 +202,7 @@ macro_rules! dvec_impl(
|
|||
|
||||
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
|
||||
}
|
||||
|
||||
|
@ -219,7 +219,7 @@ macro_rules! dvec_impl(
|
|||
|
||||
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
|
||||
}
|
||||
|
||||
|
@ -236,7 +236,7 @@ macro_rules! dvec_impl(
|
|||
|
||||
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
|
||||
}
|
||||
|
||||
|
@ -249,7 +249,7 @@ macro_rules! dvec_impl(
|
|||
|
||||
#[inline]
|
||||
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 {
|
||||
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;
|
||||
}
|
||||
|
||||
|
@ -303,13 +303,13 @@ macro_rules! dvec_impl(
|
|||
|
||||
#[inline]
|
||||
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))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
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))
|
||||
}
|
||||
}
|
||||
|
@ -321,7 +321,7 @@ macro_rules! dvec_impl(
|
|||
fn mul(self, right: N) -> $dvec<N> {
|
||||
let mut res = self;
|
||||
|
||||
for e in res.as_mut_slice().iter_mut() {
|
||||
for e in res.as_mut().iter_mut() {
|
||||
*e = *e * right
|
||||
}
|
||||
|
||||
|
@ -336,7 +336,7 @@ macro_rules! dvec_impl(
|
|||
fn div(self, right: N) -> $dvec<N> {
|
||||
let mut res = self;
|
||||
|
||||
for e in res.as_mut_slice().iter_mut() {
|
||||
for e in res.as_mut().iter_mut() {
|
||||
*e = *e / right
|
||||
}
|
||||
|
||||
|
@ -351,7 +351,7 @@ macro_rules! dvec_impl(
|
|||
fn add(self, right: N) -> $dvec<N> {
|
||||
let mut res = self;
|
||||
|
||||
for e in res.as_mut_slice().iter_mut() {
|
||||
for e in res.as_mut().iter_mut() {
|
||||
*e = *e + right
|
||||
}
|
||||
|
||||
|
@ -366,7 +366,7 @@ macro_rules! dvec_impl(
|
|||
fn sub(self, right: N) -> $dvec<N> {
|
||||
let mut res = self;
|
||||
|
||||
for e in res.as_mut_slice().iter_mut() {
|
||||
for e in res.as_mut().iter_mut() {
|
||||
*e = *e - right
|
||||
}
|
||||
|
||||
|
@ -392,7 +392,7 @@ macro_rules! small_dvec_impl (
|
|||
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 {
|
||||
return false;
|
||||
}
|
||||
|
|
|
@ -208,16 +208,16 @@ macro_rules! indexable_impl(
|
|||
|
||||
macro_rules! index_impl(
|
||||
($t: ident) => (
|
||||
impl<N> Index<usize> for $t<N> {
|
||||
type Output = N;
|
||||
impl<N, T> Index<T> for $t<N> where [N]: Index<T> {
|
||||
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]
|
||||
}
|
||||
}
|
||||
|
||||
impl<N> IndexMut<usize> for $t<N> {
|
||||
fn index_mut(&mut self, i: usize) -> &mut N {
|
||||
impl<N, T> IndexMut<T> for $t<N> where [N]: IndexMut<T> {
|
||||
fn index_mut(&mut self, i: T) -> &mut <[N] as Index<T>>::Output {
|
||||
&mut self.as_mut()[i]
|
||||
}
|
||||
}
|
||||
|
|
22
tests/mat.rs
22
tests/mat.rs
|
@ -7,7 +7,7 @@ use na::{Vec1, Vec3, Mat1, Mat2, Mat3, Mat4, Mat5, Mat6, Rot2, Rot3, Persp3, Per
|
|||
|
||||
macro_rules! test_inv_mat_impl(
|
||||
($t: ty) => (
|
||||
for _ in (0usize .. 10000) {
|
||||
for _ in 0usize .. 10000 {
|
||||
let randmat : $t = random();
|
||||
|
||||
match na::inv(&randmat) {
|
||||
|
@ -20,7 +20,7 @@ macro_rules! test_inv_mat_impl(
|
|||
|
||||
macro_rules! test_transpose_mat_impl(
|
||||
($t: ty) => (
|
||||
for _ in (0usize .. 10000) {
|
||||
for _ in 0usize .. 10000 {
|
||||
let randmat : $t = random();
|
||||
|
||||
assert!(na::transpose(&na::transpose(&randmat)) == randmat);
|
||||
|
@ -30,7 +30,7 @@ macro_rules! test_transpose_mat_impl(
|
|||
|
||||
macro_rules! test_qr_impl(
|
||||
($t: ty) => (
|
||||
for _ in (0usize .. 10000) {
|
||||
for _ in 0usize .. 10000 {
|
||||
let randmat : $t = random();
|
||||
|
||||
let (q, r) = na::qr(&randmat);
|
||||
|
@ -43,7 +43,7 @@ macro_rules! test_qr_impl(
|
|||
|
||||
macro_rules! test_cholesky_impl(
|
||||
($t: ty) => (
|
||||
for _ in (0usize .. 10000) {
|
||||
for _ in 0usize .. 10000 {
|
||||
|
||||
// construct symmetric positive definite matrix
|
||||
let mut randmat : $t = random();
|
||||
|
@ -65,7 +65,7 @@ macro_rules! test_cholesky_impl(
|
|||
|
||||
macro_rules! test_hessenberg_impl(
|
||||
($t: ty) => (
|
||||
for _ in (0usize .. 10000) {
|
||||
for _ in 0usize .. 10000 {
|
||||
|
||||
let randmat : $t = random();
|
||||
|
||||
|
@ -90,7 +90,7 @@ macro_rules! test_hessenberg_impl(
|
|||
|
||||
macro_rules! test_eigen_qr_impl(
|
||||
($t: ty) => {
|
||||
for _ in (0usize .. 10000) {
|
||||
for _ in 0usize .. 10000 {
|
||||
let randmat : $t = random();
|
||||
// Make it symetric so that we can recompose the matrix to test at the end.
|
||||
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));
|
||||
}
|
||||
|
||||
for _ in (0usize .. 10000) {
|
||||
for _ in 0usize .. 10000 {
|
||||
let randmat : $t = random();
|
||||
// Take only diagonal part
|
||||
let randmat: $t = Diag::from_diag(&randmat.diag());
|
||||
|
@ -184,7 +184,7 @@ fn test_inv_mat6() {
|
|||
|
||||
#[test]
|
||||
fn test_rotation2() {
|
||||
for _ in (0usize .. 10000) {
|
||||
for _ in 0usize .. 10000 {
|
||||
let randmat: na::Rot2<f64> = na::one();
|
||||
let ang = Vec1::new(na::abs(&random::<f64>()) % <f64 as BaseFloat>::pi());
|
||||
|
||||
|
@ -201,7 +201,7 @@ fn test_index_mat2() {
|
|||
|
||||
#[test]
|
||||
fn test_inv_rotation3() {
|
||||
for _ in (0usize .. 10000) {
|
||||
for _ in 0usize .. 10000 {
|
||||
let randmat: Rot3<f64> = na::one();
|
||||
let dir: Vec3<f64> = random();
|
||||
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.
|
||||
#[test]
|
||||
fn test_qr() {
|
||||
for _ in (0usize .. 10) {
|
||||
for _ in 0usize .. 10 {
|
||||
let dim1: usize = random();
|
||||
let dim2: usize = random();
|
||||
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);
|
||||
mat.transpose_mut();
|
||||
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)[..]);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue