Generated on Sun Feb 17 15:24:11 2019 for Gecode by doxygen 1.6.3

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  *  Contributing authors:
00007  *     Samuel Gagnon <samuel.gagnon92@gmail.com>
00008  *
00009  *  Copyright:
00010  *     Christian Schulte, 2005
00011  *     Samuel Gagnon, 2018
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     typedef typename View::VarType VarType;
00052 
00053 
00054     unsigned int degree(void) const;
00056     double afc(void) const;
00058     static bool varderived(void);
00060     VarImpType* varimp(void) const;
00061 #ifdef GECODE_HAS_CBS
00062 
00063     unsigned int id(void) const;
00064 #endif
00065 
00066 
00068 
00069 
00070     bool assigned(void) const;
00072 
00074 
00075 
00076     static void schedule(Space& home, Propagator& p, ModEvent me);
00078     static ModEvent me(const ModEventDelta& med);
00080     static ModEventDelta med(ModEvent me);
00082 
00084 
00085 
00092     void subscribe(Space& home, Propagator& p, PropCond pc, bool schedule=true);
00094     void cancel(Space& home, Propagator& p, PropCond pc);
00096     void reschedule(Space& home, Propagator& p, PropCond pc);
00102     void subscribe(Space& home, Advisor& a, bool fail=false);
00104     void cancel(Space& home, Advisor& a, bool fail=false);
00106 
00108 
00109 
00110     static ModEvent modevent(const Delta& d);
00112 
00114 
00115 
00116     void update(Space& home, ConstView& y);
00118 
00120 
00121 
00122     bool operator <(const ConstView& y) const;
00124   };
00125 
00126 
00127 
00132   template<class Var>
00133   class VarImpView {
00134   public:
00136     typedef Var VarType;
00138     typedef typename Var::VarImpType VarImpType;
00139   protected:
00141     VarImpType* x;
00143     VarImpView(void);
00145     VarImpView(VarImpType* y);
00147     void varimp(VarImpType* y);
00148   public:
00150 
00151 
00152     static bool varderived(void);
00154     VarImpType* varimp(void) const;
00156     unsigned int degree(void) const;
00158     double afc(void) const;
00159 #ifdef GECODE_HAS_CBS
00160 
00161     unsigned int id(void) const;
00162 #endif
00163 
00164 
00166 
00167 
00168     bool assigned(void) const;
00170 
00172 
00173 
00174     static void schedule(Space& home, Propagator& p, ModEvent me);
00176     static ModEvent me(const ModEventDelta& med);
00178     static ModEventDelta med(ModEvent me);
00180 
00182 
00183 
00190     void subscribe(Space& home, Propagator& p, PropCond pc, bool schedule=true);
00192     void cancel(Space& home, Propagator& p, PropCond pc);
00194     void reschedule(Space& home, Propagator& p, PropCond pc);
00200     void subscribe(Space& home, Advisor& a, bool fail=false);
00202     void cancel(Space& home, Advisor& a, bool fail=false);
00204 
00206 
00207 
00208     static ModEvent modevent(const Delta& d);
00210 
00212 
00213 
00214     void update(Space& home, VarImpView<Var>& y);
00216 
00218 
00219 
00220     bool operator <(const VarImpView& y) const;
00222   };
00223 
00224 
00229   template<class View>
00230   class DerivedView {
00231   public:
00233     typedef typename View::VarImpType VarImpType;
00235     typedef typename View::VarType VarType;
00236   protected:
00238     View x;
00240     DerivedView(void);
00241   public:
00243     DerivedView(const View& y);
00245 
00246 
00247     static bool varderived(void);
00249     VarImpType* varimp(void) const;
00251     View base(void) const;
00253     unsigned int degree(void) const;
00255     double afc(void) const;
00256 #ifdef GECODE_HAS_CBS
00257 
00258     unsigned int id(void) const;
00259 #endif
00260 
00261 
00263 
00264 
00265     bool assigned(void) const;
00267 
00269 
00270 
00271     static void schedule(Space& home, Propagator& p, ModEvent me);
00273     static ModEvent me(const ModEventDelta& med);
00275     static ModEventDelta med(ModEvent);
00277 
00279 
00280 
00287     void subscribe(Space& home, Propagator& p, PropCond pc, bool schedule=true);
00289     void cancel(Space& home, Propagator& p, PropCond pc);
00291     void reschedule(Space& home, Propagator& p, PropCond pc);
00297     void subscribe(Space& home, Advisor& a, bool fail=false);
00299     void cancel(Space& home, Advisor& a, bool fail=false);
00301 
00303 
00304 
00305     static ModEvent modevent(const Delta& d);
00307 
00309 
00310 
00311     void update(Space& home, DerivedView<View>& y);
00313 
00315 
00316 
00317     bool operator <(const DerivedView<View>& y) const;
00319   };
00320 
00321 
00326   template<class ViewA, class ViewB>
00327   bool shared(const ConstView<ViewA>&, const ConstView<ViewB>&);
00332   template<class Var, class View>
00333   bool shared(const VarImpView<Var>&, const ConstView<View>&);
00338   template<class ViewA, class ViewB>
00339   bool shared(const DerivedView<ViewA>&, const ConstView<ViewB>&);
00344   template<class View, class Var>
00345   bool shared(const ConstView<View>&, const VarImpView<Var>&);
00350   template<class ViewA, class ViewB>
00351   bool shared(const ConstView<ViewA>&, const DerivedView<ViewB>&);
00356   template<class VarA, class VarB>
00357   bool shared(const VarImpView<VarA>&, const VarImpView<VarB>&);
00362   template<class Var, class View>
00363   bool shared(const VarImpView<Var>&, const DerivedView<View>&);
00368   template<class View, class Var>
00369   bool shared(const DerivedView<View>&, const VarImpView<Var>&);
00374   template<class ViewA, class ViewB>
00375   bool shared(const DerivedView<ViewA>&, const DerivedView<ViewB>&);
00376 
00377 
00378   /*
00379    * Constant view: has no variable implementation
00380    *
00381    */
00382   template<class View>
00383   forceinline unsigned int
00384   ConstView<View>::degree(void) const {
00385     return 0;
00386   }
00387   template<class View>
00388   forceinline double
00389   ConstView<View>::afc(void) const {
00390     return 0.0;
00391   }
00392   template<class View>
00393   forceinline bool
00394   ConstView<View>::varderived(void) {
00395     return false;
00396   }
00397   template<class View>
00398   forceinline typename View::VarImpType*
00399   ConstView<View>::varimp(void) const {
00400     return NULL;
00401   }
00402 #ifdef GECODE_HAS_CBS
00403   template<class View>
00404   forceinline unsigned int
00405   ConstView<View>::id(void) const {
00406     return 0;
00407   }
00408 #endif
00409   template<class View>
00410   forceinline bool
00411   ConstView<View>::assigned(void) const {
00412     return true;
00413   }
00414   template<class View>
00415   forceinline void
00416   ConstView<View>::subscribe(Space& home, Propagator& p, PropCond,
00417                              bool schedule) {
00418     if (schedule)
00419       View::schedule(home,p,ME_GEN_ASSIGNED);
00420   }
00421   template<class View>
00422   forceinline void
00423   ConstView<View>::cancel(Space&, Propagator&, PropCond) {
00424   }
00425   template<class View>
00426   forceinline void
00427   ConstView<View>::reschedule(Space& home, Propagator& p, PropCond) {
00428     View::schedule(home,p,ME_GEN_ASSIGNED);
00429   }
00430   template<class View>
00431   forceinline void
00432   ConstView<View>::subscribe(Space&, Advisor&, bool) {
00433   }
00434   template<class View>
00435   forceinline void
00436   ConstView<View>::cancel(Space&, Advisor&, bool) {
00437   }
00438   template<class View>
00439   forceinline void
00440   ConstView<View>::schedule(Space& home, Propagator& p, ModEvent me) {
00441     View::schedule(home,p,me);
00442   }
00443   template<class View>
00444   forceinline ModEvent
00445   ConstView<View>::me(const ModEventDelta& med) {
00446     return View::me(med);
00447   }
00448   template<class View>
00449   forceinline ModEventDelta
00450   ConstView<View>::med(ModEvent me) {
00451     return View::med(me);
00452   }
00453   template<class View>
00454   forceinline ModEvent
00455   ConstView<View>::modevent(const Delta& d) {
00456     (void) d;
00457     return ME_GEN_NONE;
00458   }
00459   template<class View>
00460   forceinline void
00461   ConstView<View>::update(Space&, ConstView<View>&) {
00462   }
00463   template<class View>
00464   forceinline bool
00465   ConstView<View>::operator <(const ConstView<View>&) const {
00466     return true;
00467   }
00468 
00469 
00470   /*
00471    * Variable view: contains a pointer to a variable implementation
00472    *
00473    */
00474   template<class Var>
00475   forceinline
00476   VarImpView<Var>::VarImpView(void)
00477     : x(NULL) {}
00478   template<class Var>
00479   forceinline
00480   VarImpView<Var>::VarImpView(VarImpType* y)
00481     : x(y) {}
00482   template<class Var>
00483   forceinline void
00484   VarImpView<Var>::varimp(VarImpType* y) {
00485     x=y;
00486   }
00487   template<class Var>
00488   forceinline bool
00489   VarImpView<Var>::varderived(void) {
00490     return true;
00491   }
00492   template<class Var>
00493   forceinline typename Var::VarImpType*
00494   VarImpView<Var>::varimp(void) const {
00495     return x;
00496   }
00497  template<class Var>
00498   forceinline unsigned int
00499   VarImpView<Var>::degree(void) const {
00500     return x->degree();
00501   }
00502   template<class Var>
00503   forceinline double
00504   VarImpView<Var>::afc(void) const {
00505     return x->afc();
00506   }
00507 #ifdef GECODE_HAS_CBS
00508   template<class Var>
00509   forceinline unsigned int
00510   VarImpView<Var>::id(void) const {
00511     return x->id();
00512   }
00513 #endif
00514   template<class Var>
00515   forceinline bool
00516   VarImpView<Var>::assigned(void) const {
00517     return x->assigned();
00518   }
00519   template<class Var>
00520   forceinline void
00521   VarImpView<Var>::subscribe(Space& home, Propagator& p, PropCond pc,
00522                              bool schedule) {
00523     x->subscribe(home,p,pc,schedule);
00524   }
00525   template<class Var>
00526   forceinline void
00527   VarImpView<Var>::cancel(Space& home, Propagator& p, PropCond pc) {
00528     x->cancel(home,p,pc);
00529   }
00530   template<class Var>
00531   forceinline void
00532   VarImpView<Var>::reschedule(Space& home, Propagator& p, PropCond pc) {
00533     x->reschedule(home,p,pc);
00534   }
00535   template<class Var>
00536   forceinline void
00537   VarImpView<Var>::subscribe(Space& home, Advisor& a, bool fail) {
00538     x->subscribe(home,a,fail);
00539   }
00540   template<class Var>
00541   forceinline void
00542   VarImpView<Var>::cancel(Space& home, Advisor& a, bool fail) {
00543     x->cancel(home,a,fail);
00544   }
00545   template<class Var>
00546   forceinline void
00547   VarImpView<Var>::schedule(Space& home, Propagator& p, ModEvent me) {
00548     VarImpType::schedule(home,p,me);
00549   }
00550   template<class Var>
00551   forceinline ModEvent
00552   VarImpView<Var>::me(const ModEventDelta& med) {
00553     return VarImpType::me(med);
00554   }
00555   template<class Var>
00556   forceinline ModEventDelta
00557   VarImpView<Var>::med(ModEvent me) {
00558     return VarImpType::med(me);
00559   }
00560   template<class Var>
00561   forceinline ModEvent
00562   VarImpView<Var>::modevent(const Delta& d) {
00563     return VarImpType::modevent(d);
00564   }
00565   template<class Var>
00566   forceinline void
00567   VarImpView<Var>::update(Space& home, VarImpView<Var>& y) {
00568     x = y.x->copy(home);
00569   }
00570   template<class Var>
00571   forceinline bool
00572   VarImpView<Var>::operator <(const VarImpView<Var>& y) const {
00573     return this->varimp() < y.varimp();
00574   }
00575 
00576 
00577   /*
00578    * Derived view: contain the base view from which they are derived
00579    *
00580    */
00581 
00582   template<class View>
00583   forceinline
00584   DerivedView<View>::DerivedView(void) {}
00585 
00586   template<class View>
00587   forceinline
00588   DerivedView<View>::DerivedView(const View& y)
00589     : x(y) {}
00590 
00591   template<class View>
00592   forceinline bool
00593   DerivedView<View>::varderived(void) {
00594     return View::varderived();
00595   }
00596 
00597   template<class View>
00598   forceinline typename View::VarImpType*
00599   DerivedView<View>::varimp(void) const {
00600     return x.varimp();
00601   }
00602 
00603   template<class View>
00604   forceinline View
00605   DerivedView<View>::base(void) const {
00606     return x;
00607   }
00608 
00609   template<class View>
00610   forceinline unsigned int
00611   DerivedView<View>::degree(void) const {
00612     return x.degree();
00613   }
00614   template<class View>
00615   forceinline double
00616   DerivedView<View>::afc(void) const {
00617     return x.afc();
00618   }
00619 #ifdef GECODE_HAS_CBS
00620   template<class View>
00621   forceinline unsigned int
00622   DerivedView<View>::id(void) const {
00623     return x.id();
00624   }
00625 #endif
00626   template<class View>
00627   forceinline bool
00628   DerivedView<View>::assigned(void) const {
00629     return x.assigned();
00630   }
00631 
00632   template<class View>
00633   forceinline void
00634   DerivedView<View>::schedule(Space& home, Propagator& p, ModEvent me) {
00635     return View::schedule(home,p,me);
00636   }
00637   template<class View>
00638   forceinline ModEvent
00639   DerivedView<View>::me(const ModEventDelta& med) {
00640     return View::me(med);
00641   }
00642   template<class View>
00643   forceinline ModEventDelta
00644   DerivedView<View>::med(ModEvent me) {
00645     return View::med(me);
00646   }
00647 
00648   template<class View>
00649   forceinline void
00650   DerivedView<View>::subscribe(Space& home, Propagator& p, PropCond pc,
00651                                bool schedule) {
00652     x.subscribe(home,p,pc,schedule);
00653   }
00654   template<class View>
00655   forceinline void
00656   DerivedView<View>::cancel(Space& home, Propagator& p, PropCond pc) {
00657     x.cancel(home,p,pc);
00658   }
00659   template<class View>
00660   forceinline void
00661   DerivedView<View>::reschedule(Space& home, Propagator& p, PropCond pc) {
00662     x.reschedule(home,p,pc);
00663   }
00664   template<class View>
00665   forceinline void
00666   DerivedView<View>::subscribe(Space& home, Advisor& a, bool fail) {
00667     x.subscribe(home,a,fail);
00668   }
00669   template<class View>
00670   forceinline void
00671   DerivedView<View>::cancel(Space& home, Advisor& a, bool fail) {
00672     x.cancel(home,a,fail);
00673   }
00674   template<class View>
00675   forceinline ModEvent
00676   DerivedView<View>::modevent(const Delta& d) {
00677     return View::modevent(d);
00678   }
00679   template<class View>
00680   forceinline void
00681   DerivedView<View>::update(Space& home, DerivedView<View>& y) {
00682     x.update(home,y.x);
00683   }
00684   template<class View>
00685   forceinline bool
00686   DerivedView<View>::operator <(const DerivedView<View>& y) const {
00687     return base() < y.base();
00688   }
00689 
00690 
00691   /*
00692    * Tests whether two views are the same
00693    *
00694    */
00695 
00697   template<class ViewA, class ViewB>
00698   forceinline bool
00699   operator ==(const ConstView<ViewA>&, const ConstView<ViewB>&) {
00700     return false;
00701   }
00703   template<class Var, class View>
00704   forceinline bool
00705   operator ==(const ConstView<View>&, const VarImpView<Var>&) {
00706     return false;
00707   }
00709   template<class ViewA, class ViewB>
00710   forceinline bool
00711   operator ==(const ConstView<ViewA>&, const DerivedView<ViewB>&) {
00712     return false;
00713   }
00715   template<class Var, class View>
00716   forceinline bool
00717   operator ==(const VarImpView<Var>&, const ConstView<View>&) {
00718     return false;
00719   }
00721   template<class Var, class View>
00722   forceinline bool
00723   operator ==(const VarImpView<Var>&, const DerivedView<View>&) {
00724     return false;
00725   }
00727   template<class ViewX, class ViewY>
00728   forceinline bool
00729   operator ==(const DerivedView<ViewX>&, const ConstView<ViewY>&) {
00730     return false;
00731   }
00733   template<class View, class Var>
00734   forceinline bool
00735   operator ==(const DerivedView<View>&, const VarImpView<Var>&) {
00736     return false;
00737   }
00739   template<class ViewX, class ViewY>
00740   forceinline bool
00741   operator ==(const DerivedView<ViewX>&, const DerivedView<ViewY>&) {
00742     return false;
00743   }
00745   template<class VarX, class VarY>
00746   forceinline bool
00747   operator ==(const VarImpView<VarX>& x, const VarImpView<VarY>& y) {
00748     return x.varimp() == y.varimp();
00749   }
00750 
00752   template<class ViewA, class ViewB>
00753   forceinline bool
00754   operator !=(const ConstView<ViewA>&, const ConstView<ViewB>&) {
00755     return true;
00756   }
00758   template<class Var, class View>
00759   forceinline bool
00760   operator !=(const ConstView<View>&, const VarImpView<Var>&) {
00761     return true;
00762   }
00764   template<class ViewA, class ViewB>
00765   forceinline bool
00766   operator !=(const ConstView<ViewA>&, const DerivedView<ViewB>&) {
00767     return true;
00768   }
00770   template<class Var, class View>
00771   forceinline bool
00772   operator !=(const VarImpView<Var>&, const ConstView<View>&) {
00773     return true;
00774   }
00776   template<class Var, class View>
00777   forceinline bool
00778   operator !=(const VarImpView<Var>&, const DerivedView<View>&) {
00779     return true;
00780   }
00782   template<class ViewX, class ViewY>
00783   forceinline bool
00784   operator !=(const DerivedView<ViewX>&, const ConstView<ViewY>&) {
00785     return true;
00786   }
00788   template<class View, class Var>
00789   forceinline bool
00790   operator !=(const DerivedView<View>&, const VarImpView<Var>&) {
00791     return true;
00792   }
00794   template<class ViewX, class ViewY>
00795   forceinline bool
00796   operator !=(const DerivedView<ViewX>&, const DerivedView<ViewY>&) {
00797     return true;
00798   }
00800   template<class VarX, class VarY>
00801   forceinline bool
00802   operator !=(const VarImpView<VarX>& x, const VarImpView<VarY>& y) {
00803     return x.varimp() != y.varimp();
00804   }
00805 
00806 
00807   /*
00808    * Testing whether two views share the same variable
00809    *
00810    */
00811 
00812   template<class ViewA, class ViewB>
00813   forceinline bool
00814   shared(const ConstView<ViewA>&, const ConstView<ViewB>&) {
00815     return false;
00816   }
00817   template<class Var, class View>
00818   forceinline bool
00819   shared(const VarImpView<Var>&, const ConstView<View>&) {
00820     return false;
00821   }
00822   template<class ViewA, class ViewB>
00823   forceinline bool
00824   shared(const DerivedView<ViewA>&, const ConstView<ViewB>&) {
00825     return false;
00826   }
00827   template<class View, class Var>
00828   forceinline bool
00829   shared(const ConstView<View>&, const VarImpView<Var>&) {
00830     return false;
00831   }
00832   template<class ViewA, class ViewB>
00833   forceinline bool
00834   shared(const ConstView<ViewA>&, const DerivedView<ViewB>&) {
00835     return false;
00836   }
00837   template<class VarA, class VarB>
00838   forceinline bool
00839   shared(const VarImpView<VarA>& x, const VarImpView<VarB>& y) {
00840     return (static_cast<VarImpBase*>(x.varimp()) ==
00841             static_cast<VarImpBase*>(y.varimp()));
00842   }
00843   template<class Var, class View>
00844   forceinline bool
00845   shared(const VarImpView<Var>& x, const DerivedView<View>& y) {
00846     return (View::varderived() &&
00847             static_cast<VarImpBase*>(x.varimp()) ==
00848             static_cast<VarImpBase*>(y.varimp()));
00849   }
00850   template<class View, class Var>
00851   forceinline bool
00852   shared(const DerivedView<View>& x, const VarImpView<Var>& y) {
00853     return (View::varderived() &&
00854             static_cast<VarImpBase*>(x.varimp()) ==
00855             static_cast<VarImpBase*>(y.varimp()));
00856   }
00857   template<class ViewA, class ViewB>
00858   forceinline bool
00859   shared(const DerivedView<ViewA>& x, const DerivedView<ViewB>& y) {
00860     return (ViewA::varderived() && ViewB::varderived() &&
00861             static_cast<VarImpBase*>(x.varimp()) ==
00862             static_cast<VarImpBase*>(y.varimp()));
00863   }
00864 
00865 }
00866 
00867 // STATISTICS: kernel-var