Store constraints in Rc instead of Box.

We will implement unification by substituting one variable for
another, creating a new set of constraints.  Only the affected
constraints need to perform the substitution, the others can simply be
cloned.  As such, we will store constraints using reference counting.
This commit is contained in:
David Wang 2017-03-08 08:02:37 +11:00
parent ddf425efe0
commit bae1138501

View File

@ -39,7 +39,7 @@ pub struct Puzzle {
candidates: Vec<Candidates>,
// The list of puzzle constraints.
constraints: Vec<Box<Constraint>>,
constraints: Vec<Rc<Constraint>>,
// The list of constraints that each variable belongs to. These
// will be woken up when the variable's candidates are changed.
@ -292,7 +292,7 @@ impl Puzzle {
/// Add a constraint to the puzzle solution.
pub fn add_constraint<T>(&mut self, constraint: T)
where T: Constraint + 'static {
let constraint = Box::new(constraint);
let constraint = Rc::new(constraint);
let cidx = self.constraints.len();
for &VarToken(idx) in constraint.vars() {
self.wake[idx].insert(cidx);