Refactor a bit

This commit is contained in:
Michael Neumann 2015-11-29 14:53:51 +01:00
parent a9bf86e22a
commit c61d7152a3
2 changed files with 15 additions and 16 deletions

View File

@ -2,21 +2,17 @@ use bit_vec::BitVec;
#[derive(Debug)] #[derive(Debug)]
pub struct Coverage { pub struct Coverage {
n: usize,
rows: BitVec, rows: BitVec,
cols: BitVec, cols: BitVec,
} }
impl Coverage { impl Coverage {
// XXX: Is this needed? pub fn n(&self) -> usize { self.n }
pub fn n(&self) -> usize {
let n1 = self.rows.len();
let n2 = self.cols.len();
assert!(n1 == n2);
return n1;
}
pub fn new(n: usize) -> Coverage { pub fn new(n: usize) -> Coverage {
Coverage { Coverage {
n: n,
rows: BitVec::from_elem(n, false), rows: BitVec::from_elem(n, false),
cols: BitVec::from_elem(n, false), cols: BitVec::from_elem(n, false),
} }

View File

@ -39,6 +39,7 @@ impl<T> WeightNum for T
where T: PartialOrd + Copy + Sub<Output=T> + Add<Output=T> + Zero { } where T: PartialOrd + Copy + Sub<Output=T> + Add<Output=T> + Zero { }
#[derive(Debug)] #[derive(Debug)]
// TODO: A WeightMatrix trait
pub struct WeightMatrix<T: WeightNum> { pub struct WeightMatrix<T: WeightNum> {
c: SquareMatrix<T>, c: SquareMatrix<T>,
} }
@ -59,11 +60,19 @@ impl<T: WeightNum> WeightMatrix<T> {
self.c.n() self.c.n()
} }
#[inline(always)] #[inline]
fn is_element_zero(&self, pos: (usize, usize)) -> bool { fn is_element_zero(&self, pos: (usize, usize)) -> bool {
self.c[pos].partial_cmp(&T::zero()) == Some(Ordering::Equal) 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`. /// Return the minimum element of row `row`.
fn min_of_row(&self, row: usize) -> T { fn min_of_row(&self, row: usize) -> T {
let row_slice = self.c.row_slice(row); let row_slice = self.c.row_slice(row);
@ -92,6 +101,7 @@ impl<T: WeightNum> WeightMatrix<T> {
} }
/// Find the first uncovered element with value 0 `find_a_zero` /// 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)> { fn find_uncovered_zero(&self, cov: &Coverage) -> Option<(usize, usize)> {
let n = self.n(); let n = self.n();
@ -153,17 +163,10 @@ enum Step {
/// For each row of the matrix, find the smallest element and /// For each row of the matrix, find the smallest element and
/// subtract it from every element in its row. Go to Step 2. /// subtract it from every element in its row. Go to Step 2.
fn step1<T: WeightNum>(c: &mut WeightMatrix<T>) -> Step { fn step1<T: WeightNum>(c: &mut WeightMatrix<T>) -> Step {
let n = c.n(); c.sub_min_of_each_row();
for row in 0..n {
let min = c.min_of_row(row);
c.sub_row(row, min);
}
return Step::Step2; return Step::Step2;
} }
/// Find a zero (Z) in the resulting matrix. If there is no starred /// 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 /// zero in its row or column, star Z. Repeat for each element in the
/// matrix. Go to Step 3. /// matrix. Go to Step 3.