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-04-08 12:35:31 +0200 (Thu, 08 Apr 2010) $ by $Author: schulte $ 00011 * $Revision: 10684 $ 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 Iter::Ranges::IsRangeIter<I>(); 00071 Iter::Ranges::IsRangeIter<J>(); 00072 switch (srt) { 00073 case SRT_EQ: return Iter::Ranges::equal(i,j); 00074 case SRT_NQ: return !Iter::Ranges::equal(i,j); 00075 case SRT_SUB: return Iter::Ranges::subset(i,j); 00076 case SRT_SUP: return Iter::Ranges::subset(j,i); 00077 case SRT_DISJ: 00078 { 00079 Gecode::Iter::Ranges::Inter<I,J> inter(i,j); 00080 return !inter(); 00081 } 00082 case SRT_CMPL: 00083 { 00084 Gecode::Set::RangesCompl<J> jc(j); 00085 return Iter::Ranges::equal(i,jc); 00086 } 00087 } 00088 GECODE_NEVER; 00089 return false; 00090 } 00091 00092 public: 00094 RelSIS(Gecode::SetOpType sot0, Gecode::SetRelType srt0, 00095 int intSet, bool inverse0) 00096 : SetTest("RelOp::ConstSIS::"+str(sot0)+"::"+str(srt0)+"::"+ 00097 str(intSet)+(inverse0 ? "i" :""),2,ds_22,false) 00098 , is(iss[intSet]), sot(sot0), srt(srt0), inverse(inverse0) {} 00100 bool solution(const SetAssignment& x) const { 00101 IntSetRanges isr(is); 00102 CountableSetRanges xr0(x.lub, x[0]); 00103 CountableSetRanges xr1(x.lub, x[1]); 00104 switch (sot) { 00105 case SOT_UNION: 00106 { 00107 Iter::Ranges::Union<IntSetRanges, CountableSetRanges> 00108 u(isr, xr0); 00109 return sol(u,xr1); 00110 } 00111 break; 00112 case SOT_DUNION: 00113 { 00114 Iter::Ranges::Inter<IntSetRanges, CountableSetRanges> 00115 inter(isr, xr0); 00116 if (inter()) 00117 return false; 00118 Iter::Ranges::Union<IntSetRanges, CountableSetRanges> 00119 u(isr,xr0); 00120 return sol(u,xr1); 00121 } 00122 break; 00123 case SOT_INTER: 00124 { 00125 Iter::Ranges::Inter<IntSetRanges, CountableSetRanges> 00126 u(isr,xr0); 00127 return sol(u,xr1); 00128 } 00129 break; 00130 case SOT_MINUS: 00131 { 00132 if (!inverse) { 00133 Iter::Ranges::Diff<IntSetRanges, CountableSetRanges> 00134 u(isr,xr0); 00135 return sol(u,xr1); 00136 } else { 00137 Iter::Ranges::Diff<CountableSetRanges, IntSetRanges> 00138 u(xr0,isr); 00139 return sol(u,xr1); 00140 00141 } 00142 } 00143 break; 00144 } 00145 GECODE_NEVER; 00146 return false; 00147 } 00149 void post(Space& home, SetVarArray& x, IntVarArray&) { 00150 if (!inverse) 00151 Gecode::rel(home, is, sot, x[0], srt, x[1]); 00152 else 00153 Gecode::rel(home, x[0], sot, is, srt, x[1]); 00154 } 00155 }; 00156 00158 class RelSSI : public SetTest { 00159 private: 00160 IntSet is; 00161 Gecode::SetOpType sot; 00162 Gecode::SetRelType srt; 00163 00164 template<class I, class J> 00165 bool 00166 sol(I& i, J& j) const { 00167 Iter::Ranges::IsRangeIter<I>(); 00168 Iter::Ranges::IsRangeIter<J>(); 00169 switch (srt) { 00170 case SRT_EQ: return Iter::Ranges::equal(i,j); 00171 case SRT_NQ: return !Iter::Ranges::equal(i,j); 00172 case SRT_SUB: return Iter::Ranges::subset(i,j); 00173 case SRT_SUP: return Iter::Ranges::subset(j,i); 00174 case SRT_DISJ: 00175 { 00176 Gecode::Iter::Ranges::Inter<I,J> inter(i,j); 00177 return !inter(); 00178 } 00179 case SRT_CMPL: 00180 { 00181 Gecode::Set::RangesCompl<J> jc(j); 00182 return Iter::Ranges::equal(i,jc); 00183 } 00184 } 00185 GECODE_NEVER; 00186 return false; 00187 } 00188 00189 public: 00191 RelSSI(Gecode::SetOpType sot0, Gecode::SetRelType srt0, 00192 int intSet) 00193 : SetTest("RelOp::ConstSSI::"+str(sot0)+"::"+str(srt0)+"::"+ 00194 str(intSet),2,ds_22,false) 00195 , is(iss[intSet]), sot(sot0), srt(srt0) {} 00197 bool solution(const SetAssignment& x) const { 00198 CountableSetRanges xr0(x.lub, x[0]); 00199 CountableSetRanges xr1(x.lub, x[1]); 00200 IntSetRanges isr(is); 00201 switch (sot) { 00202 case SOT_UNION: 00203 { 00204 Iter::Ranges::Union<CountableSetRanges, CountableSetRanges> 00205 u(xr0, xr1); 00206 return sol(u,isr); 00207 } 00208 break; 00209 case SOT_DUNION: 00210 { 00211 Iter::Ranges::Inter<CountableSetRanges, CountableSetRanges> 00212 inter(xr0, xr1); 00213 if (inter()) 00214 return false; 00215 Iter::Ranges::Union<CountableSetRanges, CountableSetRanges> 00216 u(xr0, xr1); 00217 return sol(u,isr); 00218 } 00219 break; 00220 case SOT_INTER: 00221 { 00222 Iter::Ranges::Inter<CountableSetRanges, CountableSetRanges> 00223 u(xr0,xr1); 00224 return sol(u,isr); 00225 } 00226 break; 00227 case SOT_MINUS: 00228 { 00229 Iter::Ranges::Diff<CountableSetRanges, CountableSetRanges> 00230 u(xr0,xr1); 00231 return sol(u,isr); 00232 } 00233 break; 00234 } 00235 GECODE_NEVER; 00236 return false; 00237 } 00239 void post(Space& home, SetVarArray& x, IntVarArray&) { 00240 Gecode::rel(home, x[0], sot, x[1], srt, is); 00241 } 00242 }; 00243 00245 class RelISI : public SetTest { 00246 private: 00247 IntSet is0; 00248 IntSet is1; 00249 Gecode::SetOpType sot; 00250 Gecode::SetRelType srt; 00251 bool inverse; 00252 00253 template<class I, class J> 00254 bool 00255 sol(I& i, J& j) const { 00256 Iter::Ranges::IsRangeIter<I>(); 00257 Iter::Ranges::IsRangeIter<J>(); 00258 switch (srt) { 00259 case SRT_EQ: return Iter::Ranges::equal(i,j); 00260 case SRT_NQ: return !Iter::Ranges::equal(i,j); 00261 case SRT_SUB: return Iter::Ranges::subset(i,j); 00262 case SRT_SUP: return Iter::Ranges::subset(j,i); 00263 case SRT_DISJ: 00264 { 00265 Gecode::Iter::Ranges::Inter<I,J> inter(i,j); 00266 return !inter(); 00267 } 00268 case SRT_CMPL: 00269 { 00270 Gecode::Set::RangesCompl<J> jc(j); 00271 return Iter::Ranges::equal(i,jc); 00272 } 00273 } 00274 GECODE_NEVER; 00275 return false; 00276 } 00277 00278 public: 00280 RelISI(Gecode::SetOpType sot0, Gecode::SetRelType srt0, 00281 int intSet0, int intSet1, bool inverse0) 00282 : SetTest("RelOp::ConstISI::"+str(sot0)+"::"+str(srt0)+"::"+ 00283 str(intSet0)+"::"+str(intSet1)+ 00284 (inverse0 ? "i" : ""),1,ds_33,false) 00285 , is0(iss[intSet0]), is1(iss[intSet1]), sot(sot0), srt(srt0) 00286 , inverse(inverse0) {} 00288 bool solution(const SetAssignment& x) const { 00289 CountableSetRanges xr0(x.lub, x[0]); 00290 IntSetRanges isr0(is0); 00291 IntSetRanges isr1(is1); 00292 switch (sot) { 00293 case SOT_UNION: 00294 { 00295 Iter::Ranges::Union<IntSetRanges, CountableSetRanges> 00296 u(isr0, xr0); 00297 return sol(u,isr1); 00298 } 00299 break; 00300 case SOT_DUNION: 00301 { 00302 Iter::Ranges::Inter<IntSetRanges, CountableSetRanges> 00303 inter(isr0, xr0); 00304 if (inter()) 00305 return false; 00306 Iter::Ranges::Union<IntSetRanges, CountableSetRanges> 00307 u(isr0, xr0); 00308 return sol(u,isr1); 00309 } 00310 break; 00311 case SOT_INTER: 00312 { 00313 Iter::Ranges::Inter<IntSetRanges, CountableSetRanges> 00314 u(isr0,xr0); 00315 return sol(u,isr1); 00316 } 00317 break; 00318 case SOT_MINUS: 00319 { 00320 if (!inverse) { 00321 Iter::Ranges::Diff<IntSetRanges, CountableSetRanges> 00322 u(isr0,xr0); 00323 return sol(u,isr1); 00324 } else { 00325 Iter::Ranges::Diff<CountableSetRanges, IntSetRanges> 00326 u(xr0,isr0); 00327 return sol(u,isr1); 00328 } 00329 } 00330 break; 00331 } 00332 GECODE_NEVER; 00333 return false; 00334 } 00336 void post(Space& home, SetVarArray& x, IntVarArray&) { 00337 if (!inverse) 00338 Gecode::rel(home, is0, sot, x[0], srt, is1); 00339 else 00340 Gecode::rel(home, x[0], sot, is0, srt, is1); 00341 } 00342 }; 00343 00345 class Create { 00346 public: 00348 Create(void) { 00349 using namespace Gecode; 00350 for (SetRelTypes srts; srts(); ++srts) { 00351 for (SetOpTypes sots; sots(); ++sots) { 00352 for (int i=0; i<=2; i++) { 00353 (void) new RelSIS(sots.sot(),srts.srt(),i,false); 00354 (void) new RelSIS(sots.sot(),srts.srt(),i,true); 00355 (void) new RelSSI(sots.sot(),srts.srt(),i); 00356 (void) new RelISI(sots.sot(),srts.srt(),i,0,false); 00357 (void) new RelISI(sots.sot(),srts.srt(),i,1,false); 00358 (void) new RelISI(sots.sot(),srts.srt(),i,2,false); 00359 (void) new RelISI(sots.sot(),srts.srt(),i,0,true); 00360 (void) new RelISI(sots.sot(),srts.srt(),i,1,true); 00361 (void) new RelISI(sots.sot(),srts.srt(),i,2,true); 00362 } 00363 } 00364 } 00365 } 00366 }; 00367 00368 Create c; 00369 00371 00372 }}} 00373 00374 // STATISTICS: test-set