diff --git a/src/coverage.rs b/src/coverage.rs index 7a6ff2e..d83ad9b 100644 --- a/src/coverage.rs +++ b/src/coverage.rs @@ -2,21 +2,17 @@ use bit_vec::BitVec; #[derive(Debug)] pub struct Coverage { + n: usize, rows: BitVec, cols: BitVec, } impl Coverage { - // XXX: Is this needed? - pub fn n(&self) -> usize { - let n1 = self.rows.len(); - let n2 = self.cols.len(); - assert!(n1 == n2); - return n1; - } + pub fn n(&self) -> usize { self.n } pub fn new(n: usize) -> Coverage { Coverage { + n: n, rows: BitVec::from_elem(n, false), cols: BitVec::from_elem(n, false), } diff --git a/src/lib.rs b/src/lib.rs index 624903e..d80a5eb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -39,6 +39,7 @@ impl WeightNum for T where T: PartialOrd + Copy + Sub + Add + Zero { } #[derive(Debug)] +// TODO: A WeightMatrix trait pub struct WeightMatrix { c: SquareMatrix, } @@ -59,11 +60,19 @@ impl WeightMatrix { self.c.n() } - #[inline(always)] + #[inline] fn is_element_zero(&self, pos: (usize, usize)) -> bool { self.c[pos].partial_cmp(&T::zero()) == Some(Ordering::Equal) } + // for each row, subtracts the minimum of that row from each other value in the row. + pub fn sub_min_of_each_row(&mut self) { + for row in 0..self.n() { + let min = self.min_of_row(row); + self.sub_row(row, min); + } + } + /// Return the minimum element of row `row`. fn min_of_row(&self, row: usize) -> T { let row_slice = self.c.row_slice(row); @@ -92,6 +101,7 @@ impl WeightMatrix { } /// Find the first uncovered element with value 0 `find_a_zero` + /// TODO: Move into Coverage as iter_uncovered() fn find_uncovered_zero(&self, cov: &Coverage) -> Option<(usize, usize)> { let n = self.n(); @@ -153,17 +163,10 @@ enum Step { /// 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 { - let n = c.n(); - - for row in 0..n { - let min = c.min_of_row(row); - c.sub_row(row, min); - } - + c.sub_min_of_each_row(); return Step::Step2; } - /// Find a zero (Z) in the resulting matrix. If there is no starred /// zero in its row or column, star Z. Repeat for each element in the /// matrix. Go to Step 3.