1use std::ops;
2
3use nalgebra::{SimdRealField, SVector};
4
5#[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 pub fn from_element(element: T) -> Self {
15 BVector {
16 data_vector: SVector::<T, N>::from_element(element),
17 }
18 }
19
20 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 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_export]
66macro_rules! bvector {
67 ($($x:expr),+ $(,)?) => {
68 {
69 $crate::BVector::from_array([$( $x ),+])
71 }
72 };
73}
74