00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 #include "gecode/set.hh"
00041
00042
00043 namespace Gecode { namespace Set {
00044
00045
00046
00047
00048
00049 ModEvent
00050 SetVarImp::cardMin_full(Space* home) {
00051 ModEvent me = ME_SET_CARD;
00052 if (_cardMin == lub.size()) {
00053 glb.become(home, lub);
00054 me = ME_SET_VAL;
00055 }
00056 SetDelta d;
00057 return notify(home, me, &d);
00058 }
00059
00060 ModEvent
00061 SetVarImp::cardMax_full(Space* home) {
00062 ModEvent me = ME_SET_CARD;
00063 if (_cardMax == glb.size()) {
00064 lub.become(home, glb);
00065 me = ME_SET_VAL;
00066 }
00067 SetDelta d;
00068 return notify(home, me, &d);
00069 }
00070
00071 ModEvent
00072 SetVarImp::processLubChange(Space* home, SetDelta& d) {
00073 ModEvent me = ME_SET_LUB;
00074 if (_cardMax > lub.size()) {
00075 _cardMax = lub.size();
00076 if (_cardMin > _cardMax) {
00077 glb.become(home, lub);
00078 _cardMin = glb.size();
00079 _cardMax = glb.size();
00080 return ME_SET_FAILED;
00081 }
00082 me = ME_SET_CLUB;
00083 }
00084 if (_cardMax == lub.size() && _cardMin == _cardMax) {
00085 glb.become(home, lub);
00086 me = ME_SET_VAL;
00087 assert(d.glbMin() == 1);
00088 assert(d.glbMax() == 0);
00089 }
00090 return notify(home, me, &d);
00091 }
00092
00093 ModEvent
00094 SetVarImp::processGlbChange(Space* home, SetDelta& d) {
00095 ModEvent me = ME_SET_GLB;
00096 if (_cardMin < glb.size()) {
00097 _cardMin = glb.size();
00098 if (_cardMin > _cardMax) {
00099 glb.become(home, lub);
00100 _cardMin = glb.size();
00101 _cardMax = glb.size();
00102 return ME_SET_FAILED;
00103 }
00104 me = ME_SET_CGLB;
00105 }
00106 if (_cardMin == glb.size() && _cardMin == _cardMax) {
00107 lub.become(home, glb);
00108 me = ME_SET_VAL;
00109 assert(d.lubMin() == 1);
00110 assert(d.lubMax() == 0);
00111 }
00112 return notify(home, me, &d);
00113 }
00114
00115
00116
00117
00118
00119
00120 forceinline
00121 SetVarImp::SetVarImp(Space* home, bool share, SetVarImp& x)
00122 : SetVarImpBase(home,share,x),
00123 _cardMin(x._cardMin), _cardMax(x._cardMax) {
00124 lub.update(home, x.lub);
00125 if (x.assigned()) {
00126 glb.become(home,lub);
00127 } else {
00128 glb.update(home,x.glb);
00129 }
00130 }
00131
00132
00133 SetVarImp*
00134 SetVarImp::perform_copy(Space* home, bool share) {
00135 return new (home) SetVarImp(home,share,*this);
00136 }
00137
00138 Reflection::Arg*
00139 SetVarImp::spec(const Space*, Reflection::VarMap& m) const {
00140 int specIndex = m.index(this);
00141 if (specIndex != -1)
00142 return Reflection::Arg::newVar(specIndex);
00143
00144 int glbsize = 0;
00145 {
00146 BndSetRanges lrs(glb);
00147 while (lrs()) { glbsize++; ++lrs; }
00148 }
00149
00150 Reflection::IntArrayArg* glbdom = Reflection::Arg::newIntArray(glbsize*2);
00151 BndSetRanges lr(glb);
00152 for (int count=0; lr(); ++lr) {
00153 (*glbdom)[count++] = lr.min();
00154 (*glbdom)[count++] = lr.max();
00155 }
00156
00157 int lubsize = 0;
00158 {
00159 BndSetRanges urs(lub);
00160 while (urs()) { lubsize++; ++urs; }
00161 }
00162
00163 BndSetRanges ur(lub);
00164 Reflection::IntArrayArg* lubdom = Reflection::Arg::newIntArray(lubsize*2);
00165 for (int count=0; ur(); ++ur) {
00166 (*lubdom)[count++] = ur.min();
00167 (*lubdom)[count++] = ur.max();
00168 }
00169
00170 Reflection::Arg* pair =
00171 Reflection::Arg::newPair(
00172 Reflection::Arg::newPair(glbdom, Reflection::Arg::newInt(_cardMin)),
00173 Reflection::Arg::newPair(lubdom, Reflection::Arg::newInt(_cardMax)));
00174
00175 Reflection::VarSpec* spec = new Reflection::VarSpec(vti, pair,
00176 assigned());
00177 return (Reflection::Arg::newVar(m.put(this, spec)));
00178 }
00179
00180 VarImpBase*
00181 SetVarImp::create(Space* home, Reflection::VarSpec& spec) {
00182 int cardMin = spec.dom()->first()->second()->toInt();
00183 int cardMax = spec.dom()->second()->second()->toInt();
00184 Reflection::IntArrayArgRanges
00185 glb(spec.dom()->first()->first()->toIntArray());
00186 Reflection::IntArrayArgRanges
00187 lub(spec.dom()->second()->first()->toIntArray());
00188 return new (home) SetVarImp(home, IntSet(glb), IntSet(lub),
00189 cardMin, cardMax);
00190 }
00191
00192 void
00193 SetVarImp::constrain(Space* home, VarImpBase* v,
00194 Reflection::VarSpec& spec) {
00195 int cardMin = spec.dom()->first()->second()->toInt();
00196 int cardMax = spec.dom()->second()->second()->toInt();
00197 Reflection::IntArrayArgRanges
00198 glb(spec.dom()->first()->first()->toIntArray());
00199 Reflection::IntArrayArgRanges
00200 lub(spec.dom()->second()->first()->toIntArray());
00201 SetVarImp* s = static_cast<SetVarImp*>(v);
00202 GECODE_ME_FAIL(home, s->cardMin(home, cardMin));
00203 GECODE_ME_FAIL(home, s->cardMax(home, cardMax));
00204 GECODE_ME_FAIL(home, s->includeI(home, glb));
00205 GECODE_ME_FAIL(home, s->intersectI(home, lub));
00206 }
00207
00208 namespace {
00209 Reflection::VarImpRegistrar<SetVarImp> registerSetVarImp;
00210 }
00211
00212 }}
00213
00214
00215