//! Vector with dimensions unknown at compile-time. #![allow(missing_docs)] // we hide doc to not have to document the $trhs double dispatch trait. use std::slice::{Iter, IterMut}; use std::iter::{FromIterator, IntoIterator}; use std::iter::repeat; use std::ops::{Add, Sub, Mul, Div, Neg, Index, IndexMut}; use rand::{self, Rand}; use num::{Zero, One}; use traits::operations::{ApproxEq, Axpy}; use traits::geometry::{Dot, Norm}; use traits::structure::{Iterable, IterableMut, Indexable, Shape, BaseFloat, BaseNum}; #[cfg(feature="arbitrary")] use quickcheck::{Arbitrary, Gen}; /// Heap allocated, dynamically sized vector. #[derive(Eq, PartialEq, Debug, Clone)] pub struct DVec { /// Components of the vector. Contains as much elements as the vector dimension. pub at: Vec } impl DVec { /// Creates an uninitialized vec. #[inline] pub unsafe fn new_uninitialized(dim: usize) -> DVec { let mut vec = Vec::with_capacity(dim); vec.set_len(dim); DVec { at: vec } } } impl DVec { /// Builds a vector filled with a constant. #[inline] pub fn from_elem(dim: usize, elem: N) -> DVec { DVec { at: repeat(elem).take(dim).collect() } } /// Builds a vector filled with the components provided by a vector. /// /// The vector must have at least `dim` elements. #[inline] pub fn from_slice(dim: usize, vec: &[N]) -> DVec { assert!(dim <= vec.len()); DVec { at: vec[.. dim].to_vec() } } } impl DVec { /// Builds a vector filled with the result of a function. #[inline(always)] pub fn from_fn N>(dim: usize, mut f: F) -> DVec { DVec { at: (0..dim).map(|i| f(i)).collect() } } #[inline] pub fn len(&self) -> usize { self.at.len() } } impl FromIterator for DVec { #[inline] fn from_iter>(param: I) -> DVec { DVec { at: param.into_iter().collect() } } } #[cfg(feature="arbitrary")] impl Arbitrary for DVec { fn arbitrary(g: &mut G) -> DVec { DVec { at: Arbitrary::arbitrary(g) } } } dvec_impl!(DVec); /// Stack-allocated, dynamically sized vector with a maximum size of 1. pub struct DVec1 { at: [N; 1], dim: usize } small_dvec_impl!(DVec1, 1, 0); small_dvec_from_impl!(DVec1, 1, ::zero()); /// Stack-allocated, dynamically sized vector with a maximum size of 2. pub struct DVec2 { at: [N; 2], dim: usize } small_dvec_impl!(DVec2, 2, 0, 1); small_dvec_from_impl!(DVec2, 2, ::zero(), ::zero()); /// Stack-allocated, dynamically sized vector with a maximum size of 3. pub struct DVec3 { at: [N; 3], dim: usize } small_dvec_impl!(DVec3, 3, 0, 1, 2); small_dvec_from_impl!(DVec3, 3, ::zero(), ::zero(), ::zero()); /// Stack-allocated, dynamically sized vector with a maximum size of 4. pub struct DVec4 { at: [N; 4], dim: usize } small_dvec_impl!(DVec4, 4, 0, 1, 2, 3); small_dvec_from_impl!(DVec4, 4, ::zero(), ::zero(), ::zero(), ::zero()); /// Stack-allocated, dynamically sized vector with a maximum size of 5. pub struct DVec5 { at: [N; 5], dim: usize } small_dvec_impl!(DVec5, 5, 0, 1, 2, 3, 4); small_dvec_from_impl!(DVec5, 5, ::zero(), ::zero(), ::zero(), ::zero(), ::zero()); /// Stack-allocated, dynamically sized vector with a maximum size of 6. pub struct DVec6 { at: [N; 6], dim: usize } small_dvec_impl!(DVec6, 6, 0, 1, 2, 3, 4, 5); small_dvec_from_impl!(DVec6, 6, ::zero(), ::zero(), ::zero(), ::zero(), ::zero(), ::zero());