Skip to main content

only_brain/
bvector.rs

1use std::ops;
2
3use nalgebra::{SimdRealField, SVector};
4
5/// This module provides a vector type `BVector` that is optimized for compile-time
6/// dimension and uses SIMD operations for performance.
7#[derive(Debug, Clone, PartialEq)]
8pub struct BVector<T: SimdRealField + Copy, const N: usize> {
9    pub data_vector: SVector<T, N>,
10}
11
12impl<T: SimdRealField + Copy, const N: usize> BVector<T, N> {
13    /// Constructs a vector filled with `element` of a dimension `N`.
14    pub fn from_element(element: T) -> Self {
15        BVector {
16            data_vector: SVector::<T, N>::from_element(element),
17        }
18    }
19
20    /// Constructs the vector from fixed-size array.
21    pub fn from_array(data: [T; N]) -> Self
22    where
23        T: Copy,
24    {
25        BVector {
26            data_vector: SVector::<T, N>::from_row_slice(&data),
27        }
28    }
29
30    pub fn dot(&self, other: &Self) -> T {
31        self.data_vector.dot(&other.data_vector)
32    }
33
34    pub fn len(&self) -> usize {
35        N
36    }
37
38    /// Whether the vector has no elements, which is only the case when `N` is 0.
39    pub fn is_empty(&self) -> bool {
40        N == 0
41    }
42
43    pub fn get(&self, index: usize) -> T {
44        assert!(index < N, "Index out of bounds");
45        self.data_vector[index]
46    }
47}
48
49impl<T: SimdRealField + Copy, const N: usize> ops::Add for BVector<T, N> {
50    type Output = BVector<T, N>;
51
52    fn add(self, rhs: Self) -> Self::Output {
53        BVector {
54            data_vector: self.data_vector + rhs.data_vector,
55        }
56    }
57}
58
59/// Macro to construct a `BVector` with compile-time dimension inferred from the
60/// number of elements provided.
61///
62/// Example:
63/// - `let v = bvector![1.0, 2.0, 3.0]; // BVector<f64, 3>`
64/// - `let v = bvector![2.0, -1.5]; // BVector<f64, 2>`
65#[macro_export]
66macro_rules! bvector {
67    ($($x:expr),+ $(,)?) => {
68        {
69            // Let the compiler infer both element type `T` and length `N`.
70            $crate::BVector::from_array([$( $x ),+])
71        }
72    };
73}
74