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 * Last modified: 00010 * $Date: 2010-07-28 18:45:22 +0200 (Wed, 28 Jul 2010) $ by $Author: schulte $ 00011 * $Revision: 11297 $ 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 #include "test/set.hh" 00039 00040 using namespace Gecode; 00041 00042 namespace Test { namespace Set { 00043 00045 namespace RelOpConst { 00046 00052 00053 static IntSet ds_33(-3,3); 00054 static IntSet ds_22(-2,2); 00055 static IntSet ds_12(-1,2); 00056 00057 static IntSet iss[] = {IntSet(-1,1), IntSet(-4,-4), IntSet(0,2)}; 00058 00060 class RelSIS : public SetTest { 00061 private: 00062 IntSet is; 00063 Gecode::SetOpType sot; 00064 Gecode::SetRelType srt; 00065 bool inverse; 00066 00067 template<class I, class J> 00068 bool 00069 sol(I& i, J& j) const { 00070 switch (srt) { 00071 case SRT_EQ: return Iter::Ranges::equal(i,j); 00072 case SRT_NQ: return !Iter::Ranges::equal(i,j); 00073 case SRT_SUB: return Iter::Ranges::subset(i,j); 00074 case SRT_SUP: return Iter::Ranges::subset(j,i); 00075 case SRT_DISJ: 00076 { 00077 Gecode::Iter::Ranges::Inter<I,J> inter(i,j); 00078 return !inter(); 00079 } 00080 case SRT_CMPL: 00081 { 00082 Gecode::Set::RangesCompl<J> jc(j); 00083 return Iter::Ranges::equal(i,jc); 00084 } 00085 } 00086 GECODE_NEVER; 00087 return false; 00088 } 00089 00090 public: 00092 RelSIS(Gecode::SetOpType sot0, Gecode::SetRelType srt0, 00093 int intSet, bool inverse0) 00094 : SetTest("RelOp::ConstSIS::"+str(sot0)+"::"+str(srt0)+"::"+ 00095 str(intSet)+(inverse0 ? "i" :""),2,ds_22,false) 00096 , is(iss[intSet]), sot(sot0), srt(srt0), inverse(inverse0) {} 00098 bool solution(const SetAssignment& x) const { 00099 IntSetRanges isr(is); 00100 CountableSetRanges xr0(x.lub, x[0]); 00101 CountableSetRanges xr1(x.lub, x[1]); 00102 switch (sot) { 00103 case SOT_UNION: 00104 { 00105 Iter::Ranges::Union<IntSetRanges, CountableSetRanges> 00106 u(isr, xr0); 00107 return sol(u,xr1); 00108 } 00109 break; 00110 case SOT_DUNION: 00111 { 00112 Iter::Ranges::Inter<IntSetRanges, CountableSetRanges> 00113 inter(isr, xr0); 00114 if (inter()) 00115 return false; 00116 Iter::Ranges::Union<IntSetRanges, CountableSetRanges> 00117 u(isr,xr0); 00118 return sol(u,xr1); 00119 } 00120 break; 00121 case SOT_INTER: 00122 { 00123 Iter::Ranges::Inter<IntSetRanges, CountableSetRanges> 00124 u(isr,xr0); 00125 return sol(u,xr1); 00126 } 00127 break; 00128 case SOT_MINUS: 00129 { 00130 if (!inverse) { 00131 Iter::Ranges::Diff<IntSetRanges, CountableSetRanges> 00132 u(isr,xr0); 00133 return sol(u,xr1); 00134 } else { 00135 Iter::Ranges::Diff<CountableSetRanges, IntSetRanges> 00136 u(xr0,isr); 00137 return sol(u,xr1); 00138 00139 } 00140 } 00141 break; 00142 } 00143 GECODE_NEVER; 00144 return false; 00145 } 00147 void post(Space& home, SetVarArray& x, IntVarArray&) { 00148 if (!inverse) 00149 Gecode::rel(home, is, sot, x[0], srt, x[1]); 00150 else 00151 Gecode::rel(home, x[0], sot, is, srt, x[1]); 00152 } 00153 }; 00154 00156 class RelSSI : public SetTest { 00157 private: 00158 IntSet is; 00159 Gecode::SetOpType sot; 00160 Gecode::SetRelType srt; 00161 00162 template<class I, class J> 00163 bool 00164 sol(I& i, J& j) const { 00165 switch (srt) { 00166 case SRT_EQ: return Iter::Ranges::equal(i,j); 00167 case SRT_NQ: return !Iter::Ranges::equal(i,j); 00168 case SRT_SUB: return Iter::Ranges::subset(i,j); 00169 case SRT_SUP: return Iter::Ranges::subset(j,i); 00170 case SRT_DISJ: 00171 { 00172 Gecode::Iter::Ranges::Inter<I,J> inter(i,j); 00173 return !inter(); 00174 } 00175 case SRT_CMPL: 00176 { 00177 Gecode::Set::RangesCompl<J> jc(j); 00178 return Iter::Ranges::equal(i,jc); 00179 } 00180 } 00181 GECODE_NEVER; 00182 return false; 00183 } 00184 00185 public: 00187 RelSSI(Gecode::SetOpType sot0, Gecode::SetRelType srt0, 00188 int intSet) 00189 : SetTest("RelOp::ConstSSI::"+str(sot0)+"::"+str(srt0)+"::"+ 00190 str(intSet),2,ds_22,false) 00191 , is(iss[intSet]), sot(sot0), srt(srt0) {} 00193 bool solution(const SetAssignment& x) const { 00194 CountableSetRanges xr0(x.lub, x[0]); 00195 CountableSetRanges xr1(x.lub, x[1]); 00196 IntSetRanges isr(is); 00197 switch (sot) { 00198 case SOT_UNION: 00199 { 00200 Iter::Ranges::Union<CountableSetRanges, CountableSetRanges> 00201 u(xr0, xr1); 00202 return sol(u,isr); 00203 } 00204 break; 00205 case SOT_DUNION: 00206 { 00207 Iter::Ranges::Inter<CountableSetRanges, CountableSetRanges> 00208 inter(xr0, xr1); 00209 if (inter()) 00210 return false; 00211 Iter::Ranges::Union<CountableSetRanges, CountableSetRanges> 00212 u(xr0, xr1); 00213 return sol(u,isr); 00214 } 00215 break; 00216 case SOT_INTER: 00217 { 00218 Iter::Ranges::Inter<CountableSetRanges, CountableSetRanges> 00219 u(xr0,xr1); 00220 return sol(u,isr); 00221 } 00222 break; 00223 case SOT_MINUS: 00224 { 00225 Iter::Ranges::Diff<CountableSetRanges, CountableSetRanges> 00226 u(xr0,xr1); 00227 return sol(u,isr); 00228 } 00229 break; 00230 } 00231 GECODE_NEVER; 00232 return false; 00233 } 00235 void post(Space& home, SetVarArray& x, IntVarArray&) { 00236 Gecode::rel(home, x[0], sot, x[1], srt, is); 00237 } 00238 }; 00239 00241 class RelISI : public SetTest { 00242 private: 00243 IntSet is0; 00244 IntSet is1; 00245 Gecode::SetOpType sot; 00246 Gecode::SetRelType srt; 00247 bool inverse; 00248 00249 template<class I, class J> 00250 bool 00251 sol(I& i, J& j) const { 00252 switch (srt) { 00253 case SRT_EQ: return Iter::Ranges::equal(i,j); 00254 case SRT_NQ: return !Iter::Ranges::equal(i,j); 00255 case SRT_SUB: return Iter::Ranges::subset(i,j); 00256 case SRT_SUP: return Iter::Ranges::subset(j,i); 00257 case SRT_DISJ: 00258 { 00259 Gecode::Iter::Ranges::Inter<I,J> inter(i,j); 00260 return !inter(); 00261 } 00262 case SRT_CMPL: 00263 { 00264 Gecode::Set::RangesCompl<J> jc(j); 00265 return Iter::Ranges::equal(i,jc); 00266 } 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 } 00326 GECODE_NEVER; 00327 return false; 00328 } 00330 void post(Space& home, SetVarArray& x, IntVarArray&) { 00331 if (!inverse) 00332 Gecode::rel(home, is0, sot, x[0], srt, is1); 00333 else 00334 Gecode::rel(home, x[0], sot, is0, srt, is1); 00335 } 00336 }; 00337 00339 class Create { 00340 public: 00342 Create(void) { 00343 using namespace Gecode; 00344 for (SetRelTypes srts; srts(); ++srts) { 00345 for (SetOpTypes sots; sots(); ++sots) { 00346 for (int i=0; i<=2; i++) { 00347 (void) new RelSIS(sots.sot(),srts.srt(),i,false); 00348 (void) new RelSIS(sots.sot(),srts.srt(),i,true); 00349 (void) new RelSSI(sots.sot(),srts.srt(),i); 00350 (void) new RelISI(sots.sot(),srts.srt(),i,0,false); 00351 (void) new RelISI(sots.sot(),srts.srt(),i,1,false); 00352 (void) new RelISI(sots.sot(),srts.srt(),i,2,false); 00353 (void) new RelISI(sots.sot(),srts.srt(),i,0,true); 00354 (void) new RelISI(sots.sot(),srts.srt(),i,1,true); 00355 (void) new RelISI(sots.sot(),srts.srt(),i,2,true); 00356 } 00357 } 00358 } 00359 } 00360 }; 00361 00362 Create c; 00363 00365 00366 }}} 00367 00368 // STATISTICS: test-set