Generated on Mon Jul 6 18:09:07 2009 for Gecode by doxygen 1.5.9

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-02-12 16:15:51 +0100 (Thu, 12 Feb 2009) $ by $Author: schulte $
00011  *     $Revision: 8179 $
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     static bool varderived(void);
00053     VarImpBase* var(void) const;
00055   };
00056 
00057 
00058 
00063   template <class VarImp>
00064   class VarViewBase {
00065   protected:
00067     VarImp* varimp;
00069     VarViewBase(void);
00071     VarViewBase(VarImp* x);
00072   public:
00074 
00075 
00076     static bool varderived(void);
00078     VarImp* var(void) const;
00080     unsigned int degree(void) const;
00082 
00084 
00085 
00086     static void schedule(Space& home, Propagator& p, ModEvent me);
00088     static ModEvent me(const ModEventDelta& med);
00090     static ModEventDelta med(ModEvent me);
00092     static ModEvent me_combine(ModEvent me1, ModEvent me2);
00094 
00096 
00097 
00104     void subscribe(Space& home, Propagator& p, PropCond pc, bool process=true);
00106     void cancel(Space& home, Propagator& p, PropCond pc);
00108     void subscribe(Space& home, Advisor& a);
00110     void cancel(Space& home, Advisor& a);
00112   };
00113 
00118 
00119   template <class VarImp>
00120   bool same(const VarViewBase<VarImp>& x, const VarViewBase<VarImp>& y);
00122   template <class VarImp>
00123   bool before(const VarViewBase<VarImp>& x, const VarViewBase<VarImp>& y);
00125 
00126 
00131   template <class View>
00132   class DerivedViewBase {
00133   protected:
00135     View view;
00137     DerivedViewBase(void);
00139     DerivedViewBase(const View& x);
00140 
00141   public:
00143     typedef typename ViewVarImpTraits<View>::VarImp VarImp;
00144 
00146 
00147 
00148     static bool varderived(void);
00150     VarImp* var(void) const;
00152     View base(void) const;
00154     unsigned int degree(void) const;
00156   };
00157 
00158 
00163   bool shared(const ConstViewBase&, const ConstViewBase&);
00168   template <class VarImp>
00169   bool shared(const VarViewBase<VarImp>&, const ConstViewBase&);
00174   template <class ViewA>
00175   bool shared(const DerivedViewBase<ViewA>&, const ConstViewBase&);
00180   template <class VarImp>
00181   bool shared(const ConstViewBase&, const VarViewBase<VarImp>&);
00186   template <class ViewA>
00187   bool shared(const ConstViewBase&, const DerivedViewBase<ViewA>&);
00192   template <class VarImpA, class VarImpB>
00193   bool shared(const VarViewBase<VarImpA>&, const VarViewBase<VarImpB>&);
00198   template <class VarImpA, class ViewB>
00199   bool shared(const VarViewBase<VarImpA>&, const DerivedViewBase<ViewB>&);
00204   template <class ViewA, class VarImpB>
00205   bool shared(const DerivedViewBase<ViewA>&, const VarViewBase<VarImpB>&);
00210   template <class ViewA, class ViewB>
00211   bool shared(const DerivedViewBase<ViewA>&, const DerivedViewBase<ViewB>&);
00212 
00213 
00214   /*
00215    * Constant view: has no variable implementation
00216    *
00217    */
00218   forceinline unsigned int
00219   ConstViewBase::degree(void) const {
00220     return 0;
00221   }
00222   forceinline bool
00223   ConstViewBase::varderived(void) {
00224     return false;
00225   }
00226   forceinline VarImpBase*
00227   ConstViewBase::var(void) const {
00228     return NULL;
00229   }
00230 
00231   /*
00232    * Variable view: contains a pointer to a variable implementation
00233    *
00234    */
00235   template <class VarImp>
00236   forceinline
00237   VarViewBase<VarImp>::VarViewBase(void)
00238     : varimp(NULL) {}
00239   template <class VarImp>
00240   forceinline
00241   VarViewBase<VarImp>::VarViewBase(VarImp* x)
00242     : varimp(x) {}
00243   template <class VarImp>
00244   forceinline bool
00245   VarViewBase<VarImp>::varderived(void) {
00246     return true;
00247   }
00248   template <class VarImp>
00249   forceinline VarImp*
00250   VarViewBase<VarImp>::var(void) const {
00251     return varimp;
00252   }
00253   template <class VarImp>
00254   forceinline unsigned int
00255   VarViewBase<VarImp>::degree(void) const {
00256     return varimp->degree();
00257   }
00258   template <class VarImp>
00259   forceinline void
00260   VarViewBase<VarImp>::subscribe(Space& home, Propagator& p, PropCond pc,
00261                                  bool process) {
00262     varimp->subscribe(home,p,pc,process);
00263   }
00264   template <class VarImp>
00265   forceinline void
00266   VarViewBase<VarImp>::cancel(Space& home, Propagator& p, PropCond pc) {
00267     varimp->cancel(home,p,pc);
00268   }
00269   template <class VarImp>
00270   forceinline void
00271   VarViewBase<VarImp>::subscribe(Space& home, Advisor& a) {
00272     varimp->subscribe(home,a);
00273   }
00274   template <class VarImp>
00275   forceinline void
00276   VarViewBase<VarImp>::cancel(Space& home, Advisor& a) {
00277     varimp->cancel(home,a);
00278   }
00279   template <class VarImp>
00280   forceinline void
00281   VarViewBase<VarImp>::schedule(Space& home, Propagator& p, ModEvent me) {
00282     return VarImp::schedule(home,p,me);
00283   }
00284   template <class VarImp>
00285   forceinline ModEvent
00286   VarViewBase<VarImp>::me(const ModEventDelta& med) {
00287     return VarImp::me(med);
00288   }
00289   template <class VarImp>
00290   forceinline ModEventDelta
00291   VarViewBase<VarImp>::med(ModEvent me) {
00292     return VarImp::med(me);
00293   }
00294   template <class VarImp>
00295   forceinline ModEvent
00296   VarViewBase<VarImp>::me_combine(ModEvent me1, ModEvent me2) {
00297     return VarImp::me_combine(me1,me2);
00298   }
00299 
00300   template <class VarImp>
00301   forceinline bool
00302   same(const VarViewBase<VarImp>& x, const VarViewBase<VarImp>& y) {
00303     return x.var() == y.var();
00304   }
00305   template <class VarImp>
00306   forceinline bool
00307   before(const VarViewBase<VarImp>& x, const VarViewBase<VarImp>& y) {
00308     return x.var() < y.var();
00309   }
00310 
00311   /*
00312    * Derived view: contain the base view from which they are derived
00313    *
00314    */
00315 
00316   template <class View>
00317   forceinline
00318   DerivedViewBase<View>::DerivedViewBase(void) {}
00319 
00320   template <class View>
00321   forceinline
00322   DerivedViewBase<View>::DerivedViewBase(const View& x)
00323     : view(x) {}
00324 
00325   template <class View>
00326   forceinline bool
00327   DerivedViewBase<View>::varderived(void) {
00328     return View::varderived();
00329   }
00330 
00331   template <class View>
00332   forceinline typename ViewVarImpTraits<View>::VarImp*
00333   DerivedViewBase<View>::var(void) const {
00334     return view.var();
00335   }
00336 
00337   template <class View>
00338   forceinline View
00339   DerivedViewBase<View>::base(void) const {
00340     return view;
00341   }
00342 
00343   template <class View>
00344   forceinline unsigned int
00345   DerivedViewBase<View>::degree(void) const {
00346     return view.degree();
00347   }
00348 
00349 
00350   /*
00351    * Testing whether two views share the same variable
00352    *
00353    */
00354 
00355   forceinline bool
00356   shared(const ConstViewBase&, const ConstViewBase&) {
00357     return false;
00358   }
00359 
00360   template <class VarImp>
00361   forceinline bool
00362   shared(const VarViewBase<VarImp>&, const ConstViewBase&) {
00363     return false;
00364   }
00365 
00366   template <class View>
00367   forceinline bool
00368   shared(const DerivedViewBase<View>&, const ConstViewBase&) {
00369     return false;
00370   }
00371 
00372   template <class VarImp>
00373   forceinline bool
00374   shared(const ConstViewBase&, const VarViewBase<VarImp>&) {
00375     return false;
00376   }
00377 
00378   template <class View>
00379   forceinline bool
00380   shared(const ConstViewBase&, const DerivedViewBase<View>&) {
00381     return false;
00382   }
00383 
00384   template <class VarImpA, class VarImpB>
00385   forceinline bool
00386   shared(const VarViewBase<VarImpA>& x, const VarViewBase<VarImpB>& y) {
00387     return (static_cast<VarImpBase*>(x.var()) ==
00388             static_cast<VarImpBase*>(y.var()));
00389   }
00390   template <class VarImpA, class ViewB>
00391   forceinline bool
00392   shared(const VarViewBase<VarImpA>& x, const DerivedViewBase<ViewB>& y) {
00393     return (ViewB::varderived() &&
00394             static_cast<VarImpBase*>(x.var()) ==
00395             static_cast<VarImpBase*>(y.var()));
00396   }
00397   template <class ViewA, class VarImpB>
00398   forceinline bool
00399   shared(const DerivedViewBase<ViewA>& x, const VarViewBase<VarImpB>& y) {
00400     return (ViewA::varderived() &&
00401             static_cast<VarImpBase*>(x.var()) ==
00402             static_cast<VarImpBase*>(y.var()));
00403   }
00404   template <class ViewA, class ViewB>
00405   forceinline bool
00406   shared(const DerivedViewBase<ViewA>& x, const DerivedViewBase<ViewB>& y) {
00407     return (ViewA::varderived() && ViewB::varderived() &&
00408             static_cast<VarImpBase*>(x.var()) ==
00409             static_cast<VarImpBase*>(y.var()));
00410   }
00411 
00412 }
00413 
00414 // STATISTICS: kernel-var