orx_v/matrices/v1/
layout.rs

1use super::{col::Col, row::Row};
2use crate::{NVec, NVecMut, D1};
3
4/// Layout for matrices with an underlying flat vector of `D1`.
5pub trait V1MatrixLayout: Clone {
6    /// Number of rows.
7    fn num_rows(&self) -> usize;
8
9    /// Number of columns.
10    fn num_cols(&self) -> usize;
11
12    /// Number of primary children:
13    /// * number of rows if row-major,
14    /// * number of columns if col-major.
15    fn num_children(&self) -> usize;
16
17    /// Number of children of the transpose of the matrix:
18    /// * number of rows if col-major,
19    /// * number of columns if row-major.
20    fn num_children_secondary(&self) -> usize;
21
22    /// Transformation of the row and column indices (`i`, `j`) into a one dimensional
23    /// index for the underlying data.
24    fn v1_idx(&self, i: usize, j: usize) -> usize;
25
26    /// Child of the matrix:
27    /// * row if row-major,
28    /// * column if col-major.
29    fn child<T, V>(&self, data: V, first_idx: usize) -> impl NVec<D1, T>
30    where
31        V: NVec<D1, T>;
32
33    /// Mutable child of the matrix:
34    /// * row if row-major,
35    /// * column if col-major.
36    fn child_mut<T, V>(&self, data: V, first_idx: usize) -> impl NVecMut<D1, T>
37    where
38        V: NVecMut<D1, T>;
39}
40
41// row major
42
43/// Row major layout.
44#[derive(Clone)]
45pub struct V1LayoutRowMajor {
46    num_rows: usize,
47    num_cols: usize,
48}
49
50impl V1LayoutRowMajor {
51    pub(super) fn new(num_rows: usize, num_cols: usize) -> Self {
52        Self { num_rows, num_cols }
53    }
54}
55
56impl V1MatrixLayout for V1LayoutRowMajor {
57    #[inline(always)]
58    fn num_rows(&self) -> usize {
59        self.num_rows
60    }
61
62    #[inline(always)]
63    fn num_cols(&self) -> usize {
64        self.num_cols
65    }
66
67    #[inline(always)]
68    fn num_children(&self) -> usize {
69        self.num_rows
70    }
71
72    #[inline(always)]
73    fn num_children_secondary(&self) -> usize {
74        self.num_cols
75    }
76
77    #[inline(always)]
78    fn v1_idx(&self, i: usize, j: usize) -> usize {
79        self.num_cols * i + j
80    }
81
82    fn child<T, V>(&self, data: V, first_idx: usize) -> impl NVec<D1, T>
83    where
84        V: NVec<D1, T>,
85    {
86        Row::new(data, self.clone(), first_idx)
87    }
88
89    fn child_mut<T, V>(&self, data: V, first_idx: usize) -> impl NVecMut<D1, T>
90    where
91        V: NVecMut<D1, T>,
92    {
93        Row::new(data, self.clone(), first_idx)
94    }
95}
96
97// col major
98
99/// Column major layout.
100#[derive(Clone)]
101pub struct V1LayoutColMajor {
102    num_rows: usize,
103    num_cols: usize,
104}
105
106impl V1LayoutColMajor {
107    pub(super) fn new(num_rows: usize, num_cols: usize) -> Self {
108        Self { num_rows, num_cols }
109    }
110}
111
112impl V1MatrixLayout for V1LayoutColMajor {
113    #[inline(always)]
114    fn num_rows(&self) -> usize {
115        self.num_rows
116    }
117
118    #[inline(always)]
119    fn num_cols(&self) -> usize {
120        self.num_cols
121    }
122
123    #[inline(always)]
124    fn num_children(&self) -> usize {
125        self.num_cols
126    }
127
128    #[inline(always)]
129    fn num_children_secondary(&self) -> usize {
130        self.num_rows
131    }
132
133    #[inline(always)]
134    fn v1_idx(&self, i: usize, j: usize) -> usize {
135        self.num_rows * j + i
136    }
137
138    fn child<T, V>(&self, data: V, first_idx: usize) -> impl NVec<D1, T>
139    where
140        V: NVec<D1, T>,
141    {
142        Col::new(data, self.clone(), first_idx)
143    }
144
145    fn child_mut<T, V>(&self, data: V, first_idx: usize) -> impl NVecMut<D1, T>
146    where
147        V: NVecMut<D1, T>,
148    {
149        Col::new(data, self.clone(), first_idx)
150    }
151}