pub struct Matrix {
cols: usize,
contents: Vec<f64>
}
impl Matrix {
pub fn new(width: usize, height: usize) -> Matrix {
Matrix {
contents: (0..width * height).map(|_i| 0.0).collect(),
cols: width
}
}
pub fn get(&self, col: usize, row: usize) -> f64 {
debug_assert!(col * row < self.contents.len());
self.contents[row * self.cols + col]
}
pub fn set(&mut self, col: usize, row: usize, val: f64) {
debug_assert!(col * row < self.contents.len());
self.contents[row * self.cols + col] = val;
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_get() {
let mat = Matrix::new(10, 5);
assert_eq!(mat.get(0, 0), 0.0);
assert_eq!(mat.get(9, 4), 0.0);
}
#[test]
fn test_set() {
let mut mat = Matrix::new(10, 5);
mat.set(9, 4, 1.0);
assert_eq!(mat.get(0, 0), 0.0);
assert_eq!(mat.get(9, 4), 1.0);
}
}