this repo has no description
1/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ 2/* 3 * Main authors: 4 * Guido Tack <tack@gecode.org> 5 * 6 * Contributing authors: 7 * Christian Schulte <schulte@gecode.org> 8 * 9 * Copyright: 10 * Guido Tack, 2004 11 * Christian Schulte, 2004 12 * 13 * This file is part of Gecode, the generic constraint 14 * development environment: 15 * http://www.gecode.org 16 * 17 * Permission is hereby granted, free of charge, to any person obtaining 18 * a copy of this software and associated documentation files (the 19 * "Software"), to deal in the Software without restriction, including 20 * without limitation the rights to use, copy, modify, merge, publish, 21 * distribute, sublicense, and/or sell copies of the Software, and to 22 * permit persons to whom the Software is furnished to do so, subject to 23 * the following conditions: 24 * 25 * The above copyright notice and this permission notice shall be 26 * included in all copies or substantial portions of the Software. 27 * 28 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 29 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 30 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 31 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 32 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 33 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 34 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 35 * 36 */ 37 38namespace Gecode { namespace Set { 39 40 forceinline 41 SingletonView::SingletonView(void) {} 42 43 forceinline 44 SingletonView::SingletonView(Gecode::Int::IntView& y) 45 : DerivedView<Gecode::Int::IntView>(y) {} 46 47 forceinline 48 SingletonView::SingletonView(const Gecode::IntVar& y) 49 : DerivedView<Gecode::Int::IntView>(y) {} 50 51 forceinline PropCond 52 SingletonView::pc_settoint(PropCond pc) { 53 switch(pc) { 54 case PC_SET_VAL: 55 case PC_SET_CGLB: 56 case PC_SET_CARD: 57 return Gecode::Int::PC_INT_VAL; 58 default: 59 return Gecode::Int::PC_INT_DOM; 60 } 61 } 62 63 forceinline ModEvent 64 SingletonView::me_inttoset(ModEvent me) { 65 switch(me) { 66 case Gecode::Int::ME_INT_FAILED: 67 return ME_SET_FAILED; 68 case Gecode::Int::ME_INT_NONE: 69 return ME_SET_NONE; 70 case Gecode::Int::ME_INT_VAL: 71 return ME_SET_VAL; 72 case Gecode::Int::ME_INT_DOM: 73 return ME_SET_LUB; 74 default: 75 return ME_SET_LUB; 76 } 77 } 78 79 forceinline ModEvent 80 SingletonView::me_settoint(ModEvent me) { 81 switch(me) { 82 case ME_SET_FAILED: 83 return Gecode::Int::ME_INT_FAILED; 84 case ME_SET_NONE: 85 return Gecode::Int::ME_INT_NONE; 86 case ME_SET_VAL: 87 return Gecode::Int::ME_INT_VAL; 88 default: 89 return Gecode::Int::ME_INT_DOM; 90 } 91 } 92 93 forceinline unsigned int 94 SingletonView::glbSize(void) const { 95 return x.assigned() ? 1U : 0U; 96 } 97 98 forceinline unsigned int 99 SingletonView::lubSize(void) const { return x.size(); } 100 101 forceinline unsigned int 102 SingletonView::unknownSize(void) const { 103 return lubSize() - glbSize(); 104 } 105 106 forceinline bool 107 SingletonView::contains(int n) const { return x.assigned() ? 108 (x.val()==n) : false; } 109 110 forceinline bool 111 SingletonView::notContains(int n) const { return !x.in(n); } 112 113 forceinline unsigned int 114 SingletonView::cardMin() const { return 1; } 115 116 forceinline unsigned int 117 SingletonView::cardMax() const { return 1; } 118 119 forceinline int 120 SingletonView::lubMin() const { return x.min(); } 121 122 forceinline int 123 SingletonView::lubMax() const { return x.max(); } 124 125 forceinline int 126 SingletonView::glbMin() const { return x.assigned() ? 127 x.val() : BndSet::MIN_OF_EMPTY; } 128 129 forceinline int 130 SingletonView::glbMax() const { return x.assigned() ? 131 x.val() : BndSet::MAX_OF_EMPTY; } 132 133 forceinline ModEvent 134 SingletonView::cardMin(Space&, unsigned int c) { 135 return c<=1 ? ME_SET_NONE : ME_SET_FAILED; 136 } 137 138 forceinline ModEvent 139 SingletonView::cardMax(Space&, unsigned int c) { 140 return c<1 ? ME_SET_FAILED : ME_SET_NONE; 141 } 142 143 forceinline ModEvent 144 SingletonView::include(Space& home,int c) { 145 return me_inttoset(x.eq(home,c)); 146 } 147 148 forceinline ModEvent 149 SingletonView::intersect(Space& home,int c) { 150 return me_inttoset(x.eq(home,c)); 151 } 152 153 forceinline ModEvent 154 SingletonView::intersect(Space& home,int i, int j) { 155 ModEvent me1 = me_inttoset(x.gq(home,i)); 156 ModEvent me2 = me_inttoset(x.lq(home,j)); 157 if (me_failed(me1) || me_failed(me2)) 158 return ME_SET_FAILED; 159 switch (me1) { 160 case ME_SET_NONE: 161 case ME_SET_LUB: 162 return me2; 163 case ME_SET_VAL: 164 return ME_SET_VAL; 165 default: 166 GECODE_NEVER; 167 return ME_SET_VAL; 168 } 169 } 170 171 forceinline ModEvent 172 SingletonView::exclude(Space& home,int c) { 173 return me_inttoset(x.nq(home,c)); 174 } 175 176 forceinline ModEvent 177 SingletonView::include(Space& home, int j, int k) { 178 return j==k ? me_inttoset(x.eq(home,j)) : ME_SET_FAILED ; 179 } 180 181 forceinline ModEvent 182 SingletonView::exclude(Space& home, int j, int k) { 183 ModEvent me1 = me_inttoset(x.gr(home,j)); 184 ModEvent me2 = me_inttoset(x.le(home,k)); 185 if (me_failed(me1) || me_failed(me2)) 186 return ME_SET_FAILED; 187 switch (me1) { 188 case ME_SET_NONE: 189 case ME_SET_LUB: 190 return me2; 191 case ME_SET_VAL: 192 return ME_SET_VAL; 193 default: 194 GECODE_NEVER; 195 return ME_SET_VAL; 196 } 197 } 198 199 template<class I> ModEvent 200 SingletonView::excludeI(Space& home, I& iter) { 201 return me_inttoset(x.minus_r(home,iter)); 202 } 203 204 template<class I> ModEvent 205 SingletonView::includeI(Space& home, I& iter) { 206 if (!iter()) 207 return ME_SET_NONE; 208 209 if (iter.min()!=iter.max()) 210 return ME_SET_FAILED; 211 212 int val = iter.min(); 213 ++iter; 214 if ( iter() ) 215 return ME_SET_FAILED; 216 217 return me_inttoset(x.eq(home, val)); 218 } 219 220 template<class I> ModEvent 221 SingletonView::intersectI(Space& home, I& iter) { 222 return me_inttoset(x.inter_r(home,iter)); 223 } 224 225 forceinline void 226 SingletonView::subscribe(Space& home, Propagator& p, PropCond pc, 227 bool schedule) { 228 x.subscribe(home,p,pc_settoint(pc),schedule); 229 } 230 forceinline void 231 SingletonView::cancel(Space& home, Propagator& p, PropCond pc) { 232 x.cancel(home,p,pc_settoint(pc)); 233 } 234 forceinline void 235 SingletonView::reschedule(Space& home, Propagator& p, PropCond pc) { 236 x.reschedule(home,p,pc_settoint(pc)); 237 } 238 239 forceinline void 240 SingletonView::subscribe(Space& home, Advisor& a) { 241 x.subscribe(home,a); 242 } 243 forceinline void 244 SingletonView::cancel(Space& home, Advisor& a) { 245 x.cancel(home,a); 246 } 247 248 249 forceinline void 250 SingletonView::schedule(Space& home, Propagator& p, ModEvent me) { 251 return Gecode::Int::IntView::schedule(home,p,me_settoint(me)); 252 } 253 forceinline ModEvent 254 SingletonView::me(const ModEventDelta& med) { 255 return me_inttoset(Int::IntView::me(med)); 256 } 257 forceinline ModEventDelta 258 SingletonView::med(ModEvent me) { 259 return SetView::med(me_settoint(me)); 260 } 261 262 263 /* 264 * Delta information for advisors 265 * 266 * For SingletonViews, a glb change means that the view is assigned. 267 * Thus, the delta for the glb is always equal to the delta for the lub. 268 * 269 */ 270 271 forceinline ModEvent 272 SingletonView::modevent(const Delta& d) { 273 return me_inttoset(Int::IntView::modevent(d)); 274 } 275 276 forceinline int 277 SingletonView::glbMin(const Delta& d) const { return x.min(d); } 278 279 forceinline int 280 SingletonView::glbMax(const Delta& d) const { return x.max(d); } 281 282 forceinline bool 283 SingletonView::glbAny(const Delta& d) const { return x.any(d); } 284 285 forceinline int 286 SingletonView::lubMin(const Delta& d) const { return x.min(d); } 287 288 forceinline int 289 SingletonView::lubMax(const Delta& d) const { return x.max(d); } 290 291 forceinline bool 292 SingletonView::lubAny(const Delta& d) const { return x.any(d); } 293 294 forceinline bool 295 operator ==(const SingletonView& x, const SingletonView& y) { 296 return x.base() == y.base(); 297 } 298 299 forceinline bool 300 operator !=(const SingletonView& x, const SingletonView& y) { 301 return x.base() != y.base(); 302 } 303 304 305 /* 306 * Iterators 307 * 308 */ 309 310 /** 311 * \brief %Range iterator for least upper bound of singleton set view 312 * \ingroup TaskActorSetView 313 */ 314 template<> 315 class LubRanges<SingletonView> : public Gecode::Int::IntVarImpFwd { 316 public: 317 /// \name Constructors and initialization 318 //@{ 319 /// Default constructor 320 LubRanges(void); 321 /// Initialize with ranges for view \a x 322 LubRanges(const SingletonView& x); 323 /// Initialize with ranges for view \a x 324 void init(const SingletonView& x); 325 //@} 326 }; 327 328 forceinline 329 LubRanges<SingletonView>::LubRanges(void) {} 330 331 forceinline 332 LubRanges<SingletonView>::LubRanges(const SingletonView& s) : 333 Gecode::Int::IntVarImpFwd(s.base().varimp()) {} 334 335 forceinline void 336 LubRanges<SingletonView>::init(const SingletonView& s) { 337 Gecode::Int::IntVarImpFwd::init(s.base().varimp()); 338 } 339 340 /** 341 * \brief %Range iterator for greatest lower bound of singleton set view 342 * \ingroup TaskActorSetView 343 */ 344 template<> 345 class GlbRanges<SingletonView> { 346 private: 347 int val; 348 bool flag; 349 public: 350 /// \name Constructors and initialization 351 //@{ 352 /// Default constructor 353 GlbRanges(void); 354 /// Initialize with ranges for view \a x 355 GlbRanges(const SingletonView& x); 356 /// Initialize with ranges for view \a x 357 void init(const SingletonView& x); 358 359 /// \name Iteration control 360 //@{ 361 /// Test whether iterator is still at a range or done 362 bool operator ()(void) const; 363 /// Move iterator to next range (if possible) 364 void operator ++(void); 365 //@} 366 367 /// \name Range access 368 //@{ 369 /// Return smallest value of range 370 int min(void) const; 371 /// Return largest value of range 372 int max(void) const; 373 /// Return width of ranges (distance between minimum and maximum) 374 unsigned int width(void) const; 375 //@} 376 }; 377 378 forceinline 379 GlbRanges<SingletonView>::GlbRanges(void) {} 380 381 forceinline void 382 GlbRanges<SingletonView>::init(const SingletonView& s) { 383 if (s.base().assigned()) { 384 val = s.base().val(); 385 flag = true; 386 } else { 387 val = 0; 388 flag = false; 389 } 390 } 391 392 forceinline 393 GlbRanges<SingletonView>::GlbRanges(const SingletonView& s) { 394 init(s); 395 } 396 397 forceinline bool 398 GlbRanges<SingletonView>::operator ()(void) const { return flag; } 399 400 forceinline void 401 GlbRanges<SingletonView>::operator ++(void) { flag=false; } 402 403 forceinline int 404 GlbRanges<SingletonView>::min(void) const { return val; } 405 forceinline int 406 GlbRanges<SingletonView>::max(void) const { return val; } 407 forceinline unsigned int 408 GlbRanges<SingletonView>::width(void) const { return 1; } 409 410}} 411 412// STATISTICS: set-var 413