Generated on Tue May 22 09:39:51 2018 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   };
00119 
00120 
00121 
00126   template<class Var>
00127   class VarImpView {
00128   public:
00130     typedef Var VarType;
00132     typedef typename Var::VarImpType VarImpType;
00133   protected:
00135     VarImpType* x;
00137     VarImpView(void);
00139     VarImpView(VarImpType* y);
00141     void varimp(VarImpType* y);
00142   public:
00144 
00145 
00146     static bool varderived(void);
00148     VarImpType* varimp(void) const;
00150     unsigned int degree(void) const;
00152     double afc(void) const;
00153 #ifdef GECODE_HAS_CBS
00154 
00155     unsigned int id(void) const;
00156 #endif
00157 
00158 
00160 
00161 
00162     bool assigned(void) const;
00164 
00166 
00167 
00168     static void schedule(Space& home, Propagator& p, ModEvent me);
00170     static ModEvent me(const ModEventDelta& med);
00172     static ModEventDelta med(ModEvent me);
00174 
00176 
00177 
00184     void subscribe(Space& home, Propagator& p, PropCond pc, bool schedule=true);
00186     void cancel(Space& home, Propagator& p, PropCond pc);
00188     void reschedule(Space& home, Propagator& p, PropCond pc);
00194     void subscribe(Space& home, Advisor& a, bool fail=false);
00196     void cancel(Space& home, Advisor& a, bool fail=false);
00198 
00200 
00201 
00202     static ModEvent modevent(const Delta& d);
00204 
00206 
00207 
00208     void update(Space& home, VarImpView<Var>& y);
00210   };
00211 
00216 
00217   template<class VarA, class VarB>
00218   bool same(const VarImpView<VarA>& x, const VarImpView<VarB>& y);
00220   template<class ViewA, class ViewB>
00221   bool before(const ViewA& x, const ViewB& y);
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   };
00314 
00315 
00320   template<class ViewA, class ViewB>
00321   bool shared(const ConstView<ViewA>&, const ConstView<ViewB>&);
00326   template<class Var, class View>
00327   bool shared(const VarImpView<Var>&, const ConstView<View>&);
00332   template<class ViewA, class ViewB>
00333   bool shared(const DerivedView<ViewA>&, const ConstView<ViewB>&);
00338   template<class View, class Var>
00339   bool shared(const ConstView<View>&, const VarImpView<Var>&);
00344   template<class ViewA, class ViewB>
00345   bool shared(const ConstView<ViewA>&, const DerivedView<ViewB>&);
00350   template<class VarA, class VarB>
00351   bool shared(const VarImpView<VarA>&, const VarImpView<VarB>&);
00356   template<class Var, class View>
00357   bool shared(const VarImpView<Var>&, const DerivedView<View>&);
00362   template<class View, class Var>
00363   bool shared(const DerivedView<View>&, const VarImpView<Var>&);
00368   template<class ViewA, class ViewB>
00369   bool shared(const DerivedView<ViewA>&, const DerivedView<ViewB>&);
00370 
00371 
00372   /*
00373    * Constant view: has no variable implementation
00374    *
00375    */
00376   template<class View>
00377   forceinline unsigned int
00378   ConstView<View>::degree(void) const {
00379     return 0;
00380   }
00381   template<class View>
00382   forceinline double
00383   ConstView<View>::afc(void) const {
00384     return 0.0;
00385   }
00386   template<class View>
00387   forceinline bool
00388   ConstView<View>::varderived(void) {
00389     return false;
00390   }
00391   template<class View>
00392   forceinline typename View::VarImpType*
00393   ConstView<View>::varimp(void) const {
00394     return NULL;
00395   }
00396 #ifdef GECODE_HAS_CBS
00397   template<class View>
00398   forceinline unsigned int
00399   ConstView<View>::id(void) const {
00400     return 0;
00401   }
00402 #endif
00403   template<class View>
00404   forceinline bool
00405   ConstView<View>::assigned(void) const {
00406     return true;
00407   }
00408   template<class View>
00409   forceinline void
00410   ConstView<View>::subscribe(Space& home, Propagator& p, PropCond,
00411                              bool schedule) {
00412     if (schedule)
00413       View::schedule(home,p,ME_GEN_ASSIGNED);
00414   }
00415   template<class View>
00416   forceinline void
00417   ConstView<View>::cancel(Space&, Propagator&, PropCond) {
00418   }
00419   template<class View>
00420   forceinline void
00421   ConstView<View>::reschedule(Space& home, Propagator& p, PropCond) {
00422     View::schedule(home,p,ME_GEN_ASSIGNED);
00423   }
00424   template<class View>
00425   forceinline void
00426   ConstView<View>::subscribe(Space&, Advisor&, bool) {
00427   }
00428   template<class View>
00429   forceinline void
00430   ConstView<View>::cancel(Space&, Advisor&, bool) {
00431   }
00432   template<class View>
00433   forceinline void
00434   ConstView<View>::schedule(Space& home, Propagator& p, ModEvent me) {
00435     View::schedule(home,p,me);
00436   }
00437   template<class View>
00438   forceinline ModEvent
00439   ConstView<View>::me(const ModEventDelta& med) {
00440     return View::me(med);
00441   }
00442   template<class View>
00443   forceinline ModEventDelta
00444   ConstView<View>::med(ModEvent me) {
00445     return View::med(me);
00446   }
00447   template<class View>
00448   forceinline ModEvent
00449   ConstView<View>::modevent(const Delta& d) {
00450     (void) d;
00451     return ME_GEN_NONE;
00452   }
00453   template<class View>
00454   forceinline void
00455   ConstView<View>::update(Space&, ConstView<View>&) {
00456   }
00457 
00458   /*
00459    * Variable view: contains a pointer to a variable implementation
00460    *
00461    */
00462   template<class Var>
00463   forceinline
00464   VarImpView<Var>::VarImpView(void)
00465     : x(NULL) {}
00466   template<class Var>
00467   forceinline
00468   VarImpView<Var>::VarImpView(VarImpType* y)
00469     : x(y) {}
00470   template<class Var>
00471   forceinline void
00472   VarImpView<Var>::varimp(VarImpType* y) {
00473     x=y;
00474   }
00475   template<class Var>
00476   forceinline bool
00477   VarImpView<Var>::varderived(void) {
00478     return true;
00479   }
00480   template<class Var>
00481   forceinline typename Var::VarImpType*
00482   VarImpView<Var>::varimp(void) const {
00483     return x;
00484   }
00485  template<class Var>
00486   forceinline unsigned int
00487   VarImpView<Var>::degree(void) const {
00488     return x->degree();
00489   }
00490   template<class Var>
00491   forceinline double
00492   VarImpView<Var>::afc(void) const {
00493     return x->afc();
00494   }
00495 #ifdef GECODE_HAS_CBS
00496   template<class Var>
00497   forceinline unsigned int
00498   VarImpView<Var>::id(void) const {
00499     return x->id();
00500   }
00501 #endif
00502   template<class Var>
00503   forceinline bool
00504   VarImpView<Var>::assigned(void) const {
00505     return x->assigned();
00506   }
00507   template<class Var>
00508   forceinline void
00509   VarImpView<Var>::subscribe(Space& home, Propagator& p, PropCond pc,
00510                              bool schedule) {
00511     x->subscribe(home,p,pc,schedule);
00512   }
00513   template<class Var>
00514   forceinline void
00515   VarImpView<Var>::cancel(Space& home, Propagator& p, PropCond pc) {
00516     x->cancel(home,p,pc);
00517   }
00518   template<class Var>
00519   forceinline void
00520   VarImpView<Var>::reschedule(Space& home, Propagator& p, PropCond pc) {
00521     x->reschedule(home,p,pc);
00522   }
00523   template<class Var>
00524   forceinline void
00525   VarImpView<Var>::subscribe(Space& home, Advisor& a, bool fail) {
00526     x->subscribe(home,a,fail);
00527   }
00528   template<class Var>
00529   forceinline void
00530   VarImpView<Var>::cancel(Space& home, Advisor& a, bool fail) {
00531     x->cancel(home,a,fail);
00532   }
00533   template<class Var>
00534   forceinline void
00535   VarImpView<Var>::schedule(Space& home, Propagator& p, ModEvent me) {
00536     VarImpType::schedule(home,p,me);
00537   }
00538   template<class Var>
00539   forceinline ModEvent
00540   VarImpView<Var>::me(const ModEventDelta& med) {
00541     return VarImpType::me(med);
00542   }
00543   template<class Var>
00544   forceinline ModEventDelta
00545   VarImpView<Var>::med(ModEvent me) {
00546     return VarImpType::med(me);
00547   }
00548   template<class Var>
00549   forceinline ModEvent
00550   VarImpView<Var>::modevent(const Delta& d) {
00551     return VarImpType::modevent(d);
00552   }
00553   template<class Var>
00554   forceinline void
00555   VarImpView<Var>::update(Space& home, VarImpView<Var>& y) {
00556     x = y.x->copy(home);
00557   }
00558 
00559   /*
00560    * Derived view: contain the base view from which they are derived
00561    *
00562    */
00563 
00564   template<class View>
00565   forceinline
00566   DerivedView<View>::DerivedView(void) {}
00567 
00568   template<class View>
00569   forceinline
00570   DerivedView<View>::DerivedView(const View& y)
00571     : x(y) {}
00572 
00573   template<class View>
00574   forceinline bool
00575   DerivedView<View>::varderived(void) {
00576     return View::varderived();
00577   }
00578 
00579   template<class View>
00580   forceinline typename View::VarImpType*
00581   DerivedView<View>::varimp(void) const {
00582     return x.varimp();
00583   }
00584 
00585   template<class View>
00586   forceinline View
00587   DerivedView<View>::base(void) const {
00588     return x;
00589   }
00590 
00591   template<class View>
00592   forceinline unsigned int
00593   DerivedView<View>::degree(void) const {
00594     return x.degree();
00595   }
00596   template<class View>
00597   forceinline double
00598   DerivedView<View>::afc(void) const {
00599     return x.afc();
00600   }
00601 #ifdef GECODE_HAS_CBS
00602   template<class View>
00603   forceinline unsigned int
00604   DerivedView<View>::id(void) const {
00605     return x.id();
00606   }
00607 #endif
00608   template<class View>
00609   forceinline bool
00610   DerivedView<View>::assigned(void) const {
00611     return x.assigned();
00612   }
00613 
00614   template<class View>
00615   forceinline void
00616   DerivedView<View>::schedule(Space& home, Propagator& p, ModEvent me) {
00617     return View::schedule(home,p,me);
00618   }
00619   template<class View>
00620   forceinline ModEvent
00621   DerivedView<View>::me(const ModEventDelta& med) {
00622     return View::me(med);
00623   }
00624   template<class View>
00625   forceinline ModEventDelta
00626   DerivedView<View>::med(ModEvent me) {
00627     return View::med(me);
00628   }
00629 
00630   template<class View>
00631   forceinline void
00632   DerivedView<View>::subscribe(Space& home, Propagator& p, PropCond pc,
00633                                bool schedule) {
00634     x.subscribe(home,p,pc,schedule);
00635   }
00636   template<class View>
00637   forceinline void
00638   DerivedView<View>::cancel(Space& home, Propagator& p, PropCond pc) {
00639     x.cancel(home,p,pc);
00640   }
00641   template<class View>
00642   forceinline void
00643   DerivedView<View>::reschedule(Space& home, Propagator& p, PropCond pc) {
00644     x.reschedule(home,p,pc);
00645   }
00646   template<class View>
00647   forceinline void
00648   DerivedView<View>::subscribe(Space& home, Advisor& a, bool fail) {
00649     x.subscribe(home,a,fail);
00650   }
00651   template<class View>
00652   forceinline void
00653   DerivedView<View>::cancel(Space& home, Advisor& a, bool fail) {
00654     x.cancel(home,a,fail);
00655   }
00656   template<class View>
00657   forceinline ModEvent
00658   DerivedView<View>::modevent(const Delta& d) {
00659     return View::modevent(d);
00660   }
00661   template<class View>
00662   forceinline void
00663   DerivedView<View>::update(Space& home, DerivedView<View>& y) {
00664     x.update(home,y.x);
00665   }
00666 
00667 
00668   /*
00669    * Tests whether two views are the same
00670    *
00671    */
00672 
00674   template<class ViewA, class ViewB>
00675   forceinline bool
00676   same(const ConstView<ViewA>&, const ConstView<ViewB>&) {
00677     return false;
00678   }
00680   template<class Var, class View>
00681   forceinline bool
00682   same(const VarImpView<Var>&, const ConstView<View>&) {
00683     return false;
00684   }
00686   template<class ViewA, class ViewB>
00687   forceinline bool
00688   same(const ConstView<ViewA>&, const DerivedView<ViewB>&) {
00689     return false;
00690   }
00692   template<class Var, class View>
00693   forceinline bool
00694   same(const VarImpView<Var>&, const DerivedView<View>&) {
00695     return false;
00696   }
00698   template<class View, class Var>
00699   forceinline bool
00700   same(const DerivedView<View>&, const VarImpView<Var>&) {
00701     return false;
00702   }
00704   template<class Var>
00705   forceinline bool
00706   same(const VarImpView<Var>& x, const VarImpView<Var>& y) {
00707     return x.varimp() == y.varimp();
00708   }
00710   template<class ViewA, class ViewB>
00711   forceinline bool
00712   same(const DerivedView<ViewA>& x, const DerivedView<ViewB>& y) {
00713     return same(x.base(),y.base());
00714   }
00715 
00716 
00717   /*
00718    * Tests whether one view is before the other
00719    *
00720    */
00721   template<class ViewA, class ViewB>
00722   forceinline bool
00723   before(const ViewA& x, const ViewB& y) {
00724     return x.varimp() < y.varimp();
00725   }
00726 
00727 
00728   /*
00729    * Testing whether two views share the same variable
00730    *
00731    */
00732 
00733   template<class ViewA, class ViewB>
00734   forceinline bool
00735   shared(const ConstView<ViewA>&, const ConstView<ViewB>&) {
00736     return false;
00737   }
00738   template<class Var, class View>
00739   forceinline bool
00740   shared(const VarImpView<Var>&, const ConstView<View>&) {
00741     return false;
00742   }
00743   template<class ViewA, class ViewB>
00744   forceinline bool
00745   shared(const DerivedView<ViewA>&, const ConstView<ViewB>&) {
00746     return false;
00747   }
00748   template<class View, class Var>
00749   forceinline bool
00750   shared(const ConstView<View>&, const VarImpView<Var>&) {
00751     return false;
00752   }
00753   template<class ViewA, class ViewB>
00754   forceinline bool
00755   shared(const ConstView<ViewA>&, const DerivedView<ViewB>&) {
00756     return false;
00757   }
00758   template<class VarA, class VarB>
00759   forceinline bool
00760   shared(const VarImpView<VarA>& x, const VarImpView<VarB>& y) {
00761     return (static_cast<VarImpBase*>(x.varimp()) ==
00762             static_cast<VarImpBase*>(y.varimp()));
00763   }
00764   template<class Var, class View>
00765   forceinline bool
00766   shared(const VarImpView<Var>& x, const DerivedView<View>& y) {
00767     return (View::varderived() &&
00768             static_cast<VarImpBase*>(x.varimp()) ==
00769             static_cast<VarImpBase*>(y.varimp()));
00770   }
00771   template<class View, class Var>
00772   forceinline bool
00773   shared(const DerivedView<View>& x, const VarImpView<Var>& y) {
00774     return (View::varderived() &&
00775             static_cast<VarImpBase*>(x.varimp()) ==
00776             static_cast<VarImpBase*>(y.varimp()));
00777   }
00778   template<class ViewA, class ViewB>
00779   forceinline bool
00780   shared(const DerivedView<ViewA>& x, const DerivedView<ViewB>& y) {
00781     return (ViewA::varderived() && ViewB::varderived() &&
00782             static_cast<VarImpBase*>(x.varimp()) ==
00783             static_cast<VarImpBase*>(y.varimp()));
00784   }
00785 
00786 }
00787 
00788 // STATISTICS: kernel-var