gecode » set » view

00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ 00002 /* 00003 * Main authors: 00004 * Guido Tack <tack@gecode.org> 00005 * 00006 * Contributing authors: 00007 * Christian Schulte <schulte@gecode.org> 00008 * 00009 * Copyright: 00010 * Guido Tack, 2004 00011 * Christian Schulte, 2004 00012 * 00013 * Last modified: 00014 * $Date: 2009-09-08 21:10:29 +0200 (Tue, 08 Sep 2009) $ by $Author: schulte $ 00015 * $Revision: 9692 $ 00016 * 00017 * This file is part of Gecode, the generic constraint 00018 * development environment: 00019 * http://www.gecode.org 00020 * 00021 * Permission is hereby granted, free of charge, to any person obtaining 00022 * a copy of this software and associated documentation files (the 00023 * "Software"), to deal in the Software without restriction, including 00024 * without limitation the rights to use, copy, modify, merge, publish, 00025 * distribute, sublicense, and/or sell copies of the Software, and to 00026 * permit persons to whom the Software is furnished to do so, subject to 00027 * the following conditions: 00028 * 00029 * The above copyright notice and this permission notice shall be 00030 * included in all copies or substantial portions of the Software. 00031 * 00032 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 00033 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00034 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00035 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 00036 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 00037 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 00038 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00039 * 00040 */ 00041 00042 namespace Gecode { 00043 00044 namespace Set { 00045 00046 forceinline 00047 SingletonView::SingletonView(void) {} 00048 00049 forceinline 00050 SingletonView::SingletonView(Gecode::Int::IntView& i0) 00051 : DerivedViewBase<Gecode::Int::IntView>(i0) {} 00052 00053 forceinline PropCond 00054 SingletonView::pc_settoint(PropCond pc) { 00055 switch(pc) { 00056 case PC_SET_VAL: 00057 case PC_SET_CGLB: 00058 case PC_SET_CARD: 00059 return Gecode::Int::PC_INT_VAL; 00060 default: 00061 return Gecode::Int::PC_INT_DOM; 00062 } 00063 } 00064 00065 forceinline ModEvent 00066 SingletonView::me_inttoset(ModEvent me) { 00067 switch(me) { 00068 case Gecode::Int::ME_INT_FAILED: 00069 return ME_SET_FAILED; 00070 case Gecode::Int::ME_INT_NONE: 00071 return ME_SET_NONE; 00072 case Gecode::Int::ME_INT_VAL: 00073 return ME_SET_VAL; 00074 case Gecode::Int::ME_INT_DOM: 00075 return ME_SET_LUB; 00076 default: 00077 return ME_SET_LUB; 00078 } 00079 } 00080 00081 forceinline ModEvent 00082 SingletonView::me_settoint(ModEvent me) { 00083 switch(me) { 00084 case ME_SET_FAILED: 00085 return Gecode::Int::ME_INT_FAILED; 00086 case ME_SET_NONE: 00087 return Gecode::Int::ME_INT_NONE; 00088 case ME_SET_VAL: 00089 return Gecode::Int::ME_INT_VAL; 00090 default: 00091 return Gecode::Int::ME_INT_DOM; 00092 } 00093 } 00094 00095 forceinline bool 00096 SingletonView::assigned(void) const { return view.assigned(); } 00097 00098 forceinline unsigned int 00099 SingletonView::glbSize(void) const { 00100 return view.assigned() ? 1U : 0U; 00101 } 00102 00103 forceinline unsigned int 00104 SingletonView::lubSize(void) const { return view.size(); } 00105 00106 forceinline unsigned int 00107 SingletonView::unknownSize(void) const { 00108 return lubSize() - glbSize(); 00109 } 00110 00111 forceinline bool 00112 SingletonView::contains(int n) const { return view.assigned() ? 00113 (view.val()==n) : false; } 00114 00115 forceinline bool 00116 SingletonView::notContains(int n) const { return !view.in(n); } 00117 00118 forceinline unsigned int 00119 SingletonView::cardMin() const { return 1; } 00120 00121 forceinline unsigned int 00122 SingletonView::cardMax() const { return 1; } 00123 00124 forceinline int 00125 SingletonView::lubMin() const { return view.min(); } 00126 00127 forceinline int 00128 SingletonView::lubMax() const { return view.max(); } 00129 00130 forceinline int 00131 SingletonView::glbMin() const { return view.assigned() ? 00132 view.val() : BndSet::MIN_OF_EMPTY; } 00133 00134 forceinline int 00135 SingletonView::glbMax() const { return view.assigned() ? 00136 view.val() : BndSet::MAX_OF_EMPTY; } 00137 00138 forceinline ModEvent 00139 SingletonView::cardMin(Space&, unsigned int c) { 00140 return c<=1 ? ME_SET_NONE : ME_SET_FAILED; 00141 } 00142 00143 forceinline ModEvent 00144 SingletonView::cardMax(Space&, unsigned int c) { 00145 return c<1 ? ME_SET_FAILED : ME_SET_NONE; 00146 } 00147 00148 forceinline ModEvent 00149 SingletonView::include(Space& home,int c) { 00150 return me_inttoset(view.eq(home,c)); 00151 } 00152 00153 forceinline ModEvent 00154 SingletonView::intersect(Space& home,int c) { 00155 return me_inttoset(view.eq(home,c)); 00156 } 00157 00158 forceinline ModEvent 00159 SingletonView::intersect(Space& home,int i, int j) { 00160 ModEvent me1 = me_inttoset(view.gq(home,i)); 00161 ModEvent me2 = me_inttoset(view.lq(home,j)); 00162 if (me_failed(me1) || me_failed(me2)) 00163 return ME_SET_FAILED; 00164 switch (me1) { 00165 case ME_SET_NONE: 00166 case ME_SET_LUB: 00167 return me2; 00168 case ME_SET_VAL: 00169 return ME_SET_VAL; 00170 default: 00171 GECODE_NEVER; 00172 return ME_SET_VAL; 00173 } 00174 } 00175 00176 forceinline ModEvent 00177 SingletonView::exclude(Space& home,int c) { 00178 return me_inttoset(view.nq(home,c)); 00179 } 00180 00181 forceinline ModEvent 00182 SingletonView::include(Space& home, int j, int k) { 00183 return j==k ? me_inttoset(view.eq(home,j)) : ME_SET_FAILED ; 00184 } 00185 00186 forceinline ModEvent 00187 SingletonView::exclude(Space& home, int j, int k) { 00188 ModEvent me1 = me_inttoset(view.gr(home,j)); 00189 ModEvent me2 = me_inttoset(view.le(home,k)); 00190 if (me_failed(me1) || me_failed(me2)) 00191 return ME_SET_FAILED; 00192 switch (me1) { 00193 case ME_SET_NONE: 00194 case ME_SET_LUB: 00195 return me2; 00196 case ME_SET_VAL: 00197 return ME_SET_VAL; 00198 default: 00199 GECODE_NEVER; 00200 return ME_SET_VAL; 00201 } 00202 } 00203 00204 template<class I> ModEvent 00205 SingletonView::excludeI(Space& home, I& iter) { 00206 return me_inttoset(view.minus_r(home,iter)); 00207 } 00208 00209 template<class I> ModEvent 00210 SingletonView::includeI(Space& home, I& iter) { 00211 Iter::Ranges::IsRangeIter<I>(); 00212 if (!iter()) 00213 return ME_SET_NONE; 00214 00215 if (iter.min()!=iter.max()) 00216 return ME_SET_FAILED; 00217 00218 int val = iter.min(); 00219 ++iter; 00220 if ( iter() ) 00221 return ME_SET_FAILED; 00222 00223 return me_inttoset(view.eq(home, val)); 00224 } 00225 00226 template<class I> ModEvent 00227 SingletonView::intersectI(Space& home, I& iter) { 00228 return me_inttoset(view.inter_r(home,iter)); 00229 } 00230 00231 forceinline void 00232 SingletonView::subscribe(Space& home, Propagator& p, PropCond pc, 00233 bool process) { 00234 view.subscribe(home,p,pc_settoint(pc),process); 00235 } 00236 forceinline void 00237 SingletonView::cancel(Space& home, Propagator& p, PropCond pc) { 00238 view.cancel(home,p,pc_settoint(pc)); 00239 } 00240 00241 forceinline void 00242 SingletonView::subscribe(Space& home, Advisor& a) { 00243 view.subscribe(home,a); 00244 } 00245 forceinline void 00246 SingletonView::cancel(Space& home, Advisor& a) { 00247 view.cancel(home,a); 00248 } 00249 00250 00251 forceinline void 00252 SingletonView::schedule(Space& home, Propagator& p, ModEvent me) { 00253 return Gecode::Int::IntView::schedule(home,p,me_settoint(me)); 00254 } 00255 forceinline ModEvent 00256 SingletonView::me(const ModEventDelta& med) { 00257 return me_inttoset(Int::IntView::me(med)); 00258 } 00259 forceinline ModEventDelta 00260 SingletonView::med(ModEvent me) { 00261 return SetView::med(me_settoint(me)); 00262 } 00263 00264 forceinline void 00265 SingletonView::update(Space& home, bool share, SingletonView& y) { 00266 view.update(home,share,y.view); 00267 } 00268 00269 00270 /* 00271 * Delta information for advisors 00272 * 00273 * For SingletonViews, a glb change means that the view is assigned. 00274 * Thus, the delta for the glb is always equal to the delta for the lub. 00275 * 00276 */ 00277 00278 forceinline ModEvent 00279 SingletonView::modevent(const Delta& d) { 00280 return me_inttoset(Int::IntView::modevent(d)); 00281 } 00282 00283 forceinline int 00284 SingletonView::glbMin(const Delta& d) const { return view.min(d); } 00285 00286 forceinline int 00287 SingletonView::glbMax(const Delta& d) const { return view.max(d); } 00288 00289 forceinline bool 00290 SingletonView::glbAny(const Delta& d) const { return view.any(d); } 00291 00292 forceinline int 00293 SingletonView::lubMin(const Delta& d) const { return view.min(d); } 00294 00295 forceinline int 00296 SingletonView::lubMax(const Delta& d) const { return view.max(d); } 00297 00298 forceinline bool 00299 SingletonView::lubAny(const Delta& d) const { return view.any(d); } 00300 00301 /* 00302 * Iterators 00303 * 00304 */ 00305 00310 template<> 00311 class LubRanges<SingletonView> : public Gecode::Int::IntVarImpFwd { 00312 public: 00314 00315 00316 LubRanges(void); 00318 LubRanges(const SingletonView& x); 00320 void init(const SingletonView& x); 00322 }; 00323 00324 forceinline 00325 LubRanges<SingletonView>::LubRanges(void) {} 00326 00327 forceinline 00328 LubRanges<SingletonView>::LubRanges(const SingletonView& s) : 00329 Gecode::Int::IntVarImpFwd(s.base().var()) {} 00330 00331 forceinline void 00332 LubRanges<SingletonView>::init(const SingletonView& s) { 00333 Gecode::Int::IntVarImpFwd::init(s.base().var()); 00334 } 00335 00340 template<> 00341 class GlbRanges<SingletonView> { 00342 private: 00343 int val; 00344 bool flag; 00345 public: 00347 00348 00349 GlbRanges(void); 00351 GlbRanges(const SingletonView& x); 00353 void init(const SingletonView& x); 00354 00356 00357 00358 bool operator ()(void) const; 00360 void operator ++(void); 00362 00364 00365 00366 int min(void) const; 00368 int max(void) const; 00370 unsigned int width(void) const; 00372 }; 00373 00374 forceinline 00375 GlbRanges<SingletonView>::GlbRanges(void) {} 00376 00377 forceinline void 00378 GlbRanges<SingletonView>::init(const SingletonView& s) { 00379 if (s.base().assigned()) { 00380 val = s.base().val(); 00381 flag = true; 00382 } else { 00383 val = 0; 00384 flag = false; 00385 } 00386 } 00387 00388 forceinline 00389 GlbRanges<SingletonView>::GlbRanges(const SingletonView& s) { 00390 init(s); 00391 } 00392 00393 forceinline bool 00394 GlbRanges<SingletonView>::operator ()(void) const { return flag; } 00395 00396 forceinline void 00397 GlbRanges<SingletonView>::operator ++(void) { flag=false; } 00398 00399 forceinline int 00400 GlbRanges<SingletonView>::min(void) const { return val; } 00401 forceinline int 00402 GlbRanges<SingletonView>::max(void) const { return val; } 00403 forceinline unsigned int 00404 GlbRanges<SingletonView>::width(void) const { return 1; } 00405 00406 } 00407 00408 /* 00409 * Testing 00410 * 00411 */ 00412 forceinline bool 00413 same(const Set::SingletonView& x, const Set::SingletonView& y) { 00414 return same(x.base(),y.base()); 00415 } 00416 forceinline bool 00417 before(const Set::SingletonView& x, const Set::SingletonView& y) { 00418 return before(x.base(),y.base()); 00419 } 00420 00421 00422 } 00423 00424 // STATISTICS: set-var 00425