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
use crate::dr_matrix::DrMatrixRef;
use alloc::vec::Vec;
use cl_traits::{Length, Push, Storage};
use core::iter::Extend;
pub type DrMatrixRowConstructorMut<'a, DATA> = DrMatrixRowsConstructor<'a, &'a mut [DATA]>;
pub type DrMatrixRowConstructorRef<'a, DATA> = DrMatrixRowsConstructor<'a, &'a [DATA]>;
pub type DrMatrixRowConstructorVec<'a, T> = DrMatrixRowsConstructor<'a, Vec<T>>;
#[derive(Debug, PartialEq)]
pub struct DrMatrixRowsConstructor<'a, DS> {
pub(crate) data: &'a mut DS,
pub(crate) cols: usize,
pub(crate) rows: &'a mut usize,
}
impl<'a, DS> DrMatrixRowsConstructor<'a, DS> {
pub(crate) fn new(rows: &'a mut usize, cols: usize, data: &'a mut DS) -> Self {
DrMatrixRowsConstructor { data, rows, cols }
}
}
impl<'a, DATA, DS> DrMatrixRowsConstructor<'a, DS>
where
DS: Storage<Item = DATA>,
{
pub fn fill_row(self, elem: DATA) -> Self
where
DATA: Clone,
DS: Extend<DATA>,
{
self.data.extend((0..self.cols).map(|_| elem.clone()));
*self.rows += 1;
self
}
pub fn fill_rows(self, rows: usize, elem: DATA) -> Self
where
DATA: Clone,
DS: Extend<DATA>,
{
self.data.extend((0..rows * self.cols).map(|_| elem.clone()));
*self.rows += rows;
self
}
pub fn matrix_ref(self, other: DrMatrixRef<'_, DATA>) -> Option<Self>
where
DATA: Clone,
DS: Extend<DATA>,
{
if self.cols != other.cols {
return None;
}
self.data.extend(other.data.iter().cloned());
*self.rows += other.rows();
Some(self)
}
pub fn row_cb<F>(self, mut cb: F) -> Self
where
DS: Push<Input = DATA>,
F: FnMut(usize) -> DATA,
{
for idx in 0..self.cols {
self.data.push(cb(idx));
}
*self.rows += 1;
self
}
pub fn row_iter<I>(self, i: I) -> Self
where
DATA: Default,
DS: Extend<DATA> + Length<Output = usize>,
I: Iterator<Item = DATA>,
{
let old_len = self.data.length();
self.data.extend(i.take(self.cols));
let new_len = self.data.length();
let diff = self.cols - (new_len - old_len);
self.data.extend((0..diff).map(|_| DATA::default()));
*self.rows += 1;
self
}
pub fn row_slice(self, row: &[DS::Item]) -> Option<Self>
where
DS::Item: Clone,
DS: Extend<DATA>,
{
if row.len() != self.cols {
return None;
}
self.data.extend(row.iter().cloned());
*self.rows += 1;
Some(self)
}
}