orx_v/matrices/v2/
v2_col_major.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
use super::super::{matrix::Matrix, MatrixColMajor, MatrixColMajorMut};
use crate::{matrices::MatrixMut, IntoIdx, NVec, NVecMut, D1, D2};
use core::marker::PhantomData;

/// A column major matrix.
pub struct V2MatrixColMajor<T, V>
where
    V: NVec<D2, T>,
{
    data: V,
    phantom: PhantomData<T>,
}

impl<T, V> V2MatrixColMajor<T, V>
where
    V: NVec<D2, T>,
{
    pub(super) fn new(data: V) -> Self {
        assert!(
            data.is_rectangular(),
            "D2 vector to be converted to Matrix does not have rectangular cardinality."
        );
        Self {
            data,
            phantom: PhantomData,
        }
    }
}

// matrix

impl<T, V> Matrix<T> for V2MatrixColMajor<T, V>
where
    V: NVec<D2, T>,
{
    #[inline(always)]
    fn num_rows(&self) -> usize {
        match self.num_cols() {
            0 => 0,
            _ => self.data.card([0]),
        }
    }

    #[inline(always)]
    fn num_cols(&self) -> usize {
        self.data.card([])
    }

    #[inline(always)]
    fn at(&self, idx: impl IntoIdx<D2>) -> T {
        let [i, j] = idx.into_idx();
        self.data.at([j, i])
    }

    fn all(&self) -> impl Iterator<Item = T> {
        self.data.all()
    }
}

impl<T, V> MatrixMut<T> for V2MatrixColMajor<T, V>
where
    V: NVecMut<D2, T>,
{
    #[inline(always)]
    fn at_mut<Idx: IntoIdx<D2>>(&mut self, idx: Idx) -> &mut T {
        let [i, j] = idx.into_idx();
        self.data.at_mut([j, i])
    }

    fn mut_all<F>(&mut self, f: F)
    where
        F: FnMut(&mut T),
    {
        self.data.mut_all(f);
    }

    fn reset_all(&mut self, value: T)
    where
        T: PartialEq + Copy,
    {
        self.data.reset_all(value);
    }
}

impl<T, V> MatrixColMajor<T> for V2MatrixColMajor<T, V>
where
    V: NVec<D2, T>,
{
    fn col(&self, i: usize) -> impl NVec<D1, T> {
        self.data.child(i)
    }
}

impl<T, V> MatrixColMajorMut<T> for V2MatrixColMajor<T, V>
where
    V: NVecMut<D2, T>,
{
    fn col_mut(&mut self, i: usize) -> impl NVecMut<D1, T> {
        self.data.child_mut(i)
    }
}