From 8faebad15792c41456c5fc8dae7210d4ad2a9c4d Mon Sep 17 00:00:00 2001 From: Michael Neumann Date: Tue, 20 Oct 2015 00:17:03 +0200 Subject: [PATCH] Refactor MarkMatrix into mark_matrix.rs --- src/lib.rs | 101 +++------------------------------------------ src/mark_matrix.rs | 97 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+), 96 deletions(-) create mode 100644 src/mark_matrix.rs diff --git a/src/lib.rs b/src/lib.rs index 00fefb2..08a3d32 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -23,9 +23,14 @@ use std::num::Zero; use std::cmp; use square_matrix::SquareMatrix; use coverage::Coverage; +use mark_matrix::MarkMatrix; + +#[cfg(test)] +use mark_matrix::Mark; pub mod square_matrix; mod coverage; +mod mark_matrix; pub trait WeightNum: Ord + Eq + Copy + Sub + Add + Zero {} @@ -122,102 +127,6 @@ enum Step { Done, } -#[derive(Clone, Copy, PartialEq, Eq, Debug)] -enum Mark { - None, - Star, - Prime -} - -#[derive(Debug)] -struct MarkMatrix { - marks: SquareMatrix -} - -impl MarkMatrix { - fn new(n: usize) -> MarkMatrix { - MarkMatrix {marks: SquareMatrix::from_row_vec(n, (0..n*n).map(|_| Mark::None).collect())} - } - - fn n(&self) -> usize { self.marks.n() } - - fn toggle_star(&mut self, pos: (usize, usize)) { - if self.is_star(pos) { - self.unmark(pos); - } else { - self.star(pos); - } - } - - #[cfg(test)] - fn get(&self, pos: (usize, usize)) -> Mark { - self.marks[pos] - } - - fn unmark(&mut self, pos: (usize, usize)) { - self.marks[pos] = Mark::None; - } - - fn star(&mut self, pos: (usize, usize)) { - self.marks[pos] = Mark::Star; - } - - fn prime(&mut self, pos: (usize, usize)) { - self.marks[pos] = Mark::Prime; - } - - fn is_star(&self, pos: (usize, usize)) -> bool { - match self.marks[pos] { - Mark::Star => true, - _ => false - } - } - - fn is_prime(&self, pos: (usize, usize)) -> bool { - match self.marks[pos] { - Mark::Prime => true, - _ => false - } - } - - fn find_first_star_in_row(&self, row: usize) -> Option { - for col in 0..self.n() { - if self.is_star((row, col)) { - return Some(col); - } - } - return None; - } - - fn find_first_prime_in_row(&self, row: usize) -> Option { - for col in 0..self.n() { - if self.is_prime((row, col)) { - return Some(col); - } - } - return None; - } - - fn find_first_star_in_col(&self, col: usize) -> Option { - for row in 0..self.n() { - if self.is_star((row, col)) { - return Some(row); - } - } - return None; - } - - fn clear_primes(&mut self) { - for row in 0..self.n() { - for col in 0..self.n() { - if let Mark::Prime = self.marks[(row, col)] { - self.marks[(row, col)] = Mark::None; - } - } - } - } -} - /// For each row of the matrix, find the smallest element and /// subtract it from every element in its row. Go to Step 2. fn step1(c: &mut WeightMatrix) -> Step { diff --git a/src/mark_matrix.rs b/src/mark_matrix.rs new file mode 100644 index 0000000..09e32c8 --- /dev/null +++ b/src/mark_matrix.rs @@ -0,0 +1,97 @@ +use ::square_matrix::SquareMatrix; + +#[derive(Clone, Copy, PartialEq, Eq, Debug)] +pub enum Mark { + None, + Star, + Prime +} + +#[derive(Debug)] +pub struct MarkMatrix { + marks: SquareMatrix +} + +impl MarkMatrix { + pub fn new(n: usize) -> MarkMatrix { + MarkMatrix {marks: SquareMatrix::from_row_vec(n, (0..n*n).map(|_| Mark::None).collect())} + } + + pub fn n(&self) -> usize { self.marks.n() } + + pub fn toggle_star(&mut self, pos: (usize, usize)) { + if self.is_star(pos) { + self.unmark(pos); + } else { + self.star(pos); + } + } + + #[cfg(test)] + pub fn get(&self, pos: (usize, usize)) -> Mark { + self.marks[pos] + } + + pub fn unmark(&mut self, pos: (usize, usize)) { + self.marks[pos] = Mark::None; + } + + pub fn star(&mut self, pos: (usize, usize)) { + self.marks[pos] = Mark::Star; + } + + pub fn prime(&mut self, pos: (usize, usize)) { + self.marks[pos] = Mark::Prime; + } + + pub fn is_star(&self, pos: (usize, usize)) -> bool { + match self.marks[pos] { + Mark::Star => true, + _ => false + } + } + + pub fn is_prime(&self, pos: (usize, usize)) -> bool { + match self.marks[pos] { + Mark::Prime => true, + _ => false + } + } + + pub fn find_first_star_in_row(&self, row: usize) -> Option { + for col in 0..self.n() { + if self.is_star((row, col)) { + return Some(col); + } + } + return None; + } + + pub fn find_first_prime_in_row(&self, row: usize) -> Option { + for col in 0..self.n() { + if self.is_prime((row, col)) { + return Some(col); + } + } + return None; + } + + pub fn find_first_star_in_col(&self, col: usize) -> Option { + for row in 0..self.n() { + if self.is_star((row, col)) { + return Some(row); + } + } + return None; + } + + pub fn clear_primes(&mut self) { + for row in 0..self.n() { + for col in 0..self.n() { + if self.is_prime((row, col)) { + self.unmark((row, col)); + } + } + } + } +}