2013-09-22 16:58:21 +08:00
|
|
|
//! Vector with dimensions unknown at compile-time.
|
|
|
|
|
2014-11-01 00:40:47 +08:00
|
|
|
#![allow(missing_docs)] // we hide doc to not have to document the $trhs double dispatch trait.
|
2013-09-15 16:48:18 +08:00
|
|
|
|
2014-12-24 02:01:49 +08:00
|
|
|
use std::slice::{Iter, IterMut};
|
2013-09-09 17:19:54 +08:00
|
|
|
use std::iter::FromIterator;
|
2015-01-02 06:23:35 +08:00
|
|
|
use std::iter::repeat;
|
2015-01-04 16:37:56 +08:00
|
|
|
use std::ops::{Add, Sub, Mul, Div, Neg, Index, IndexMut};
|
2015-02-17 20:32:54 +08:00
|
|
|
use rand::{self, Rand};
|
2014-12-18 06:28:32 +08:00
|
|
|
use traits::operations::{ApproxEq, Axpy};
|
2013-10-14 16:22:32 +08:00
|
|
|
use traits::geometry::{Dot, Norm};
|
2014-11-16 21:04:15 +08:00
|
|
|
use traits::structure::{Iterable, IterableMut, Indexable, Shape, BaseFloat, BaseNum, Zero, One};
|
2015-01-10 08:36:13 +08:00
|
|
|
#[cfg(feature="arbitrary")]
|
|
|
|
use quickcheck::{Arbitrary, Gen};
|
2013-05-31 17:01:07 +08:00
|
|
|
|
2014-08-16 18:16:26 +08:00
|
|
|
/// Heap allocated, dynamically sized vector.
|
2015-02-01 23:15:55 +08:00
|
|
|
#[derive(Eq, PartialEq, Debug, Clone)]
|
2013-08-05 16:13:44 +08:00
|
|
|
pub struct DVec<N> {
|
2013-08-05 15:44:56 +08:00
|
|
|
/// Components of the vector. Contains as much elements as the vector dimension.
|
2014-04-02 04:58:06 +08:00
|
|
|
pub at: Vec<N>
|
2013-05-31 17:01:07 +08:00
|
|
|
}
|
|
|
|
|
2013-09-13 17:11:04 +08:00
|
|
|
impl<N> DVec<N> {
|
|
|
|
/// Creates an uninitialized vec.
|
|
|
|
#[inline]
|
2015-01-10 05:26:05 +08:00
|
|
|
pub unsafe fn new_uninitialized(dim: usize) -> DVec<N> {
|
2014-03-15 19:23:54 +08:00
|
|
|
let mut vec = Vec::with_capacity(dim);
|
2013-12-16 19:04:02 +08:00
|
|
|
vec.set_len(dim);
|
2013-09-13 17:11:04 +08:00
|
|
|
|
|
|
|
DVec {
|
|
|
|
at: vec
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-07 14:43:17 +08:00
|
|
|
impl<N: Clone> DVec<N> {
|
|
|
|
/// Builds a vector filled with a constant.
|
|
|
|
#[inline]
|
2015-01-10 05:26:05 +08:00
|
|
|
pub fn from_elem(dim: usize, elem: N) -> DVec<N> {
|
2015-01-02 06:23:35 +08:00
|
|
|
DVec { at: repeat(elem).take(dim).collect() }
|
2013-09-07 14:43:17 +08:00
|
|
|
}
|
2013-09-22 20:22:17 +08:00
|
|
|
|
|
|
|
/// Builds a vector filled with the components provided by a vector.
|
|
|
|
///
|
|
|
|
/// The vector must have at least `dim` elements.
|
|
|
|
#[inline]
|
2015-01-10 05:26:05 +08:00
|
|
|
pub fn from_slice(dim: usize, vec: &[N]) -> DVec<N> {
|
2013-09-22 20:22:17 +08:00
|
|
|
assert!(dim <= vec.len());
|
|
|
|
|
|
|
|
DVec {
|
2015-01-10 05:46:26 +08:00
|
|
|
at: vec[.. dim].to_vec()
|
2013-09-22 20:22:17 +08:00
|
|
|
}
|
|
|
|
}
|
2013-09-07 14:43:17 +08:00
|
|
|
}
|
|
|
|
|
2013-09-13 16:53:59 +08:00
|
|
|
impl<N> DVec<N> {
|
2013-09-07 14:43:17 +08:00
|
|
|
/// Builds a vector filled with the result of a function.
|
|
|
|
#[inline(always)]
|
2015-01-10 05:26:05 +08:00
|
|
|
pub fn from_fn<F: FnMut(usize) -> N>(dim: usize, mut f: F) -> DVec<N> {
|
2015-01-24 04:46:49 +08:00
|
|
|
DVec { at: (0us .. dim).map(|i| f(i)).collect() }
|
2013-09-07 14:43:17 +08:00
|
|
|
}
|
2014-11-03 05:47:11 +08:00
|
|
|
|
|
|
|
#[inline]
|
2015-01-10 05:26:05 +08:00
|
|
|
pub fn len(&self) -> usize {
|
2014-11-03 05:47:11 +08:00
|
|
|
self.at.len()
|
|
|
|
}
|
2013-09-07 14:43:17 +08:00
|
|
|
}
|
|
|
|
|
2013-08-16 16:14:01 +08:00
|
|
|
impl<N> FromIterator<N> for DVec<N> {
|
2013-09-08 23:19:50 +08:00
|
|
|
#[inline]
|
2015-01-05 02:03:28 +08:00
|
|
|
fn from_iter<I: Iterator<Item = N>>(mut param: I) -> DVec<N> {
|
2014-03-15 19:23:54 +08:00
|
|
|
let mut res = DVec { at: Vec::new() };
|
2013-06-29 05:03:40 +08:00
|
|
|
|
2013-08-05 16:13:44 +08:00
|
|
|
for e in param {
|
|
|
|
res.at.push(e)
|
|
|
|
}
|
2013-06-29 05:03:40 +08:00
|
|
|
|
2013-08-05 15:44:56 +08:00
|
|
|
res
|
|
|
|
}
|
2013-06-29 05:03:40 +08:00
|
|
|
}
|
|
|
|
|
2015-01-10 08:36:13 +08:00
|
|
|
#[cfg(feature="arbitrary")]
|
|
|
|
impl<N: Arbitrary> Arbitrary for DVec<N> {
|
|
|
|
fn arbitrary<G: Gen>(g: &mut G) -> DVec<N> {
|
|
|
|
DVec { at: Arbitrary::arbitrary(g) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:01:07 +08:00
|
|
|
|
2014-12-19 22:33:01 +08:00
|
|
|
dvec_impl!(DVec);
|
2013-05-31 17:01:07 +08:00
|
|
|
|
2014-08-16 18:16:26 +08:00
|
|
|
/// Stack-allocated, dynamically sized vector with a maximum size of 1.
|
|
|
|
pub struct DVec1<N> {
|
2015-01-03 22:19:52 +08:00
|
|
|
at: [N; 1],
|
2015-01-10 05:26:05 +08:00
|
|
|
dim: usize
|
2013-05-31 17:01:07 +08:00
|
|
|
}
|
|
|
|
|
2014-12-19 22:33:01 +08:00
|
|
|
small_dvec_impl!(DVec1, 1, 0);
|
|
|
|
small_dvec_from_impl!(DVec1, 1, ::zero());
|
2013-05-31 17:01:07 +08:00
|
|
|
|
|
|
|
|
2014-08-16 18:16:26 +08:00
|
|
|
/// Stack-allocated, dynamically sized vector with a maximum size of 2.
|
|
|
|
pub struct DVec2<N> {
|
2015-01-03 22:19:52 +08:00
|
|
|
at: [N; 2],
|
2015-01-10 05:26:05 +08:00
|
|
|
dim: usize
|
2014-08-16 18:16:26 +08:00
|
|
|
}
|
2013-05-31 17:01:07 +08:00
|
|
|
|
2014-12-19 22:33:01 +08:00
|
|
|
small_dvec_impl!(DVec2, 2, 0, 1);
|
|
|
|
small_dvec_from_impl!(DVec2, 2, ::zero(), ::zero());
|
2013-05-31 17:01:07 +08:00
|
|
|
|
|
|
|
|
2014-08-16 18:16:26 +08:00
|
|
|
/// Stack-allocated, dynamically sized vector with a maximum size of 3.
|
|
|
|
pub struct DVec3<N> {
|
2015-01-03 22:19:52 +08:00
|
|
|
at: [N; 3],
|
2015-01-10 05:26:05 +08:00
|
|
|
dim: usize
|
2013-05-31 17:01:07 +08:00
|
|
|
}
|
|
|
|
|
2014-12-19 22:33:01 +08:00
|
|
|
small_dvec_impl!(DVec3, 3, 0, 1, 2);
|
|
|
|
small_dvec_from_impl!(DVec3, 3, ::zero(), ::zero(), ::zero());
|
2013-05-31 17:01:07 +08:00
|
|
|
|
|
|
|
|
2014-08-16 18:16:26 +08:00
|
|
|
/// Stack-allocated, dynamically sized vector with a maximum size of 4.
|
|
|
|
pub struct DVec4<N> {
|
2015-01-03 22:19:52 +08:00
|
|
|
at: [N; 4],
|
2015-01-10 05:26:05 +08:00
|
|
|
dim: usize
|
2014-08-16 18:16:26 +08:00
|
|
|
}
|
2013-05-31 17:01:07 +08:00
|
|
|
|
2014-12-19 22:33:01 +08:00
|
|
|
small_dvec_impl!(DVec4, 4, 0, 1, 2, 3);
|
|
|
|
small_dvec_from_impl!(DVec4, 4, ::zero(), ::zero(), ::zero(), ::zero());
|
2013-05-31 17:01:07 +08:00
|
|
|
|
|
|
|
|
2014-08-16 18:16:26 +08:00
|
|
|
/// Stack-allocated, dynamically sized vector with a maximum size of 5.
|
|
|
|
pub struct DVec5<N> {
|
2015-01-03 22:19:52 +08:00
|
|
|
at: [N; 5],
|
2015-01-10 05:26:05 +08:00
|
|
|
dim: usize
|
2013-05-31 17:01:07 +08:00
|
|
|
}
|
|
|
|
|
2014-12-19 22:33:01 +08:00
|
|
|
small_dvec_impl!(DVec5, 5, 0, 1, 2, 3, 4);
|
|
|
|
small_dvec_from_impl!(DVec5, 5, ::zero(), ::zero(), ::zero(), ::zero(), ::zero());
|
2013-05-31 17:01:07 +08:00
|
|
|
|
2013-06-09 20:09:22 +08:00
|
|
|
|
2014-08-16 18:16:26 +08:00
|
|
|
/// Stack-allocated, dynamically sized vector with a maximum size of 6.
|
|
|
|
pub struct DVec6<N> {
|
2015-01-03 22:19:52 +08:00
|
|
|
at: [N; 6],
|
2015-01-10 05:26:05 +08:00
|
|
|
dim: usize
|
2013-05-31 17:01:07 +08:00
|
|
|
}
|
2013-09-15 16:48:18 +08:00
|
|
|
|
2014-12-19 22:33:01 +08:00
|
|
|
small_dvec_impl!(DVec6, 6, 0, 1, 2, 3, 4, 5);
|
|
|
|
small_dvec_from_impl!(DVec6, 6, ::zero(), ::zero(), ::zero(), ::zero(), ::zero(), ::zero());
|