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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
use crate::{seal::Seal, ColMut, ColRef, MatMut, MatRef, RowMut, RowRef};
use assert2::{assert as fancy_assert, debug_assert as fancy_debug_assert};
use reborrow::*;

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum Diag {
    /// Do not include diagonal of matrix
    Skip,
    /// Include diagonal of matrix
    Include,
}

pub trait CwiseMat<'short, Outlives = &'short Self>: Seal {
    type Item;

    fn transpose(self) -> Self;
    fn nrows(&self) -> usize;
    fn ncols(&self) -> usize;
    fn is_col_major(&self) -> bool;
    fn is_row_major(&self) -> bool;
    unsafe fn get_unchecked(&'short mut self, i: usize, j: usize) -> Self::Item;
    unsafe fn get_col_major_unchecked(&'short mut self, i: usize, j: usize) -> Self::Item;
    unsafe fn get_row_major_unchecked(&'short mut self, i: usize, j: usize) -> Self::Item;
}

pub trait CwiseCol<'short, Outlives = &'short Self>: Seal {
    type Item;

    fn nrows(&self) -> usize;
    fn is_contiguous(&self) -> bool;
    unsafe fn get_unchecked(&'short mut self, i: usize) -> Self::Item;
    unsafe fn get_contiguous_unchecked(&'short mut self, i: usize) -> Self::Item;
}

pub trait CwiseRow<'short, Outlives = &'short Self>: Seal {
    type Item;

    fn ncols(&self) -> usize;
    fn is_contiguous(&self) -> bool;
    unsafe fn get_unchecked(&'short mut self, i: usize) -> Self::Item;
    unsafe fn get_contiguous_unchecked(&'short mut self, i: usize) -> Self::Item;
}

/// Simple wrapper indicating that values contained in this matrix may be uninitialized, and thus
/// references to them shouldn't be created.
pub struct MatUninit<'a, T>(pub MatMut<'a, T>);

/// Simple wrapper indicating that values contained in this column may be uninitialized, and thus
/// references to them shouldn't be created.
pub struct ColUninit<'a, T>(pub ColMut<'a, T>);

/// Simple wrapper indicating that values contained in this row may be uninitialized, and thus
/// references to them shouldn't be created.
pub struct RowUninit<'a, T>(pub RowMut<'a, T>);

impl<'a, T> MatUninit<'a, T> {
    #[inline]
    pub fn cwise(self) -> ZipMat<(Self,)> {
        ZipMat { tuple: (self,) }
    }
}
impl<'a, T> ColUninit<'a, T> {
    #[inline]
    pub fn cwise(self) -> ZipCol<(Self,)> {
        ZipCol { tuple: (self,) }
    }
}
impl<'a, T> RowUninit<'a, T> {
    #[inline]
    pub fn cwise(self) -> ZipRow<(Self,)> {
        ZipRow { tuple: (self,) }
    }
}

impl<'a, T> crate::seal::Seal for MatUninit<'a, T> {}
impl<'a, T> crate::seal::Seal for ColUninit<'a, T> {}
impl<'a, T> crate::seal::Seal for RowUninit<'a, T> {}

impl<'short, 'a, T> CwiseMat<'short> for MatRef<'a, T> {
    type Item = &'short T;
    #[inline]
    fn nrows(&self) -> usize {
        self.nrows()
    }
    #[inline]
    fn ncols(&self) -> usize {
        self.ncols()
    }
    #[inline]
    fn is_col_major(&self) -> bool {
        self.row_stride() == 1
    }
    #[inline]
    fn is_row_major(&self) -> bool {
        self.col_stride() == 1
    }
    #[inline]
    unsafe fn get_unchecked(&'short mut self, i: usize, j: usize) -> Self::Item {
        (*self).rb().get_unchecked(i, j)
    }
    #[inline]
    unsafe fn get_col_major_unchecked(&'short mut self, i: usize, j: usize) -> Self::Item {
        &*(*self).rb().ptr_in_bounds_at_unchecked(0, j).add(i)
    }
    #[inline]
    unsafe fn get_row_major_unchecked(&'short mut self, i: usize, j: usize) -> Self::Item {
        &*(*self).rb().ptr_in_bounds_at_unchecked(i, 0).add(j)
    }
    #[inline]
    fn transpose(self) -> Self {
        self.transpose()
    }
}

impl<'short, 'a, T> CwiseMat<'short> for MatMut<'a, T> {
    type Item = &'short mut T;
    #[inline]
    fn nrows(&self) -> usize {
        self.nrows()
    }
    #[inline]
    fn ncols(&self) -> usize {
        self.ncols()
    }
    #[inline]
    fn is_col_major(&self) -> bool {
        self.row_stride() == 1
    }
    #[inline]
    fn is_row_major(&self) -> bool {
        self.col_stride() == 1
    }
    #[inline]
    unsafe fn get_unchecked(&'short mut self, i: usize, j: usize) -> Self::Item {
        (*self).rb_mut().get_unchecked(i, j)
    }
    #[inline]
    unsafe fn get_col_major_unchecked(&'short mut self, i: usize, j: usize) -> Self::Item {
        &mut *(*self).rb_mut().ptr_in_bounds_at_unchecked(0, j).add(i)
    }
    #[inline]
    unsafe fn get_row_major_unchecked(&'short mut self, i: usize, j: usize) -> Self::Item {
        &mut *(*self).rb_mut().ptr_in_bounds_at_unchecked(i, 0).add(j)
    }
    #[inline]
    fn transpose(self) -> Self {
        self.transpose()
    }
}

impl<'short, 'a, T> CwiseMat<'short> for MatUninit<'a, T> {
    type Item = *mut T;
    #[inline]
    fn nrows(&self) -> usize {
        self.0.nrows()
    }
    #[inline]
    fn ncols(&self) -> usize {
        self.0.ncols()
    }
    #[inline]
    fn is_col_major(&self) -> bool {
        self.0.row_stride() == 1
    }
    #[inline]
    fn is_row_major(&self) -> bool {
        self.0.col_stride() == 1
    }
    #[inline]
    unsafe fn get_unchecked(&'short mut self, i: usize, j: usize) -> Self::Item {
        self.0.rb_mut().ptr_in_bounds_at_unchecked(i, j)
    }
    #[inline]
    unsafe fn get_col_major_unchecked(&'short mut self, i: usize, j: usize) -> Self::Item {
        self.0.rb_mut().ptr_in_bounds_at_unchecked(0, j).add(i)
    }
    #[inline]
    unsafe fn get_row_major_unchecked(&'short mut self, i: usize, j: usize) -> Self::Item {
        self.0.rb_mut().ptr_in_bounds_at_unchecked(i, 0).add(j)
    }
    #[inline]
    fn transpose(self) -> Self {
        MatUninit(self.0.transpose())
    }
}

impl<'short, 'a, T> CwiseCol<'short> for ColRef<'a, T> {
    type Item = &'short T;
    #[inline]
    fn nrows(&self) -> usize {
        self.nrows()
    }
    #[inline]
    fn is_contiguous(&self) -> bool {
        self.row_stride() == 1
    }
    #[inline]
    unsafe fn get_unchecked(&'short mut self, i: usize) -> Self::Item {
        (*self).rb().get_unchecked(i)
    }
    #[inline]
    unsafe fn get_contiguous_unchecked(&'short mut self, i: usize) -> Self::Item {
        &*(*self).rb().as_ptr().add(i)
    }
}

impl<'short, 'a, T> CwiseCol<'short> for ColMut<'a, T> {
    type Item = &'short mut T;
    #[inline]
    fn nrows(&self) -> usize {
        self.nrows()
    }
    #[inline]
    fn is_contiguous(&self) -> bool {
        self.row_stride() == 1
    }
    #[inline]
    unsafe fn get_unchecked(&'short mut self, i: usize) -> Self::Item {
        (*self).rb_mut().get_unchecked(i)
    }
    #[inline]
    unsafe fn get_contiguous_unchecked(&'short mut self, i: usize) -> Self::Item {
        &mut *(*self).rb_mut().as_ptr().add(i)
    }
}

impl<'short, 'a, T> CwiseCol<'short> for ColUninit<'a, T> {
    type Item = *mut T;
    #[inline]
    fn nrows(&self) -> usize {
        self.0.nrows()
    }
    #[inline]
    fn is_contiguous(&self) -> bool {
        self.0.row_stride() == 1
    }
    #[inline]
    unsafe fn get_unchecked(&'short mut self, i: usize) -> Self::Item {
        (*self).0.rb_mut().get_unchecked(i)
    }
    #[inline]
    unsafe fn get_contiguous_unchecked(&'short mut self, i: usize) -> Self::Item {
        (*self).0.rb_mut().as_ptr().add(i)
    }
}

impl<'short, 'a, T> CwiseRow<'short> for RowRef<'a, T> {
    type Item = &'short T;
    #[inline]
    fn ncols(&self) -> usize {
        self.ncols()
    }
    #[inline]
    fn is_contiguous(&self) -> bool {
        self.col_stride() == 1
    }
    #[inline]
    unsafe fn get_unchecked(&'short mut self, i: usize) -> Self::Item {
        (*self).rb().get_unchecked(i)
    }
    #[inline]
    unsafe fn get_contiguous_unchecked(&'short mut self, i: usize) -> Self::Item {
        &*(*self).rb().as_ptr().add(i)
    }
}

impl<'short, 'a, T> CwiseRow<'short> for RowMut<'a, T> {
    type Item = &'short mut T;
    #[inline]
    fn ncols(&self) -> usize {
        self.ncols()
    }
    #[inline]
    fn is_contiguous(&self) -> bool {
        self.col_stride() == 1
    }
    #[inline]
    unsafe fn get_unchecked(&'short mut self, i: usize) -> Self::Item {
        (*self).rb_mut().get_unchecked(i)
    }
    #[inline]
    unsafe fn get_contiguous_unchecked(&'short mut self, i: usize) -> Self::Item {
        &mut *(*self).rb_mut().as_ptr().add(i)
    }
}

impl<'short, 'a, T> CwiseRow<'short> for RowUninit<'a, T> {
    type Item = *mut T;
    #[inline]
    fn ncols(&self) -> usize {
        self.0.ncols()
    }
    #[inline]
    fn is_contiguous(&self) -> bool {
        self.0.col_stride() == 1
    }
    #[inline]
    unsafe fn get_unchecked(&'short mut self, i: usize) -> Self::Item {
        (*self).0.rb_mut().get_unchecked(i)
    }
    #[inline]
    unsafe fn get_contiguous_unchecked(&'short mut self, i: usize) -> Self::Item {
        (*self).0.rb_mut().as_ptr().add(i)
    }
}

pub struct ZipMat<Tuple> {
    pub(crate) tuple: Tuple,
}

pub struct ZipRow<Tuple> {
    pub(crate) tuple: Tuple,
}

pub struct ZipCol<Tuple> {
    pub(crate) tuple: Tuple,
}

include!(concat!(env!("OUT_DIR"), "/zip.rs"));