1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

//! Dense stack-allocated vector representation.

use std::ops::{Add, AddAssign, Sub, SubAssign, Mul, MulAssign, Div, DivAssign};

use num_traits::{NumAssign, MulAdd, MulAddAssign};
use arrayvec::{Array, ArrayVec};

use {Vector, VectorOps, VectorAssignOps};

mod add;
mod sub;
mod mul;
mod div;
mod mul_add;

mod dot;
mod distance;

mod debug;
mod iter;

pub use self::iter::{Iter, IntoIter};

/// A dense stack-allocated multi-dimensional vector.
pub struct DenseVector<A>
where
    A: Array,
{
    components: ArrayVec<A>,
}

impl<T, A> DenseVector<A>
where
    A: Array<Item = T>,
{
    /// The number of components in `self`
    #[inline]
    pub fn len(&self) -> usize {
        self.components.len()
    }

    /// `true` if `self.len() == 0`, otherwise `false`
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.components.is_empty()
    }

    /// A borrowing iterator over `self`
    #[inline]
    pub fn iter<'a>(&'a self) -> Iter<'a, T> {
        Iter::new(&self.components[..])
    }
}

impl<T, A> Clone for DenseVector<A>
where
    T: Clone,
    A: Array<Item = T>,
{
    fn clone(&self) -> Self {
        let components = self.components.clone();
        Self { components }
    }
}

impl<T, A> PartialEq for DenseVector<A>
where
    T: PartialEq,
    A: Array<Item = T>,
{
    fn eq(&self, other: &Self) -> bool {
        self.components.eq(&other.components)
    }
}

impl<T, A> From<A> for DenseVector<A>
where
    A: Array<Item = T>,
{
    #[inline]
    fn from(items: A) -> Self {
        Self { components: ArrayVec::from(items) }
    }
}

impl<T, A> From<ArrayVec<A>> for DenseVector<A>
where
    A: Array<Item = T>,
{
    #[inline]
    fn from(items: ArrayVec<A>) -> Self {
        Self { components: items }
    }
}

impl<V, T, A> VectorOps<V, T> for DenseVector<A>
where
    Self: Add<V, Output = Self> + Sub<V, Output = Self> + Mul<T, Output = Self> + Div<T, Output = Self> + MulAdd<T, V, Output = Self>,
    T: Copy + NumAssign + MulAdd<T, T, Output = T>,
    A: Copy + Array<Item = T>,
{}

impl<V, T, A> VectorAssignOps<V, T> for DenseVector<A>
where
    Self: AddAssign<V> + SubAssign<V> + MulAssign<T> + DivAssign<T> + MulAddAssign<T, V>,
    T: Copy + NumAssign + MulAddAssign,
    A: Copy + Array<Item = T>,
{}

impl<T, A> Vector<T> for DenseVector<A>
where
    Self: VectorOps<Self, T>,
    T: Copy + NumAssign + MulAdd<T, T, Output = T>,
    A: Copy + Array<Item = T>,
{
    type Scalar = T;
}

#[cfg(test)]
mod test {
    use super::*;

    use expectest::prelude::*;

    #[test]
    fn from() {
        const VALUES: [f32; 5] = [0.0, 1.0, 0.5, 0.25, 0.125];
        let subject = DenseVector::from(VALUES.clone());
        let expected = ArrayVec::from(VALUES);
        expect!(subject.components).to(be_equal_to(expected));
    }
}