summaryrefslogtreecommitdiffstats
path: root/src/jrummikub/ai/fdsolver/constraint/ComparatorPropagator.java
blob: b3a30895252210a02973ab42528ff391b8de80ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package jrummikub.ai.fdsolver.constraint;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

import jrummikub.ai.fdsolver.Propagator;
import jrummikub.ai.fdsolver.Var;

public class ComparatorPropagator<T> implements Propagator {
	private Var<T> x, y;
	private Comparator<T> comparator;
	private boolean allowEqual;
	public ComparatorPropagator(Comparator<T> comparator, boolean allowEqual, Var<T> x, Var<T> y) {
		this.comparator = comparator;
		this.allowEqual = allowEqual;
		this.x = x;
		this.y = y;
	}

	@Override
	public Collection<Var<?>> getWatchedVars() {
		return Arrays.<Var<?>>asList(y);
	}

	@Override
	public void propagate() {
		T maxY = Collections.max(y.getRange(), comparator);
		
		for(Iterator<T> i = x.iterator(); i.hasNext();) {
			T value = i.next();
			int comparision = comparator.compare(value, maxY);
			if (comparision > 0 || comparision == 0 && !allowEqual) {
				i.remove();
			}
		}
	}
}