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
use std::vec::Vec;
pub struct Matrix<T> {
data: Vec<T>,
pub cols: usize,
pub rows: usize
}
impl<T: Clone> Matrix<T> {
pub fn new(rows: usize, cols: usize, initial: T) -> Matrix<T> {
let data = (0..rows*cols).map(|_| initial.clone()).collect();
Matrix {
cols: cols,
rows: rows,
data: data
}
}
pub fn with_data(rows: usize, cols: usize, initial: T, old_data: &Vec<T>, data_rows: usize, data_cols: usize) -> Matrix<T> {
let mut data: Vec<T> = (0..rows * cols).map(|_| initial.clone()).collect();
for row in 0..data_rows {
for col in 0..data_cols {
data[(row * cols) + col] = old_data[(row * data_cols) + col].clone();
}
}
return Matrix {
data: data,
rows: rows,
cols: cols
}
}
pub fn get(&self, row: usize, col: usize) -> T {
self.data[(row * self.cols) + col].clone()
}
pub fn set(&mut self, row: usize, col: usize, val: T) {
self.data[(row * self.cols) + col] = val;
}
pub fn resize(&self, new_rows: usize, new_cols: usize, initial: T) -> Matrix<T> {
Matrix::with_data(new_rows, new_cols, initial, &self.data, self.rows, self.cols)
}
}