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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
mod dr_matrix_error;
mod dr_matrix_row_iter_impls;
mod dr_matrix_rows_constructor;
use alloc::vec::Vec;
use cl_traits::{ArrayWrapper, Clear, Storage, Truncate, WithCapacity};
use core::cmp::Ordering;
pub use {dr_matrix_error::*, dr_matrix_row_iter_impls::*, dr_matrix_rows_constructor::*};
pub type DrMatrixArray<DA> = DrMatrix<ArrayWrapper<DA>>;
pub type DrMatrixMut<'a, DATA> = DrMatrix<&'a mut [DATA]>;
pub type DrMatrixRef<'a, DATA> = DrMatrix<&'a [DATA]>;
pub type DrMatrixVec<T> = DrMatrix<Vec<T>>;
pub type Result<T> = core::result::Result<T, DrMatrixError>;
#[cfg_attr(feature = "with-serde", derive(serde::Deserialize, serde::Serialize))]
#[derive(Clone, Copy, Debug, Default, Hash, PartialEq, PartialOrd)]
pub struct DrMatrix<DS> {
pub(crate) data: DS,
pub(crate) cols: usize,
pub(crate) rows: usize,
}
impl<DS> DrMatrix<DS> {
pub fn constructor(&mut self) -> DrMatrixRowsConstructor<'_, DS> {
DrMatrixRowsConstructor::new(&mut self.rows, self.cols, &mut self.data)
}
pub fn clear(&mut self)
where
DS: Clear,
{
self.data.clear();
self.rows = 0;
}
#[inline]
pub fn cols(&self) -> usize {
self.cols
}
#[inline]
pub fn rows(&self) -> usize {
self.rows
}
pub fn truncate(&mut self, until_row_idx: usize)
where
DS: Truncate<Input = usize>,
{
self.data.truncate(self.cols.saturating_mul(until_row_idx));
self.rows = until_row_idx;
}
fn row_range(&self, row_idx: usize) -> Option<core::ops::Range<usize>> {
let stride = self.stride(row_idx);
if stride == usize::MAX {
return None;
}
Some(stride..stride + self.cols)
}
#[inline]
fn stride(&self, row_idx: usize) -> usize {
self.cols.saturating_mul(row_idx)
}
}
impl<DS> DrMatrix<DS>
where
DS: WithCapacity<Input = usize>,
{
pub fn with_capacity(rows: usize, cols: usize) -> Self {
DrMatrix { data: DS::with_capacity(rows * cols), cols, rows: 0 }
}
}
impl<DATA, DS> DrMatrix<DS>
where
DS: AsRef<[DATA]> + Storage<Item = DATA>,
{
pub fn new<IDS>(rows: usize, cols: usize, into_data: IDS) -> Result<Self>
where
IDS: Into<DS>,
{
let data = into_data.into();
if rows.saturating_mul(cols) != data.as_ref().len() {
return Err(DrMatrixError::DataLenDiffColsTimesRows);
}
Ok(Self { data, rows, cols })
}
pub fn as_ref(&self) -> DrMatrixRef<'_, DATA> {
DrMatrixRef { cols: self.cols, data: self.data.as_ref(), rows: self.rows }
}
pub fn data(&self) -> &[DATA] {
&self.data.as_ref()
}
pub fn row(&self, row_idx: usize) -> Option<&[DATA]> {
self.data().get(self.row_range(row_idx)?)
}
pub fn row_iter(&self) -> DrMatrixRowIter<'_, DATA> {
DrMatrixRowIter::new(self.rows(), self.cols, self.data().as_ref())
}
pub fn to_vec(&self) -> DrMatrixVec<DATA>
where
DATA: Clone,
{
DrMatrixVec { cols: self.cols, data: self.data.as_ref().to_vec(), rows: self.rows }
}
pub fn value(&self, row_idx: usize, col_idx: usize) -> Option<&DATA> {
self.data().get(self.stride(row_idx).saturating_add(col_idx))
}
}
impl<DATA, DS> DrMatrix<DS>
where
DS: AsMut<[DATA]> + Storage<Item = DATA>,
{
pub fn data_mut(&mut self) -> &mut [DATA] {
self.data.as_mut()
}
pub fn remove_row(&mut self, idx: usize)
where
DS: Truncate<Input = usize>,
{
assert!(idx < self.rows);
let mut from_row_idx = idx;
let mut to_row_idx = idx + 1;
while to_row_idx < self.rows {
self.swap_rows(from_row_idx, to_row_idx);
from_row_idx += 1;
to_row_idx += 1;
}
self.truncate(self.rows - 1);
}
pub fn row_mut(&mut self, row_idx: usize) -> Option<&mut [DATA]> {
let row_range = self.row_range(row_idx)?;
self.data_mut().get_mut(row_range)
}
pub fn row_iter_mut(&mut self) -> DrMatrixRowIterMut<'_, DATA> {
DrMatrixRowIterMut::new(self.rows, self.cols, self.data.as_mut())
}
pub fn swap(&mut self, a: [usize; 2], b: [usize; 2]) -> bool
where
DS: AsRef<[DATA]>,
{
let a_data_idx = self.stride(a[0]).saturating_add(a[1]);
let b_data_idx = self.stride(b[0]).saturating_add(b[1]);
if self.data().get(a_data_idx).is_none() {
return false;
}
if self.data().get(b_data_idx).is_none() {
return false;
}
self.data_mut().swap(a_data_idx, b_data_idx);
true
}
pub fn swap_rows(&mut self, a: usize, b: usize) -> bool {
if let Some([first_row, second_row]) = self.two_rows_mut(a, b) {
first_row.swap_with_slice(second_row);
true
} else {
false
}
}
pub fn two_rows_mut(&mut self, a: usize, b: usize) -> Option<[&mut [DATA]; 2]> {
let [max, min] = match a.cmp(&b) {
Ordering::Equal => return None,
Ordering::Greater => [a, b],
Ordering::Less => [b, a],
};
let max_stride = self.stride(max);
let min_stride = self.stride(min);
let (first, second) = self.data.as_mut().split_at_mut(max_stride);
let first_range = min_stride..min_stride.saturating_add(self.cols);
let second_range = ..self.cols;
Some([first.get_mut(first_range)?, second.get_mut(second_range)?])
}
pub fn value_mut(&mut self, row_idx: usize, col_idx: usize) -> Option<&mut DATA> {
let data_idx = self.stride(row_idx).saturating_add(col_idx);
self.data_mut().get_mut(data_idx)
}
}
#[cfg(feature = "with-rand")]
impl<DATA, DS> DrMatrix<DS>
where
DS: Default + cl_traits::Push<Input = DATA> + Storage<Item = DATA>,
{
pub fn new_random_with_rand<F, R>(rows: usize, cols: usize, rng: &mut R, mut cb: F) -> Self
where
F: FnMut(&mut R, usize, usize) -> DATA,
R: rand::Rng,
{
let mut data = DS::default();
for row in 0..rows {
for col in 0..cols {
data.push(cb(rng, row, col));
}
}
DrMatrix { cols, data, rows }
}
}