vectors/dense/stack/
mod.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
5//! Dense stack-allocated vector representation.
6
7use std::ops::{Add, AddAssign, Sub, SubAssign, Mul, MulAssign, Div, DivAssign};
8
9use num_traits::{NumAssign, MulAdd, MulAddAssign};
10use arrayvec::{Array, ArrayVec};
11
12use {Vector, VectorOps, VectorAssignOps};
13
14mod add;
15mod sub;
16mod mul;
17mod div;
18mod mul_add;
19
20mod dot;
21mod distance;
22
23mod debug;
24mod iter;
25
26pub use self::iter::{Iter, IntoIter};
27
28/// A dense stack-allocated multi-dimensional vector.
29pub struct DenseVector<A>
30where
31    A: Array,
32{
33    components: ArrayVec<A>,
34}
35
36impl<T, A> DenseVector<A>
37where
38    A: Array<Item = T>,
39{
40    /// The number of components in `self`
41    #[inline]
42    pub fn len(&self) -> usize {
43        self.components.len()
44    }
45
46    /// `true` if `self.len() == 0`, otherwise `false`
47    #[inline]
48    pub fn is_empty(&self) -> bool {
49        self.components.is_empty()
50    }
51
52    /// A borrowing iterator over `self`
53    #[inline]
54    pub fn iter<'a>(&'a self) -> Iter<'a, T> {
55        Iter::new(&self.components[..])
56    }
57}
58
59impl<T, A> Clone for DenseVector<A>
60where
61    T: Clone,
62    A: Array<Item = T>,
63{
64    fn clone(&self) -> Self {
65        let components = self.components.clone();
66        Self { components }
67    }
68}
69
70impl<T, A> PartialEq for DenseVector<A>
71where
72    T: PartialEq,
73    A: Array<Item = T>,
74{
75    fn eq(&self, other: &Self) -> bool {
76        self.components.eq(&other.components)
77    }
78}
79
80impl<T, A> From<A> for DenseVector<A>
81where
82    A: Array<Item = T>,
83{
84    #[inline]
85    fn from(items: A) -> Self {
86        Self { components: ArrayVec::from(items) }
87    }
88}
89
90impl<T, A> From<ArrayVec<A>> for DenseVector<A>
91where
92    A: Array<Item = T>,
93{
94    #[inline]
95    fn from(items: ArrayVec<A>) -> Self {
96        Self { components: items }
97    }
98}
99
100impl<V, T, A> VectorOps<V, T> for DenseVector<A>
101where
102    Self: Add<V, Output = Self> + Sub<V, Output = Self> + Mul<T, Output = Self> + Div<T, Output = Self> + MulAdd<T, V, Output = Self>,
103    T: Copy + NumAssign + MulAdd<T, T, Output = T>,
104    A: Copy + Array<Item = T>,
105{}
106
107impl<V, T, A> VectorAssignOps<V, T> for DenseVector<A>
108where
109    Self: AddAssign<V> + SubAssign<V> + MulAssign<T> + DivAssign<T> + MulAddAssign<T, V>,
110    T: Copy + NumAssign + MulAddAssign,
111    A: Copy + Array<Item = T>,
112{}
113
114impl<T, A> Vector<T> for DenseVector<A>
115where
116    Self: VectorOps<Self, T>,
117    T: Copy + NumAssign + MulAdd<T, T, Output = T>,
118    A: Copy + Array<Item = T>,
119{
120    type Scalar = T;
121}
122
123#[cfg(test)]
124mod test {
125    use super::*;
126
127    use expectest::prelude::*;
128
129    #[test]
130    fn from() {
131        const VALUES: [f32; 5] = [0.0, 1.0, 0.5, 0.25, 0.125];
132        let subject = DenseVector::from(VALUES.clone());
133        let expected = ArrayVec::from(VALUES);
134        expect!(subject.components).to(be_equal_to(expected));
135    }
136}