Generated on Thu Mar 22 10:39:37 2012 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  *  Copyright:
00007  *     Christian Schulte, 2005
00008  *
00009  *  Last modified:
00010  *     $Date: 2010-10-11 12:10:44 +0200 (Mon, 11 Oct 2010) $ by $Author: tack $
00011  *     $Revision: 11514 $
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;
00062 
00064 
00065 
00066     bool assigned(void) const;
00068 
00070 
00071 
00072     static void schedule(Space& home, Propagator& p, ModEvent me);
00074     static ModEvent me(const ModEventDelta& med);
00076     static ModEventDelta med(ModEvent me);
00078 
00080 
00081 
00088     void subscribe(Space& home, Propagator& p, PropCond pc, bool schedule=true);
00090     void cancel(Space& home, Propagator& p, PropCond pc);
00092     void subscribe(Space& home, Advisor& a);
00094     void cancel(Space& home, Advisor& a);
00096 
00098 
00099 
00100     static ModEvent modevent(const Delta& d);
00102 
00104 
00105 
00106     void update(Space& home, bool share, ConstView& y);
00108   };
00109 
00110 
00111 
00116   template<class Var>
00117   class VarImpView {
00118   public:
00120     typedef Var VarType;
00122     typedef typename Var::VarImpType VarImpType;
00123   protected:
00125     VarImpType* x;
00127     VarImpView(void);
00129     VarImpView(VarImpType* y);
00130   public:
00132 
00133 
00134     static bool varderived(void);
00136     VarImpType* varimp(void) const;
00138     unsigned int degree(void) const;
00140     double afc(void) const;
00142 
00144 
00145 
00146     bool assigned(void) const;
00148 
00150 
00151 
00152     static void schedule(Space& home, Propagator& p, ModEvent me);
00154     static ModEvent me(const ModEventDelta& med);
00156     static ModEventDelta med(ModEvent me);
00158 
00160 
00161 
00168     void subscribe(Space& home, Propagator& p, PropCond pc, bool schedule=true);
00170     void cancel(Space& home, Propagator& p, PropCond pc);
00172     void subscribe(Space& home, Advisor& a);
00174     void cancel(Space& home, Advisor& a);
00176 
00178 
00179 
00180     static ModEvent modevent(const Delta& d);
00182 
00184 
00185 
00186     void update(Space& home, bool share, VarImpView<Var>& y);
00188   };
00189 
00194 
00195   template<class VarA, class VarB>
00196   bool same(const VarImpView<VarA>& x, const VarImpView<VarB>& y);
00198   template<class ViewA, class ViewB>
00199   bool before(const ViewA& x, const ViewB& y);
00201 
00202 
00207   template<class View>
00208   class DerivedView {
00209   public:
00211     typedef typename View::VarImpType VarImpType;
00213     typedef typename View::VarType VarType;
00214   protected:
00216     View x;
00218     DerivedView(void);
00220     DerivedView(const View& y);
00221   public:
00223 
00224 
00225     static bool varderived(void);
00227     VarImpType* varimp(void) const;
00229     View base(void) const;
00231     unsigned int degree(void) const;
00233     double afc(void) const;
00235 
00237 
00238 
00239     bool assigned(void) const;
00241 
00243 
00244 
00245     static void schedule(Space& home, Propagator& p, ModEvent me);
00247     static ModEvent me(const ModEventDelta& med);
00249     static ModEventDelta med(ModEvent);
00251 
00253 
00254 
00261     void subscribe(Space& home, Propagator& p, PropCond pc, bool schedule=true);
00263     void cancel(Space& home, Propagator& p, PropCond pc);
00265     void subscribe(Space& home, Advisor& a);
00267     void cancel(Space& home, Advisor& a);
00269 
00271 
00272 
00273     static ModEvent modevent(const Delta& d);
00275 
00277 
00278 
00279     void update(Space& home, bool share, DerivedView<View>& y);
00281   };
00282 
00283 
00288   template<class ViewA, class ViewB>
00289   bool shared(const ConstView<ViewA>&, const ConstView<ViewB>&);
00294   template<class Var, class View>
00295   bool shared(const VarImpView<Var>&, const ConstView<View>&);
00300   template<class ViewA, class ViewB>
00301   bool shared(const DerivedView<ViewA>&, const ConstView<ViewB>&);
00306   template<class View, class Var>
00307   bool shared(const ConstView<View>&, const VarImpView<Var>&);
00312   template<class ViewA, class ViewB>
00313   bool shared(const ConstView<ViewA>&, const DerivedView<ViewB>&);
00318   template<class VarA, class VarB>
00319   bool shared(const VarImpView<VarA>&, const VarImpView<VarB>&);
00324   template<class Var, class View>
00325   bool shared(const VarImpView<Var>&, const DerivedView<View>&);
00330   template<class View, class Var>
00331   bool shared(const DerivedView<View>&, const VarImpView<Var>&);
00336   template<class ViewA, class ViewB>
00337   bool shared(const DerivedView<ViewA>&, const DerivedView<ViewB>&);
00338 
00339 
00340   /*
00341    * Constant view: has no variable implementation
00342    *
00343    */
00344   template<class View>
00345   forceinline unsigned int
00346   ConstView<View>::degree(void) const {
00347     return 0;
00348   }
00349   template<class View>
00350   forceinline double
00351   ConstView<View>::afc(void) const {
00352     return 0.0;
00353   }
00354   template<class View>
00355   forceinline bool
00356   ConstView<View>::varderived(void) {
00357     return false;
00358   }
00359   template<class View>
00360   forceinline typename View::VarImpType*
00361   ConstView<View>::varimp(void) const {
00362     return NULL;
00363   }
00364   template<class View>
00365   forceinline bool
00366   ConstView<View>::assigned(void) const {
00367     return true;
00368   }
00369   template<class View>
00370   forceinline void
00371   ConstView<View>::subscribe(Space& home, Propagator& p, PropCond, 
00372                              bool schedule) {
00373     if (schedule)
00374       View::schedule(home,p,ME_GEN_ASSIGNED);
00375   }
00376   template<class View>
00377   forceinline void
00378   ConstView<View>::cancel(Space&, Propagator&, PropCond) {
00379   }
00380   template<class View>
00381   forceinline void
00382   ConstView<View>::subscribe(Space&, Advisor&) {
00383   }
00384   template<class View>
00385   forceinline void
00386   ConstView<View>::cancel(Space&, Advisor&) {
00387   }
00388   template<class View>
00389   forceinline void
00390   ConstView<View>::schedule(Space& home, Propagator& p, ModEvent me) {
00391     View::schedule(home,p,me);
00392   }
00393   template<class View>
00394   forceinline ModEvent
00395   ConstView<View>::me(const ModEventDelta& med) {
00396     return View::me(med);
00397   }
00398   template<class View>
00399   forceinline ModEventDelta
00400   ConstView<View>::med(ModEvent me) {
00401     return View::med(me);
00402   }
00403   template<class View>
00404   forceinline ModEvent
00405   ConstView<View>::modevent(const Delta& d) {
00406     (void) d;
00407     return ME_GEN_NONE;
00408   }
00409   template<class View>
00410   forceinline void
00411   ConstView<View>::update(Space&, bool, ConstView<View>&) {
00412   }
00413 
00414   /*
00415    * Variable view: contains a pointer to a variable implementation
00416    *
00417    */
00418   template<class Var>
00419   forceinline
00420   VarImpView<Var>::VarImpView(void)
00421     : x(NULL) {}
00422   template<class Var>
00423   forceinline
00424   VarImpView<Var>::VarImpView(VarImpType* y)
00425     : x(y) {}
00426   template<class Var>
00427   forceinline bool
00428   VarImpView<Var>::varderived(void) {
00429     return true;
00430   }
00431   template<class Var>
00432   forceinline typename Var::VarImpType*
00433   VarImpView<Var>::varimp(void) const {
00434     return x;
00435   }
00436   template<class Var>
00437   forceinline unsigned int
00438   VarImpView<Var>::degree(void) const {
00439     return x->degree();
00440   }
00441   template<class Var>
00442   forceinline double
00443   VarImpView<Var>::afc(void) const {
00444     return x->afc();
00445   }
00446   template<class Var>
00447   forceinline bool
00448   VarImpView<Var>::assigned(void) const {
00449     return x->assigned();
00450   }
00451   template<class Var>
00452   forceinline void
00453   VarImpView<Var>::subscribe(Space& home, Propagator& p, PropCond pc,
00454                              bool schedule) {
00455     x->subscribe(home,p,pc,schedule);
00456   }
00457   template<class Var>
00458   forceinline void
00459   VarImpView<Var>::cancel(Space& home, Propagator& p, PropCond pc) {
00460     x->cancel(home,p,pc);
00461   }
00462   template<class Var>
00463   forceinline void
00464   VarImpView<Var>::subscribe(Space& home, Advisor& a) {
00465     x->subscribe(home,a);
00466   }
00467   template<class Var>
00468   forceinline void
00469   VarImpView<Var>::cancel(Space& home, Advisor& a) {
00470     x->cancel(home,a);
00471   }
00472   template<class Var>
00473   forceinline void
00474   VarImpView<Var>::schedule(Space& home, Propagator& p, ModEvent me) {
00475     VarImpType::schedule(home,p,me);
00476   }
00477   template<class Var>
00478   forceinline ModEvent
00479   VarImpView<Var>::me(const ModEventDelta& med) {
00480     return VarImpType::me(med);
00481   }
00482   template<class Var>
00483   forceinline ModEventDelta
00484   VarImpView<Var>::med(ModEvent me) {
00485     return VarImpType::med(me);
00486   }
00487   template<class Var>
00488   forceinline ModEvent
00489   VarImpView<Var>::modevent(const Delta& d) {
00490     return VarImpType::modevent(d);
00491   }
00492   template<class Var>
00493   forceinline void
00494   VarImpView<Var>::update(Space& home, bool share, VarImpView<Var>& y) {
00495     x = y.x->copy(home,share);
00496   }
00497 
00498   /*
00499    * Derived view: contain the base view from which they are derived
00500    *
00501    */
00502 
00503   template<class View>
00504   forceinline
00505   DerivedView<View>::DerivedView(void) {}
00506 
00507   template<class View>
00508   forceinline
00509   DerivedView<View>::DerivedView(const View& y)
00510     : x(y) {}
00511 
00512   template<class View>
00513   forceinline bool
00514   DerivedView<View>::varderived(void) {
00515     return View::varderived();
00516   }
00517 
00518   template<class View>
00519   forceinline typename View::VarImpType*
00520   DerivedView<View>::varimp(void) const {
00521     return x.varimp();
00522   }
00523 
00524   template<class View>
00525   forceinline View
00526   DerivedView<View>::base(void) const {
00527     return x;
00528   }
00529 
00530   template<class View>
00531   forceinline unsigned int
00532   DerivedView<View>::degree(void) const {
00533     return x.degree();
00534   }
00535   template<class View>
00536   forceinline double
00537   DerivedView<View>::afc(void) const {
00538     return x.afc();
00539   }
00540   template<class View>
00541   forceinline bool
00542   DerivedView<View>::assigned(void) const {
00543     return x.assigned();
00544   }
00545 
00546   template<class View>
00547   forceinline void
00548   DerivedView<View>::schedule(Space& home, Propagator& p, ModEvent me) {
00549     return View::schedule(home,p,me);
00550   }
00551   template<class View>
00552   forceinline ModEvent
00553   DerivedView<View>::me(const ModEventDelta& med) {
00554     return View::me(med);
00555   }
00556   template<class View>
00557   forceinline ModEventDelta
00558   DerivedView<View>::med(ModEvent me) {
00559     return View::med(me);
00560   }
00561 
00562   template<class View>
00563   forceinline void
00564   DerivedView<View>::subscribe(Space& home, Propagator& p, PropCond pc,
00565                                bool schedule) {
00566     x.subscribe(home,p,pc,schedule);
00567   }
00568   template<class View>
00569   forceinline void
00570   DerivedView<View>::cancel(Space& home, Propagator& p, PropCond pc) {
00571     x.cancel(home,p,pc);
00572   }
00573   template<class View>
00574   forceinline void
00575   DerivedView<View>::subscribe(Space& home, Advisor& a) {
00576     x.subscribe(home,a);
00577   }
00578   template<class View>
00579   forceinline void
00580   DerivedView<View>::cancel(Space& home, Advisor& a) {
00581     x.cancel(home,a);
00582   }
00583   template<class View>
00584   forceinline ModEvent
00585   DerivedView<View>::modevent(const Delta& d) {
00586     return View::modevent(d);
00587   }
00588   template<class View>
00589   forceinline void
00590   DerivedView<View>::update(Space& home, bool share, DerivedView<View>& y) {
00591     x.update(home,share,y.x);
00592   }
00593 
00594 
00595   /*
00596    * Tests whether two views are the same
00597    *
00598    */
00599 
00601   template<class ViewA, class ViewB>
00602   forceinline bool 
00603   same(const ConstView<ViewA>&, const ConstView<ViewB>&) {
00604     return false;
00605   }
00607   template<class Var, class View>
00608   forceinline bool 
00609   same(const VarImpView<Var>&, const ConstView<View>&) {
00610     return false;
00611   }
00613   template<class ViewA, class ViewB>
00614   forceinline bool 
00615   same(const ConstView<ViewA>&, const DerivedView<ViewB>&) {
00616     return false;
00617   }
00619   template<class Var, class View>
00620   forceinline bool 
00621   same(const VarImpView<Var>&, const DerivedView<View>&) {
00622     return false;
00623   }
00625   template<class View, class Var>
00626   forceinline bool 
00627   same(const DerivedView<View>&, const VarImpView<Var>&) {
00628     return false;
00629   }
00631   template<class Var>
00632   forceinline bool
00633   same(const VarImpView<Var>& x, const VarImpView<Var>& y) {
00634     return x.varimp() == y.varimp();
00635   }
00637   template<class ViewA, class ViewB>
00638   forceinline bool 
00639   same(const DerivedView<ViewA>& x, const DerivedView<ViewB>& y) {
00640     return same(x.base(),y.base());
00641   }
00642 
00643 
00644   /*
00645    * Tests whether one view is before the other
00646    *
00647    */
00648   template<class ViewA, class ViewB>
00649   forceinline bool
00650   before(const ViewA& x, const ViewB& y) {
00651     return x.varimp() < y.varimp();
00652   }
00653 
00654 
00655   /*
00656    * Testing whether two views share the same variable
00657    *
00658    */
00659 
00660   template<class ViewA, class ViewB>
00661   forceinline bool
00662   shared(const ConstView<ViewA>&, const ConstView<ViewB>&) {
00663     return false;
00664   }
00665   template<class Var, class View>
00666   forceinline bool
00667   shared(const VarImpView<Var>&, const ConstView<View>&) {
00668     return false;
00669   }
00670   template<class ViewA, class ViewB>
00671   forceinline bool
00672   shared(const DerivedView<ViewA>&, const ConstView<ViewB>&) {
00673     return false;
00674   }
00675   template<class View, class Var>
00676   forceinline bool
00677   shared(const ConstView<View>&, const VarImpView<Var>&) {
00678     return false;
00679   }
00680   template<class ViewA, class ViewB>
00681   forceinline bool
00682   shared(const ConstView<ViewA>&, const DerivedView<ViewB>&) {
00683     return false;
00684   }
00685   template<class VarA, class VarB>
00686   forceinline bool
00687   shared(const VarImpView<VarA>& x, const VarImpView<VarB>& y) {
00688     return (static_cast<VarImpBase*>(x.varimp()) ==
00689             static_cast<VarImpBase*>(y.varimp()));
00690   }
00691   template<class Var, class View>
00692   forceinline bool
00693   shared(const VarImpView<Var>& x, const DerivedView<View>& y) {
00694     return (View::varderived() &&
00695             static_cast<VarImpBase*>(x.varimp()) ==
00696             static_cast<VarImpBase*>(y.varimp()));
00697   }
00698   template<class View, class Var>
00699   forceinline bool
00700   shared(const DerivedView<View>& x, const VarImpView<Var>& y) {
00701     return (View::varderived() &&
00702             static_cast<VarImpBase*>(x.varimp()) ==
00703             static_cast<VarImpBase*>(y.varimp()));
00704   }
00705   template<class ViewA, class ViewB>
00706   forceinline bool
00707   shared(const DerivedView<ViewA>& x, const DerivedView<ViewB>& y) {
00708     return (ViewA::varderived() && ViewB::varderived() &&
00709             static_cast<VarImpBase*>(x.varimp()) ==
00710             static_cast<VarImpBase*>(y.varimp()));
00711   }
00712 
00713 }
00714 
00715 // STATISTICS: kernel-var