Generated on Thu Jul 6 07:06:43 2006 for Gecode by doxygen 1.4.7

view.icc

Go to the documentation of this file.
00001 /*
00002  *  Main authors:
00003  *     Christian Schulte <schulte@gecode.org>
00004  *
00005  *  Copyright:
00006  *     Christian Schulte, 2005
00007  *
00008  *  Last modified:
00009  *     $Date: 2006-06-02 16:40:56 +0200 (Fri, 02 Jun 2006) $ by $Author: zayenz $
00010  *     $Revision: 3261 $
00011  *
00012  *  This file is part of Gecode, the generic constraint
00013  *  development environment:
00014  *     http://www.gecode.org
00015  *
00016  *  See the file "LICENSE" for information on usage and
00017  *  redistribution of this file, and for a
00018  *     DISCLAIMER OF ALL WARRANTIES.
00019  *
00020  */
00021 
00022 #include <iostream>
00023 
00024 namespace Gecode {
00025 
00030   class ConstantViewBase {
00031   public:
00033 
00034 
00035     bool modified(void) const;
00037     unsigned int degree(void) const;
00039     static bool varderived(void);
00041     VarBase* variable(void) const;
00043   private:
00044     static void* operator new(size_t);
00045     static void operator delete(void*);
00046   };
00047 
00048 
00049 
00054   template <class Var>
00055   class VariableViewBase {
00056   protected:
00058     Var* var;
00060     VariableViewBase(void);
00062     VariableViewBase(Var* x);
00063 
00064   public:
00066 
00067 
00068     static bool varderived(void);
00070     Var* variable(void) const;
00072     bool modified(void) const;
00074     unsigned int degree(void) const;
00076 
00078 
00079 
00080     static ModEvent     pme(const Propagator* p);
00082     static PropModEvent pme(ModEvent me);
00084     static ModEvent     combine(ModEvent me1, ModEvent me2);
00086 
00088 
00089 
00090     void subscribe(Space*, Propagator* p, PropCond pc);
00092     void cancel(Space* home, Propagator* p, PropCond pc);
00094   private:
00095     static void* operator new(size_t);
00096     static void operator delete(void*);
00097   };
00098 
00103 
00104   template <class Var>
00105   bool same(const VariableViewBase<Var>& x, const VariableViewBase<Var>& y);
00107   template <class Var>
00108   bool before(const VariableViewBase<Var>& x, const VariableViewBase<Var>& y);
00110 
00111 
00112 
00122   template <class View>
00123   class ViewVarTraits {};
00124 
00129   template <class View>
00130   class DerivedViewBase {
00131   protected:
00133     View view;
00135     DerivedViewBase(void);
00137     DerivedViewBase(const View& x);
00138 
00139   public:
00141     typedef typename ViewVarTraits<View>::Var Var;
00142 
00144 
00145 
00146     static bool varderived(void);
00148     Var* variable(void) const;
00150     View base(void) const;
00152     bool modified(void) const;
00154     unsigned int degree(void) const;
00156 
00157   private:
00158     static void* operator new(size_t);
00159     static void operator delete(void*);
00160   };
00161 
00162 
00167   bool shared(const ConstantViewBase&, const ConstantViewBase&);
00172   template <class Var>
00173   bool shared(const VariableViewBase<Var>&, const ConstantViewBase&);
00178   template <class ViewA>
00179   bool shared(const DerivedViewBase<ViewA>&, const ConstantViewBase&);
00184   template <class Var>
00185   bool shared(const ConstantViewBase&, const VariableViewBase<Var>&);
00190   template <class ViewA>
00191   bool shared(const ConstantViewBase&, const DerivedViewBase<ViewA>&);
00196   template <class VarA, class VarB>
00197   bool shared(const VariableViewBase<VarA>&, 
00198               const VariableViewBase<VarB>&);
00203   template <class VarA, class ViewB>
00204   bool shared(const VariableViewBase<VarA>&, 
00205               const DerivedViewBase<ViewB>&);
00210   template <class ViewA, class VarB>
00211   bool shared(const DerivedViewBase<ViewA>&, 
00212               const VariableViewBase<VarB>&);
00217   template <class ViewA, class ViewB>
00218   bool shared(const DerivedViewBase<ViewA>&, 
00219               const DerivedViewBase<ViewB>&);
00220 
00221 
00230   template <class View, unsigned int n>
00231   class ViewTuple {
00232   private:
00234     View x[n];
00235   public:
00237 
00238 
00239     ViewTuple(void);
00241      
00243 
00244 
00245     View& operator[](unsigned int i);
00247     const View& operator[](unsigned int i) const;
00249 
00251 
00252 
00253     void subscribe(Space* home, Propagator* p, PropCond pc);
00255     void cancel(Space* home, Propagator* p, PropCond pc);
00257 
00259 
00260 
00261     void update(Space* home, bool share, ViewTuple& xs);
00263 
00264   private:
00265     static void* operator new(size_t);
00266     static void operator delete(void*);
00267   };
00268 
00274 
00275   template <class View, unsigned int n>
00276   bool same(const ViewTuple<View,n>& x, const ViewTuple<View,n>& y);
00278   template <class View, unsigned int n>
00279   bool before(const ViewTuple<View,n>& x, const ViewTuple<View,n>& y);
00281 
00282 }
00283 
00288 template <class View, unsigned int n>
00289 std::ostream&
00290 operator<<(std::ostream& os, const Gecode::ViewTuple<View,n>& xy);
00291   
00292 
00293 namespace Gecode {  
00294 
00295   /*
00296    * Constant view: has no variable implementation
00297    *
00298    */
00299   forceinline bool
00300   ConstantViewBase::modified(void) const {
00301     return true;
00302   }
00303   forceinline unsigned int
00304   ConstantViewBase::degree(void) const {
00305     return 0;
00306   }
00307   forceinline bool
00308   ConstantViewBase::varderived(void) {
00309     return false;
00310   }
00311   forceinline VarBase*
00312   ConstantViewBase::variable(void) const {
00313     return NULL;
00314   }
00315 
00316 
00317   /*
00318    * Variable view: contains a pointer to a variable implementation
00319    *
00320    */
00321   template <class Var>
00322   forceinline
00323   VariableViewBase<Var>::VariableViewBase(void) {}
00324   template <class Var>
00325   forceinline
00326   VariableViewBase<Var>::VariableViewBase(Var* x)
00327     : var(x) {}
00328   template <class Var>
00329   forceinline bool
00330   VariableViewBase<Var>::varderived(void) {
00331     return true;
00332   }
00333   template <class Var>
00334   forceinline Var*
00335   VariableViewBase<Var>::variable(void) const {
00336     return var;
00337   }
00338   template <class Var>
00339   forceinline bool
00340   VariableViewBase<Var>::modified(void) const {
00341     return var->modified();
00342   }
00343   template <class Var>
00344   forceinline unsigned int
00345   VariableViewBase<Var>::degree(void) const {
00346     return var->degree();
00347   }
00348   template <class Var>
00349   forceinline void
00350   VariableViewBase<Var>::subscribe(Space* home, Propagator* p, PropCond pc) {
00351     var->subscribe(home,p,pc);
00352   }
00353   template <class Var>
00354   forceinline void
00355   VariableViewBase<Var>::cancel(Space* home, Propagator* p, PropCond pc) {
00356     var->cancel(home,p,pc);
00357   }
00358   template <class Var>
00359   forceinline ModEvent
00360   VariableViewBase<Var>::pme(const Propagator* p) {
00361     return Var::pme(p);
00362   }
00363   template <class Var>
00364   forceinline PropModEvent
00365   VariableViewBase<Var>::pme(ModEvent me) {
00366     return Var::pme(me);
00367   }
00368   template <class Var>
00369   forceinline ModEvent
00370   VariableViewBase<Var>::combine(ModEvent me1, ModEvent me2) {
00371     return Var::combine(me1,me2);
00372   }
00373 
00374   template <class Var>
00375   forceinline bool 
00376   same(const VariableViewBase<Var>& x, const VariableViewBase<Var>& y) {
00377     return x.variable() == y.variable();
00378   }
00379   template <class Var>
00380   forceinline bool 
00381   before(const VariableViewBase<Var>& x, const VariableViewBase<Var>& y) {
00382     return x.variable() < y.variable();    
00383   }
00384 
00385   /*
00386    * Derived view: contain the base view from which they are derived
00387    *
00388    */
00389 
00390   template <class View>
00391   forceinline
00392   DerivedViewBase<View>::DerivedViewBase(void) {}
00393 
00394   template <class View>
00395   forceinline
00396   DerivedViewBase<View>::DerivedViewBase(const View& x)
00397     : view(x) {}
00398 
00399   template <class View>
00400   forceinline bool
00401   DerivedViewBase<View>::varderived(void) {
00402     return View::varderived();
00403   }
00404 
00405   template <class View>
00406   forceinline typename ViewVarTraits<View>::Var*
00407   DerivedViewBase<View>::variable(void) const {
00408     return view.variable();
00409   }
00410 
00411   template <class View>
00412   forceinline View
00413   DerivedViewBase<View>::base(void) const {
00414     return view;
00415   }
00416 
00417   template <class View>
00418   forceinline bool
00419   DerivedViewBase<View>::modified(void) const {
00420     return view.modified();
00421   }
00422 
00423   template <class View>
00424   forceinline unsigned int
00425   DerivedViewBase<View>::degree(void) const {
00426     return view.degree();
00427   }
00428 
00429 
00430   /*
00431    * Testing whether two views share the same variable
00432    *
00433    */
00434 
00435   forceinline bool
00436   shared(const ConstantViewBase&, const ConstantViewBase&) {
00437     return false;
00438   }
00439 
00440   template <class Var>
00441   forceinline bool
00442   shared(const VariableViewBase<Var>&, const ConstantViewBase&) {
00443     return false;
00444   }
00445 
00446   template <class View>
00447   forceinline bool
00448   shared(const DerivedViewBase<View>&, const ConstantViewBase&) {
00449     return false;
00450   }
00451 
00452   template <class Var>
00453   forceinline bool
00454   shared(const ConstantViewBase&, const VariableViewBase<Var>&) {
00455     return false;
00456   }
00457 
00458   template <class View>
00459   forceinline bool
00460   shared(const ConstantViewBase&, const DerivedViewBase<View>&) {
00461     return false;
00462   }
00463 
00464   template <class VarA, class VarB>
00465   forceinline bool
00466   shared(const VariableViewBase<VarA>& x, 
00467          const VariableViewBase<VarB>& y) {
00468     return (static_cast<VarBase*>(x.variable()) ==
00469             static_cast<VarBase*>(y.variable()));
00470   }
00471   template <class VarA, class ViewB>
00472   forceinline bool
00473   shared(const VariableViewBase<VarA>& x, 
00474          const DerivedViewBase<ViewB>& y) {
00475     return (ViewB::varderived() &&
00476             static_cast<VarBase*>(x.variable()) ==
00477             static_cast<VarBase*>(y.variable()));
00478   }
00479   template <class ViewA, class VarB>
00480   forceinline bool
00481   shared(const DerivedViewBase<ViewA>& x, 
00482          const VariableViewBase<VarB>& y) {
00483     return (ViewA::varderived() &&
00484             static_cast<VarBase*>(x.variable()) ==
00485             static_cast<VarBase*>(y.variable()));
00486   }
00487   template <class ViewA, class ViewB>
00488   forceinline bool
00489   shared(const DerivedViewBase<ViewA>& x, 
00490          const DerivedViewBase<ViewB>& y) {
00491     return (ViewA::varderived() && ViewB::varderived() &&
00492             static_cast<VarBase*>(x.variable()) ==
00493             static_cast<VarBase*>(y.variable()));
00494   }
00495 
00496 
00497   /*
00498    * Tuple of views
00499    *
00500    */
00501   template <class View, unsigned int n>
00502   forceinline
00503   ViewTuple<View,n>::ViewTuple(void) {}
00504 
00505   template <class View, unsigned int n>
00506   forceinline const View&
00507   ViewTuple<View,n>::operator[](unsigned int i) const {
00508     assert((i>=0)&&(i<n));
00509     return x[i];
00510   }
00511   template <class View, unsigned int n>
00512   forceinline View&
00513   ViewTuple<View,n>::operator[](unsigned int i) {
00514     assert((i>=0)&&(i<n));
00515     return x[i];
00516   }
00517 
00518   template <class View, unsigned int n>
00519   forceinline void
00520   ViewTuple<View,n>::subscribe(Space* home, Propagator* p, PropCond pc) {
00521     for (unsigned int i=0; i<n; i++)
00522       x[i].subscribe(home,p,pc);
00523   }
00524   template <class View, unsigned int n>
00525   forceinline void
00526   ViewTuple<View,n>::cancel(Space* home, Propagator* p, PropCond pc) {
00527     for (unsigned int i=0; i<n; i++)
00528       x[i].cancel(home,p,pc);
00529   }
00530   template <class View, unsigned int n>
00531   forceinline void
00532   ViewTuple<View,n>::update(Space* home, bool share, ViewTuple& xs) {
00533     for (unsigned int i=0; i<n; i++)
00534       x[i].update(home,share,xs.x[i]);
00535   }
00536 
00537   template <class View, unsigned int n>
00538   forceinline bool
00539   same(const ViewTuple<View,n>& x, const ViewTuple<View,n>& y) {
00540     for (unsigned int i=0; i<n; i++)
00541       if (!same(x[i],y[i]))
00542         return false;
00543     return true;
00544   }
00545   template <class View, unsigned int n>
00546   forceinline bool
00547   before(const ViewTuple<View,n>& x, const ViewTuple<View,n>& y){
00548     for (unsigned int i=0; i<n; i++)
00549       if (before(x[i],y[i])) {
00550         return true;
00551       } else if (before(y[i],x[i])) {
00552         return false;
00553       }
00554     return false;
00555   }
00556 
00557 }
00558 
00559 template <class View, unsigned int n>
00560 inline std::ostream&
00561 operator<<(std::ostream& os, const Gecode::ViewTuple<View,n>& xs) {
00562   os << "[";
00563   for (unsigned int i=0; i<n-1; i++)
00564     os << xs[i] << ",";
00565   return os << xs[n-1] << "]";
00566 }
00567   
00568 
00569 // STATISTICS: kernel-other