orx_v/matrices/v1/
v1_matrix.rs

1use super::layout::{V1LayoutColMajor, V1LayoutRowMajor, V1MatrixLayout};
2use crate::{
3    matrices::{
4        Matrix, MatrixColMajor, MatrixColMajorMut, MatrixMut, MatrixRowMajor, MatrixRowMajorMut,
5    },
6    IntoIdx, NVec, NVecMut, D1, D2,
7};
8use core::marker::PhantomData;
9
10/// A row-major matrix represented by a flat one-dimensional vector `V1`.
11///
12/// Type alias for [`V1Matrix<T, V, V1LayoutRowMajor>`].
13pub type V1MatrixRowMajor<T, V> = V1Matrix<T, V, V1LayoutRowMajor>;
14
15/// A col-major matrix represented by a flat one-dimensional vector `V1`.
16///
17/// Type alias for [`V1Matrix<T, V, V1LayoutColMajor>`].
18pub type V1MatrixColMajor<T, V> = V1Matrix<T, V, V1LayoutColMajor>;
19
20/// A matrix represented by a flat one-dimensional vector `V1`.
21#[derive(Clone)]
22pub struct V1Matrix<T, V, L>
23where
24    V: NVec<D1, T>,
25    L: V1MatrixLayout,
26{
27    layout: L,
28    data: V,
29    phantom: PhantomData<T>,
30}
31
32impl<T, V, L> V1Matrix<T, V, L>
33where
34    V: NVec<D1, T>,
35    L: V1MatrixLayout,
36{
37    pub(super) fn new(layout: L, data: V) -> Self {
38        let (num_rows, num_cols) = (layout.num_rows(), layout.num_cols());
39        assert_eq!(num_rows * num_cols, data.card([]));
40
41        Self {
42            layout,
43            data,
44            phantom: PhantomData,
45        }
46    }
47}
48
49// matrix
50
51impl<T, V, L> Matrix<T> for V1Matrix<T, V, L>
52where
53    V: NVec<D1, T>,
54    L: V1MatrixLayout,
55{
56    #[inline(always)]
57    fn num_rows(&self) -> usize {
58        self.layout.num_rows()
59    }
60
61    #[inline(always)]
62    fn num_cols(&self) -> usize {
63        self.layout.num_cols()
64    }
65
66    #[inline(always)]
67    fn at(&self, idx: impl IntoIdx<D2>) -> T {
68        let [i, j] = idx.into_idx();
69        let idx = self.layout.v1_idx(i, j);
70        self.data.at(idx)
71    }
72
73    fn all(&self) -> impl Iterator<Item = T> {
74        self.data.all()
75    }
76}
77
78impl<T, V, L> MatrixMut<T> for V1Matrix<T, V, L>
79where
80    V: NVecMut<D1, T>,
81    L: V1MatrixLayout,
82{
83    fn at_mut<Idx: IntoIdx<D2>>(&mut self, idx: Idx) -> &mut T {
84        let [i, j] = idx.into_idx();
85        let idx = self.layout.v1_idx(i, j);
86        self.data.at_mut(idx)
87    }
88
89    fn mut_all<F>(&mut self, f: F)
90    where
91        F: FnMut(&mut T),
92    {
93        self.data.mut_all(f);
94    }
95
96    fn reset_all(&mut self, value: T)
97    where
98        T: PartialEq + Copy,
99    {
100        self.data.reset_all(value);
101    }
102}
103
104impl<T, V> MatrixRowMajor<T> for V1Matrix<T, V, V1LayoutRowMajor>
105where
106    V: NVec<D1, T>,
107{
108    fn row(&self, i: usize) -> impl NVec<D1, T> {
109        self.layout.child(&self.data, i)
110    }
111}
112
113impl<T, V> MatrixRowMajorMut<T> for V1Matrix<T, V, V1LayoutRowMajor>
114where
115    V: NVecMut<D1, T>,
116{
117    fn row_mut(&mut self, i: usize) -> impl NVecMut<D1, T> {
118        self.layout.child_mut(&mut self.data, i)
119    }
120}
121
122impl<T, V> MatrixColMajor<T> for V1Matrix<T, V, V1LayoutColMajor>
123where
124    V: NVec<D1, T>,
125{
126    fn col(&self, i: usize) -> impl NVec<D1, T> {
127        self.layout.child(&self.data, i)
128    }
129}
130
131impl<T, V> MatrixColMajorMut<T> for V1Matrix<T, V, V1LayoutColMajor>
132where
133    V: NVecMut<D1, T>,
134{
135    fn col_mut(&mut self, i: usize) -> impl NVecMut<D1, T> {
136        self.layout.child_mut(&mut self.data, i)
137    }
138}