diff --git a/matrix/Cargo.toml b/matrix/Cargo.toml index 2f6e9d1..1e2bbff 100644 --- a/matrix/Cargo.toml +++ b/matrix/Cargo.toml @@ -8,3 +8,4 @@ edition = "2018" [dependencies] approx = "0.4" +bencher = "0.1.5" diff --git a/matrix/src/lib.rs b/matrix/src/lib.rs index 378d3d1..79f1f47 100644 --- a/matrix/src/lib.rs +++ b/matrix/src/lib.rs @@ -14,6 +14,20 @@ impl Matrix { matrix: matrix, } } + + pub fn from_array(matrix: [[f32; 4]; 4]) -> Matrix { + let mut m = Vec::with_capacity(4); + for row in 0..matrix.len() { + let mut r = Vec::with_capacity(4); + for col in 0..matrix[row].len() { + r.push(matrix[row][col]); + } + m.push(r); + } + Matrix { + matrix: m, + } + } } impl Index for Matrix { @@ -66,6 +80,25 @@ mod tests { assert_eq!(15.5, matrix[3][2]); } + #[test] + fn matrix_4x4_array() { + let m = [ + [1.0, 2.0, 3.0, 4.0], + [5.5, 6.5, 7.5, 8.5], + [9.0, 10.0, 11.0, 12.0], + [13.5, 14.5, 15.5, 16.5], + ]; + + let matrix = Matrix::from_array(m); + assert_eq!(1.0, matrix[0][0]); + assert_eq!(4.0, matrix[0][3]); + assert_eq!(5.5, matrix[1][0]); + assert_eq!(7.5, matrix[1][2]); + assert_eq!(11.0, matrix[2][2]); + assert_eq!(13.5, matrix[3][0]); + assert_eq!(15.5, matrix[3][2]); + } + #[test] fn matrix_2x2() { let m = vec![