1/* BEGIN LICENSE BLOCK
2 * Version: CMPL 1.1
3 *
4 * The contents of this file are subject to the Cisco-style Mozilla Public
5 * License Version 1.1 (the "License"); you may not use this file except
6 * in compliance with the License.  You may obtain a copy of the License
7 * at www.eclipse-clp.org/license.
8 *
9 * Software distributed under the License is distributed on an "AS IS"
10 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.  See
11 * the License for the specific language governing rights and limitations
12 * under the License.
13 *
14 * The Original Code is  The ECLiPSe Constraint Logic Programming System.
15 * The Initial Developer of the Original Code is  Cisco Systems, Inc.
16 * Portions created by the Initial Developer are
17 * Copyright (C) 2006 Cisco Systems, Inc.  All Rights Reserved.
18 *
19 * Contributor(s): Pascal Brisset
20 *
21 * END LICENSE BLOCK */
22
23// $Id: outof.cc,v 1.1 2006/09/23 01:54:04 snovello Exp $
24
25#define OUTOF_CC
26#include "stdecil.h"
27
28class OutOfI: public IlcConstraintI {
29  IlcIntVar _var;
30  IlcIntVarArray _array;
31  IlcBool first_time;
32public:
33  OutOfI(IlcManager m, IlcIntVar var, IlcIntVarArray array):
34    IlcConstraintI(m), _var(var), _array(array) { first_time = IlcTrue;}
35  void propagate();
36  void post();
37  IlcBool isViolated() const;
38  void metaPost(IlcGoalI*);
39  IlcConstraintI* makeOpposite() const;
40};
41
42IlcConstraint
43OutOf(IlcIntVar var, IlcIntVarArray array)
44{
45  IlcManager m = var.getManager();
46  return new (m.getHeap()) OutOfI(m, var, array);
47}
48
49class OneOfI: public IlcConstraintI {
50  IlcIntVar _var;
51  IlcIntVarArray _array;
52  IlcBool first_time;
53public:
54  OneOfI(IlcManager m, IlcIntVar var, IlcIntVarArray array):
55    IlcConstraintI(m), _var(var), _array(array) { first_time = IlcTrue;}
56  void propagate();
57  void post();
58  IlcBool isViolated() const;
59  void metaPost(IlcGoalI*);
60  IlcConstraintI* makeOpposite() const;
61};
62
63IlcConstraint
64OneOf(IlcIntVar var, IlcIntVarArray array)
65{
66  IlcManager m = var.getManager();
67  return new (m.getHeap()) OneOfI(m, var, array);
68}
69
70IlcConstraintI*
71OutOfI::makeOpposite() const
72{
73  return OneOf(_var, _array).getImpl();
74}
75
76void
77OutOfI::post()
78{
79  _var.whenValue(this);
80  _array.whenValue(this);
81}
82
83void
84OutOfI::propagate()
85{
86  if (_var.isBound()) {
87    int i;
88    IlcInt val = _var.getValue();
89    for(i= 0; i < _array.getSize(); i++) {
90      _array[i].removeValue(val);
91    }
92  } else {
93    if (first_time == IlcTrue) {
94      first_time = IlcFalse;
95      for(int i = 0; i < _array.getSize(); i++) {
96	if (_array[i].isBound()) {
97	  _var.removeValue(_array[i].getValue());
98	}
99      }
100    } else {
101      int i = _array.getIndexValue();
102      _var.removeValue(_array[i].getValue());
103    }
104  }
105}
106
107IlcBool
108OutOfI::isViolated() const // if _var == _array[i] for some i
109{
110  if (_var.isBound()) {
111    IlcInt val = _var.getValue();
112    int i;
113    for(i= 0; i < _array.getSize(); i++) {
114      if (_array[i].isBound() && _array[i].getValue() == val) {
115	return IlcTrue;
116      }
117    }
118  }
119  return IlcFalse;
120}
121
122void
123OutOfI::metaPost(IlcGoalI* c)
124{
125  _var.whenValue(c);
126  _array.whenValue(c);
127}
128
129
130
131IlcConstraintI*
132OneOfI::makeOpposite() const
133{
134  return OutOf(_var, _array).getImpl();
135}
136
137void
138OneOfI::post()
139{
140  _var.whenValue(this);
141  _array.whenValue(this);
142}
143
144void
145OneOfI::propagate()
146{
147  if (_var.isBound()) {
148    int i;
149    IlcInt val = _var.getValue();
150    for(i= 0; i < _array.getSize(); i++) {
151      if ((! _array[i].isBound()) || val == val) {
152	return;
153      }
154    }
155    m.fail();
156  }
157}
158
159IlcBool
160OneOfI::isViolated() const // if _var != _array[i] for all i
161{
162  if (_var.isBound()) {
163    IlcInt val = _var.getValue();
164    int i;
165    for(i= 0; i < _array.getSize(); i++) {
166      if (_array[i].isBound() && _array[i].getValue() == val) {
167	return IlcFalse;
168      }
169    }
170    return IlcTrue;
171  }
172  return IlcFalse;
173}
174
175void
176OneOfI::metaPost(IlcGoalI* c)
177{
178  _var.whenValue(c);
179  _array.whenValue(c);
180}
181
182