singleton.hpp
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 * 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: 2010-07-28 17:35:33 +0200 (Wed, 28 Jul 2010) $ by $Author: schulte $ 00015 * $Revision: 11294 $ 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 { namespace Set { 00043 00044 forceinline 00045 SingletonView::SingletonView(void) {} 00046 00047 forceinline 00048 SingletonView::SingletonView(Gecode::Int::IntView& y) 00049 : DerivedView<Gecode::Int::IntView>(y) {} 00050 00051 forceinline PropCond 00052 SingletonView::pc_settoint(PropCond pc) { 00053 switch(pc) { 00054 case PC_SET_VAL: 00055 case PC_SET_CGLB: 00056 case PC_SET_CARD: 00057 return Gecode::Int::PC_INT_VAL; 00058 default: 00059 return Gecode::Int::PC_INT_DOM; 00060 } 00061 } 00062 00063 forceinline ModEvent 00064 SingletonView::me_inttoset(ModEvent me) { 00065 switch(me) { 00066 case Gecode::Int::ME_INT_FAILED: 00067 return ME_SET_FAILED; 00068 case Gecode::Int::ME_INT_NONE: 00069 return ME_SET_NONE; 00070 case Gecode::Int::ME_INT_VAL: 00071 return ME_SET_VAL; 00072 case Gecode::Int::ME_INT_DOM: 00073 return ME_SET_LUB; 00074 default: 00075 return ME_SET_LUB; 00076 } 00077 } 00078 00079 forceinline ModEvent 00080 SingletonView::me_settoint(ModEvent me) { 00081 switch(me) { 00082 case ME_SET_FAILED: 00083 return Gecode::Int::ME_INT_FAILED; 00084 case ME_SET_NONE: 00085 return Gecode::Int::ME_INT_NONE; 00086 case ME_SET_VAL: 00087 return Gecode::Int::ME_INT_VAL; 00088 default: 00089 return Gecode::Int::ME_INT_DOM; 00090 } 00091 } 00092 00093 forceinline unsigned int 00094 SingletonView::glbSize(void) const { 00095 return x.assigned() ? 1U : 0U; 00096 } 00097 00098 forceinline unsigned int 00099 SingletonView::lubSize(void) const { return x.size(); } 00100 00101 forceinline unsigned int 00102 SingletonView::unknownSize(void) const { 00103 return lubSize() - glbSize(); 00104 } 00105 00106 forceinline bool 00107 SingletonView::contains(int n) const { return x.assigned() ? 00108 (x.val()==n) : false; } 00109 00110 forceinline bool 00111 SingletonView::notContains(int n) const { return !x.in(n); } 00112 00113 forceinline unsigned int 00114 SingletonView::cardMin() const { return 1; } 00115 00116 forceinline unsigned int 00117 SingletonView::cardMax() const { return 1; } 00118 00119 forceinline int 00120 SingletonView::lubMin() const { return x.min(); } 00121 00122 forceinline int 00123 SingletonView::lubMax() const { return x.max(); } 00124 00125 forceinline int 00126 SingletonView::glbMin() const { return x.assigned() ? 00127 x.val() : BndSet::MIN_OF_EMPTY; } 00128 00129 forceinline int 00130 SingletonView::glbMax() const { return x.assigned() ? 00131 x.val() : BndSet::MAX_OF_EMPTY; } 00132 00133 forceinline ModEvent 00134 SingletonView::cardMin(Space&, unsigned int c) { 00135 return c<=1 ? ME_SET_NONE : ME_SET_FAILED; 00136 } 00137 00138 forceinline ModEvent 00139 SingletonView::cardMax(Space&, unsigned int c) { 00140 return c<1 ? ME_SET_FAILED : ME_SET_NONE; 00141 } 00142 00143 forceinline ModEvent 00144 SingletonView::include(Space& home,int c) { 00145 return me_inttoset(x.eq(home,c)); 00146 } 00147 00148 forceinline ModEvent 00149 SingletonView::intersect(Space& home,int c) { 00150 return me_inttoset(x.eq(home,c)); 00151 } 00152 00153 forceinline ModEvent 00154 SingletonView::intersect(Space& home,int i, int j) { 00155 ModEvent me1 = me_inttoset(x.gq(home,i)); 00156 ModEvent me2 = me_inttoset(x.lq(home,j)); 00157 if (me_failed(me1) || me_failed(me2)) 00158 return ME_SET_FAILED; 00159 switch (me1) { 00160 case ME_SET_NONE: 00161 case ME_SET_LUB: 00162 return me2; 00163 case ME_SET_VAL: 00164 return ME_SET_VAL; 00165 default: 00166 GECODE_NEVER; 00167 return ME_SET_VAL; 00168 } 00169 } 00170 00171 forceinline ModEvent 00172 SingletonView::exclude(Space& home,int c) { 00173 return me_inttoset(x.nq(home,c)); 00174 } 00175 00176 forceinline ModEvent 00177 SingletonView::include(Space& home, int j, int k) { 00178 return j==k ? me_inttoset(x.eq(home,j)) : ME_SET_FAILED ; 00179 } 00180 00181 forceinline ModEvent 00182 SingletonView::exclude(Space& home, int j, int k) { 00183 ModEvent me1 = me_inttoset(x.gr(home,j)); 00184 ModEvent me2 = me_inttoset(x.le(home,k)); 00185 if (me_failed(me1) || me_failed(me2)) 00186 return ME_SET_FAILED; 00187 switch (me1) { 00188 case ME_SET_NONE: 00189 case ME_SET_LUB: 00190 return me2; 00191 case ME_SET_VAL: 00192 return ME_SET_VAL; 00193 default: 00194 GECODE_NEVER; 00195 return ME_SET_VAL; 00196 } 00197 } 00198 00199 template<class I> ModEvent 00200 SingletonView::excludeI(Space& home, I& iter) { 00201 return me_inttoset(x.minus_r(home,iter)); 00202 } 00203 00204 template<class I> ModEvent 00205 SingletonView::includeI(Space& home, I& iter) { 00206 if (!iter()) 00207 return ME_SET_NONE; 00208 00209 if (iter.min()!=iter.max()) 00210 return ME_SET_FAILED; 00211 00212 int val = iter.min(); 00213 ++iter; 00214 if ( iter() ) 00215 return ME_SET_FAILED; 00216 00217 return me_inttoset(x.eq(home, val)); 00218 } 00219 00220 template<class I> ModEvent 00221 SingletonView::intersectI(Space& home, I& iter) { 00222 return me_inttoset(x.inter_r(home,iter)); 00223 } 00224 00225 forceinline void 00226 SingletonView::subscribe(Space& home, Propagator& p, PropCond pc, 00227 bool schedule) { 00228 x.subscribe(home,p,pc_settoint(pc),schedule); 00229 } 00230 forceinline void 00231 SingletonView::cancel(Space& home, Propagator& p, PropCond pc) { 00232 x.cancel(home,p,pc_settoint(pc)); 00233 } 00234 00235 forceinline void 00236 SingletonView::subscribe(Space& home, Advisor& a) { 00237 x.subscribe(home,a); 00238 } 00239 forceinline void 00240 SingletonView::cancel(Space& home, Advisor& a) { 00241 x.cancel(home,a); 00242 } 00243 00244 00245 forceinline void 00246 SingletonView::schedule(Space& home, Propagator& p, ModEvent me) { 00247 return Gecode::Int::IntView::schedule(home,p,me_settoint(me)); 00248 } 00249 forceinline ModEvent 00250 SingletonView::me(const ModEventDelta& med) { 00251 return me_inttoset(Int::IntView::me(med)); 00252 } 00253 forceinline ModEventDelta 00254 SingletonView::med(ModEvent me) { 00255 return SetView::med(me_settoint(me)); 00256 } 00257 00258 00259 /* 00260 * Delta information for advisors 00261 * 00262 * For SingletonViews, a glb change means that the view is assigned. 00263 * Thus, the delta for the glb is always equal to the delta for the lub. 00264 * 00265 */ 00266 00267 forceinline ModEvent 00268 SingletonView::modevent(const Delta& d) { 00269 return me_inttoset(Int::IntView::modevent(d)); 00270 } 00271 00272 forceinline int 00273 SingletonView::glbMin(const Delta& d) const { return x.min(d); } 00274 00275 forceinline int 00276 SingletonView::glbMax(const Delta& d) const { return x.max(d); } 00277 00278 forceinline bool 00279 SingletonView::glbAny(const Delta& d) const { return x.any(d); } 00280 00281 forceinline int 00282 SingletonView::lubMin(const Delta& d) const { return x.min(d); } 00283 00284 forceinline int 00285 SingletonView::lubMax(const Delta& d) const { return x.max(d); } 00286 00287 forceinline bool 00288 SingletonView::lubAny(const Delta& d) const { return x.any(d); } 00289 00290 /* 00291 * Iterators 00292 * 00293 */ 00294 00299 template<> 00300 class LubRanges<SingletonView> : public Gecode::Int::IntVarImpFwd { 00301 public: 00303 00304 00305 LubRanges(void); 00307 LubRanges(const SingletonView& x); 00309 void init(const SingletonView& x); 00311 }; 00312 00313 forceinline 00314 LubRanges<SingletonView>::LubRanges(void) {} 00315 00316 forceinline 00317 LubRanges<SingletonView>::LubRanges(const SingletonView& s) : 00318 Gecode::Int::IntVarImpFwd(s.base().varimp()) {} 00319 00320 forceinline void 00321 LubRanges<SingletonView>::init(const SingletonView& s) { 00322 Gecode::Int::IntVarImpFwd::init(s.base().varimp()); 00323 } 00324 00329 template<> 00330 class GlbRanges<SingletonView> { 00331 private: 00332 int val; 00333 bool flag; 00334 public: 00336 00337 00338 GlbRanges(void); 00340 GlbRanges(const SingletonView& x); 00342 void init(const SingletonView& x); 00343 00345 00346 00347 bool operator ()(void) const; 00349 void operator ++(void); 00351 00353 00354 00355 int min(void) const; 00357 int max(void) const; 00359 unsigned int width(void) const; 00361 }; 00362 00363 forceinline 00364 GlbRanges<SingletonView>::GlbRanges(void) {} 00365 00366 forceinline void 00367 GlbRanges<SingletonView>::init(const SingletonView& s) { 00368 if (s.base().assigned()) { 00369 val = s.base().val(); 00370 flag = true; 00371 } else { 00372 val = 0; 00373 flag = false; 00374 } 00375 } 00376 00377 forceinline 00378 GlbRanges<SingletonView>::GlbRanges(const SingletonView& s) { 00379 init(s); 00380 } 00381 00382 forceinline bool 00383 GlbRanges<SingletonView>::operator ()(void) const { return flag; } 00384 00385 forceinline void 00386 GlbRanges<SingletonView>::operator ++(void) { flag=false; } 00387 00388 forceinline int 00389 GlbRanges<SingletonView>::min(void) const { return val; } 00390 forceinline int 00391 GlbRanges<SingletonView>::max(void) const { return val; } 00392 forceinline unsigned int 00393 GlbRanges<SingletonView>::width(void) const { return 1; } 00394 00395 }} 00396 00397 // STATISTICS: set-var 00398