Generated on Wed Jan 4 17:49:09 2006 for Gecode by doxygen 1.4.6

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: 2005-11-28 22:00:24 +0100 (Mon, 28 Nov 2005) $ by $Author: schulte $
00010  *     $Revision: 2658 $
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 
00044   private:
00045     static void* operator new(size_t);
00046     static void operator delete(void*);
00047   };
00048 
00049 
00050 
00055   template <class Var>
00056   class VariableViewBase {
00057   protected:
00059     Var* var;
00061     VariableViewBase(void);
00063     VariableViewBase(Var* x);
00064 
00065   public:
00067 
00068 
00069     static bool varderived(void);
00071     Var* variable(void) const;
00073     bool modified(void) const;
00075     unsigned int degree(void) const;
00077 
00079 
00080 
00081     static ModEvent     pme(const Propagator* p);
00083     static PropModEvent pme(ModEvent me);
00085     static ModEvent     combine(ModEvent me1, ModEvent me2);
00087 
00089 
00090 
00091     void subscribe(Space*, Propagator* p, PropCond pc);
00093     void cancel(Propagator* p, PropCond pc);
00095   private:
00096     static void* operator new(size_t);
00097     static void operator delete(void*);
00098   };
00099 
00104 
00105   template <class Var>
00106   bool same(const VariableViewBase<Var>& x, const VariableViewBase<Var>& y);
00108   template <class Var>
00109   bool before(const VariableViewBase<Var>& x, const VariableViewBase<Var>& y);
00111 
00112 
00113 
00121   template <class View>
00122   class ViewVarTraits {};
00123 
00128   template <class View>
00129   class DerivedViewBase {
00130   protected:
00132     View view;
00134     DerivedViewBase(void);
00136     DerivedViewBase(const View& x);
00137 
00138   public:
00140     typedef typename ViewVarTraits<View>::Var Var;
00141 
00143 
00144 
00145     static bool varderived(void);
00147     Var* variable(void) const;
00149     View base(void) const;
00151     bool modified(void) const;
00153     unsigned int degree(void) const;
00155 
00156   private:
00157     static void* operator new(size_t);
00158     static void operator delete(void*);
00159   };
00160 
00161 
00166   bool shared(const ConstantViewBase&, const ConstantViewBase&);
00171   template <class Var>
00172   bool shared(const VariableViewBase<Var>&, const ConstantViewBase&);
00177   template <class ViewA>
00178   bool shared(const DerivedViewBase<ViewA>&, const ConstantViewBase&);
00183   template <class Var>
00184   bool shared(const ConstantViewBase&, const VariableViewBase<Var>&);
00189   template <class ViewA>
00190   bool shared(const ConstantViewBase&, const DerivedViewBase<ViewA>&);
00195   template <class VarA, class VarB>
00196   bool shared(const VariableViewBase<VarA>&, 
00197               const VariableViewBase<VarB>&);
00202   template <class VarA, class ViewB>
00203   bool shared(const VariableViewBase<VarA>&, 
00204               const DerivedViewBase<ViewB>&);
00209   template <class ViewA, class VarB>
00210   bool shared(const DerivedViewBase<ViewA>&, 
00211               const VariableViewBase<VarB>&);
00216   template <class ViewA, class ViewB>
00217   bool shared(const DerivedViewBase<ViewA>&, 
00218               const DerivedViewBase<ViewB>&);
00219 
00220 
00229   template <class View, unsigned int n>
00230   class ViewTuple {
00231   private:
00233     View x[n];
00234   public:
00236 
00237 
00238     ViewTuple(void);
00240      
00242 
00243 
00244     View& operator[](unsigned int i);
00246     const View& operator[](unsigned int i) const;
00248 
00250 
00251 
00252     void subscribe(Space* home, Propagator* p, PropCond pc);
00254     void cancel(Propagator* p, PropCond pc);
00256 
00258 
00259 
00260     void update(Space* home, bool share, ViewTuple& xs);
00262 
00263   private:
00264     static void* operator new(size_t);
00265     static void operator delete(void*);
00266   };
00267 
00273 
00274   template <class View, unsigned int n>
00275   bool same(const ViewTuple<View,n>& x, const ViewTuple<View,n>& y);
00277   template <class View, unsigned int n>
00278   bool before(const ViewTuple<View,n>& x, const ViewTuple<View,n>& y);
00280 
00281 }
00282 
00287 template <class View, unsigned int n>
00288 std::ostream&
00289 operator<<(std::ostream& os, const Gecode::ViewTuple<View,n>& xy);
00290   
00291 
00292 namespace Gecode {  
00293 
00294   /*
00295    * Constant view: has no variable implementation
00296    *
00297    */
00298   forceinline bool
00299   ConstantViewBase::modified(void) const {
00300     return true;
00301   }
00302   forceinline unsigned int
00303   ConstantViewBase::degree(void) const {
00304     return 0;
00305   }
00306   forceinline bool
00307   ConstantViewBase::varderived(void) {
00308     return false;
00309   }
00310   forceinline VarBase*
00311   ConstantViewBase::variable(void) const {
00312     return NULL;
00313   }
00314 
00315 
00316   /*
00317    * Variable view: contains a pointer to a variable implementation
00318    *
00319    */
00320   template <class Var>
00321   forceinline
00322   VariableViewBase<Var>::VariableViewBase(void) {}
00323   template <class Var>
00324   forceinline
00325   VariableViewBase<Var>::VariableViewBase(Var* x)
00326     : var(x) {}
00327   template <class Var>
00328   forceinline bool
00329   VariableViewBase<Var>::varderived(void) {
00330     return true;
00331   }
00332   template <class Var>
00333   forceinline Var*
00334   VariableViewBase<Var>::variable(void) const {
00335     return var;
00336   }
00337   template <class Var>
00338   forceinline bool
00339   VariableViewBase<Var>::modified(void) const {
00340     return var->modified();
00341   }
00342   template <class Var>
00343   forceinline unsigned int
00344   VariableViewBase<Var>::degree(void) const {
00345     return var->degree();
00346   }
00347   template <class Var>
00348   forceinline void
00349   VariableViewBase<Var>::subscribe(Space* home, Propagator* p, PropCond pc) {
00350     var->subscribe(home,p,pc);
00351   }
00352   template <class Var>
00353   forceinline void
00354   VariableViewBase<Var>::cancel(Propagator* p, PropCond pc) {
00355     var->cancel(p,pc);
00356   }
00357   template <class Var>
00358   forceinline ModEvent
00359   VariableViewBase<Var>::pme(const Propagator* p) {
00360     return Var::pme(p);
00361   }
00362   template <class Var>
00363   forceinline PropModEvent
00364   VariableViewBase<Var>::pme(ModEvent me) {
00365     return Var::pme(me);
00366   }
00367   template <class Var>
00368   forceinline ModEvent
00369   VariableViewBase<Var>::combine(ModEvent me1, ModEvent me2) {
00370     return Var::combine(me1,me2);
00371   }
00372 
00373   template <class Var>
00374   forceinline bool 
00375   same(const VariableViewBase<Var>& x, const VariableViewBase<Var>& y) {
00376     return x.variable() == y.variable();
00377   }
00378   template <class Var>
00379   forceinline bool 
00380   before(const VariableViewBase<Var>& x, const VariableViewBase<Var>& y) {
00381     return x.variable() < y.variable();    
00382   }
00383 
00384   /*
00385    * Derived view: contain the base view from which they are derived
00386    *
00387    */
00388 
00389   template <class View>
00390   forceinline
00391   DerivedViewBase<View>::DerivedViewBase(void) {}
00392 
00393   template <class View>
00394   forceinline
00395   DerivedViewBase<View>::DerivedViewBase(const View& x)
00396     : view(x) {}
00397 
00398   template <class View>
00399   forceinline bool
00400   DerivedViewBase<View>::varderived(void) {
00401     return View::varderived();
00402   }
00403 
00404   template <class View>
00405   forceinline typename ViewVarTraits<View>::Var*
00406   DerivedViewBase<View>::variable(void) const {
00407     return view.variable();
00408   }
00409 
00410   template <class View>
00411   forceinline View
00412   DerivedViewBase<View>::base(void) const {
00413     return view;
00414   }
00415 
00416   template <class View>
00417   forceinline bool
00418   DerivedViewBase<View>::modified(void) const {
00419     return view.modified();
00420   }
00421 
00422   template <class View>
00423   forceinline unsigned int
00424   DerivedViewBase<View>::degree(void) const {
00425     return view.degree();
00426   }
00427 
00428 
00429   /*
00430    * Testing whether two views share the same variable
00431    *
00432    */
00433 
00434   forceinline bool
00435   shared(const ConstantViewBase&, const ConstantViewBase&) {
00436     return false;
00437   }
00438 
00439   template <class Var>
00440   forceinline bool
00441   shared(const VariableViewBase<Var>&, const ConstantViewBase&) {
00442     return false;
00443   }
00444 
00445   template <class View>
00446   forceinline bool
00447   shared(const DerivedViewBase<View>&, const ConstantViewBase&) {
00448     return false;
00449   }
00450 
00451   template <class Var>
00452   forceinline bool
00453   shared(const ConstantViewBase&, const VariableViewBase<Var>&) {
00454     return false;
00455   }
00456 
00457   template <class View>
00458   forceinline bool
00459   shared(const ConstantViewBase&, const DerivedViewBase<View>&) {
00460     return false;
00461   }
00462 
00463   template <class VarA, class VarB>
00464   forceinline bool
00465   shared(const VariableViewBase<VarA>& x, 
00466          const VariableViewBase<VarB>& y) {
00467     return (static_cast<VarBase*>(x.variable()) ==
00468             static_cast<VarBase*>(y.variable()));
00469   }
00470   template <class VarA, class ViewB>
00471   forceinline bool
00472   shared(const VariableViewBase<VarA>& x, 
00473          const DerivedViewBase<ViewB>& y) {
00474     return (ViewB::varderived() &&
00475             static_cast<VarBase*>(x.variable()) ==
00476             static_cast<VarBase*>(y.variable()));
00477   }
00478   template <class ViewA, class VarB>
00479   forceinline bool
00480   shared(const DerivedViewBase<ViewA>& x, 
00481          const VariableViewBase<VarB>& y) {
00482     return (ViewA::varderived() &&
00483             static_cast<VarBase*>(x.variable()) ==
00484             static_cast<VarBase*>(y.variable()));
00485   }
00486   template <class ViewA, class ViewB>
00487   forceinline bool
00488   shared(const DerivedViewBase<ViewA>& x, 
00489          const DerivedViewBase<ViewB>& y) {
00490     return (ViewA::varderived() && ViewB::varderived() &&
00491             static_cast<VarBase*>(x.variable()) ==
00492             static_cast<VarBase*>(y.variable()));
00493   }
00494 
00495 
00496   /*
00497    * Tuple of views
00498    *
00499    */
00500   template <class View, unsigned int n>
00501   forceinline
00502   ViewTuple<View,n>::ViewTuple(void) {}
00503 
00504   template <class View, unsigned int n>
00505   forceinline const View&
00506   ViewTuple<View,n>::operator[](unsigned int i) const {
00507     assert((i>=0)&&(i<n));
00508     return x[i];
00509   }
00510   template <class View, unsigned int n>
00511   forceinline View&
00512   ViewTuple<View,n>::operator[](unsigned int i) {
00513     assert((i>=0)&&(i<n));
00514     return x[i];
00515   }
00516 
00517   template <class View, unsigned int n>
00518   forceinline void
00519   ViewTuple<View,n>::subscribe(Space* home, Propagator* p, PropCond pc) {
00520     for (unsigned int i=0; i<n; i++)
00521       x[i].subscribe(home,p,pc);
00522   }
00523   template <class View, unsigned int n>
00524   forceinline void
00525   ViewTuple<View,n>::cancel(Propagator* p, PropCond pc) {
00526     for (unsigned int i=0; i<n; i++)
00527       x[i].cancel(p,pc);
00528   }
00529   template <class View, unsigned int n>
00530   forceinline void
00531   ViewTuple<View,n>::update(Space* home, bool share, ViewTuple& xs) {
00532     for (unsigned int i=0; i<n; i++)
00533       x[i].update(home,share,xs.x[i]);
00534   }
00535 
00536   template <class View, unsigned int n>
00537   forceinline bool
00538   same(const ViewTuple<View,n>& x, const ViewTuple<View,n>& y) {
00539     for (unsigned int i=0; i<n; i++)
00540       if (!same(x[i],y[i]))
00541         return false;
00542     return true;
00543   }
00544   template <class View, unsigned int n>
00545   forceinline bool
00546   before(const ViewTuple<View,n>& x, const ViewTuple<View,n>& y){
00547     for (unsigned int i=0; i<n; i++)
00548       if (before(x[i],y[i])) {
00549         return true;
00550       } else if (before(y[i],x[i])) {
00551         return false;
00552       }
00553     return false;
00554   }
00555 
00556 }
00557 
00558 template <class View, unsigned int n>
00559 inline std::ostream&
00560 operator<<(std::ostream& os, const Gecode::ViewTuple<View,n>& xs) {
00561   os << "[";
00562   for (unsigned int i=0; i<n-1; i++)
00563     os << xs[i] << ",";
00564   return os << xs[n-1] << "]";
00565 }
00566   
00567 
00568 // STATISTICS: kernel-other