orx_v/matrices/
matrix_row_major.rs

1use super::{Matrix, MatrixMut};
2use crate::{NVec, NVecMut, D1};
3
4/// A row major matrix.
5///
6/// Say i represents row-index and j represents col-index.
7/// In a row-major matrix:
8/// * it is more efficient to iterate first over i, and then over j,
9/// * [`row(i)`] often (1) returns a vector over a contagious memory location.
10///
11/// *(1) When the data is represented by a complete allocation; however, recall that
12/// it is possible to use a function or a sparse vector backed up with a lookup as
13/// the underlying vector of the matrix.*
14pub trait MatrixRowMajor<T>: Matrix<T> {
15    /// Returns the `i`-th row of the matrix which is a `D1` vector.
16    fn row(&self, i: usize) -> impl NVec<D1, T>;
17
18    /// Returns an iterator over the rows of the matrix.
19    fn rows(&self) -> impl Iterator<Item = impl NVec<D1, T>> {
20        (0..self.num_rows()).map(|i| self.row(i))
21    }
22}
23
24impl<T, M: MatrixRowMajor<T>> MatrixRowMajor<T> for &M {
25    fn row(&self, j: usize) -> impl NVec<D1, T> {
26        <M as MatrixRowMajor<T>>::row(self, j)
27    }
28}
29
30impl<T, M: MatrixRowMajor<T>> MatrixRowMajor<T> for &mut M {
31    fn row(&self, j: usize) -> impl NVec<D1, T> {
32        <M as MatrixRowMajor<T>>::row(self, j)
33    }
34}
35
36// mut
37
38/// A mutable row major matrix.
39pub trait MatrixRowMajorMut<T>: MatrixRowMajor<T> + MatrixMut<T> {
40    /// Returns a mutable reference to the `i`-th row of the matrix which is a `D1` vector.
41    fn row_mut(&mut self, i: usize) -> impl NVecMut<D1, T>;
42}
43
44impl<T, M: MatrixRowMajorMut<T>> MatrixRowMajorMut<T> for &mut M {
45    fn row_mut(&mut self, j: usize) -> impl NVecMut<D1, T> {
46        <M as MatrixRowMajorMut<T>>::row_mut(self, j)
47    }
48}