Generated on Wed Jan 4 17:49:11 2006 for Gecode by doxygen 1.4.6

rel.cc

Go to the documentation of this file.
00001 /*
00002  *  Main authors:
00003  *     Christian Schulte <schulte@gecode.org>
00004  *     Gabor Szokoli <szokoli@gecode.org>
00005  *
00006  *  Copyright:
00007  *     Christian Schulte, 2002
00008  *     Gabor Szokoli, 2003
00009  *
00010  *  Last modified:
00011  *     $Date: 2005-10-27 21:01:30 +0200 (Thu, 27 Oct 2005) $ by $Author: schulte $
00012  *     $Revision: 2420 $
00013  *
00014  *  This file is part of Gecode, the generic constraint
00015  *  development environment:
00016  *     http://www.gecode.org
00017  *
00018  *  See the file "LICENSE" for information on usage and
00019  *  redistribution of this file, and for a
00020  *     DISCLAIMER OF ALL WARRANTIES.
00021  *
00022  */
00023 
00024 #include "int/rel.hh"
00025 
00026 #include <algorithm>
00027 
00028 namespace Gecode {
00029 
00030   using namespace Int;
00031 
00032   void
00033   rel(Space* home, IntVar x0, IntRelType r, int c, IntConLevel) {
00034     if (home->failed()) return;
00035     IntView x(x0);
00036     switch (r) {
00037     case IRT_EQ:
00038       GECODE_ME_FAIL(home,x.eq(home,c)); break;
00039     case IRT_NQ:
00040       GECODE_ME_FAIL(home,x.nq(home,c)); break;
00041     case IRT_LQ:
00042       GECODE_ME_FAIL(home,x.lq(home,c)); break;
00043     case IRT_LE:
00044       GECODE_ME_FAIL(home,x.le(home,c)); break;
00045     case IRT_GQ:
00046       GECODE_ME_FAIL(home,x.gq(home,c)); break;
00047     case IRT_GR:
00048       GECODE_ME_FAIL(home,x.gr(home,c)); break;
00049     default:
00050       throw UnknownRelation("Int::rel");
00051     }
00052   }
00053 
00054   void
00055   rel(Space* home, IntVar x0, IntRelType r, IntVar x1, IntConLevel icl) {
00056     if (home->failed()) return;
00057     switch (r) {
00058     case IRT_EQ:
00059       if (icl == ICL_BND) {
00060         GECODE_ES_FAIL(home,Rel::EqBnd<IntView>::post(home,x0,x1));
00061       } else {
00062         GECODE_ES_FAIL(home,Rel::EqDom<IntView>::post(home,x0,x1));
00063       }
00064       break;
00065     case IRT_NQ:
00066       GECODE_ES_FAIL(home,Rel::Nq<IntView>::post(home,x0,x1)); break;
00067     case IRT_GQ:
00068       std::swap(x0,x1); // Fall through
00069     case IRT_LQ:
00070       GECODE_ES_FAIL(home,Rel::Lq<IntView>::post(home,x0,x1)); break;
00071     case IRT_GR:
00072       std::swap(x0,x1); // Fall through
00073     case IRT_LE:
00074       GECODE_ES_FAIL(home,Rel::Le<IntView>::post(home,x0,x1)); break;
00075     default:
00076       throw UnknownRelation("Int::rel");
00077     }
00078   }
00079 
00080 
00081   void
00082   rel(Space* home, IntVar x0, IntRelType r, IntVar x1, BoolVar b, 
00083       IntConLevel icl) {
00084     if (home->failed()) return;
00085     switch (r) {
00086     case IRT_EQ:
00087       if (icl == ICL_BND) {
00088         if (Rel::ReEqBnd<IntView,BoolView>::post(home,x0,x1,b) 
00089             == ES_FAILED)
00090           home->fail();
00091       } else {
00092         if (Rel::ReEqDom<IntView,BoolView>::post(home,x0,x1,b)
00093             == ES_FAILED)
00094           home->fail();
00095       }
00096       break;
00097     case IRT_NQ:
00098       {
00099         NegBoolView n(b);
00100         if (icl == ICL_BND) {
00101           if (Rel::ReEqBnd<IntView,NegBoolView>::post(home,x0,x1,n)
00102               == ES_FAILED)
00103             home->fail();
00104         } else {
00105           if (Rel::ReEqDom<IntView,NegBoolView>::post(home,x0,x1,n)
00106               == ES_FAILED)
00107             home->fail();
00108         }
00109       }
00110       break;
00111     case IRT_GQ:
00112       std::swap(x0,x1); // Fall through
00113     case IRT_LQ:
00114       if (Rel::ReLq<IntView,BoolView>::post(home,x0,x1,b) == ES_FAILED)
00115         home->fail();
00116       break;
00117     case IRT_LE:
00118       std::swap(x0,x1); // Fall through
00119     case IRT_GR:
00120       {
00121         NegBoolView n(b);
00122         if (Rel::ReLq<IntView,NegBoolView>::post(home,x0,x1,n) == ES_FAILED)
00123           home->fail();
00124       }
00125       break;
00126     default:
00127       throw UnknownRelation("Int::rel");
00128     }
00129   }
00130 
00131   void
00132   rel(Space* home, IntVar x, IntRelType r, int c, BoolVar b, 
00133       IntConLevel icl) {
00134     if (home->failed()) return;
00135     switch (r) {
00136     case IRT_EQ:
00137       if (icl == ICL_BND) {
00138         if (Rel::ReEqBndInt<IntView,BoolView>::post(home,x,c,b)
00139             == ES_FAILED)
00140           home->fail();
00141       } else {
00142         if (Rel::ReEqDomInt<IntView,BoolView>::post(home,x,c,b)
00143             == ES_FAILED)
00144           home->fail();
00145       }
00146       break;
00147     case IRT_NQ:
00148       {
00149         NegBoolView n(b);
00150         if (icl == ICL_BND) {
00151           if (Rel::ReEqBndInt<IntView,NegBoolView>::post(home,x,c,n)
00152               == ES_FAILED)
00153             home->fail();
00154         } else {
00155           if (Rel::ReEqDomInt<IntView,NegBoolView>::post(home,x,c,n)
00156               == ES_FAILED)
00157             home->fail();
00158         }
00159       }
00160       break;
00161     case IRT_LE:
00162       c--; // Fall through
00163     case IRT_LQ:
00164       if (Rel::ReLqInt<IntView,BoolView>::post(home,x,c,b) == ES_FAILED)
00165         home->fail();
00166       break;
00167     case IRT_GQ:
00168       c--; // Fall through
00169     case IRT_GR: 
00170       {
00171         NegBoolView n(b);
00172         if (Rel::ReLqInt<IntView,NegBoolView>::post(home,x,c,n) == ES_FAILED)
00173           home->fail();
00174       }
00175       break;
00176     default:
00177       throw UnknownRelation("Int::rel");
00178     }
00179   }
00180 
00181   void
00182   eq(Space* home, IntVar x0, IntVar x1, IntConLevel icl) {
00183     if (home->failed()) return;
00184     if (icl == ICL_BND) {
00185       GECODE_ES_FAIL(home,Rel::EqBnd<IntView>::post(home,x0,x1));
00186     } else {
00187       GECODE_ES_FAIL(home,Rel::EqDom<IntView>::post(home,x0,x1));
00188     }
00189   }
00190 
00191   void
00192   eq(Space* home, IntVar x0, int c, IntConLevel) {
00193     if (home->failed()) return;
00194     IntView x(x0);
00195     GECODE_ME_FAIL(home,x.eq(home,c));
00196   }
00197 
00198   void
00199   eq(Space* home, IntVar x0, IntVar x1, BoolVar b, IntConLevel icl) {
00200     if (home->failed()) return;
00201     if (icl == ICL_BND) {
00202       if (Rel::ReEqBnd<IntView,BoolView>::post(home,x0,x1,b) == ES_FAILED)
00203         home->fail();
00204     } else {
00205       if (Rel::ReEqDom<IntView,BoolView>::post(home,x0,x1,b) == ES_FAILED)
00206         home->fail();
00207     }
00208   }
00209 
00210   void
00211   eq(Space* home, IntVar x, int c, BoolVar b, IntConLevel icl) {
00212     if (home->failed()) return;
00213     if (icl == ICL_BND) {
00214       if (Rel::ReEqBndInt<IntView,BoolView>::post(home,x,c,b) == ES_FAILED)
00215         home->fail();
00216     } else {
00217       if (Rel::ReEqDomInt<IntView,BoolView>::post(home,x,c,b) == ES_FAILED)
00218         home->fail();
00219     }
00220   }
00221 
00222   void
00223   eq(Space* home, const IntVarArgs& x, IntConLevel icl) {
00224     if (home->failed()) return;
00225     ViewArray<IntView> xv(home,x);
00226     if (icl == ICL_BND) {
00227       GECODE_ES_FAIL(home,Rel::NaryEqBnd<IntView>::post(home,xv));
00228     } else {
00229       GECODE_ES_FAIL(home,Rel::NaryEqDom<IntView>::post(home,xv));
00230     }
00231   }
00232 
00233 
00234   void
00235   lex(Space* home, const IntVarArgs& x, IntRelType r, const IntVarArgs& y,
00236       IntConLevel) {
00237     if (x.size() != y.size())
00238       throw ArgumentSizeMismatch("Int::lex");
00239     if (home->failed()) return;
00240 
00241     ViewArray<ViewTuple<IntView,2> > xy(home,x.size());
00242     switch (r) {
00243     case IRT_GR:
00244       for (int i = x.size(); i--; ) {
00245         xy[i][0]=y[i]; xy[i][1]=x[i];
00246       }
00247       goto le;
00248     case IRT_LE:
00249       for (int i = x.size(); i--; ) {
00250         xy[i][0]=x[i]; xy[i][1]=y[i];
00251       }
00252     le:
00253       if (Rel::Lex<IntView>::post(home,xy,true) == ES_FAILED)
00254         home->fail();
00255       break;
00256     case IRT_GQ:
00257       for (int i = x.size(); i--; ) {
00258         xy[i][0]=y[i]; xy[i][1]=x[i];
00259       }
00260       goto lq;
00261     case IRT_LQ:
00262       for (int i = x.size(); i--; ) {
00263         xy[i][0]=x[i]; xy[i][1]=y[i];
00264       }
00265     lq:
00266       if (Rel::Lex<IntView>::post(home,xy,false) == ES_FAILED)
00267         home->fail();
00268       break;
00269     default:
00270       throw OnlyInequalityRelations("Int::lex");
00271     }
00272   }
00273 
00274 }
00275 
00276 // STATISTICS: int-post
00277