orx_v/matrices/v2/
v2_col_major.rs

1use super::super::{matrix::Matrix, MatrixColMajor, MatrixColMajorMut};
2use crate::{matrices::MatrixMut, IntoIdx, NVec, NVecMut, D1, D2};
3use core::marker::PhantomData;
4
5/// A column major matrix.
6pub struct V2MatrixColMajor<T, V>
7where
8    V: NVec<D2, T>,
9{
10    data: V,
11    phantom: PhantomData<T>,
12}
13
14impl<T, V> V2MatrixColMajor<T, V>
15where
16    V: NVec<D2, T>,
17{
18    pub(super) fn new(data: V) -> Self {
19        assert!(
20            data.is_rectangular(),
21            "D2 vector to be converted to Matrix does not have rectangular cardinality."
22        );
23        Self {
24            data,
25            phantom: PhantomData,
26        }
27    }
28}
29
30// matrix
31
32impl<T, V> Matrix<T> for V2MatrixColMajor<T, V>
33where
34    V: NVec<D2, T>,
35{
36    #[inline(always)]
37    fn num_rows(&self) -> usize {
38        match self.num_cols() {
39            0 => 0,
40            _ => self.data.card([0]),
41        }
42    }
43
44    #[inline(always)]
45    fn num_cols(&self) -> usize {
46        self.data.card([])
47    }
48
49    #[inline(always)]
50    fn at(&self, idx: impl IntoIdx<D2>) -> T {
51        let [i, j] = idx.into_idx();
52        self.data.at([j, i])
53    }
54
55    fn all(&self) -> impl Iterator<Item = T> {
56        self.data.all()
57    }
58}
59
60impl<T, V> MatrixMut<T> for V2MatrixColMajor<T, V>
61where
62    V: NVecMut<D2, T>,
63{
64    #[inline(always)]
65    fn at_mut<Idx: IntoIdx<D2>>(&mut self, idx: Idx) -> &mut T {
66        let [i, j] = idx.into_idx();
67        self.data.at_mut([j, i])
68    }
69
70    fn mut_all<F>(&mut self, f: F)
71    where
72        F: FnMut(&mut T),
73    {
74        self.data.mut_all(f);
75    }
76
77    fn reset_all(&mut self, value: T)
78    where
79        T: PartialEq + Copy,
80    {
81        self.data.reset_all(value);
82    }
83}
84
85impl<T, V> MatrixColMajor<T> for V2MatrixColMajor<T, V>
86where
87    V: NVec<D2, T>,
88{
89    fn col(&self, i: usize) -> impl NVec<D1, T> {
90        self.data.child(i)
91    }
92}
93
94impl<T, V> MatrixColMajorMut<T> for V2MatrixColMajor<T, V>
95where
96    V: NVecMut<D2, T>,
97{
98    fn col_mut(&mut self, i: usize) -> impl NVecMut<D1, T> {
99        self.data.child_mut(i)
100    }
101}