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
use std::fmt;
use std::cell::RefCell;
use std::rc::Rc;
use shape::Shape;
pub struct Array<T: Copy> {
pub data: Rc<RefCell<Vec<T>>>,
pub shape: Shape,
}
impl<T: Copy> Array<T> {
#[inline]
pub fn new_bounded(data: Vec<T>, shape: Vec<i32>, start: usize, end: usize) -> Array<T> {
return Array {
data: Rc::new(RefCell::new(data)),
shape: Shape::new(shape, start, end)
};
}
pub fn new(data: Vec<T>, shape: Vec<i32>) -> Array<T> {
let length = data.len();
return Array::new_bounded(data, shape, 0, length);
}
#[inline]
pub fn shape(&self) -> &Shape {
&self.shape
}
#[inline]
pub fn get_shape(&self) -> &Vec<i32> {
self.shape.get_shape()
}
#[inline]
pub fn reshape(&mut self, shape: Vec<i32>) -> &Array<T> {
self.shape.set_shape(shape);
return self;
}
#[inline]
pub fn set_shape(&mut self, shape: Vec<i32>) -> () {
self.shape.set_shape(shape);
}
fn check_indices_size(&self, indices: &Vec<usize>) -> () {
let shape_len = self.shape.get_shape().len();
if indices.len() / 2 > shape_len {
panic!(
"Invalid indices given: shape dimensions {}, indices dimensions {}",
shape_len,
indices.len() / 2
);
}
}
pub fn set(&self, indices: Vec<usize>, value: T) -> () {
self.check_indices_size(&indices);
let shape = self.shape.indices_to_shape(indices);
let mut data = self.data.borrow_mut();
for i in shape.get_bounds() {
data[i] = value;
}
}
pub fn fill(&self, value: T) -> &Array<T> {
let mut data = self.data.borrow_mut();
for i in self.shape.get_bounds() {
data[i] = value;
}
return self;
}
pub fn get(&self, indices: Vec<usize>) -> Array<T> {
self.check_indices_size(&indices);
let new_shape = self.shape.indices_to_shape(indices);
return Array {
shape: new_shape,
data: self.data.clone(),
};
}
pub fn len(&self) -> usize {
return Shape::total_len(&self.shape) as usize;
}
#[inline]
pub fn base_len(&self) -> usize {
return self.data.borrow().len();
}
#[inline]
pub fn view(&self) -> Array<T> {
return Array {
data: self.data.clone(),
shape: self.shape.clone(),
}
}
#[inline]
pub fn bounded_view(&self, shape: &Vec<i32>, start: usize, end: usize) -> Array<T> {
return Array {
data: self.data.clone(),
shape: Shape::new(shape.clone(), start, end),
}
}
pub fn collect(&self) -> Vec<T> {
let data = self.data.borrow();
return data[self.shape.get_bounds()].to_vec();
}
}
impl<T: Copy> Clone for Array<T> {
fn clone(&self) -> Array<T> {
let data = self.data.borrow().to_vec().clone();
return Array {
data: Rc::new(RefCell::new(data)),
shape: self.shape.clone(),
};
}
}
impl <T: fmt::Debug + Copy> fmt::Debug for Array<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let data = self.data.borrow();
return write!(f, "data: \t{:?}\nshape: \t{:?}", &data[self.shape.get_bounds()], self.get_shape());
}
}