summaryrefslogtreecommitdiffstats
path: root/src/jrummikub/ai/fdsolver/Var.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/jrummikub/ai/fdsolver/Var.java')
-rw-r--r--src/jrummikub/ai/fdsolver/Var.java40
1 files changed, 36 insertions, 4 deletions
diff --git a/src/jrummikub/ai/fdsolver/Var.java b/src/jrummikub/ai/fdsolver/Var.java
index eb6f432..e023f34 100644
--- a/src/jrummikub/ai/fdsolver/Var.java
+++ b/src/jrummikub/ai/fdsolver/Var.java
@@ -4,13 +4,16 @@ import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
-public class Var<T> {
+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() {
@@ -23,7 +26,7 @@ public class Var<T> {
return range;
}
- public void choose(T value) {
+ void choose(T value) {
for (Iterator<T> i = this.iterator(); i.hasNext();) {
if (i.next() != value) {
i.remove();
@@ -31,15 +34,19 @@ public class Var<T> {
}
}
- public void makeDirty() {
+ void makeDirty() {
this.solver.dirtyVars.add(this);
}
- public void invalidate(T value) {
+ void invalidate(T value) {
range.remove(value);
solver.logInvalidation(this, value);
makeDirty();
}
+
+ HashSet<Constraint> getConstraints() {
+ return constraints;
+ }
public Iterator<T> iterator() {
final Iterator<T> iterator = range.iterator();
@@ -69,10 +76,35 @@ public class Var<T> {
}
};
}
+
+ 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 count;
+ }
+
+ @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());
+ }
}