MarkMatrix - Refactor
This commit is contained in:
parent
ec53b07747
commit
a870cb4cc7
@ -36,17 +36,14 @@ impl MarkMatrix {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn toggle_star(&mut self, pos: Position) {
|
pub fn toggle_star(&mut self, pos: Position) {
|
||||||
if self.is_star(pos) {
|
let cell = self.marks.get_mut((pos.row, pos.column)).unwrap();
|
||||||
self.unmark(pos);
|
if *cell == Mark::Star {
|
||||||
|
*cell = Mark::None;
|
||||||
} else {
|
} else {
|
||||||
self.star(pos);
|
*cell = Mark::Star;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn unmark(&mut self, pos: Position) {
|
|
||||||
self.set_mark(pos, Mark::None);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn star(&mut self, pos: Position) {
|
pub fn star(&mut self, pos: Position) {
|
||||||
self.set_mark(pos, Mark::Star);
|
self.set_mark(pos, Mark::Star);
|
||||||
}
|
}
|
||||||
@ -77,56 +74,54 @@ impl MarkMatrix {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
pub fn each_star<F>(&self, mut f: F)
|
pub fn each_star<F>(&self, mut f: F)
|
||||||
where
|
where
|
||||||
F: FnMut(Position),
|
F: FnMut(Position),
|
||||||
{
|
{
|
||||||
let n = self.n();
|
for (row, row_data) in self.marks.genrows().into_iter().enumerate() {
|
||||||
|
for (column, &cell) in row_data.iter().enumerate() {
|
||||||
for row in 0..n {
|
if cell == Mark::Star {
|
||||||
for column in 0..n {
|
f(Position { row, column });
|
||||||
let pos = Position { row, column };
|
|
||||||
if self.is_star(pos) {
|
|
||||||
f(pos);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn find_first_mark_in_row(&self, row: usize, mark: Mark) -> Option<usize> {
|
||||||
|
self.marks
|
||||||
|
.row(row)
|
||||||
|
.iter()
|
||||||
|
.enumerate()
|
||||||
|
.find(|(_, &cell)| cell == mark)
|
||||||
|
.map(|(column, _)| column)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn find_first_mark_in_column(&self, column: usize, mark: Mark) -> Option<usize> {
|
||||||
|
self.marks
|
||||||
|
.column(column)
|
||||||
|
.iter()
|
||||||
|
.enumerate()
|
||||||
|
.find(|(_, &cell)| cell == mark)
|
||||||
|
.map(|(row, _)| row)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn find_first_star_in_row(&self, row: usize) -> Option<usize> {
|
pub fn find_first_star_in_row(&self, row: usize) -> Option<usize> {
|
||||||
for column in 0..self.n() {
|
self.find_first_mark_in_row(row, Mark::Star)
|
||||||
if self.is_star(Position { row, column }) {
|
|
||||||
return Some(column);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return None;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn find_first_prime_in_row(&self, row: usize) -> Option<usize> {
|
pub fn find_first_prime_in_row(&self, row: usize) -> Option<usize> {
|
||||||
for column in 0..self.n() {
|
self.find_first_mark_in_row(row, Mark::Prime)
|
||||||
if self.is_prime(Position { row, column }) {
|
|
||||||
return Some(column);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return None;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn find_first_star_in_column(&self, column: usize) -> Option<usize> {
|
pub fn find_first_star_in_column(&self, column: usize) -> Option<usize> {
|
||||||
for row in 0..self.n() {
|
self.find_first_mark_in_column(column, Mark::Star)
|
||||||
if self.is_star(Position { row, column }) {
|
|
||||||
return Some(row);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return None;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn clear_primes(&mut self) {
|
pub fn clear_primes(&mut self) {
|
||||||
for row in 0..self.n() {
|
for cell in self.marks.iter_mut() {
|
||||||
for column in 0..self.n() {
|
if *cell == Mark::Prime {
|
||||||
let pos = Position { row, column };
|
*cell = Mark::None
|
||||||
if self.is_prime(pos) {
|
|
||||||
self.unmark(pos);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user