Generated on Thu Apr 11 13:59:23 2019 for Gecode by doxygen 1.6.3

rel-op-const.cpp

Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
00002 /*
00003  *  Main authors:
00004  *     Guido Tack <tack@gecode.org>
00005  *
00006  *  Copyright:
00007  *     Guido Tack, 2005
00008  *
00009  *  This file is part of Gecode, the generic constraint
00010  *  development environment:
00011  *     http://www.gecode.org
00012  *
00013  *  Permission is hereby granted, free of charge, to any person obtaining
00014  *  a copy of this software and associated documentation files (the
00015  *  "Software"), to deal in the Software without restriction, including
00016  *  without limitation the rights to use, copy, modify, merge, publish,
00017  *  distribute, sublicense, and/or sell copies of the Software, and to
00018  *  permit persons to whom the Software is furnished to do so, subject to
00019  *  the following conditions:
00020  *
00021  *  The above copyright notice and this permission notice shall be
00022  *  included in all copies or substantial portions of the Software.
00023  *
00024  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00025  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00026  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00027  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00028  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00029  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00030  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00031  *
00032  */
00033 
00034 #include "test/set.hh"
00035 
00036 using namespace Gecode;
00037 
00038 namespace Test { namespace Set {
00039 
00041   namespace RelOpConst {
00042 
00048 
00049     static IntSet ds_33(-3,3);
00050     static IntSet ds_22(-2,2);
00051     static IntSet ds_12(-1,2);
00052 
00053     static IntSet iss[] = {IntSet(-1,1), IntSet(-4,-4), IntSet(0,2)};
00054 
00056     class RelSIS : public SetTest {
00057     private:
00058       IntSet is;
00059       Gecode::SetOpType sot;
00060       Gecode::SetRelType srt;
00061       bool inverse;
00062 
00063       template<class I, class J>
00064       bool
00065       sol(I& i, J& j) const {
00066         switch (srt) {
00067         case SRT_EQ: return Iter::Ranges::equal(i,j);
00068         case SRT_NQ: return !Iter::Ranges::equal(i,j);
00069         case SRT_SUB: return Iter::Ranges::subset(i,j);
00070         case SRT_SUP: return Iter::Ranges::subset(j,i);
00071         case SRT_DISJ:
00072           {
00073             Gecode::Iter::Ranges::Inter<I,J> inter(i,j);
00074             return !inter();
00075           }
00076         case SRT_CMPL:
00077           {
00078             Gecode::Set::RangesCompl<J> jc(j);
00079             return Iter::Ranges::equal(i,jc);
00080           }
00081         default: GECODE_NEVER;
00082         }
00083         return false;
00084       }
00085 
00086     public:
00088       RelSIS(Gecode::SetOpType sot0, Gecode::SetRelType srt0,
00089              int intSet, bool inverse0)
00090        : SetTest("RelOp::ConstSIS::"+str(sot0)+"::"+str(srt0)+"::"+
00091                  str(intSet)+(inverse0 ? "i" :""),2,ds_22,false)
00092        , is(iss[intSet]), sot(sot0), srt(srt0), inverse(inverse0) {}
00094       bool solution(const SetAssignment& x) const {
00095         IntSetRanges isr(is);
00096         CountableSetRanges xr0(x.lub, x[0]);
00097         CountableSetRanges xr1(x.lub, x[1]);
00098         switch (sot) {
00099         case SOT_UNION:
00100           {
00101             Iter::Ranges::Union<IntSetRanges, CountableSetRanges>
00102               u(isr, xr0);
00103             return sol(u,xr1);
00104           }
00105           break;
00106         case SOT_DUNION:
00107           {
00108             Iter::Ranges::Inter<IntSetRanges, CountableSetRanges>
00109               inter(isr, xr0);
00110             if (inter())
00111               return false;
00112             Iter::Ranges::Union<IntSetRanges, CountableSetRanges>
00113               u(isr,xr0);
00114             return sol(u,xr1);
00115           }
00116           break;
00117         case SOT_INTER:
00118           {
00119             Iter::Ranges::Inter<IntSetRanges, CountableSetRanges>
00120               u(isr,xr0);
00121             return sol(u,xr1);
00122           }
00123           break;
00124         case SOT_MINUS:
00125           {
00126             if (!inverse) {
00127               Iter::Ranges::Diff<IntSetRanges, CountableSetRanges>
00128                 u(isr,xr0);
00129               return sol(u,xr1);
00130             } else {
00131               Iter::Ranges::Diff<CountableSetRanges, IntSetRanges>
00132                 u(xr0,isr);
00133               return sol(u,xr1);
00134 
00135             }
00136           }
00137           break;
00138         default: GECODE_NEVER;
00139         }
00140         GECODE_NEVER;
00141         return false;
00142       }
00144       void post(Space& home, SetVarArray& x, IntVarArray&) {
00145         if (!inverse)
00146           Gecode::rel(home, is, sot, x[0], srt, x[1]);
00147         else
00148           Gecode::rel(home, x[0], sot, is, srt, x[1]);
00149       }
00150     };
00151 
00153     class RelSSI : public SetTest {
00154     private:
00155       IntSet is;
00156       Gecode::SetOpType sot;
00157       Gecode::SetRelType srt;
00158 
00159       template<class I, class J>
00160       bool
00161       sol(I& i, J& j) const {
00162         switch (srt) {
00163         case SRT_EQ: return Iter::Ranges::equal(i,j);
00164         case SRT_NQ: return !Iter::Ranges::equal(i,j);
00165         case SRT_SUB: return Iter::Ranges::subset(i,j);
00166         case SRT_SUP: return Iter::Ranges::subset(j,i);
00167         case SRT_DISJ:
00168           {
00169             Gecode::Iter::Ranges::Inter<I,J> inter(i,j);
00170             return !inter();
00171           }
00172         case SRT_CMPL:
00173           {
00174             Gecode::Set::RangesCompl<J> jc(j);
00175             return Iter::Ranges::equal(i,jc);
00176           }
00177         default: GECODE_NEVER;
00178         }
00179         GECODE_NEVER;
00180         return false;
00181       }
00182 
00183     public:
00185       RelSSI(Gecode::SetOpType sot0, Gecode::SetRelType srt0,
00186              int intSet)
00187        : SetTest("RelOp::ConstSSI::"+str(sot0)+"::"+str(srt0)+"::"+
00188                  str(intSet),2,ds_22,false)
00189        , is(iss[intSet]), sot(sot0), srt(srt0) {}
00191       bool solution(const SetAssignment& x) const {
00192         CountableSetRanges xr0(x.lub, x[0]);
00193         CountableSetRanges xr1(x.lub, x[1]);
00194         IntSetRanges isr(is);
00195         switch (sot) {
00196         case SOT_UNION:
00197           {
00198             Iter::Ranges::Union<CountableSetRanges, CountableSetRanges>
00199               u(xr0, xr1);
00200             return sol(u,isr);
00201           }
00202           break;
00203         case SOT_DUNION:
00204           {
00205             Iter::Ranges::Inter<CountableSetRanges, CountableSetRanges>
00206               inter(xr0, xr1);
00207             if (inter())
00208               return false;
00209             Iter::Ranges::Union<CountableSetRanges, CountableSetRanges>
00210               u(xr0, xr1);
00211             return sol(u,isr);
00212           }
00213           break;
00214         case SOT_INTER:
00215           {
00216             Iter::Ranges::Inter<CountableSetRanges, CountableSetRanges>
00217               u(xr0,xr1);
00218             return sol(u,isr);
00219           }
00220           break;
00221         case SOT_MINUS:
00222           {
00223             Iter::Ranges::Diff<CountableSetRanges, CountableSetRanges>
00224               u(xr0,xr1);
00225             return sol(u,isr);
00226           }
00227           break;
00228         default: GECODE_NEVER;
00229         }
00230         GECODE_NEVER;
00231         return false;
00232       }
00234       void post(Space& home, SetVarArray& x, IntVarArray&) {
00235         Gecode::rel(home, x[0], sot, x[1], srt, is);
00236       }
00237     };
00238 
00240     class RelISI : public SetTest {
00241     private:
00242       IntSet is0;
00243       IntSet is1;
00244       Gecode::SetOpType sot;
00245       Gecode::SetRelType srt;
00246       bool inverse;
00247 
00248       template<class I, class J>
00249       bool
00250       sol(I& i, J& j) const {
00251         switch (srt) {
00252         case SRT_EQ: return Iter::Ranges::equal(i,j);
00253         case SRT_NQ: return !Iter::Ranges::equal(i,j);
00254         case SRT_SUB: return Iter::Ranges::subset(i,j);
00255         case SRT_SUP: return Iter::Ranges::subset(j,i);
00256         case SRT_DISJ:
00257           {
00258             Gecode::Iter::Ranges::Inter<I,J> inter(i,j);
00259             return !inter();
00260           }
00261         case SRT_CMPL:
00262           {
00263             Gecode::Set::RangesCompl<J> jc(j);
00264             return Iter::Ranges::equal(i,jc);
00265           }
00266         default: GECODE_NEVER;
00267         }
00268         GECODE_NEVER;
00269         return false;
00270       }
00271 
00272     public:
00274       RelISI(Gecode::SetOpType sot0, Gecode::SetRelType srt0,
00275              int intSet0, int intSet1, bool inverse0)
00276        : SetTest("RelOp::ConstISI::"+str(sot0)+"::"+str(srt0)+"::"+
00277                  str(intSet0)+"::"+str(intSet1)+
00278                  (inverse0 ? "i" : ""),1,ds_33,false)
00279        , is0(iss[intSet0]), is1(iss[intSet1]), sot(sot0), srt(srt0)
00280        , inverse(inverse0) {}
00282       bool solution(const SetAssignment& x) const {
00283         CountableSetRanges xr0(x.lub, x[0]);
00284         IntSetRanges isr0(is0);
00285         IntSetRanges isr1(is1);
00286         switch (sot) {
00287         case SOT_UNION:
00288           {
00289             Iter::Ranges::Union<IntSetRanges, CountableSetRanges>
00290               u(isr0, xr0);
00291             return sol(u,isr1);
00292           }
00293           break;
00294         case SOT_DUNION:
00295           {
00296             Iter::Ranges::Inter<IntSetRanges, CountableSetRanges>
00297               inter(isr0, xr0);
00298             if (inter())
00299               return false;
00300             Iter::Ranges::Union<IntSetRanges, CountableSetRanges>
00301               u(isr0, xr0);
00302             return sol(u,isr1);
00303           }
00304           break;
00305         case SOT_INTER:
00306           {
00307             Iter::Ranges::Inter<IntSetRanges, CountableSetRanges>
00308               u(isr0,xr0);
00309             return sol(u,isr1);
00310           }
00311           break;
00312         case SOT_MINUS:
00313           {
00314             if (!inverse) {
00315               Iter::Ranges::Diff<IntSetRanges, CountableSetRanges>
00316                 u(isr0,xr0);
00317               return sol(u,isr1);
00318             } else {
00319               Iter::Ranges::Diff<CountableSetRanges, IntSetRanges>
00320                 u(xr0,isr0);
00321               return sol(u,isr1);
00322             }
00323           }
00324           break;
00325         default: GECODE_NEVER;
00326         }
00327         GECODE_NEVER;
00328         return false;
00329       }
00331       void post(Space& home, SetVarArray& x, IntVarArray&) {
00332         if (!inverse)
00333           Gecode::rel(home, is0, sot, x[0], srt, is1);
00334         else
00335           Gecode::rel(home, x[0], sot, is0, srt, is1);
00336       }
00337     };
00338 
00340     class Create {
00341     public:
00343       Create(void) {
00344         using namespace Gecode;
00345         for (SetRelTypes srts; srts(); ++srts) {
00346           for (SetOpTypes sots; sots(); ++sots) {
00347             for (int i=0; i<=2; i++) {
00348               (void) new RelSIS(sots.sot(),srts.srt(),i,false);
00349               (void) new RelSIS(sots.sot(),srts.srt(),i,true);
00350               (void) new RelSSI(sots.sot(),srts.srt(),i);
00351               (void) new RelISI(sots.sot(),srts.srt(),i,0,false);
00352               (void) new RelISI(sots.sot(),srts.srt(),i,1,false);
00353               (void) new RelISI(sots.sot(),srts.srt(),i,2,false);
00354               (void) new RelISI(sots.sot(),srts.srt(),i,0,true);
00355               (void) new RelISI(sots.sot(),srts.srt(),i,1,true);
00356               (void) new RelISI(sots.sot(),srts.srt(),i,2,true);
00357             }
00358           }
00359         }
00360       }
00361     };
00362 
00363     Create c;
00364 
00366 
00367 }}}
00368 
00369 // STATISTICS: test-set