diff options
Diffstat (limited to 'src/jrummikub/ai/fdsolver/Var.java')
-rw-r--r-- | src/jrummikub/ai/fdsolver/Var.java | 113 |
1 files changed, 0 insertions, 113 deletions
diff --git a/src/jrummikub/ai/fdsolver/Var.java b/src/jrummikub/ai/fdsolver/Var.java deleted file mode 100644 index e885cef..0000000 --- a/src/jrummikub/ai/fdsolver/Var.java +++ /dev/null @@ -1,113 +0,0 @@ -package jrummikub.ai.fdsolver; - -import java.util.Collection; -import java.util.HashSet; -import java.util.Iterator; - -public class Var<T> implements Comparable<Var<T>> { - private Solver solver; - private HashSet<T> range; - private HashSet<Constraint> constraints; - private T recorded; - - public Var(Solver solver, Collection<T> range) { - this.solver = solver; - this.range = new HashSet<T>(range); - constraints = new HashSet<Constraint>(); - } - - public T getValue() { - if (range.size() != 1) - return null; - return range.iterator().next(); - } - - public HashSet<T> getRange() { - return range; - } - - void choose(T value) { - for (Iterator<T> i = this.iterator(); i.hasNext();) { - if (i.next() != value) { - i.remove(); - } - } - } - - void makeDirty() { - this.solver.dirtyVars.add(this); - } - - public void invalidate(T value) { - range.remove(value); - solver.logInvalidation(this, value); - makeDirty(); - if (range.size() == 0) { - solver.contradiction = true; - } - } - - HashSet<Constraint> getConstraints() { - return constraints; - } - - public Iterator<T> iterator() { - final Iterator<T> iterator = range.iterator(); - return new Iterator<T>() { - T lastValue; - - @Override - public boolean hasNext() { - return iterator.hasNext(); - } - - @Override - public T next() { - lastValue = iterator.next(); - return lastValue; - } - - @Override - public void remove() { - // TODO logging - iterator.remove(); - solver.logInvalidation(Var.this, lastValue); - makeDirty(); - if (range.size() == 0) { - solver.contradiction = true; - } - } - }; - } - - void record() { - recorded = getValue(); - } - - void restore() { - range.clear(); - range.add(recorded); - } - - @Override - public String toString() { - return "Var" + range; - } - - private int neighborCount() { - /* int count = 0; - for (Constraint constraint : constraints) { - count += constraint.getWatchedVars().size(); - } */ - return constraints.size(); - } - - @Override - public int compareTo(Var<T> other) { - int rangeCompare = ((Integer)range.size()).compareTo(other.range.size()); - if (rangeCompare != 0) - return rangeCompare; - return -((Integer)neighborCount()).compareTo(other.neighborCount()); - } - -} |