Use BitVec

This commit is contained in:
Michael Neumann 2015-10-19 23:03:22 +02:00
parent 018162fb35
commit 566f848524
2 changed files with 19 additions and 13 deletions

View File

@ -5,3 +5,4 @@ authors = ["Michael Neumann <mneumann@ntecs.de>"]
[dependencies] [dependencies]
nalgebra = "*" nalgebra = "*"
bit-vec = "*"

View File

@ -11,35 +11,40 @@
// TODO: // TODO:
// * Use bitarrays for Coverage.
// * Implement SquareMatrix. Get rid of nalgebra. // * Implement SquareMatrix. Get rid of nalgebra.
// * Reuse path Vec in step5 // * Reuse path Vec in step5
// * Cleanup // * Cleanup
extern crate nalgebra as na; extern crate nalgebra as na;
extern crate bit_vec;
use na::{DMat, BaseNum}; use na::{DMat, BaseNum};
use std::ops::{Neg, Sub}; 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;
mod square_matrix; mod square_matrix;
#[derive(Debug)] #[derive(Debug)]
struct Coverage { struct Coverage {
n: usize, rows: BitVec,
rows: Vec<bool>, cols: BitVec,
cols: Vec<bool>,
} }
impl Coverage { impl Coverage {
fn n(&self) -> usize { self.n } // 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 { fn new(n: usize) -> Coverage {
Coverage {n: n, Coverage {rows: BitVec::from_elem(n, false),
rows: (0..n).map(|_| false).collect(), cols: BitVec::from_elem(n, false)}
cols: (0..n).map(|_| false).collect(),}
} }
fn is_covered(&self, pos: (usize, usize)) -> bool { fn is_covered(&self, pos: (usize, usize)) -> bool {
@ -70,20 +75,20 @@ impl Coverage {
} }
fn cover_col(&mut self, col: usize) { fn cover_col(&mut self, col: usize) {
self.cols[col] = true; self.cols.set(col, true);
} }
fn uncover_col(&mut self, col: usize) { fn uncover_col(&mut self, col: usize) {
self.cols[col] = false; self.cols.set(col, false);
} }
fn cover_row(&mut self, row: usize) { fn cover_row(&mut self, row: usize) {
self.rows[row] = true; self.rows.set(row, true);
} }
fn clear(&mut self) { fn clear(&mut self) {
for e in self.rows.iter_mut() { *e = false; } self.rows.clear();
for e in self.cols.iter_mut() { *e = false; } self.cols.clear();
} }
} }