Add convenience functions for unify.

This commit is contained in:
David Wang 2017-03-11 10:38:15 +11:00
parent f265a165dd
commit 93cf70c0ca
2 changed files with 21 additions and 5 deletions

View File

@ -60,7 +60,6 @@ impl Constraint for Unify {
#[cfg(test)]
mod tests {
use ::Puzzle;
use super::Unify;
#[test]
fn test_unify_alldifferent() {
@ -69,7 +68,7 @@ mod tests {
let v1 = puzzle.new_var_with_candidates(&[1,2]);
puzzle.all_different(&[v0, v1]);
puzzle.add_constraint(Unify::new(v0, v1));
puzzle.unify(v0, v1);
let search = puzzle.step();
assert!(search.is_none());
@ -83,7 +82,7 @@ mod tests {
let v2 = puzzle.new_var_with_candidates(&[1,2,3,4]);
puzzle.equals(v0 + 2 * v1 + v2, 6);
puzzle.add_constraint(Unify::new(v0, v1));
puzzle.unify(v0, v1);
let search = puzzle.step().expect("contradiction");
assert_eq!(search[v0], 1);
@ -98,8 +97,8 @@ mod tests {
let v1 = puzzle.new_var_with_candidates(&[1,2,3,4]);
let v2 = puzzle.new_var_with_candidates(&[1,2,3,4]);
puzzle.add_constraint(Unify::new(v0, v1));
puzzle.add_constraint(Unify::new(v1, v2));
puzzle.unify(v0, v1);
puzzle.unify(v1, v2);
let search = puzzle.step().expect("contradiction");
assert_eq!(search[v0], 1);

View File

@ -333,6 +333,23 @@ impl Puzzle {
self.add_constraint(constraint::Equality::new(lhs.into() - rhs.into()));
}
/// Add a Unify constraint.
///
/// # Examples
///
/// ```
/// let mut send_more_money = puzzle_solver::Puzzle::new();
/// let carry = send_more_money.new_vars_with_candidates_1d(4, &[0,1]);
/// let vars = send_more_money.new_vars_with_candidates_1d(8,
/// &[0,1,2,3,4,5,6,7,8,9]);
///
/// let m = vars[4];
/// send_more_money.unify(m, carry[3]);
/// ```
pub fn unify(&mut self, var1: VarToken, var2: VarToken) {
self.add_constraint(constraint::Unify::new(var1, var2));
}
/// Find any solution to the given puzzle.
///
/// # Examples