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
#[derive (Clone)]
pub struct Vec2d<T> {
data: Vec<T>,
width: usize,
}
impl<T: Default> Vec2d<T> {
pub fn new_with_default(rows: usize, cols: usize) -> Vec2d<T> {
Vec2d::new_empty(rows, cols).initialize_to_default()
}
pub fn add_row_of_default(&mut self) {
let size = self.count() + self.width;
while self.data.len() < size {
self.data.push(T::default());
}
}
fn initialize_to_default(mut self) -> Vec2d<T> {
let size = self.data.capacity();
while self.data.len() < size {
self.data.push(T::default());
}
self
}
}
impl<T: Copy> Vec2d<T> {
pub fn new_with_value(rows: usize, cols: usize, val:T) -> Vec2d<T> {
Vec2d::new_empty(rows, cols).initialize_to_value(val)
}
pub fn from_slice(cols: usize, arr: &[T]) -> Vec2d<T> {
let size = arr.len();
assert!(size % cols == 0);
let mut data: Vec<T> = Vec::with_capacity(size);
for idx in 0..size {
data.push(arr[idx]);
}
Vec2d {
data: data,
width: cols,
}
}
pub fn from_index<U: std::ops::Index<usize, Output=T>>(size: usize, cols: usize, arr: &U) -> Vec2d<T> {
assert!(size % cols == 0);
let mut data: Vec<T> = Vec::with_capacity(size);
for idx in 0..size {
data.push(arr[idx]);
}
Vec2d {
data: data,
width: cols,
}
}
fn initialize_to_value(mut self, val: T) -> Vec2d<T> {
let size = self.data.capacity();
while self.data.len() < size {
self.data.push(val);
}
self
}
}
impl<T: PartialEq> Vec2d<T> {
pub fn contains(&self, val: &T) -> bool {
for idx in 0..self.count() {
if *val == self.data[idx] { return true; }
}
false
}
}
impl<T> Vec2d<T> {
pub fn new() -> Vec2d<T> {
Vec2d {
data: Vec::new(),
width: 0,
}
}
pub fn from_fn(rows: usize, cols: usize, initializer: &dyn Fn(usize, usize) -> T) -> Vec2d<T> {
Vec2d::new_empty(rows, cols).initialize_from_fn(initializer)
}
pub fn count(&self) -> usize {
self.width * self.count_rows()
}
pub fn count_cols(&self) -> usize {
self.width
}
pub fn count_rows(&self) -> usize {
if self.width == 0 { 0 } else { self.data.len() / self.width }
}
pub fn size(&self) -> (usize, usize) {
(self.count_rows(), self.count_cols())
}
fn new_empty(rows: usize, cols: usize) -> Vec2d<T> {
Vec2d {
data: Vec::with_capacity(rows*cols),
width: cols,
}
}
fn initialize_from_fn(mut self, initializer: &dyn Fn(usize, usize) -> T) -> Vec2d<T> {
let size = self.data.capacity();
for idx in 0..size {
let row = idx / self.width;
let col = idx - (row * self.width);
self.data.push(initializer(row, col));
}
self
}
}
impl<T> std::ops::Index<usize> for Vec2d<T> {
type Output = [T];
fn index(&self, row: usize) -> &[T] {
let start = row * self.width;
let end = start + self.width;
&self.data[start..end]
}
}
impl<T> std::ops::IndexMut<usize> for Vec2d<T> {
fn index_mut<'a>(&'a mut self, row: usize) -> &'a mut Self::Output {
let start = row * self.width;
let end = start + self.width;
&mut self.data[start..end]
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn check_initializer() {
let v = Vec2d::from_fn(3, 3, &|row, col| row + col);
for idx in 0..v.count() {
let row = idx / v.count_cols();
let col = idx - (row * v.count_cols());
assert_eq!(v[row][col], row+col);
}
}
#[test]
#[should_panic]
fn panics_on_mismatched_arr_size() {
let arr: [i32;4] = [1,2,3,4];
let slc: &[i32] = &arr[0..3];
let _v: Vec2d<i32> = Vec2d::from_slice(2, slc);
let arr: [i32;5] = [1,2,3,4,5];
let _v = Vec2d::from_slice(2, &arr);
}
#[test]
fn build_from_data() {
let data: [i32;12] = [1,2,3,4,5,6,7,8,9,10,11,12];
for divs in 1..13 {
if 12 % divs == 0 {
let _v = Vec2d::from_slice(divs, &data);
for row in 0.._v.count_rows() {
for col in 0.._v.count_cols() {
let idx = row * _v.count_cols() + col;
assert_eq!(data[idx], _v[row][col]);
}
}
}
}
}
}