orx_v/matrices/v1/
layout.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
use super::{col::Col, row::Row};
use crate::{NVec, NVecMut, D1};

/// Layout for matrices with an underlying flat vector of `D1`.
pub trait V1MatrixLayout: Clone {
    /// Number of rows.
    fn num_rows(&self) -> usize;

    /// Number of columns.
    fn num_cols(&self) -> usize;

    /// Number of primary children:
    /// * number of rows if row-major,
    /// * number of columns if col-major.
    fn num_children(&self) -> usize;

    /// Number of children of the transpose of the matrix:
    /// * number of rows if col-major,
    /// * number of columns if row-major.
    fn num_children_secondary(&self) -> usize;

    /// Transformation of the row and column indices (`i`, `j`) into a one dimensional
    /// index for the underlying data.
    fn v1_idx(&self, i: usize, j: usize) -> usize;

    /// Child of the matrix:
    /// * row if row-major,
    /// * column if col-major.
    fn child<T, V>(&self, data: V, first_idx: usize) -> impl NVec<D1, T>
    where
        V: NVec<D1, T>;

    /// Mutable child of the matrix:
    /// * row if row-major,
    /// * column if col-major.
    fn child_mut<T, V>(&self, data: V, first_idx: usize) -> impl NVecMut<D1, T>
    where
        V: NVecMut<D1, T>;
}

// row major

/// Row major layout.
#[derive(Clone)]
pub struct V1LayoutRowMajor {
    num_rows: usize,
    num_cols: usize,
}

impl V1LayoutRowMajor {
    pub(super) fn new(num_rows: usize, num_cols: usize) -> Self {
        Self { num_rows, num_cols }
    }
}

impl V1MatrixLayout for V1LayoutRowMajor {
    #[inline(always)]
    fn num_rows(&self) -> usize {
        self.num_rows
    }

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

    #[inline(always)]
    fn num_children(&self) -> usize {
        self.num_rows
    }

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

    #[inline(always)]
    fn v1_idx(&self, i: usize, j: usize) -> usize {
        self.num_cols * i + j
    }

    fn child<T, V>(&self, data: V, first_idx: usize) -> impl NVec<D1, T>
    where
        V: NVec<D1, T>,
    {
        Row::new(data, self.clone(), first_idx)
    }

    fn child_mut<T, V>(&self, data: V, first_idx: usize) -> impl NVecMut<D1, T>
    where
        V: NVecMut<D1, T>,
    {
        Row::new(data, self.clone(), first_idx)
    }
}

// col major

/// Column major layout.
#[derive(Clone)]
pub struct V1LayoutColMajor {
    num_rows: usize,
    num_cols: usize,
}

impl V1LayoutColMajor {
    pub(super) fn new(num_rows: usize, num_cols: usize) -> Self {
        Self { num_rows, num_cols }
    }
}

impl V1MatrixLayout for V1LayoutColMajor {
    #[inline(always)]
    fn num_rows(&self) -> usize {
        self.num_rows
    }

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

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

    #[inline(always)]
    fn num_children_secondary(&self) -> usize {
        self.num_rows
    }

    #[inline(always)]
    fn v1_idx(&self, i: usize, j: usize) -> usize {
        self.num_rows * j + i
    }

    fn child<T, V>(&self, data: V, first_idx: usize) -> impl NVec<D1, T>
    where
        V: NVec<D1, T>,
    {
        Col::new(data, self.clone(), first_idx)
    }

    fn child_mut<T, V>(&self, data: V, first_idx: usize) -> impl NVecMut<D1, T>
    where
        V: NVecMut<D1, T>,
    {
        Col::new(data, self.clone(), first_idx)
    }
}