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
00041
00042 namespace Gecode { namespace Int { namespace Channel {
00043
00048 template <class View>
00049 class ValInfo {
00050 public:
00052 View view;
00054 bool a;
00056 static ValInfo<View>* allocate(Space* home, int n);
00058 void init(View x, int n);
00060 void update(Space* home, bool share, ValInfo<View>& vi);
00062 bool doval(void) const;
00064 bool dodom(void) const;
00066 void assigned(void);
00068 void removed(int i);
00070 void done(void);
00071 };
00072
00073 template <class View>
00074 forceinline ValInfo<View>*
00075 ValInfo<View>::allocate(Space* home, int n) {
00076 return static_cast<ValInfo<View>*>
00077 (home->alloc(n*sizeof(ValInfo<View>)));
00078 }
00079
00080 template <class View>
00081 forceinline void
00082 ValInfo<View>::init(View x, int) {
00083 view = x; a = false;
00084 }
00085
00086 template <class View>
00087 forceinline void
00088 ValInfo<View>::update(Space* home, bool share, ValInfo<View>& vi) {
00089 view.update(home,share,vi.view); a = vi.a;
00090 }
00091
00092 template <class View>
00093 forceinline bool
00094 ValInfo<View>::doval(void) const {
00095 return !a && view.assigned();
00096 }
00097
00098 template <class View>
00099 forceinline bool
00100 ValInfo<View>::dodom(void) const {
00101 return false;
00102 }
00103
00104 template <class View>
00105 forceinline void
00106 ValInfo<View>::assigned(void) {
00107 a = true;
00108 }
00109
00110 template <class View>
00111 forceinline void
00112 ValInfo<View>::removed(int) {}
00113
00114 template <class View>
00115 forceinline void
00116 ValInfo<View>::done(void) {}
00117
00118
00119
00120 template <class View, class Info>
00121 ExecStatus
00122 doprop_val(Space* home, int n, Info* x, Info* y,
00123 int& n_na, ProcessStack& xa, ProcessStack& ya) {
00124 do {
00125 int i = xa.pop();
00126 int j = x[i].view.val();
00127
00128 {
00129 ModEvent me = y[j].view.eq(home,i);
00130 if (me_failed(me))
00131 return ES_FAILED;
00132
00133 if (me_modified(me))
00134 ya.push(j);
00135 }
00136
00137 for (int k=i; k--; ) {
00138 ModEvent me = x[k].view.nq(home,j);
00139 if (me_failed(me))
00140 return ES_FAILED;
00141 if (me_modified(me)) {
00142 if (me == ME_INT_VAL) {
00143
00144 xa.push(k);
00145 } else {
00146
00147
00148
00149 x[k].removed(j);
00150 }
00151 }
00152 }
00153
00154 for (int k=i+1; k<n; k++) {
00155 ModEvent me = x[k].view.nq(home,j);
00156 if (me_failed(me))
00157 return ES_FAILED;
00158 if (me_modified(me)) {
00159 if (me == ME_INT_VAL) {
00160
00161 xa.push(k);
00162 } else {
00163
00164
00165
00166 x[k].removed(j);
00167 }
00168 }
00169 }
00170 x[i].assigned(); n_na--;
00171 } while (!xa.empty());
00172 return ES_OK;
00173 }
00174
00175
00176 template <class View, class Info>
00177 forceinline ExecStatus
00178 prop_val(Space* home, int n, Info* x, Info* y,
00179 int& n_na, ProcessStack& xa, ProcessStack& ya) {
00180 if (xa.empty())
00181 return ES_OK;
00182 return doprop_val<View,Info>(home,n,x,y,n_na,xa,ya);
00183 }
00184
00185
00186
00187
00188
00189 template <class View, bool shared>
00190 forceinline
00191 Val<View,shared>::Val(Space* home, int n, ValInfo<View>* xy)
00192 : Base<ValInfo<View>,PC_INT_VAL>(home,n,xy) {}
00193
00194 template <class View, bool shared>
00195 forceinline
00196 Val<View,shared>::Val(Space* home, bool share, Val<View,shared>& p)
00197 : Base<ValInfo<View>,PC_INT_VAL>(home,share,p) {}
00198
00199 template <class View, bool shared>
00200 Actor*
00201 Val<View,shared>::copy(Space* home, bool share) {
00202 return new (home) Val<View,shared>(home,share,*this);
00203 }
00204
00205 template <class View, bool shared>
00206 ExecStatus
00207 Val<View,shared>::propagate(Space* home, ModEventDelta) {
00208 GECODE_AUTOSTACK(int,-1, xa, n);
00209 GECODE_AUTOSTACK(int,-1, ya, n);
00210
00211 ValInfo<View>* x = xy;
00212 ValInfo<View>* y = xy+n;
00213
00214
00215 for (int i = n; i--; ) {
00216 if (x[i].doval()) xa.push(i);
00217 if (y[i].doval()) ya.push(i);
00218 }
00219
00220 do {
00221
00222 GECODE_ES_CHECK((prop_val<View,ValInfo<View> >(home,n,x,y,n_na,xa,ya)));
00223
00224 GECODE_ES_CHECK((prop_val<View,ValInfo<View> >(home,n,y,x,n_na,ya,xa)));
00225 assert(ya.empty());
00226 } while (!xa.empty());
00227
00228 if (n_na == 0)
00229 return ES_SUBSUMED(this,home);
00230 return shared ? ES_NOFIX : ES_FIX;
00231 }
00232
00233 template <class View, bool shared>
00234 Support::Symbol
00235 Val<View,shared>::ati(void) {
00236 return Reflection::mangle<View>("Gecode::Int::Channel::Val",shared);
00237 }
00238
00239 template <class View, bool shared>
00240 Reflection::ActorSpec
00241 Val<View,shared>::spec(const Space* home, Reflection::VarMap& m) const {
00242 return Base<ValInfo<View>,PC_INT_VAL>::spec(home, m, ati());
00243 }
00244
00245 template <class View, bool shared>
00246 void
00247 Val<View,shared>::post(Space* home, Reflection::VarMap& vars,
00248 const Reflection::ActorSpec& spec) {
00249 const int n = spec.noOfArgs();
00250 ValInfo<View>* vi
00251 = ValInfo<View>::allocate(home,n);
00252 for (int i=0; i<n; i++) {
00253 vi[i].init(View(home, vars, spec[i]),n/2);
00254 }
00255 (void) new (home) Val<View,shared>(home,n/2,vi);
00256 }
00257
00258 template <class View, bool shared>
00259 ExecStatus
00260 Val<View,shared>::post(Space* home, int n, ValInfo<View>* xy) {
00261 assert(n > 0);
00262 if (n == 1) {
00263 GECODE_ME_CHECK(xy[0].view.eq(home,0));
00264 GECODE_ME_CHECK(xy[1].view.eq(home,0));
00265 return ES_OK;
00266 }
00267 for (int i=2*n; i--; ) {
00268 GECODE_ME_CHECK(xy[i].view.gq(home,0));
00269 GECODE_ME_CHECK(xy[i].view.le(home,n));
00270 }
00271 (void) new (home) Val<View,shared>(home,n,xy);
00272 return ES_OK;
00273 }
00274
00275 }}}
00276
00277
00278