package be.kuleuven.jchr.runtime.primitive;_x000D_
_x000D_
import java.util.Iterator;_x000D_
_x000D_
import be.kuleuven.jchr.runtime.Constraint;_x000D_
import be.kuleuven.jchr.runtime.FailureException;_x000D_
_x000D_
_x000D_
public class IntEqualitySolverImpl implements IntEqualitySolver {_x000D_
_x000D_
    public void tellEqual(LogicalInt X, int value) {_x000D_
        final LogicalInt Xrepr = X.find();_x000D_
        final boolean oldHasValue = Xrepr.hasValue;_x000D_
        _x000D_
        if (oldHasValue) {_x000D_
            if (Xrepr.value != value)_x000D_
                throw new FailureException("Cannot make equal " + Xrepr.value + " != " + value);_x000D_
        }_x000D_
        else {_x000D_
            Xrepr.value = value;_x000D_
            Xrepr.hasValue = true;_x000D_
_x000D_
            Xrepr.rehashAll();_x000D_
            _x000D_
            final Iterator<Constraint> observers = Xrepr.variableObservers.iterator();_x000D_
            while (observers.hasNext()) _x000D_
                observers.next().reactivate();   /* notify */_x000D_
        }_x000D_
    }_x000D_
_x000D_
    // TODO :: deze extra indirectie kan uiteraard weg in uiteindelijk versie..._x000D_
    public void tellEqual(int val, LogicalInt X) {_x000D_
        tellEqual(X, val);_x000D_
    }_x000D_
    _x000D_
    public void tellEqual(LogicalInt X, LogicalInt Y) {_x000D_
        final LogicalInt Xrepr = X.find();_x000D_
        final LogicalInt Yrepr = Y.find();_x000D_
        _x000D_
        if (Xrepr != Yrepr) {_x000D_
            final boolean _x000D_
                XhasValue = Xrepr.hasValue, _x000D_
	        	YhasValue = Yrepr.hasValue;_x000D_
            _x000D_
            final int Xrank = Xrepr.rank;_x000D_
            int Yrank = Yrepr.rank;_x000D_
            _x000D_
            if (Xrank >= Yrank) {_x000D_
                Yrepr.parent = Xrepr;_x000D_
                if (Xrank == Yrank) Xrepr.rank++;_x000D_
                _x000D_
                if (! XhasValue) {_x000D_
                    if (YhasValue) {_x000D_
                        Xrepr.value = Yrepr.value;_x000D_
                        Xrepr.hasValue = true;_x000D_
                        Xrepr.rehashAll();_x000D_
                    }_x000D_
                } else /* XhasValue */ {_x000D_
                    if (YhasValue && Xrepr.value != Yrepr.value)_x000D_
                        throw new FailureException("Cannot make equal " _x000D_
                                + Xrepr.value + " != " + Yrepr.value);_x000D_
                }_x000D_
                _x000D_
                if (Yrepr.hashObservers != null) {_x000D_
                    Xrepr.mergeHashObservers(Yrepr.hashObservers);_x000D_
                    Yrepr.hashObservers = null;_x000D_
                }_x000D_
                _x000D_
                Xrepr.variableObservers.mergeWith(Yrepr.variableObservers);_x000D_
                Yrepr.variableObservers = null;_x000D_
                _x000D_
                final Iterator<Constraint> observers = Xrepr.variableObservers.iterator();_x000D_
                while (observers.hasNext()) _x000D_
                    observers.next().reactivate();   /* notify */_x000D_
            }_x000D_
            else {_x000D_
                Xrepr.parent = Yrepr;_x000D_
                _x000D_
                if (! YhasValue) {_x000D_
                    if (XhasValue) {_x000D_
                        Yrepr.value = Xrepr.value;_x000D_
                        Yrepr.hasValue = true;_x000D_
                        _x000D_
                        Yrepr.rehashAll();_x000D_
                    }_x000D_
                } else /* YhasValue */ {_x000D_
                    if (XhasValue & Xrepr.value != Yrepr.value)_x000D_
                        throw new FailureException("Cannot make equal " _x000D_
                                + Xrepr.value + " != " + Yrepr.value);_x000D_
                }_x000D_
                _x000D_
                if (Xrepr.hashObservers != null) {_x000D_
                    Yrepr.mergeHashObservers(Xrepr.hashObservers);_x000D_
                    Xrepr.hashObservers = null;_x000D_
                }_x000D_
                _x000D_
                Yrepr.variableObservers.mergeWith(Xrepr.variableObservers);                _x000D_
                Xrepr.variableObservers = null;_x000D_
                _x000D_
                final Iterator<Constraint> observers = Yrepr.variableObservers.iterator();_x000D_
                while (observers.hasNext()) _x000D_
                    observers.next().reactivate();   /* notify */_x000D_
            }_x000D_
        }_x000D_
    }_x000D_
_x000D_
    public boolean askEqual(LogicalInt X, int value) {_x000D_
        final LogicalInt representative = X.find();_x000D_
        return representative.hasValue && representative.value == value;_x000D_
    }_x000D_
    _x000D_
    public boolean askEqual(int value, LogicalInt X) {_x000D_
        final LogicalInt representative = X.find();_x000D_
        return representative.hasValue && representative.value == value;_x000D_
    }_x000D_
_x000D_
    public boolean askEqual(LogicalInt X, LogicalInt Y) {_x000D_
        final LogicalInt Xrepr = X.find();_x000D_
        final LogicalInt Yrepr = Y.find();_x000D_
        _x000D_
        return (Xrepr == Yrepr) _x000D_
            || (Xrepr.hasValue && Yrepr.hasValue && Xrepr.value == Yrepr.value);_x000D_
    }_x000D_
}
// TODO :: deze extra indirectie kan uiteraard weg in uiteindelijk versie..._x000D_
package be.kuleuven.jchr.runtime.primitive;_x000D_
_x000D_
import java.util.Iterator;_x000D_
_x000D_
import be.kuleuven.jchr.runtime.Constraint;_x000D_
import be.kuleuven.jchr.runtime.FailureException;_x000D_
_x000D_
_x000D_
public class IntEqualitySolverImpl implements IntEqualitySolver {_x000D_
_x000D_
    public void tellEqual(LogicalInt X, int value) {_x000D_
        final LogicalInt Xrepr = X.find();_x000D_
        final boolean oldHasValue = Xrepr.hasValue;_x000D_
        _x000D_
        if (oldHasValue) {_x000D_
            if (Xrepr.value != value)_x000D_
                throw new FailureException("Cannot make equal " + Xrepr.value + " != " + value);_x000D_
        }_x000D_
        else {_x000D_
            Xrepr.value = value;_x000D_
            Xrepr.hasValue = true;_x000D_
_x000D_
            Xrepr.rehashAll();_x000D_
            _x000D_
            final Iterator<Constraint> observers = Xrepr.variableObservers.iterator();_x000D_
            while (observers.hasNext()) _x000D_
                observers.next().reactivate();   /* notify */_x000D_
        }_x000D_
    }_x000D_
_x000D_
    // TODO ::<SUF>
    public void tellEqual(int val, LogicalInt X) {_x000D_
        tellEqual(X, val);_x000D_
    }_x000D_
    _x000D_
    public void tellEqual(LogicalInt X, LogicalInt Y) {_x000D_
        final LogicalInt Xrepr = X.find();_x000D_
        final LogicalInt Yrepr = Y.find();_x000D_
        _x000D_
        if (Xrepr != Yrepr) {_x000D_
            final boolean _x000D_
                XhasValue = Xrepr.hasValue, _x000D_
	        	YhasValue = Yrepr.hasValue;_x000D_
            _x000D_
            final int Xrank = Xrepr.rank;_x000D_
            int Yrank = Yrepr.rank;_x000D_
            _x000D_
            if (Xrank >= Yrank) {_x000D_
                Yrepr.parent = Xrepr;_x000D_
                if (Xrank == Yrank) Xrepr.rank++;_x000D_
                _x000D_
                if (! XhasValue) {_x000D_
                    if (YhasValue) {_x000D_
                        Xrepr.value = Yrepr.value;_x000D_
                        Xrepr.hasValue = true;_x000D_
                        Xrepr.rehashAll();_x000D_
                    }_x000D_
                } else /* XhasValue */ {_x000D_
                    if (YhasValue && Xrepr.value != Yrepr.value)_x000D_
                        throw new FailureException("Cannot make equal " _x000D_
                                + Xrepr.value + " != " + Yrepr.value);_x000D_
                }_x000D_
                _x000D_
                if (Yrepr.hashObservers != null) {_x000D_
                    Xrepr.mergeHashObservers(Yrepr.hashObservers);_x000D_
                    Yrepr.hashObservers = null;_x000D_
                }_x000D_
                _x000D_
                Xrepr.variableObservers.mergeWith(Yrepr.variableObservers);_x000D_
                Yrepr.variableObservers = null;_x000D_
                _x000D_
                final Iterator<Constraint> observers = Xrepr.variableObservers.iterator();_x000D_
                while (observers.hasNext()) _x000D_
                    observers.next().reactivate();   /* notify */_x000D_
            }_x000D_
            else {_x000D_
                Xrepr.parent = Yrepr;_x000D_
                _x000D_
                if (! YhasValue) {_x000D_
                    if (XhasValue) {_x000D_
                        Yrepr.value = Xrepr.value;_x000D_
                        Yrepr.hasValue = true;_x000D_
                        _x000D_
                        Yrepr.rehashAll();_x000D_
                    }_x000D_
                } else /* YhasValue */ {_x000D_
                    if (XhasValue & Xrepr.value != Yrepr.value)_x000D_
                        throw new FailureException("Cannot make equal " _x000D_
                                + Xrepr.value + " != " + Yrepr.value);_x000D_
                }_x000D_
                _x000D_
                if (Xrepr.hashObservers != null) {_x000D_
                    Yrepr.mergeHashObservers(Xrepr.hashObservers);_x000D_
                    Xrepr.hashObservers = null;_x000D_
                }_x000D_
                _x000D_
                Yrepr.variableObservers.mergeWith(Xrepr.variableObservers);                _x000D_
                Xrepr.variableObservers = null;_x000D_
                _x000D_
                final Iterator<Constraint> observers = Yrepr.variableObservers.iterator();_x000D_
                while (observers.hasNext()) _x000D_
                    observers.next().reactivate();   /* notify */_x000D_
            }_x000D_
        }_x000D_
    }_x000D_
_x000D_
    public boolean askEqual(LogicalInt X, int value) {_x000D_
        final LogicalInt representative = X.find();_x000D_
        return representative.hasValue && representative.value == value;_x000D_
    }_x000D_
    _x000D_
    public boolean askEqual(int value, LogicalInt X) {_x000D_
        final LogicalInt representative = X.find();_x000D_
        return representative.hasValue && representative.value == value;_x000D_
    }_x000D_
_x000D_
    public boolean askEqual(LogicalInt X, LogicalInt Y) {_x000D_
        final LogicalInt Xrepr = X.find();_x000D_
        final LogicalInt Yrepr = Y.find();_x000D_
        _x000D_
        return (Xrepr == Yrepr) _x000D_
            || (Xrepr.hasValue && Yrepr.hasValue && Xrepr.value == Yrepr.value);_x000D_
    }_x000D_
}