on_assing -> propagate

This commit is contained in:
Andrey Tkachenko 2021-12-09 22:36:40 +04:00
parent e363a552f0
commit 25a6634069
7 changed files with 11 additions and 9 deletions

View File

@ -37,7 +37,7 @@ impl Constraint for AllDifferent {
Box::new(self.vars.iter())
}
fn on_assigned(&self, search: &mut PuzzleSearch, var: VarToken, val: Val) -> PsResult<()> {
fn propagate(&self, search: &mut PuzzleSearch, var: VarToken, val: Val) -> PsResult<()> {
for &var2 in self.vars.iter().filter(|&v| *v != var) {
search.remove_candidate(var2, val)?;
}

View File

@ -35,7 +35,7 @@ impl Constraint for Equality {
Box::new(self.eqn.coef.keys())
}
fn on_assigned(&self, search: &mut PuzzleSearch, _var: VarToken, _val: Val) -> PsResult<()> {
fn propagate(&self, search: &mut PuzzleSearch, _var: VarToken, _val: Val) -> PsResult<()> {
let mut sum = self.eqn.constant;
let mut unassigned_var = None;

View File

@ -16,9 +16,7 @@ pub trait Constraint: Debug {
fn vars(&self) -> Box<dyn Iterator<Item = &'_ VarToken> + '_>;
/// Applied after a variable has been assigned.
fn on_assigned(&self, _search: &mut PuzzleSearch, _var: VarToken, _val: Val) -> PsResult<()> {
Ok(())
}
fn propagate(&self, _search: &mut PuzzleSearch, _var: VarToken, _val: Val) -> PsResult<()>;
/// Applied after a variable's candidates has been modified.
fn on_updated(&self, _search: &mut PuzzleSearch) -> PsResult<()> {

View File

@ -3,7 +3,7 @@
use std::iter;
use std::rc::Rc;
use crate::{Constraint, PsResult, PuzzleSearch, VarToken};
use crate::{Constraint, PsResult, PuzzleSearch, Val, VarToken};
#[derive(Debug)]
pub struct Unify {
@ -39,6 +39,10 @@ impl Constraint for Unify {
}
}
fn propagate(&self, _search: &mut PuzzleSearch, _var: VarToken, _val: Val) -> PsResult<()> {
Ok(())
}
fn on_updated(&self, search: &mut PuzzleSearch) -> PsResult<()> {
if self.var1 != self.var2 {
search.unify(self.var1, self.var2)

View File

@ -857,7 +857,7 @@ impl<'a> PuzzleSearch<'a> {
for cidx in 0..self.constraints.constraints.len() {
if self.constraints.wake[idx].contains(cidx) {
let constraint = self.constraints.constraints[cidx].clone();
constraint.on_assigned(self, var, val)?;
constraint.propagate(self, var, val)?;
}
}

View File

@ -17,7 +17,7 @@ impl Constraint for NoDiagonal {
Box::new(self.vars.iter())
}
fn on_assigned(&self, search: &mut PuzzleSearch, var: VarToken, val: Val) -> PsResult<()> {
fn propagate(&self, search: &mut PuzzleSearch, var: VarToken, val: Val) -> PsResult<()> {
let y1 = self
.vars
.iter()

View File

@ -23,7 +23,7 @@ impl Constraint for BinaryRepr {
Box::new(iter::once(&self.value).chain(&self.bits))
}
fn on_assigned(&self, search: &mut PuzzleSearch, var: VarToken, val: Val) -> PsResult<()> {
fn propagate(&self, search: &mut PuzzleSearch, var: VarToken, val: Val) -> PsResult<()> {
if var == self.value {
let mut val = val;
for &var in self.bits.iter() {