diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..0642edd --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "approximate-gcd" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..01b8298 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,5 @@ +mod matrix; + +fn main() { + println!(""); +} diff --git a/src/matrix.rs b/src/matrix.rs new file mode 100644 index 0000000..b0f36e1 --- /dev/null +++ b/src/matrix.rs @@ -0,0 +1,59 @@ +type Element = f64; + +use std::ops::{Index, IndexMut}; + +#[derive(Debug, PartialEq)] +pub struct Matrix { + pub n: usize, + values: Vec, +} + +impl Matrix { + pub fn new(n: usize, values: Vec) -> Option { + if n.pow(2) == values.len() { + Some(Matrix { n, values }) + } else { + None + } + } +} + + +impl Index<(usize, usize)> for Matrix { + type Output = f64; + fn index(&self, index: (usize, usize)) -> &Self::Output { + &self.values[(self.n * index.0) + index.1] + } +} + +impl IndexMut<(usize, usize)> for Matrix { + fn index_mut(&mut self, index: (usize, usize)) -> &mut Self::Output { + &mut self.values[(self.n * index.0) + index.1] + } +} + + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn simple_matrix() { + assert_eq!( + Matrix { + n: 2, + values: Vec::from([1.0, 2.0, 3.0, 4.0]) + }, + Matrix::new(2, Vec::from([1.0, 2.0, 3.0, 4.0])).unwrap() + ); + assert!(Matrix::new(3, Vec::from([1.0, 2.0])).is_none()); + } + #[test] + fn indexes() { + let mut m = Matrix::new(2, vec![1.0, 2.0, 3.0, 4.0]).unwrap(); + assert_eq!(m[(0, 0)], 1.0); + assert_eq!(m[(1, 0)], 3.0); + + m[(1, 0)] = 5.0; + assert_eq!(m[(1, 0)], 5.0); + } +}