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.java69
1 files changed, 61 insertions, 8 deletions
diff --git a/src/jrummikub/ai/fdsolver/Var.java b/src/jrummikub/ai/fdsolver/Var.java
index a2b5cd7..eb6f432 100644
--- a/src/jrummikub/ai/fdsolver/Var.java
+++ b/src/jrummikub/ai/fdsolver/Var.java
@@ -2,24 +2,77 @@ package jrummikub.ai.fdsolver;
import java.util.Collection;
import java.util.HashSet;
-import java.util.Set;
+import java.util.Iterator;
public class Var<T> {
- Set<T> range;
-
+ private Solver solver;
+ private HashSet<T> range;
+
public Var(Solver solver, Collection<T> range) {
+ this.solver = solver;
this.range = new HashSet<T>(range);
}
- public static <T> Var<T> range(Solver solver, T low, T high) {
- // TODO todo todo todo
- return null;
- }
-
public T getValue() {
if (range.size() != 1)
return null;
return range.iterator().next();
}
+ public HashSet<T> getRange() {
+ return range;
+ }
+
+ public void choose(T value) {
+ for (Iterator<T> i = this.iterator(); i.hasNext();) {
+ if (i.next() != value) {
+ i.remove();
+ }
+ }
+ }
+
+ public void makeDirty() {
+ this.solver.dirtyVars.add(this);
+ }
+
+ public void invalidate(T value) {
+ range.remove(value);
+ solver.logInvalidation(this, value);
+ makeDirty();
+ }
+
+ 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;
+ }
+ }
+ };
+ }
+
+ @Override
+ public String toString() {
+ return "Var" + range;
+ }
+
}