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