val.icc
Go to the documentation of this file.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 namespace Gecode { namespace Int { namespace Distinct {
00039
00040
00041
00042
00043
00044 template <class View, bool complete>
00045 ExecStatus
00046 prop_val(Space* home, ViewArray<View>& x) {
00047 assert(x.size() > 1);
00048 int n = x.size();
00049
00050 GECODE_AUTOARRAY(int, stack, n);
00051 int* c_v = &stack[0];
00052
00053 int c_n = 0;
00054
00055
00056 for (int i = n; i--; )
00057 if (x[i].assigned()) {
00058 c_v[c_n++]=x[i].val(); x[i]=x[--n];
00059 }
00060
00061
00062 int t = 0;
00063 do {
00064 t++;
00065 if (!complete && (t > 16)) {
00066
00067
00068
00069 ExecStatus es = ES_FIX;
00070 while (c_n > 0) {
00071 int v = c_v[--c_n];
00072
00073 for (int i = c_n; i--; )
00074 if (c_v[i] == v)
00075 goto failed;
00076
00077 for (int i = n; i--; ) {
00078 ModEvent me = x[i].nq(home,v);
00079 if (me_failed(me))
00080 goto failed;
00081 if (me == ME_INT_VAL)
00082 es = ES_NOFIX;
00083 }
00084 }
00085 x.size(n);
00086 return es;
00087 }
00088 if (c_n > 31) {
00089
00090 IntSet d(&c_v[0],c_n);
00091
00092 unsigned int s = 0;
00093 for (int i = d.size(); i--; )
00094 s += d.width(i);
00095
00096
00097 if (s != static_cast<unsigned int>(c_n))
00098 goto failed;
00099
00100 c_n = 0;
00101
00102 for (int i = n; i--; )
00103 if ((d.min() <= x[i].max()) && (d.max() >= x[i].min())) {
00104 IntSetRanges dr(d);
00105 ModEvent me = x[i].minus_r(home,dr,false);
00106 if (me_failed(me))
00107 goto failed;
00108 if (me == ME_INT_VAL) {
00109 c_v[c_n++]=x[i].val(); x[i]=x[--n];
00110 }
00111 }
00112 } else {
00113
00114 int* n_v = &c_v[c_n];
00115
00116 int n_n = 0;
00117 while (c_n > 0) {
00118 int v = c_v[--c_n];
00119
00120 for (int i = c_n; i--; )
00121 if (c_v[i] == v)
00122 goto failed;
00123
00124 for (int i = n_n; i--; )
00125 if (n_v[i] == v)
00126 goto failed;
00127
00128 for (int i = n; i--; ) {
00129 ModEvent me = x[i].nq(home,v);
00130 if (me_failed(me))
00131 goto failed;
00132 if (me == ME_INT_VAL) {
00133 n_v[n_n++]=x[i].val(); x[i]=x[--n];
00134 }
00135 }
00136 }
00137 c_v = n_v; c_n = n_n;
00138 }
00139 } while (c_n > 0);
00140 x.size(n);
00141 return ES_FIX;
00142 failed:
00143 x.size(0);
00144 return ES_FAILED;
00145 }
00146
00147
00148
00149
00150
00151
00152 template <class View>
00153 forceinline
00154 Val<View>::Val(Space* home, ViewArray<View>& x)
00155 : NaryPropagator<View,PC_INT_VAL>(home,x) {}
00156
00157 template <class View>
00158 forceinline
00159 Val<View>::Val(Space* home, bool share, Val<View>& p)
00160 : NaryPropagator<View,PC_INT_VAL>(home,share,p) {}
00161
00162 template <class View>
00163 Actor*
00164 Val<View>::copy(Space* home, bool share) {
00165 return new (home) Val<View>(home,share,*this);
00166 }
00167
00168 template <class View>
00169 Support::Symbol
00170 Val<View>::ati(void) {
00171 return Reflection::mangle<View>("Gecode::Int::Distinct::Val");
00172 }
00173
00174 template <class View>
00175 Reflection::ActorSpec
00176 Val<View>::spec(const Space* home, Reflection::VarMap& m) const {
00177 return NaryPropagator<View,PC_INT_VAL>::spec(home, m, ati());
00178 }
00179
00180 template <class View>
00181 ExecStatus
00182 Val<View>::propagate(Space* home, ModEventDelta) {
00183 GECODE_ES_CHECK((prop_val<View,true>(home,x)));
00184 return (x.size() < 2) ? ES_SUBSUMED(this,home) : ES_FIX;
00185 }
00186
00187 template <class View>
00188 ExecStatus
00189 Val<View>::post(Space* home, ViewArray<View>& x) {
00190 if (x.size() == 2)
00191 return Rel::Nq<View>::post(home,x[0],x[1]);
00192 if (x.size() > 2)
00193 (void) new (home) Val<View>(home,x);
00194 return ES_OK;
00195 }
00196
00197 template <class View>
00198 void
00199 Val<View>::post(Space* home, Reflection::VarMap& vars,
00200 const Reflection::ActorSpec& spec) {
00201 spec.checkArity(1);
00202 ViewArray<View> x(home, vars, spec[0]);
00203 (void) new (home) Val<View>(home, x);
00204 }
00205
00206 }}}
00207
00208
00209