view.hpp
Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ 00002 /* 00003 * Main authors: 00004 * Christian Schulte <schulte@gecode.org> 00005 * 00006 * Copyright: 00007 * Christian Schulte, 2005 00008 * 00009 * Last modified: 00010 * $Date: 2010-06-29 10:39:13 +0200 (Tue, 29 Jun 2010) $ by $Author: schulte $ 00011 * $Revision: 11118 $ 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 namespace Gecode { 00039 00044 template<class View> 00045 class ConstView { 00046 public: 00048 typedef typename View::VarImpType VarImpType; 00050 00051 00052 unsigned int degree(void) const; 00054 double afc(void) const; 00056 static bool varderived(void); 00058 VarImpType* varimp(void) const; 00060 00062 00063 00064 bool assigned(void) const; 00066 00068 00069 00070 static void schedule(Space& home, Propagator& p, ModEvent me); 00072 static ModEvent me(const ModEventDelta& med); 00074 static ModEventDelta med(ModEvent me); 00076 00078 00079 00086 void subscribe(Space& home, Propagator& p, PropCond pc, bool schedule=true); 00088 void cancel(Space& home, Propagator& p, PropCond pc); 00090 void subscribe(Space& home, Advisor& a); 00092 void cancel(Space& home, Advisor& a); 00094 00096 00097 00098 static ModEvent modevent(const Delta& d); 00100 00102 00103 00104 void update(Space& home, bool share, ConstView& y); 00106 }; 00107 00108 00109 00114 template<class VarImp> 00115 class VarImpView { 00116 protected: 00118 VarImp* x; 00120 VarImpView(void); 00122 VarImpView(VarImp* y); 00123 public: 00125 typedef VarImp VarImpType; 00127 00128 00129 static bool varderived(void); 00131 VarImp* varimp(void) const; 00133 unsigned int degree(void) const; 00135 double afc(void) const; 00137 00139 00140 00141 bool assigned(void) const; 00143 00145 00146 00147 static void schedule(Space& home, Propagator& p, ModEvent me); 00149 static ModEvent me(const ModEventDelta& med); 00151 static ModEventDelta med(ModEvent me); 00153 00155 00156 00163 void subscribe(Space& home, Propagator& p, PropCond pc, bool schedule=true); 00165 void cancel(Space& home, Propagator& p, PropCond pc); 00167 void subscribe(Space& home, Advisor& a); 00169 void cancel(Space& home, Advisor& a); 00171 00173 00174 00175 static ModEvent modevent(const Delta& d); 00177 00179 00180 00181 void update(Space& home, bool share, VarImpView<VarImp>& y); 00183 }; 00184 00189 00190 template<class VarImpA, class VarImpB> 00191 bool same(const VarImpView<VarImpA>& x, const VarImpView<VarImpB>& y); 00193 template<class ViewA, class ViewB> 00194 bool before(const ViewA& x, const ViewB& y); 00196 00197 00202 template<class View> 00203 class DerivedView { 00204 protected: 00206 View x; 00208 DerivedView(void); 00210 DerivedView(const View& y); 00211 public: 00213 typedef typename View::VarImpType VarImpType; 00215 00216 00217 static bool varderived(void); 00219 VarImpType* varimp(void) const; 00221 View base(void) const; 00223 unsigned int degree(void) const; 00225 double afc(void) const; 00227 00229 00230 00231 bool assigned(void) const; 00233 00235 00236 00237 static void schedule(Space& home, Propagator& p, ModEvent me); 00239 static ModEvent me(const ModEventDelta& med); 00241 static ModEventDelta med(ModEvent); 00243 00245 00246 00253 void subscribe(Space& home, Propagator& p, PropCond pc, bool schedule=true); 00255 void cancel(Space& home, Propagator& p, PropCond pc); 00257 void subscribe(Space& home, Advisor& a); 00259 void cancel(Space& home, Advisor& a); 00261 00263 00264 00265 static ModEvent modevent(const Delta& d); 00267 00269 00270 00271 void update(Space& home, bool share, DerivedView<View>& y); 00273 }; 00274 00275 00280 template<class ViewA, class ViewB> 00281 bool shared(const ConstView<ViewA>&, const ConstView<ViewB>&); 00286 template<class VarImp, class View> 00287 bool shared(const VarImpView<VarImp>&, const ConstView<View>&); 00292 template<class ViewA, class ViewB> 00293 bool shared(const DerivedView<ViewA>&, const ConstView<ViewB>&); 00298 template<class View, class VarImp> 00299 bool shared(const ConstView<View>&, const VarImpView<VarImp>&); 00304 template<class ViewA, class ViewB> 00305 bool shared(const ConstView<ViewA>&, const DerivedView<ViewB>&); 00310 template<class VarImpA, class VarImpB> 00311 bool shared(const VarImpView<VarImpA>&, const VarImpView<VarImpB>&); 00316 template<class VarImpA, class ViewB> 00317 bool shared(const VarImpView<VarImpA>&, const DerivedView<ViewB>&); 00322 template<class ViewA, class VarImpB> 00323 bool shared(const DerivedView<ViewA>&, const VarImpView<VarImpB>&); 00328 template<class ViewA, class ViewB> 00329 bool shared(const DerivedView<ViewA>&, const DerivedView<ViewB>&); 00330 00331 00332 /* 00333 * Constant view: has no variable implementation 00334 * 00335 */ 00336 template<class View> 00337 forceinline unsigned int 00338 ConstView<View>::degree(void) const { 00339 return 0; 00340 } 00341 template<class View> 00342 forceinline double 00343 ConstView<View>::afc(void) const { 00344 return 0.0; 00345 } 00346 template<class View> 00347 forceinline bool 00348 ConstView<View>::varderived(void) { 00349 return false; 00350 } 00351 template<class View> 00352 forceinline typename View::VarImpType* 00353 ConstView<View>::varimp(void) const { 00354 return NULL; 00355 } 00356 template<class View> 00357 forceinline bool 00358 ConstView<View>::assigned(void) const { 00359 return true; 00360 } 00361 template<class View> 00362 forceinline void 00363 ConstView<View>::subscribe(Space& home, Propagator& p, PropCond, 00364 bool schedule) { 00365 if (schedule) 00366 View::schedule(home,p,ME_GEN_ASSIGNED); 00367 } 00368 template<class View> 00369 forceinline void 00370 ConstView<View>::cancel(Space&, Propagator&, PropCond) { 00371 } 00372 template<class View> 00373 forceinline void 00374 ConstView<View>::subscribe(Space&, Advisor&) { 00375 } 00376 template<class View> 00377 forceinline void 00378 ConstView<View>::cancel(Space&, Advisor&) { 00379 } 00380 template<class View> 00381 forceinline void 00382 ConstView<View>::schedule(Space& home, Propagator& p, ModEvent me) { 00383 View::schedule(home,p,me); 00384 } 00385 template<class View> 00386 forceinline ModEvent 00387 ConstView<View>::me(const ModEventDelta& med) { 00388 return View::me(med); 00389 } 00390 template<class View> 00391 forceinline ModEventDelta 00392 ConstView<View>::med(ModEvent me) { 00393 return View::med(me); 00394 } 00395 template<class View> 00396 forceinline ModEvent 00397 ConstView<View>::modevent(const Delta& d) { 00398 return ME_GEN_NONE; 00399 } 00400 template<class View> 00401 forceinline void 00402 ConstView<View>::update(Space&, bool, ConstView&) { 00403 } 00404 00405 /* 00406 * Variable view: contains a pointer to a variable implementation 00407 * 00408 */ 00409 template<class VarImp> 00410 forceinline 00411 VarImpView<VarImp>::VarImpView(void) 00412 : x(NULL) {} 00413 template<class VarImp> 00414 forceinline 00415 VarImpView<VarImp>::VarImpView(VarImp* y) 00416 : x(y) {} 00417 template<class VarImp> 00418 forceinline bool 00419 VarImpView<VarImp>::varderived(void) { 00420 return true; 00421 } 00422 template<class VarImp> 00423 forceinline VarImp* 00424 VarImpView<VarImp>::varimp(void) const { 00425 return x; 00426 } 00427 template<class VarImp> 00428 forceinline unsigned int 00429 VarImpView<VarImp>::degree(void) const { 00430 return x->degree(); 00431 } 00432 template<class VarImp> 00433 forceinline double 00434 VarImpView<VarImp>::afc(void) const { 00435 return x->afc(); 00436 } 00437 template<class VarImp> 00438 forceinline bool 00439 VarImpView<VarImp>::assigned(void) const { 00440 return x->assigned(); 00441 } 00442 template<class VarImp> 00443 forceinline void 00444 VarImpView<VarImp>::subscribe(Space& home, Propagator& p, PropCond pc, 00445 bool schedule) { 00446 x->subscribe(home,p,pc,schedule); 00447 } 00448 template<class VarImp> 00449 forceinline void 00450 VarImpView<VarImp>::cancel(Space& home, Propagator& p, PropCond pc) { 00451 x->cancel(home,p,pc); 00452 } 00453 template<class VarImp> 00454 forceinline void 00455 VarImpView<VarImp>::subscribe(Space& home, Advisor& a) { 00456 x->subscribe(home,a); 00457 } 00458 template<class VarImp> 00459 forceinline void 00460 VarImpView<VarImp>::cancel(Space& home, Advisor& a) { 00461 x->cancel(home,a); 00462 } 00463 template<class VarImp> 00464 forceinline void 00465 VarImpView<VarImp>::schedule(Space& home, Propagator& p, ModEvent me) { 00466 VarImp::schedule(home,p,me); 00467 } 00468 template<class VarImp> 00469 forceinline ModEvent 00470 VarImpView<VarImp>::me(const ModEventDelta& med) { 00471 return VarImp::me(med); 00472 } 00473 template<class VarImp> 00474 forceinline ModEventDelta 00475 VarImpView<VarImp>::med(ModEvent me) { 00476 return VarImp::med(me); 00477 } 00478 template<class VarImp> 00479 forceinline ModEvent 00480 VarImpView<VarImp>::modevent(const Delta& d) { 00481 return VarImp::modevent(d); 00482 } 00483 template<class VarImp> 00484 forceinline void 00485 VarImpView<VarImp>::update(Space& home, bool share, VarImpView<VarImp>& y) { 00486 x = y.x->copy(home,share); 00487 } 00488 00489 /* 00490 * Derived view: contain the base view from which they are derived 00491 * 00492 */ 00493 00494 template<class View> 00495 forceinline 00496 DerivedView<View>::DerivedView(void) {} 00497 00498 template<class View> 00499 forceinline 00500 DerivedView<View>::DerivedView(const View& y) 00501 : x(y) {} 00502 00503 template<class View> 00504 forceinline bool 00505 DerivedView<View>::varderived(void) { 00506 return View::varderived(); 00507 } 00508 00509 template<class View> 00510 forceinline typename View::VarImpType* 00511 DerivedView<View>::varimp(void) const { 00512 return x.varimp(); 00513 } 00514 00515 template<class View> 00516 forceinline View 00517 DerivedView<View>::base(void) const { 00518 return x; 00519 } 00520 00521 template<class View> 00522 forceinline unsigned int 00523 DerivedView<View>::degree(void) const { 00524 return x.degree(); 00525 } 00526 template<class View> 00527 forceinline double 00528 DerivedView<View>::afc(void) const { 00529 return x.afc(); 00530 } 00531 template<class View> 00532 forceinline bool 00533 DerivedView<View>::assigned(void) const { 00534 return x.assigned(); 00535 } 00536 00537 template<class View> 00538 forceinline void 00539 DerivedView<View>::schedule(Space& home, Propagator& p, ModEvent me) { 00540 return View::schedule(home,p,me); 00541 } 00542 template<class View> 00543 forceinline ModEvent 00544 DerivedView<View>::me(const ModEventDelta& med) { 00545 return View::me(med); 00546 } 00547 template<class View> 00548 forceinline ModEventDelta 00549 DerivedView<View>::med(ModEvent me) { 00550 return View::med(me); 00551 } 00552 00553 template<class View> 00554 forceinline void 00555 DerivedView<View>::subscribe(Space& home, Propagator& p, PropCond pc, 00556 bool schedule) { 00557 x.subscribe(home,p,pc,schedule); 00558 } 00559 template<class View> 00560 forceinline void 00561 DerivedView<View>::cancel(Space& home, Propagator& p, PropCond pc) { 00562 x.cancel(home,p,pc); 00563 } 00564 template<class View> 00565 forceinline void 00566 DerivedView<View>::subscribe(Space& home, Advisor& a) { 00567 x.subscribe(home,a); 00568 } 00569 template<class View> 00570 forceinline void 00571 DerivedView<View>::cancel(Space& home, Advisor& a) { 00572 x.cancel(home,a); 00573 } 00574 template<class View> 00575 forceinline ModEvent 00576 DerivedView<View>::modevent(const Delta& d) { 00577 return View::modevent(d); 00578 } 00579 template<class View> 00580 forceinline void 00581 DerivedView<View>::update(Space& home, bool share, DerivedView<View>& y) { 00582 x.update(home,share,y.x); 00583 } 00584 00585 00586 /* 00587 * Tests whether two views are the same 00588 * 00589 */ 00590 00592 template<class ViewA, class ViewB> 00593 forceinline bool 00594 same(const ConstView<ViewA>&, const ConstView<ViewB>&) { 00595 return false; 00596 } 00598 template<class VarImp, class View> 00599 forceinline bool 00600 same(const VarImpView<VarImp>&, const ConstView<View>&) { 00601 return false; 00602 } 00604 template<class ViewA, class ViewB> 00605 forceinline bool 00606 same(const ConstView<ViewA>&, const DerivedView<ViewB>&) { 00607 return false; 00608 } 00610 template<class VarImpA, class ViewB> 00611 forceinline bool 00612 same(const VarImpView<VarImpA>&, const DerivedView<ViewB>&) { 00613 return false; 00614 } 00616 template<class ViewA, class VarImpB> 00617 forceinline bool 00618 same(const DerivedView<ViewA>&, const VarImpView<VarImpB>&) { 00619 return false; 00620 } 00622 template<class VarImp> 00623 forceinline bool 00624 same(const VarImpView<VarImp>& x, const VarImpView<VarImp>& y) { 00625 return x.varimp() == y.varimp(); 00626 } 00628 template<class ViewA, class ViewB> 00629 forceinline bool 00630 same(const DerivedView<ViewA>& x, const DerivedView<ViewB>& y) { 00631 return same(x.base(),y.base()); 00632 } 00633 00634 00635 /* 00636 * Tests whether one view is before the other 00637 * 00638 */ 00639 template<class ViewA, class ViewB> 00640 forceinline bool 00641 before(const ViewA& x, const ViewB& y) { 00642 return x.varimp() < y.varimp(); 00643 } 00644 00645 00646 /* 00647 * Testing whether two views share the same variable 00648 * 00649 */ 00650 00651 template<class ViewA, class ViewB> 00652 forceinline bool 00653 shared(const ConstView<ViewA>&, const ConstView<ViewB>&) { 00654 return false; 00655 } 00656 template<class VarImp, class View> 00657 forceinline bool 00658 shared(const VarImpView<VarImp>&, const ConstView<View>&) { 00659 return false; 00660 } 00661 template<class ViewA, class ViewB> 00662 forceinline bool 00663 shared(const DerivedView<ViewA>&, const ConstView<ViewB>&) { 00664 return false; 00665 } 00666 template<class View, class VarImp> 00667 forceinline bool 00668 shared(const ConstView<View>&, const VarImpView<VarImp>&) { 00669 return false; 00670 } 00671 template<class ViewA, class ViewB> 00672 forceinline bool 00673 shared(const ConstView<ViewA>&, const DerivedView<ViewB>&) { 00674 return false; 00675 } 00676 template<class VarImpA, class VarImpB> 00677 forceinline bool 00678 shared(const VarImpView<VarImpA>& x, const VarImpView<VarImpB>& y) { 00679 return (static_cast<VarImpBase*>(x.varimp()) == 00680 static_cast<VarImpBase*>(y.varimp())); 00681 } 00682 template<class VarImpA, class ViewB> 00683 forceinline bool 00684 shared(const VarImpView<VarImpA>& x, const DerivedView<ViewB>& y) { 00685 return (ViewB::varderived() && 00686 static_cast<VarImpBase*>(x.varimp()) == 00687 static_cast<VarImpBase*>(y.varimp())); 00688 } 00689 template<class ViewA, class VarImpB> 00690 forceinline bool 00691 shared(const DerivedView<ViewA>& x, const VarImpView<VarImpB>& y) { 00692 return (ViewA::varderived() && 00693 static_cast<VarImpBase*>(x.varimp()) == 00694 static_cast<VarImpBase*>(y.varimp())); 00695 } 00696 template<class ViewA, class ViewB> 00697 forceinline bool 00698 shared(const DerivedView<ViewA>& x, const DerivedView<ViewB>& y) { 00699 return (ViewA::varderived() && ViewB::varderived() && 00700 static_cast<VarImpBase*>(x.varimp()) == 00701 static_cast<VarImpBase*>(y.varimp())); 00702 } 00703 00704 } 00705 00706 // STATISTICS: kernel-var