Refactor MarkMatrix into mark_matrix.rs
This commit is contained in:
parent
be964d166e
commit
8faebad157
101
src/lib.rs
101
src/lib.rs
@ -23,9 +23,14 @@ use std::num::Zero;
|
||||
use std::cmp;
|
||||
use square_matrix::SquareMatrix;
|
||||
use coverage::Coverage;
|
||||
use mark_matrix::MarkMatrix;
|
||||
|
||||
#[cfg(test)]
|
||||
use mark_matrix::Mark;
|
||||
|
||||
pub mod square_matrix;
|
||||
mod coverage;
|
||||
mod mark_matrix;
|
||||
|
||||
pub trait WeightNum: Ord + Eq + Copy + Sub<Output=Self> + Add<Output=Self> + Zero {}
|
||||
|
||||
@ -122,102 +127,6 @@ enum Step {
|
||||
Done,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
||||
enum Mark {
|
||||
None,
|
||||
Star,
|
||||
Prime
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct MarkMatrix {
|
||||
marks: SquareMatrix<Mark>
|
||||
}
|
||||
|
||||
impl MarkMatrix {
|
||||
fn new(n: usize) -> MarkMatrix {
|
||||
MarkMatrix {marks: SquareMatrix::from_row_vec(n, (0..n*n).map(|_| Mark::None).collect())}
|
||||
}
|
||||
|
||||
fn n(&self) -> usize { self.marks.n() }
|
||||
|
||||
fn toggle_star(&mut self, pos: (usize, usize)) {
|
||||
if self.is_star(pos) {
|
||||
self.unmark(pos);
|
||||
} else {
|
||||
self.star(pos);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
fn get(&self, pos: (usize, usize)) -> Mark {
|
||||
self.marks[pos]
|
||||
}
|
||||
|
||||
fn unmark(&mut self, pos: (usize, usize)) {
|
||||
self.marks[pos] = Mark::None;
|
||||
}
|
||||
|
||||
fn star(&mut self, pos: (usize, usize)) {
|
||||
self.marks[pos] = Mark::Star;
|
||||
}
|
||||
|
||||
fn prime(&mut self, pos: (usize, usize)) {
|
||||
self.marks[pos] = Mark::Prime;
|
||||
}
|
||||
|
||||
fn is_star(&self, pos: (usize, usize)) -> bool {
|
||||
match self.marks[pos] {
|
||||
Mark::Star => true,
|
||||
_ => false
|
||||
}
|
||||
}
|
||||
|
||||
fn is_prime(&self, pos: (usize, usize)) -> bool {
|
||||
match self.marks[pos] {
|
||||
Mark::Prime => true,
|
||||
_ => false
|
||||
}
|
||||
}
|
||||
|
||||
fn find_first_star_in_row(&self, row: usize) -> Option<usize> {
|
||||
for col in 0..self.n() {
|
||||
if self.is_star((row, col)) {
|
||||
return Some(col);
|
||||
}
|
||||
}
|
||||
return None;
|
||||
}
|
||||
|
||||
fn find_first_prime_in_row(&self, row: usize) -> Option<usize> {
|
||||
for col in 0..self.n() {
|
||||
if self.is_prime((row, col)) {
|
||||
return Some(col);
|
||||
}
|
||||
}
|
||||
return None;
|
||||
}
|
||||
|
||||
fn find_first_star_in_col(&self, col: usize) -> Option<usize> {
|
||||
for row in 0..self.n() {
|
||||
if self.is_star((row, col)) {
|
||||
return Some(row);
|
||||
}
|
||||
}
|
||||
return None;
|
||||
}
|
||||
|
||||
fn clear_primes(&mut self) {
|
||||
for row in 0..self.n() {
|
||||
for col in 0..self.n() {
|
||||
if let Mark::Prime = self.marks[(row, col)] {
|
||||
self.marks[(row, col)] = Mark::None;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 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<T: WeightNum>(c: &mut WeightMatrix<T>) -> Step {
|
||||
|
97
src/mark_matrix.rs
Normal file
97
src/mark_matrix.rs
Normal file
@ -0,0 +1,97 @@
|
||||
use ::square_matrix::SquareMatrix;
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
||||
pub enum Mark {
|
||||
None,
|
||||
Star,
|
||||
Prime
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct MarkMatrix {
|
||||
marks: SquareMatrix<Mark>
|
||||
}
|
||||
|
||||
impl MarkMatrix {
|
||||
pub fn new(n: usize) -> MarkMatrix {
|
||||
MarkMatrix {marks: SquareMatrix::from_row_vec(n, (0..n*n).map(|_| Mark::None).collect())}
|
||||
}
|
||||
|
||||
pub fn n(&self) -> usize { self.marks.n() }
|
||||
|
||||
pub fn toggle_star(&mut self, pos: (usize, usize)) {
|
||||
if self.is_star(pos) {
|
||||
self.unmark(pos);
|
||||
} else {
|
||||
self.star(pos);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub fn get(&self, pos: (usize, usize)) -> Mark {
|
||||
self.marks[pos]
|
||||
}
|
||||
|
||||
pub fn unmark(&mut self, pos: (usize, usize)) {
|
||||
self.marks[pos] = Mark::None;
|
||||
}
|
||||
|
||||
pub fn star(&mut self, pos: (usize, usize)) {
|
||||
self.marks[pos] = Mark::Star;
|
||||
}
|
||||
|
||||
pub fn prime(&mut self, pos: (usize, usize)) {
|
||||
self.marks[pos] = Mark::Prime;
|
||||
}
|
||||
|
||||
pub fn is_star(&self, pos: (usize, usize)) -> bool {
|
||||
match self.marks[pos] {
|
||||
Mark::Star => true,
|
||||
_ => false
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_prime(&self, pos: (usize, usize)) -> bool {
|
||||
match self.marks[pos] {
|
||||
Mark::Prime => true,
|
||||
_ => false
|
||||
}
|
||||
}
|
||||
|
||||
pub fn find_first_star_in_row(&self, row: usize) -> Option<usize> {
|
||||
for col in 0..self.n() {
|
||||
if self.is_star((row, col)) {
|
||||
return Some(col);
|
||||
}
|
||||
}
|
||||
return None;
|
||||
}
|
||||
|
||||
pub fn find_first_prime_in_row(&self, row: usize) -> Option<usize> {
|
||||
for col in 0..self.n() {
|
||||
if self.is_prime((row, col)) {
|
||||
return Some(col);
|
||||
}
|
||||
}
|
||||
return None;
|
||||
}
|
||||
|
||||
pub fn find_first_star_in_col(&self, col: usize) -> Option<usize> {
|
||||
for row in 0..self.n() {
|
||||
if self.is_star((row, col)) {
|
||||
return Some(row);
|
||||
}
|
||||
}
|
||||
return None;
|
||||
}
|
||||
|
||||
pub fn clear_primes(&mut self) {
|
||||
for row in 0..self.n() {
|
||||
for col in 0..self.n() {
|
||||
if self.is_prime((row, col)) {
|
||||
self.unmark((row, col));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user