Generated on Wed Nov 1 15:04:38 2006 for Gecode by doxygen 1.4.5

rel.cc

Go to the documentation of this file.
00001 /*
00002  *  Main authors:
00003  *     Guido Tack <tack@gecode.org>
00004  *
00005  *  Contributing authors:
00006  *     Gabor Szokoli <szokoli@gecode.org>
00007  *
00008  *  Copyright:
00009  *     Guido Tack, 2004, 2005
00010  *
00011  *  Last modified:
00012  *     $Date: 2006-04-11 15:58:37 +0200 (Tue, 11 Apr 2006) $ by $Author: tack $
00013  *     $Revision: 3188 $
00014  *
00015  *  This file is part of Gecode, the generic constraint
00016  *  development environment:
00017  *     http://www.gecode.org
00018  *
00019  *  See the file "LICENSE" for information on usage and
00020  *  redistribution of this file, and for a
00021  *     DISCLAIMER OF ALL WARRANTIES.
00022  *
00023  */
00024 
00025 #include "gecode/set.hh"
00026 #include "gecode/iter.hh"
00027 #include "gecode/set/rel.hh"
00028 #include "gecode/set/rel-op.hh"
00029 #include "gecode/set/int.hh"
00030 
00031 namespace Gecode {
00032   using namespace Set;
00033   using namespace Set::Rel;
00034   using namespace Set::RelOp;
00035   
00036   template <class View0, class View1>
00037   void
00038   rel_post(Space* home, View0 x0, SetRelType r, View1 x1) {
00039     if (home->failed()) return;
00040     switch(r) {
00041     case SRT_EQ:
00042       {
00043         GECODE_ES_FAIL(home,
00044                        (Eq<View0,View1>::post(home,x0,x1)));
00045       }
00046       break;
00047     case SRT_NQ:
00048       {
00049         GECODE_ES_FAIL(home,
00050                        (Distinct<View0,View1>::post(home,x0,x1)));
00051       }
00052       break;
00053     case SRT_SUB:
00054       {
00055         GECODE_ES_FAIL(home,
00056                        (SubSet<View0,View1>::post(home, x0,x1)));
00057       }
00058       break;
00059     case SRT_SUP:
00060       {
00061         GECODE_ES_FAIL(home,
00062                        (SubSet<View1,View0>::post(home, x1,x0)));
00063       }
00064       break;
00065     case SRT_DISJ:
00066       {
00067         EmptyView emptyset;
00068         GECODE_ES_FAIL(home,(SuperOfInter<View0,View1,EmptyView>
00069                              ::post(home, x0, x1, emptyset)));
00070       }
00071       break;
00072     case SRT_CMPL:
00073       {
00074         ComplementView<View0> cx0(x0);
00075         GECODE_ES_FAIL(home,
00076                        (Eq<ComplementView<View0>, View1>
00077                         ::post(home, cx0, x1)));
00078       }
00079       break;
00080     }
00081   }
00082 
00083   template <class View0, class View1>
00084   void
00085   rel_re(Space* home, View0 x, SetRelType r, View1 y, BoolVar b) {
00086     if (home->failed()) return;
00087     switch(r) {
00088     case SRT_EQ:
00089       {
00090         GECODE_ES_FAIL(home,
00091                        (ReEq<View0,View1>::post(home, x,y,b)));
00092       }
00093       break;
00094     case SRT_NQ:
00095       {
00096         BoolVar notb(home, 0, 1);
00097         bool_not(home, b, notb);
00098         GECODE_ES_FAIL(home,
00099                        (ReEq<View0,View1>::post(home,
00100                                                            x,y,notb)));
00101       }
00102       break;
00103     case SRT_SUB:
00104       {
00105         GECODE_ES_FAIL(home,
00106                        (ReSubset<View0,View1>::post(home, x,y,b)));
00107       }
00108       break;
00109     case SRT_SUP:
00110       {
00111         GECODE_ES_FAIL(home,
00112                        (ReSubset<View1,View0>::post(home, y,x,b)));
00113       }
00114       break;
00115     case SRT_DISJ:
00116       {
00117         // x||y <=> b is equivalent to
00118         // ( y <= complement(x) and x<=complement(y) ) <=> b
00119 
00120         // set up BoolVars for the conjunction
00121         BoolVar b1(home, 0, 1);
00122         BoolVar b2(home, 0, 1);
00123         bool_and(home, b1, b2, b);      
00124 
00125         ComplementView<View0> xc(x);
00126         ComplementView<View1> yc(y);
00127 
00128         GECODE_ES_FAIL(home,
00129                        (ReSubset<View0,ComplementView<View1> >
00130                         ::post(home, x, yc, b1)));
00131         GECODE_ES_FAIL(home,
00132                        (ReSubset<View1,ComplementView<View0> >
00133                         ::post(home, y, xc, b2)));
00134       }
00135       break;
00136     case SRT_CMPL:
00137       {
00138         ComplementView<View0> cx(x);
00139         GECODE_ES_FAIL(home,
00140                        (ReEq<ComplementView<View0>,
00141                         View1>::post(home, cx, y, b)));
00142       }
00143       break;
00144     }
00145   }
00146 
00147   void
00148   rel(Space* home, SetVar x, SetRelType r, SetVar y) {
00149     rel_post<SetView,SetView>(home,x,r,y);
00150   }
00151 
00152   void
00153   rel(Space* home, SetVar s, SetRelType r, IntVar x) {
00154     Gecode::Int::IntView xv(x);
00155     SingletonView xsingle(xv);
00156     rel_post<SetView,SingletonView>(home,s,r,xv);
00157   }
00158 
00159   void
00160   rel(Space* home, IntVar x, SetRelType r, SetVar s) {
00161     switch(r) {
00162     case SRT_SUB:
00163       rel(home, s, SRT_SUP, x);
00164       break;
00165     case SRT_SUP:
00166       rel(home, s, SRT_SUB, x);
00167       break;
00168     default:
00169       rel(home, s, r, x);
00170     }
00171   }
00172 
00173   void
00174   rel(Space* home, SetVar x, SetRelType r, SetVar y, BoolVar b) {
00175     rel_re<SetView,SetView>(home,x,r,y,b);
00176   }
00177 
00178   void
00179   rel(Space* home, SetVar s, SetRelType r, IntVar x, BoolVar b) {
00180     Gecode::Int::IntView xv(x);
00181     SingletonView xsingle(xv);
00182     rel_re<SetView,SingletonView>(home,s,r,xsingle,b);
00183   }
00184 
00185   void
00186   rel(Space* home, IntVar x, SetRelType r, SetVar s, BoolVar b) {
00187     switch(r) {
00188     case SRT_SUB:
00189       rel(home, s, SRT_SUP, x, b);
00190       break;
00191     case SRT_SUP:
00192       rel(home, s, SRT_SUB, x, b);
00193       break;
00194     default:
00195       rel(home, s, r, x, b);
00196     }
00197   }
00198 
00199 }
00200 
00201 // STATISTICS: set-post