Refactor Coverage out into coverage.rs

This commit is contained in:
Michael Neumann 2015-10-19 23:11:38 +02:00
parent 566f848524
commit 86ba0e26f7
2 changed files with 76 additions and 66 deletions

74
src/coverage.rs Normal file
View File

@ -0,0 +1,74 @@
use ::bit_vec::BitVec;
#[derive(Debug)]
pub struct Coverage {
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 new(n: usize) -> Coverage {
Coverage {rows: BitVec::from_elem(n, false),
cols: BitVec::from_elem(n, false)}
}
#[inline]
pub fn is_covered(&self, pos: (usize, usize)) -> bool {
match pos {
(row, col) => self.rows[row] || self.cols[col]
}
}
#[inline]
pub fn is_uncovered(&self, pos: (usize, usize)) -> bool {
!self.is_covered(pos)
}
#[inline]
pub fn is_row_covered(&self, row: usize) -> bool {
self.rows[row]
}
#[inline]
pub fn is_col_covered(&self, col: usize) -> bool {
self.cols[col]
}
#[inline]
pub fn cover(&mut self, pos: (usize, usize)) {
match pos {
(row, col) => {
self.cover_row(row);
self.cover_col(col);
}
}
}
#[inline]
pub fn cover_col(&mut self, col: usize) {
self.cols.set(col, true);
}
#[inline]
pub fn uncover_col(&mut self, col: usize) {
self.cols.set(col, false);
}
#[inline]
pub fn cover_row(&mut self, row: usize) {
self.rows.set(row, true);
}
pub fn clear(&mut self) {
self.rows.clear();
self.cols.clear();
}
}

View File

@ -23,74 +23,10 @@ use std::ops::{Neg, Sub};
use std::num::Zero; use std::num::Zero;
use std::cmp; use std::cmp;
use square_matrix::SquareMatrix; use square_matrix::SquareMatrix;
use bit_vec::BitVec; use coverage::Coverage;
mod square_matrix; mod square_matrix;
mod coverage;
#[derive(Debug)]
struct Coverage {
rows: BitVec,
cols: BitVec,
}
impl Coverage {
// XXX: Is this needed?
fn n(&self) -> usize {
let n1 = self.rows.len();
let n2 = self.cols.len();
assert!(n1 == n2);
return n1;
}
fn new(n: usize) -> Coverage {
Coverage {rows: BitVec::from_elem(n, false),
cols: BitVec::from_elem(n, false)}
}
fn is_covered(&self, pos: (usize, usize)) -> bool {
match pos {
(row, col) => self.rows[row] || self.cols[col]
}
}
fn is_uncovered(&self, pos: (usize, usize)) -> bool {
!self.is_covered(pos)
}
fn is_row_covered(&self, row: usize) -> bool {
self.rows[row]
}
fn is_col_covered(&self, col: usize) -> bool {
self.cols[col]
}
fn cover(&mut self, pos: (usize, usize)) {
match pos {
(row, col) => {
self.cover_row(row);
self.cover_col(col);
}
}
}
fn cover_col(&mut self, col: usize) {
self.cols.set(col, true);
}
fn uncover_col(&mut self, col: usize) {
self.cols.set(col, false);
}
fn cover_row(&mut self, row: usize) {
self.rows.set(row, true);
}
fn clear(&mut self) {
self.rows.clear();
self.cols.clear();
}
}
#[derive(Debug)] #[derive(Debug)]
pub struct WeightMatrix<T: Copy> { pub struct WeightMatrix<T: Copy> {