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-05-31 05:14:16 +08:00
|
|
|
use std::rand::Rand;
|
|
|
|
use std::rand;
|
2014-03-22 04:57:58 +08:00
|
|
|
use std::slice::{Items, MutItems};
|
2013-09-09 17:19:54 +08:00
|
|
|
use std::iter::FromIterator;
|
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};
|
2013-05-31 17:01:07 +08:00
|
|
|
|
2014-08-16 18:16:26 +08:00
|
|
|
/// Heap allocated, dynamically sized vector.
|
2014-06-04 04:37:46 +08:00
|
|
|
#[deriving(Eq, PartialEq, Show, 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]
|
|
|
|
pub unsafe fn new_uninitialized(dim: uint) -> 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]
|
|
|
|
pub fn from_elem(dim: uint, elem: N) -> DVec<N> {
|
2014-03-15 19:23:54 +08:00
|
|
|
DVec { at: Vec::from_elem(dim, elem) }
|
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]
|
2014-08-16 18:16:26 +08:00
|
|
|
pub fn from_slice(dim: uint, vec: &[N]) -> DVec<N> {
|
2013-09-22 20:22:17 +08:00
|
|
|
assert!(dim <= vec.len());
|
|
|
|
|
|
|
|
DVec {
|
2014-09-27 15:54:03 +08:00
|
|
|
at: vec.slice_to(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)]
|
2013-11-27 18:16:16 +08:00
|
|
|
pub fn from_fn(dim: uint, f: |uint| -> N) -> DVec<N> {
|
2014-03-15 19:23:54 +08:00
|
|
|
DVec { at: Vec::from_fn(dim, |i| f(i)) }
|
2013-09-07 14:43:17 +08:00
|
|
|
}
|
2014-11-03 05:47:11 +08:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn len(&self) -> uint {
|
|
|
|
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]
|
2014-04-02 04:58:06 +08:00
|
|
|
fn from_iter<I: Iterator<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
|
|
|
}
|
|
|
|
|
2013-05-31 17:01:07 +08:00
|
|
|
|
2014-11-26 21:17:34 +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> {
|
|
|
|
at: [N, ..1],
|
|
|
|
dim: uint
|
2013-05-31 17:01:07 +08:00
|
|
|
}
|
|
|
|
|
2014-11-26 21:17:34 +08:00
|
|
|
small_dvec_impl!(DVec1, 1, 0)
|
2014-11-16 21:04:15 +08:00
|
|
|
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> {
|
|
|
|
at: [N, ..2],
|
|
|
|
dim: uint
|
|
|
|
}
|
2013-05-31 17:01:07 +08:00
|
|
|
|
2014-11-26 21:17:34 +08:00
|
|
|
small_dvec_impl!(DVec2, 2, 0, 1)
|
2014-11-16 21:04:15 +08:00
|
|
|
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> {
|
|
|
|
at: [N, ..3],
|
|
|
|
dim: uint
|
2013-05-31 17:01:07 +08:00
|
|
|
}
|
|
|
|
|
2014-11-26 21:17:34 +08:00
|
|
|
small_dvec_impl!(DVec3, 3, 0, 1, 2)
|
2014-11-16 21:04:15 +08:00
|
|
|
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> {
|
|
|
|
at: [N, ..4],
|
|
|
|
dim: uint
|
|
|
|
}
|
2013-05-31 17:01:07 +08:00
|
|
|
|
2014-11-26 21:17:34 +08:00
|
|
|
small_dvec_impl!(DVec4, 4, 0, 1, 2, 3)
|
2014-11-16 21:04:15 +08:00
|
|
|
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> {
|
|
|
|
at: [N, ..5],
|
|
|
|
dim: uint
|
2013-05-31 17:01:07 +08:00
|
|
|
}
|
|
|
|
|
2014-11-26 21:17:34 +08:00
|
|
|
small_dvec_impl!(DVec5, 5, 0, 1, 2, 3, 4)
|
2014-11-16 21:04:15 +08:00
|
|
|
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> {
|
|
|
|
at: [N, ..6],
|
|
|
|
dim: uint
|
2013-05-31 17:01:07 +08:00
|
|
|
}
|
2013-09-15 16:48:18 +08:00
|
|
|
|
2014-11-26 21:17:34 +08:00
|
|
|
small_dvec_impl!(DVec6, 6, 0, 1, 2, 3, 4, 5)
|
2014-11-16 21:04:15 +08:00
|
|
|
small_dvec_from_impl!(DVec6, 6, ::zero(), ::zero(), ::zero(), ::zero(), ::zero(), ::zero())
|