Skip to content

Commit 0bc54e6

Browse files
committed
bump
1 parent 8fab8d3 commit 0bc54e6

File tree

1 file changed

+93
-1
lines changed
  • rust_puzzle/src/constraint

1 file changed

+93
-1
lines changed

rust_puzzle/src/constraint/mod.rs

+93-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,97 @@
1+
use std::any::Any;
2+
3+
use crate::SudokuGrid;
4+
5+
pub mod composite;
6+
pub mod irreducible;
7+
pub mod reducible;
8+
9+
pub use composite::*;
10+
pub use irreducible::*;
11+
pub use reducible::*;
12+
13+
pub type Group = Vec<(usize, usize)>;
14+
15+
#[inline]
16+
pub(crate) fn default_check<C>(this: &C, grid: &SudokuGrid) -> bool
17+
where C: Constraint + ?Sized {
18+
let size = grid.size();
19+
20+
for row in 0..size {
21+
for column in 0..size {
22+
if !this.check_cell(grid, column, row) {
23+
return false;
24+
}
25+
}
26+
}
27+
28+
true
29+
}
30+
31+
#[inline]
32+
pub(crate) fn default_check_cell<C>(this: &C, grid: &SudokuGrid, column: usize, row: usize) -> bool
33+
where C: Constraint + ?Sized {
34+
if let Some(number) = grid.get_cell(column, row).unwrap() {
35+
this.check_number(grid, column, row, number)
36+
}
37+
else {
38+
true
39+
}
40+
}
41+
42+
#[derive(Debug)]
43+
pub enum ReductionError {
44+
InvalidReduction
45+
}
46+
147
pub trait Constraint{
248
type Reduction;
349
type RevertInfo;
4-
//line 215
50+
51+
fn check(&self, grid: &SudokuGrid) -> bool {
52+
default_check(self, grid)
53+
}
54+
55+
fn check_cell(&self, grid: &SudokuGrid, column: usize, row: usize) -> bool {
56+
default_check_cell(self, grid, column, row)
57+
}
58+
59+
fn check_number(&self, grid: &SudokuGrid, column: usize, row: usize, number: usize) -> bool;
60+
61+
fn get_groups(&self, grid: &SudokuGrid) -> Vec<Group>;
62+
63+
fn list_reductions(&self, solution: &SudokuGrid) -> Vec<Self::Reduction>;
64+
65+
fn reduce(&mut self, solution: &SudokuGrid, reduction: &Self::Reduction) -> Result<Self::RevertInfo, ReductionError>;
66+
67+
fn revert(&mut self, solution: &SudokuGrid, reduction: &Self::Reduction, revert_info: Self::RevertInfo);
68+
69+
fn to_objects(&self) -> Vec<&dyn Any>
70+
where Self: Sized + 'static {
71+
vec![self]
72+
}
73+
}
74+
75+
pub trait Subconstraint {
76+
fn get_subconstraint<S: Constraint + Sized + 'static>(&self) -> Option<&S>;
77+
78+
fn has_subconstraints<S>(&self) -> bool
79+
where S: Constraint + Sized + 'static {
80+
self.get_subconstraint::<S>().is_some()
81+
}
82+
}
83+
84+
impl<C: Constraint + Sized + 'static> Subconstraint for C {
85+
fn get_subconstraint<S>(&self) -> Option<&S>
86+
where S: Constraint + Sized + 'static {
87+
for object in self.to_objects() {
88+
let subconstraint = object.downcast_ref();
89+
90+
if subconstraint.is_some() {
91+
return subconstraint;
92+
}
93+
}
94+
95+
None
96+
}
597
}

0 commit comments

Comments
 (0)