puzzle_solver/constraint/
mod.rs

1//! Constraint trait, and some common constraints.
2//!
3//! Note that all puzzle states visited during the solution search
4//! share the same set of constraint objects.  This means that you
5//! cannot store additional information about the state (e.g. caches)
6//! in the constraint to reuse later.
7
8use std::rc::Rc;
9
10use ::{PsResult,PuzzleSearch,Val,VarToken};
11
12/// Constraint trait.
13pub trait Constraint {
14    /// An iterator over the variables that are involved in the constraint.
15    fn vars<'a>(&'a self) -> Box<Iterator<Item=&'a VarToken> + 'a>;
16
17    /// Applied after a variable has been assigned.
18    fn on_assigned(&self, _search: &mut PuzzleSearch, _var: VarToken, _val: Val)
19            -> PsResult<()> {
20        Ok(())
21    }
22
23    /// Applied after a variable's candidates has been modified.
24    fn on_updated(&self, _search: &mut PuzzleSearch) -> PsResult<()> {
25        Ok(())
26    }
27
28    /// Substitute the "from" variable with the "to" variable.
29    ///
30    /// Returns a new constraint with all instances of "from" replaced
31    /// with "to", or Err if a contradiction was found.
32    fn substitute(&self, from: VarToken, to: VarToken)
33            -> PsResult<Rc<Constraint>>;
34}
35
36pub use self::alldifferent::AllDifferent;
37pub use self::equality::Equality;
38pub use self::unify::Unify;
39
40mod alldifferent;
41mod equality;
42mod unify;