Generated on Mon May 5 05:53:58 2008 for Gecode by doxygen 1.5.5

nq.icc

Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
00002 /*
00003  *  Main authors:
00004  *     Christian Schulte <schulte@gecode.org>
00005  *
00006  *  Copyright:
00007  *     Christian Schulte, 2004
00008  *
00009  *  Last modified:
00010  *     $Date: 2008-01-31 18:29:16 +0100 (Thu, 31 Jan 2008) $ by $Author: tack $
00011  *     $Revision: 6017 $
00012  *
00013  *  This file is part of Gecode, the generic constraint
00014  *  development environment:
00015  *     http://www.gecode.org
00016  *
00017  *  Permission is hereby granted, free of charge, to any person obtaining
00018  *  a copy of this software and associated documentation files (the
00019  *  "Software"), to deal in the Software without restriction, including
00020  *  without limitation the rights to use, copy, modify, merge, publish,
00021  *  distribute, sublicense, and/or sell copies of the Software, and to
00022  *  permit persons to whom the Software is furnished to do so, subject to
00023  *  the following conditions:
00024  *
00025  *  The above copyright notice and this permission notice shall be
00026  *  included in all copies or substantial portions of the Software.
00027  *
00028  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00029  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00030  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00031  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00032  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00033  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00034  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00035  *
00036  */
00037 
00038 namespace Gecode { namespace Int { namespace Rel {
00039 
00040   /*
00041    * Disequality
00042    *
00043    */
00044   template <class View>
00045   forceinline
00046   Nq<View>::Nq(Space* home, View x0, View x1)
00047     : BinaryPropagator<View,PC_INT_VAL>(home,x0,x1) {}
00048 
00049   template <class View>
00050   ExecStatus
00051   Nq<View>::post(Space* home, View x0, View x1){
00052     if (x0.assigned()) {
00053       GECODE_ME_CHECK(x1.nq(home,x0.val()));
00054     } else if (x1.assigned()) {
00055       GECODE_ME_CHECK(x0.nq(home,x1.val()));
00056     } else if (same(x0,x1)) {
00057       return ES_FAILED;
00058     } else {
00059       (void) new (home) Nq<View>(home,x0,x1);
00060     }
00061     return ES_OK;
00062   }
00063 
00064   template <class View>
00065   forceinline
00066   Nq<View>::Nq(Space* home, bool share, Nq<View>& p)
00067     : BinaryPropagator<View,PC_INT_VAL>(home,share,p) {}
00068 
00069   template <class View>
00070   Actor*
00071   Nq<View>::copy(Space* home, bool share) {
00072     return new (home) Nq<View>(home,share,*this);
00073   }
00074 
00075   template <class View>
00076   PropCost
00077   Nq<View>::cost(ModEventDelta) const {
00078     return PC_UNARY_LO;
00079   }
00080 
00081   template <class View>
00082   inline Support::Symbol
00083   Nq<View>::ati(void) {
00084     return Reflection::mangle<View>("Gecode::Int::Rel::Nq");
00085   }
00086 
00087   template <class View>
00088   Reflection::ActorSpec
00089   Nq<View>::spec(const Space* home, Reflection::VarMap& m) const {
00090     return BinaryPropagator<View,PC_INT_VAL>::spec(home, m, ati());
00091   }
00092 
00093   template <class View>
00094   void
00095   Nq<View>::post(Space* home, Reflection::VarMap& vars,
00096   const Reflection::ActorSpec& spec) {
00097     spec.checkArity(2);
00098     View x0(home, vars, spec[0]);
00099     View x1(home, vars, spec[1]);
00100     (void) new (home) Nq<View>(home, x0, x1);
00101   }
00102 
00103   template <class View>
00104   ExecStatus
00105   Nq<View>::propagate(Space* home, ModEventDelta) {
00106     if (x0.assigned()) {
00107       GECODE_ME_CHECK(x1.nq(home,x0.val()));
00108       x1.cancel(home,this,PC_INT_VAL);
00109     } else {
00110       GECODE_ME_CHECK(x0.nq(home,x1.val()));
00111       x0.cancel(home,this,PC_INT_VAL);
00112     }
00113     return ES_SUBSUMED(this,sizeof(*this));
00114   }
00115 
00116 
00117   /*
00118    * Nary disequality
00119    *
00120    */
00121   template<class View>
00122   forceinline
00123   NaryNq<View>::NaryNq(Space* home, ViewArray<ViewTuple<View,2> >& x0)
00124     : BinaryPropagator<ViewTuple<View,2>,PC_INT_DOM>(home,
00125                                                      x0[x0.size()-2],
00126                                                      x0[x0.size()-1]), x(x0) {
00127     assert(x.size() >= 2);
00128     x.size(x.size()-2);
00129   }
00130 
00131   template<class View>
00132   forceinline
00133   NaryNq<View>::NaryNq(Space* home, bool share, NaryNq<View>& p)
00134     : BinaryPropagator<ViewTuple<View,2>,PC_INT_DOM>(home,share,p) {
00135     x.update(home,share,p.x);
00136   }
00137 
00138   template<class View>
00139   forceinline ExecStatus
00140   NaryNq<View>::post(Space* home, ViewArray<ViewTuple<View,2> >& x) {
00141     int n = x.size();
00142     for (int i=n; i--; )
00143       switch (rtest_nq_dom(x[i][0],x[i][1])) {
00144       case RT_FALSE: 
00145         x[i]=x[--n]; 
00146         break;
00147       case RT_TRUE:  
00148         return ES_OK;
00149       case RT_MAYBE: 
00150         break;
00151       default:
00152         GECODE_NEVER;
00153       }
00154     x.size(n);
00155     if (n == 0)
00156       return ES_FAILED;
00157     if (n == 1)
00158       return Nq<View>::post(home,x[0][0],x[0][1]);
00159     (void) new (home) NaryNq(home,x);
00160     return ES_OK;
00161   }
00162 
00163   template<class View>
00164   Actor*
00165   NaryNq<View>::copy(Space* home, bool share) {
00166     return new (home) NaryNq<View>(home,share,*this);
00167   }
00168 
00169   template <class View>
00170   inline Support::Symbol
00171   NaryNq<View>::ati(void) {
00172     return Reflection::mangle<View>("Gecode::Int::Rel::NaryNq");
00173   }
00174 
00175   template <class View>
00176   Reflection::ActorSpec
00177   NaryNq<View>::spec(const Space* home, Reflection::VarMap& m) const {
00178     Reflection::ActorSpec s =
00179       BinaryPropagator<ViewTuple<View,2>,PC_INT_DOM>::spec(home, m, ati());
00180     Reflection::ArrayArg* a = Reflection::Arg::newArray(x.size());
00181     for (int i=x.size(); i--; )
00182       (*a)[i] = x[i][0].spec(home, m);
00183     s << a;
00184 
00185     Reflection::ArrayArg* b = Reflection::Arg::newArray(x.size());
00186     for (int i=x.size(); i--; )
00187       (*b)[i] = x[i][1].spec(home, m);
00188     s << b;
00189 
00190     return s;
00191   }
00192 
00193   template <class View>
00194   void
00195   NaryNq<View>::post(Space* home, Reflection::VarMap& vars,
00196                      const Reflection::ActorSpec& spec) {
00197     spec.checkArity(4);
00198     ViewTuple<View,2> x0(home, vars, spec[0]);
00199     ViewTuple<View,2> x1(home, vars, spec[1]);
00200 
00201     Reflection::ArrayArg* x = spec[2]->toArray();
00202     Reflection::ArrayArg* y = spec[3]->toArray();
00203     
00204     ViewArray<ViewTuple<View,2> > xy(home, x->size()+2);
00205     for (int i=x->size(); i--; ) {
00206       xy[i][0] = View(home, vars, (*x)[i]);
00207       xy[i][1] = View(home, vars, (*y)[i]);
00208     }
00209     xy[x->size()]   = x0;
00210     xy[x->size()+1] = x1;
00211     (void) new (home) NaryNq<View>(home, xy);
00212   }
00213 
00214   template<class View>
00215   ExecStatus
00216   NaryNq<View>::propagate(Space* home, ModEventDelta) {
00217     // Eliminate equal pairs
00218     int n = x.size();
00219     for (int i=n; i--; )
00220       switch (rtest_nq_dom(x[i][0],x[i][1])) {
00221       case RT_TRUE:  
00222         return ES_SUBSUMED(this,home);
00223       case RT_FALSE: 
00224         x[i].cancel(home,this,PC_INT_DOM);
00225         x[i]=x[--n]; 
00226         break;
00227       case RT_MAYBE: 
00228         break;
00229       default:
00230         GECODE_NEVER;
00231       }
00232     switch (rtest_nq_dom(x0[0],x0[1])) {
00233     case RT_TRUE: 
00234       return ES_SUBSUMED(this,home);
00235     case RT_FALSE:
00236       if (n == 0)
00237         GECODE_REWRITE(this,(Nq<View>::post(home,x1[0],x1[1])));
00238       x0.cancel(home,this,PC_INT_DOM);
00239       x0=x[--n];
00240       x0.subscribe(home,this,PC_INT_DOM);
00241       break;
00242     case RT_MAYBE: 
00243       break;
00244     default:
00245       GECODE_NEVER;
00246     }
00247     switch (rtest_nq_dom(x1[0],x1[1])) {
00248     case RT_TRUE: 
00249       return ES_SUBSUMED(this,home);
00250     case RT_FALSE:
00251       if (n == 0)
00252         GECODE_REWRITE(this,(Nq<View>::post(home,x0[0],x0[1])));
00253       x1.cancel(home,this,PC_INT_DOM);
00254       x1=x[--n];
00255       x1.subscribe(home,this,PC_INT_DOM);
00256       break;
00257     case RT_MAYBE: 
00258       break;
00259     default:
00260       GECODE_NEVER;
00261     }
00262     x.size(n);
00263     return ES_FIX;
00264   }
00265 
00266 }}}
00267 
00268 // STATISTICS: int-prop
00269