This commit is contained in:
Michael Neumann 2015-10-26 00:12:54 +01:00
parent 9619f69a4e
commit 4ee267acc5
4 changed files with 186 additions and 175 deletions

View File

@ -1,4 +1,4 @@
use ::bit_vec::BitVec;
use bit_vec::BitVec;
#[derive(Debug)]
pub struct Coverage {
@ -16,8 +16,10 @@ impl Coverage {
}
pub fn new(n: usize) -> Coverage {
Coverage {rows: BitVec::from_elem(n, false),
cols: BitVec::from_elem(n, false)}
Coverage {
rows: BitVec::from_elem(n, false),
cols: BitVec::from_elem(n, false),
}
}
#[inline]

View File

@ -40,7 +40,7 @@ where T: Ord + Eq + Copy + Sub<Output=T> + Add<Output=T> + Zero { }
#[derive(Debug)]
pub struct WeightMatrix<T: WeightNum> {
c: SquareMatrix<T>
c: SquareMatrix<T>,
}
impl<T: WeightNum> WeightMatrix<T> {
@ -50,7 +50,9 @@ impl<T: WeightNum> WeightMatrix<T> {
}
#[inline(always)]
fn n(&self) -> usize { self.c.n() }
fn n(&self) -> usize {
self.c.n()
}
#[inline(always)]
fn is_element_zero(&self, pos: (usize, usize)) -> bool {
@ -87,7 +89,9 @@ impl<T: WeightNum> WeightMatrix<T> {
let n = self.n();
for col in 0..n {
if cov.is_col_covered(col) { continue; }
if cov.is_col_covered(col) {
continue;
}
for row in 0..n {
if !cov.is_row_covered(row) && self.is_element_zero((row, col)) {
return Some((row, col));
@ -103,9 +107,13 @@ impl<T: WeightNum> WeightMatrix<T> {
let mut min = None;
let n = self.n();
for row in 0..n {
if cov.is_row_covered(row) { continue; }
if cov.is_row_covered(row) {
continue;
}
for col in 0..n {
if cov.is_col_covered(col) { continue; }
if cov.is_col_covered(col) {
continue;
}
let elm = self.c[(row, col)];
min = Some(match min {
None => elm,
@ -153,9 +161,13 @@ fn step2<T: WeightNum>(c: &WeightMatrix<T>, marks: &mut MarkMatrix, cov: &mut Co
assert!(cov.n() == n);
for row in 0..n {
if cov.is_row_covered(row) { continue; }
if cov.is_row_covered(row) {
continue;
}
for col in 0..n {
if cov.is_col_covered(col) { continue; }
if cov.is_col_covered(col) {
continue;
}
if c.is_element_zero((row, col)) {
marks.star((row, col));
cov.cover((row, col));
@ -221,7 +233,7 @@ fn step4<T: WeightNum>(c: &WeightMatrix<T>, marks: &mut MarkMatrix, cov: &mut Co
}
None => {
// in Python: self.Z0_r, self.Z0_c
return Step::Step5(row, col)
return Step::Step5(row, col);
}
}
}
@ -238,7 +250,11 @@ fn step4<T: WeightNum>(c: &WeightMatrix<T>, marks: &mut MarkMatrix, cov: &mut Co
/// that has no starred zero in its column. Unstar each starred zero
/// of the series, star each primed zero of the series, erase all
/// primes and uncover every line in the matrix. Return to Step 3
fn step5<T: WeightNum>(c: &WeightMatrix<T>, marks: &mut MarkMatrix, cov: &mut Coverage, z0: (usize, usize)) -> Step {
fn step5<T: WeightNum>(c: &WeightMatrix<T>,
marks: &mut MarkMatrix,
cov: &mut Coverage,
z0: (usize, usize))
-> Step {
let n = c.n();
assert!(marks.n() == n);
@ -351,18 +367,14 @@ pub fn solve_assignment<T: WeightNum>(weights: &mut WeightMatrix<T>) -> Vec<(usi
#[test]
fn test_step1() {
let c = vec![250, 400, 350,
400, 600, 350,
200, 400, 250];
let c = vec![250, 400, 350, 400, 600, 350, 200, 400, 250];
let mut weights: WeightMatrix<i32> = WeightMatrix::from_row_vec(3, c);
let next_step = step1(&mut weights);
assert_eq!(Step::Step2, next_step);
let exp = &[0, 150, 100,
50, 250, 0,
0, 200, 50];
let exp = &[0, 150, 100, 50, 250, 0, 0, 200, 50];
assert_eq!(exp, weights.c.as_slice());
}
@ -370,9 +382,7 @@ fn test_step1() {
#[test]
fn test_step2() {
let c = vec![0, 150, 100,
50, 250, 0,
0, 200, 50];
let c = vec![0, 150, 100, 50, 250, 0, 0, 200, 50];
let weights: WeightMatrix<i32> = WeightMatrix::from_row_vec(3, c);
let mut marks = MarkMatrix::new(weights.n());
@ -401,22 +411,20 @@ fn test_step2() {
assert_eq!(false, coverage.is_col_covered(1));
assert_eq!(false, coverage.is_col_covered(2));
/*
assert_eq!(true, coverage.is_row_covered(0));
assert_eq!(true, coverage.is_row_covered(1));
assert_eq!(false, coverage.is_row_covered(2));
assert_eq!(true, coverage.is_col_covered(0));
assert_eq!(false, coverage.is_col_covered(1));
assert_eq!(true, coverage.is_col_covered(2));
*/
//
// assert_eq!(true, coverage.is_row_covered(0));
// assert_eq!(true, coverage.is_row_covered(1));
// assert_eq!(false, coverage.is_row_covered(2));
//
// assert_eq!(true, coverage.is_col_covered(0));
// assert_eq!(false, coverage.is_col_covered(1));
// assert_eq!(true, coverage.is_col_covered(2));
//
}
#[test]
fn test_step3() {
let c = vec![0, 150, 100,
50, 250, 0,
0, 200, 50];
let c = vec![0, 150, 100, 50, 250, 0, 0, 200, 50];
let weights: WeightMatrix<i32> = WeightMatrix::from_row_vec(3, c);
let mut marks = MarkMatrix::new(weights.n());
@ -439,9 +447,7 @@ fn test_step3() {
#[test]
fn test_step4_case1() {
let c = vec![0, 150, 100,
50, 250, 0,
0, 200, 50];
let c = vec![0, 150, 100, 50, 250, 0, 0, 200, 50];
let weights: WeightMatrix<i32> = WeightMatrix::from_row_vec(3, c);
let mut marks = MarkMatrix::new(weights.n());
@ -478,9 +484,7 @@ fn test_step4_case1() {
#[test]
fn test_step6() {
let c = vec![0, 150, 100,
50, 250, 0,
0, 200, 50];
let c = vec![0, 150, 100, 50, 250, 0, 0, 200, 50];
let mut weights: WeightMatrix<i32> = WeightMatrix::from_row_vec(3, c);
let mut marks = MarkMatrix::new(weights.n());
@ -495,18 +499,14 @@ fn test_step6() {
assert_eq!(Step::Step4(None), next_step);
let exp = &[0, 0, 100,
50, 100, 0,
0, 50, 50];
let exp = &[0, 0, 100, 50, 100, 0, 0, 50, 50];
assert_eq!(exp, weights.c.as_slice());
}
#[test]
fn test_step4_case2() {
let c = vec![0, 0, 100,
50, 100, 0,
0, 50, 50];
let c = vec![0, 0, 100, 50, 100, 0, 0, 50, 50];
let weights: WeightMatrix<i32> = WeightMatrix::from_row_vec(3, c);
let mut marks = MarkMatrix::new(weights.n());
@ -543,9 +543,7 @@ fn test_step4_case2() {
#[test]
fn test_step5() {
let c = vec![0, 0, 100,
50, 100, 0,
0, 50, 50];
let c = vec![0, 0, 100, 50, 100, 0, 0, 50, 50];
let weights: WeightMatrix<i32> = WeightMatrix::from_row_vec(3, c);
let mut marks = MarkMatrix::new(weights.n());
@ -587,9 +585,7 @@ fn test_step5() {
#[test]
fn test_solve() {
let c = vec![250, 400, 350,
400, 600, 350,
200, 400, 250];
let c = vec![250, 400, 350, 400, 600, 350, 200, 400, 250];
let mut weights: WeightMatrix<i32> = WeightMatrix::from_row_vec(3, c);
let matching = solve_assignment(&mut weights);
@ -625,19 +621,20 @@ fn test_solve_random10() {
assert_eq!(1071, cost);
let exp = &[(0, 7), (1, 9), (2, 3), (3, 4), (4, 1),
(5, 0), (6, 5), (7, 6), (8, 2), (9, 8)];
let exp = &[(0, 7), (1, 9), (2, 3), (3, 4), (4, 1), (5, 0), (6, 5), (7, 6), (8, 2), (9, 8)];
assert_eq!(exp, &matching[..]);
}
#[cfg(test)]
fn gen_matrix(n: usize) -> Vec<i32> {
(0..n*n).map(|i| {
(0..n * n)
.map(|i| {
let row = i / n;
let col = i % n;
(row * col) as i32
}).collect()
})
.collect()
}
#[cfg(test)]

View File

@ -1,25 +1,30 @@
use ::square_matrix::SquareMatrix;
use square_matrix::SquareMatrix;
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
enum Mark {
None,
Star,
Prime
Prime,
}
#[derive(Debug)]
pub struct MarkMatrix {
marks: SquareMatrix<Mark>
marks: SquareMatrix<Mark>,
}
// XXX: Use two bitmatrices. Each row has a fixed number of u64 integers. Use bit-search
// XXX: Use two bitmatrices. Each row has a fixed number of u64 integers. Use
// bit-search
impl MarkMatrix {
pub fn new(n: usize) -> MarkMatrix {
MarkMatrix {marks: SquareMatrix::from_row_vec(n, (0..n*n).map(|_| Mark::None).collect())}
MarkMatrix {
marks: SquareMatrix::from_row_vec(n, (0..n * n).map(|_| Mark::None).collect()),
}
}
pub fn n(&self) -> usize { self.marks.n() }
pub fn n(&self) -> usize {
self.marks.n()
}
pub fn toggle_star(&mut self, pos: (usize, usize)) {
if self.is_star(pos) {
@ -44,14 +49,14 @@ impl MarkMatrix {
pub fn is_star(&self, pos: (usize, usize)) -> bool {
match self.marks[pos] {
Mark::Star => true,
_ => false
_ => false,
}
}
pub fn is_prime(&self, pos: (usize, usize)) -> bool {
match self.marks[pos] {
Mark::Prime => true,
_ => false
_ => false,
}
}
@ -59,7 +64,7 @@ impl MarkMatrix {
pub fn is_none(&self, pos: (usize, usize)) -> bool {
match self.marks[pos] {
Mark::None => true,
_ => false
_ => false,
}
}

View File

@ -3,7 +3,7 @@ use std::ops::{Index, IndexMut};
#[derive(Debug)]
pub struct SquareMatrix<T> {
n: usize,
data: Box<[T]>
data: Box<[T]>,
}
impl<T> Index<(usize, usize)> for SquareMatrix<T> {
@ -43,12 +43,19 @@ impl<T: Copy> SquareMatrix<T> {
pub fn from_row_vec(n: usize, data: Vec<T>) -> SquareMatrix<T> {
assert!(n > 0);
assert!(data.len() == n * n);
SquareMatrix {n: n, data: data.into_boxed_slice()}
SquareMatrix {
n: n,
data: data.into_boxed_slice(),
}
}
#[inline(always)]
pub fn n(&self) -> usize { self.n }
pub fn n(&self) -> usize {
self.n
}
pub fn as_slice<'a>(&'a self) -> &'a[T] { &self.data[..] }
pub fn as_slice<'a>(&'a self) -> &'a [T] {
&self.data[..]
}
#[inline]
pub fn row_slice(&self, row: usize) -> &[T] {