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-02-18 19:13:40 +08:00
|
|
|
use std::num::{Zero, One, Float};
|
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};
|
2014-01-10 03:48:30 +08:00
|
|
|
use traits::operations::ApproxEq;
|
2013-09-09 17:19:54 +08:00
|
|
|
use std::iter::FromIterator;
|
2013-10-14 16:22:32 +08:00
|
|
|
use traits::geometry::{Dot, Norm};
|
2014-10-26 17:46:51 +08:00
|
|
|
use traits::structure::{Iterable, IterableMut, Indexable, Shape};
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-08-16 18:16:26 +08:00
|
|
|
impl<N> Collection for DVec<N> {
|
2013-08-05 15:44:56 +08:00
|
|
|
#[inline]
|
2014-08-16 18:16:26 +08:00
|
|
|
fn len(&self) -> uint {
|
|
|
|
self.at.len()
|
2013-07-02 18:00:55 +08:00
|
|
|
}
|
2013-05-31 17:01:07 +08:00
|
|
|
}
|
|
|
|
|
2014-08-16 18:16:26 +08:00
|
|
|
dvec_impl!(DVec, DVecMulRhs, DVecDivRhs, DVecAddRhs, DVecSubRhs)
|
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-08-16 18:16:26 +08:00
|
|
|
small_dvec_impl!(DVec1, 1, DVec1MulRhs, DVec1DivRhs, DVec1AddRhs, DVec1SubRhs, 0)
|
|
|
|
small_dvec_from_impl!(DVec1, 1, 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 2.
|
|
|
|
pub struct DVec2<N> {
|
|
|
|
at: [N, ..2],
|
|
|
|
dim: uint
|
|
|
|
}
|
2013-05-31 17:01:07 +08:00
|
|
|
|
2014-08-16 18:16:26 +08:00
|
|
|
small_dvec_impl!(DVec2, 2, DVec2MulRhs, DVec2DivRhs, DVec2AddRhs, DVec2SubRhs, 0, 1)
|
|
|
|
small_dvec_from_impl!(DVec2, 2, 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 3.
|
|
|
|
pub struct DVec3<N> {
|
|
|
|
at: [N, ..3],
|
|
|
|
dim: uint
|
2013-05-31 17:01:07 +08:00
|
|
|
}
|
|
|
|
|
2014-08-16 18:16:26 +08:00
|
|
|
small_dvec_impl!(DVec3, 3, DVec3MulRhs, DVec3DivRhs, DVec3AddRhs, DVec3SubRhs, 0, 1, 2)
|
|
|
|
small_dvec_from_impl!(DVec3, 3, Zero::zero(), 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 4.
|
|
|
|
pub struct DVec4<N> {
|
|
|
|
at: [N, ..4],
|
|
|
|
dim: uint
|
|
|
|
}
|
2013-05-31 17:01:07 +08:00
|
|
|
|
2014-08-16 18:16:26 +08:00
|
|
|
small_dvec_impl!(DVec4, 4, DVec4MulRhs, DVec4DivRhs, DVec4AddRhs, DVec4SubRhs, 0, 1, 2, 3)
|
|
|
|
small_dvec_from_impl!(DVec4, 4, Zero::zero(), Zero::zero(), 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-08-16 18:16:26 +08:00
|
|
|
small_dvec_impl!(DVec5, 5, DVec5MulRhs, DVec5DivRhs, DVec5AddRhs, DVec5SubRhs, 0, 1, 2, 3, 4)
|
|
|
|
small_dvec_from_impl!(DVec5, 5, Zero::zero(), Zero::zero(), Zero::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-08-16 18:16:26 +08:00
|
|
|
small_dvec_impl!(DVec6, 6, DVec6MulRhs, DVec6DivRhs, DVec6AddRhs, DVec6SubRhs, 0, 1, 2, 3, 4, 5)
|
|
|
|
small_dvec_from_impl!(DVec6, 6, Zero::zero(), Zero::zero(), Zero::zero(), Zero::zero(), Zero::zero(), Zero::zero())
|