orx_v/matrices/v2/
v2_row_major.rs

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