Generated on Mon Nov 30 23:53:24 2009 for Gecode by doxygen 1.6.1

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: 2009-11-04 17:06:58 +0100 (Wed, 04 Nov 2009) $ by $Author: tack $
00011  *     $Revision: 10046 $
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   class ConstViewBase {
00045   public:
00047 
00048 
00049     unsigned int degree(void) const;
00051     double afc(void) const;
00053     static bool varderived(void);
00055     VarImpBase* var(void) const;
00057   };
00058 
00059 
00060 
00065   template<class VarImp>
00066   class VarViewBase {
00067   protected:
00069     VarImp* varimp;
00071     VarViewBase(void);
00073     VarViewBase(VarImp* x);
00074   public:
00076 
00077 
00078     static bool varderived(void);
00080     VarImp* var(void) const;
00082     unsigned int degree(void) const;
00084     double afc(void) const;
00086 
00088 
00089 
00090     static void schedule(Space& home, Propagator& p, ModEvent me);
00092     static ModEvent me(const ModEventDelta& med);
00094     static ModEventDelta med(ModEvent me);
00096     static ModEvent me_combine(ModEvent me1, ModEvent me2);
00098 
00100 
00101 
00108     void subscribe(Space& home, Propagator& p, PropCond pc, bool process=true);
00110     void cancel(Space& home, Propagator& p, PropCond pc);
00112     void subscribe(Space& home, Advisor& a);
00114     void cancel(Space& home, Advisor& a);
00116   };
00117 
00122 
00123   template<class VarImpA, class VarImpB>
00124   bool same(const VarViewBase<VarImpA>& x, const VarViewBase<VarImpB>& y);
00126   template<class ViewA, class ViewB>
00127   bool before(const ViewA& x, const ViewB& y);
00129 
00130 
00135   template<class View>
00136   class DerivedViewBase {
00137   protected:
00139     View view;
00141     DerivedViewBase(void);
00143     DerivedViewBase(const View& x);
00144 
00145   public:
00147     typedef typename ViewVarImpTraits<View>::VarImp VarImp;
00148 
00150 
00151 
00152     static bool varderived(void);
00154     VarImp* var(void) const;
00156     View base(void) const;
00158     unsigned int degree(void) const;
00160     double afc(void) const;
00162   };
00163 
00164 
00169   bool shared(const ConstViewBase&, const ConstViewBase&);
00174   template<class VarImp>
00175   bool shared(const VarViewBase<VarImp>&, const ConstViewBase&);
00180   template<class ViewA>
00181   bool shared(const DerivedViewBase<ViewA>&, const ConstViewBase&);
00186   template<class VarImp>
00187   bool shared(const ConstViewBase&, const VarViewBase<VarImp>&);
00192   template<class ViewA>
00193   bool shared(const ConstViewBase&, const DerivedViewBase<ViewA>&);
00198   template<class VarImpA, class VarImpB>
00199   bool shared(const VarViewBase<VarImpA>&, const VarViewBase<VarImpB>&);
00204   template<class VarImpA, class ViewB>
00205   bool shared(const VarViewBase<VarImpA>&, const DerivedViewBase<ViewB>&);
00210   template<class ViewA, class VarImpB>
00211   bool shared(const DerivedViewBase<ViewA>&, const VarViewBase<VarImpB>&);
00216   template<class ViewA, class ViewB>
00217   bool shared(const DerivedViewBase<ViewA>&, const DerivedViewBase<ViewB>&);
00218 
00219 
00221   inline
00222   bool same(const ConstViewBase&, const ConstViewBase&) {
00223     return false;
00224   }
00226   template<class VarImp>
00227   bool same(const VarViewBase<VarImp>&, const ConstViewBase&) {
00228     return false;
00229   }
00231   template<class ViewA>
00232   bool same(const ConstViewBase&, const DerivedViewBase<ViewA>&) {
00233     return false;
00234   }
00236   template<class VarImpA, class ViewB>
00237   bool same(const VarViewBase<VarImpA>&, const DerivedViewBase<ViewB>&) {
00238     return false;
00239   }
00241   template<class ViewA, class VarImpB>
00242   bool same(const DerivedViewBase<ViewA>&, const VarViewBase<VarImpB>&) {
00243     return false;
00244   }
00246   template<class ViewA, class ViewB>
00247   bool same(const DerivedViewBase<ViewA>&, const DerivedViewBase<ViewB>&) {
00248     return false;
00249   }
00250 
00251   /*
00252    * Constant view: has no variable implementation
00253    *
00254    */
00255   forceinline unsigned int
00256   ConstViewBase::degree(void) const {
00257     return 0;
00258   }
00259   forceinline double
00260   ConstViewBase::afc(void) const {
00261     return 0.0;
00262   }
00263   forceinline bool
00264   ConstViewBase::varderived(void) {
00265     return false;
00266   }
00267   forceinline VarImpBase*
00268   ConstViewBase::var(void) const {
00269     return NULL;
00270   }
00271 
00272   /*
00273    * Variable view: contains a pointer to a variable implementation
00274    *
00275    */
00276   template<class VarImp>
00277   forceinline
00278   VarViewBase<VarImp>::VarViewBase(void)
00279     : varimp(NULL) {}
00280   template<class VarImp>
00281   forceinline
00282   VarViewBase<VarImp>::VarViewBase(VarImp* x)
00283     : varimp(x) {}
00284   template<class VarImp>
00285   forceinline bool
00286   VarViewBase<VarImp>::varderived(void) {
00287     return true;
00288   }
00289   template<class VarImp>
00290   forceinline VarImp*
00291   VarViewBase<VarImp>::var(void) const {
00292     return varimp;
00293   }
00294   template<class VarImp>
00295   forceinline unsigned int
00296   VarViewBase<VarImp>::degree(void) const {
00297     return varimp->degree();
00298   }
00299   template<class VarImp>
00300   forceinline double
00301   VarViewBase<VarImp>::afc(void) const {
00302     return varimp->afc();
00303   }
00304   template<class VarImp>
00305   forceinline void
00306   VarViewBase<VarImp>::subscribe(Space& home, Propagator& p, PropCond pc,
00307                                  bool process) {
00308     varimp->subscribe(home,p,pc,process);
00309   }
00310   template<class VarImp>
00311   forceinline void
00312   VarViewBase<VarImp>::cancel(Space& home, Propagator& p, PropCond pc) {
00313     varimp->cancel(home,p,pc);
00314   }
00315   template<class VarImp>
00316   forceinline void
00317   VarViewBase<VarImp>::subscribe(Space& home, Advisor& a) {
00318     varimp->subscribe(home,a);
00319   }
00320   template<class VarImp>
00321   forceinline void
00322   VarViewBase<VarImp>::cancel(Space& home, Advisor& a) {
00323     varimp->cancel(home,a);
00324   }
00325   template<class VarImp>
00326   forceinline void
00327   VarViewBase<VarImp>::schedule(Space& home, Propagator& p, ModEvent me) {
00328     return VarImp::schedule(home,p,me);
00329   }
00330   template<class VarImp>
00331   forceinline ModEvent
00332   VarViewBase<VarImp>::me(const ModEventDelta& med) {
00333     return VarImp::me(med);
00334   }
00335   template<class VarImp>
00336   forceinline ModEventDelta
00337   VarViewBase<VarImp>::med(ModEvent me) {
00338     return VarImp::med(me);
00339   }
00340   template<class VarImp>
00341   forceinline ModEvent
00342   VarViewBase<VarImp>::me_combine(ModEvent me1, ModEvent me2) {
00343     return VarImp::me_combine(me1,me2);
00344   }
00345 
00346   template<class VarImp>
00347   forceinline bool
00348   same(const VarViewBase<VarImp>& x, const VarViewBase<VarImp>& y) {
00349     return x.var() == y.var();
00350   }
00351   template<class ViewA, class ViewB>
00352   forceinline bool
00353   before(const ViewA& x, const ViewB& y) {
00354     return x.var() < y.var();
00355   }
00356 
00357   /*
00358    * Derived view: contain the base view from which they are derived
00359    *
00360    */
00361 
00362   template<class View>
00363   forceinline
00364   DerivedViewBase<View>::DerivedViewBase(void) {}
00365 
00366   template<class View>
00367   forceinline
00368   DerivedViewBase<View>::DerivedViewBase(const View& x)
00369     : view(x) {}
00370 
00371   template<class View>
00372   forceinline bool
00373   DerivedViewBase<View>::varderived(void) {
00374     return View::varderived();
00375   }
00376 
00377   template<class View>
00378   forceinline typename ViewVarImpTraits<View>::VarImp*
00379   DerivedViewBase<View>::var(void) const {
00380     return view.var();
00381   }
00382 
00383   template<class View>
00384   forceinline View
00385   DerivedViewBase<View>::base(void) const {
00386     return view;
00387   }
00388 
00389   template<class View>
00390   forceinline unsigned int
00391   DerivedViewBase<View>::degree(void) const {
00392     return view.degree();
00393   }
00394   template<class View>
00395   forceinline double
00396   DerivedViewBase<View>::afc(void) const {
00397     return view.afc();
00398   }
00399 
00400 
00401   /*
00402    * Testing whether two views share the same variable
00403    *
00404    */
00405 
00406   forceinline bool
00407   shared(const ConstViewBase&, const ConstViewBase&) {
00408     return false;
00409   }
00410 
00411   template<class VarImp>
00412   forceinline bool
00413   shared(const VarViewBase<VarImp>&, const ConstViewBase&) {
00414     return false;
00415   }
00416 
00417   template<class View>
00418   forceinline bool
00419   shared(const DerivedViewBase<View>&, const ConstViewBase&) {
00420     return false;
00421   }
00422 
00423   template<class VarImp>
00424   forceinline bool
00425   shared(const ConstViewBase&, const VarViewBase<VarImp>&) {
00426     return false;
00427   }
00428 
00429   template<class View>
00430   forceinline bool
00431   shared(const ConstViewBase&, const DerivedViewBase<View>&) {
00432     return false;
00433   }
00434 
00435   template<class VarImpA, class VarImpB>
00436   forceinline bool
00437   shared(const VarViewBase<VarImpA>& x, const VarViewBase<VarImpB>& y) {
00438     return (static_cast<VarImpBase*>(x.var()) ==
00439             static_cast<VarImpBase*>(y.var()));
00440   }
00441   template<class VarImpA, class ViewB>
00442   forceinline bool
00443   shared(const VarViewBase<VarImpA>& x, const DerivedViewBase<ViewB>& y) {
00444     return (ViewB::varderived() &&
00445             static_cast<VarImpBase*>(x.var()) ==
00446             static_cast<VarImpBase*>(y.var()));
00447   }
00448   template<class ViewA, class VarImpB>
00449   forceinline bool
00450   shared(const DerivedViewBase<ViewA>& x, const VarViewBase<VarImpB>& y) {
00451     return (ViewA::varderived() &&
00452             static_cast<VarImpBase*>(x.var()) ==
00453             static_cast<VarImpBase*>(y.var()));
00454   }
00455   template<class ViewA, class ViewB>
00456   forceinline bool
00457   shared(const DerivedViewBase<ViewA>& x, const DerivedViewBase<ViewB>& y) {
00458     return (ViewA::varderived() && ViewB::varderived() &&
00459             static_cast<VarImpBase*>(x.var()) ==
00460             static_cast<VarImpBase*>(y.var()));
00461   }
00462 
00463 }
00464 
00465 // STATISTICS: kernel-var