Generated on Mon May 5 05:53:52 2008 for Gecode by doxygen 1.5.5

view.icc

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: 2008-02-22 06:55:38 +0100 (Fri, 22 Feb 2008) $ by $Author: schulte $
00011  *     $Revision: 6274 $
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 #include <iostream>
00039 
00040 namespace Gecode {
00041 
00046   class ConstViewBase {
00047   public:
00049 
00050 
00051     unsigned int degree(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 
00086 
00087 
00088     static void schedule(Space* home, Propagator* p, ModEvent me);
00090     static ModEvent me(ModEventDelta med);
00092     static ModEventDelta med(ModEvent me);
00094     static ModEvent me_combine(ModEvent me1, ModEvent me2);
00096 
00098 
00099 
00106     void subscribe(Space* home, Propagator* p, PropCond pc, bool process=true);
00108     void cancel(Space* home, Propagator* p, PropCond pc);
00110     void subscribe(Space* home, Advisor* a);
00112     void cancel(Space* home, Advisor* a);
00114   };
00115 
00120 
00121   template <class VarImp>
00122   bool same(const VarViewBase<VarImp>& x, const VarViewBase<VarImp>& y);
00124   template <class VarImp>
00125   bool before(const VarViewBase<VarImp>& x, const VarViewBase<VarImp>& y);
00127 
00128 
00133   template <class View>
00134   class DerivedViewBase {
00135   protected:
00137     View view;
00139     DerivedViewBase(void);
00141     DerivedViewBase(const View& x);
00142 
00143   public:
00145     typedef typename ViewVarImpTraits<View>::VarImp VarImp;
00146 
00148 
00149 
00150     static bool varderived(void);
00152     VarImp* var(void) const;
00154     View base(void) const;
00156     unsigned int degree(void) const;
00158   };
00159 
00160 
00165   bool shared(const ConstViewBase&, const ConstViewBase&);
00170   template <class VarImp>
00171   bool shared(const VarViewBase<VarImp>&, const ConstViewBase&);
00176   template <class ViewA>
00177   bool shared(const DerivedViewBase<ViewA>&, const ConstViewBase&);
00182   template <class VarImp>
00183   bool shared(const ConstViewBase&, const VarViewBase<VarImp>&);
00188   template <class ViewA>
00189   bool shared(const ConstViewBase&, const DerivedViewBase<ViewA>&);
00194   template <class VarImpA, class VarImpB>
00195   bool shared(const VarViewBase<VarImpA>&, const VarViewBase<VarImpB>&);
00200   template <class VarImpA, class ViewB>
00201   bool shared(const VarViewBase<VarImpA>&, const DerivedViewBase<ViewB>&);
00206   template <class ViewA, class VarImpB>
00207   bool shared(const DerivedViewBase<ViewA>&, const VarViewBase<VarImpB>&);
00212   template <class ViewA, class ViewB>
00213   bool shared(const DerivedViewBase<ViewA>&, const DerivedViewBase<ViewB>&);
00214 
00215 
00224   template <class View, unsigned int n>
00225   class ViewTuple {
00226   private:
00228     View x[n];
00229   public:
00231 
00232 
00233     ViewTuple(void);
00235     ViewTuple(Space* home, const Reflection::VarMap& vars,
00236               Reflection::Arg* arg);
00238 
00240 
00241 
00242     View& operator[](unsigned int i);
00244     const View& operator[](unsigned int i) const;
00246 
00248 
00249 
00256     void subscribe(Space* home, Propagator* p, PropCond pc, bool process=true);
00258     void cancel(Space* home, Propagator* p, PropCond pc);
00260     void subscribe(Space* home, Advisor* a);
00262     void cancel(Space* home, Advisor* a);
00264 
00266 
00267 
00268     void update(Space* home, bool share, ViewTuple& xs);
00270 
00272 
00273 
00274     Reflection::Arg*  spec(const Space* home, Reflection::VarMap& m) const;
00275     static Support::Symbol type(void);
00277   };
00278 
00284 
00285   template <class View, unsigned int n>
00286   bool same(const ViewTuple<View,n>& x, const ViewTuple<View,n>& y);
00288   template <class View, unsigned int n>
00289   bool before(const ViewTuple<View,n>& x, const ViewTuple<View,n>& y);
00291 
00292 }
00293 
00298 template <class View, unsigned int n>
00299 std::ostream&
00300 operator<<(std::ostream& os, const Gecode::ViewTuple<View,n>& xy);
00301 
00302 
00303 namespace Gecode {
00304 
00305   /*
00306    * Constant view: has no variable implementation
00307    *
00308    */
00309   forceinline unsigned int
00310   ConstViewBase::degree(void) const {
00311     return 0;
00312   }
00313   forceinline bool
00314   ConstViewBase::varderived(void) {
00315     return false;
00316   }
00317   forceinline VarImpBase*
00318   ConstViewBase::var(void) const {
00319     return NULL;
00320   }
00321 
00322   /*
00323    * Variable view: contains a pointer to a variable implementation
00324    *
00325    */
00326   template <class VarImp>
00327   forceinline
00328   VarViewBase<VarImp>::VarViewBase(void) {}
00329   template <class VarImp>
00330   forceinline
00331   VarViewBase<VarImp>::VarViewBase(VarImp* x)
00332     : varimp(x) {}
00333   template <class VarImp>
00334   forceinline bool
00335   VarViewBase<VarImp>::varderived(void) {
00336     return true;
00337   }
00338   template <class VarImp>
00339   forceinline VarImp*
00340   VarViewBase<VarImp>::var(void) const {
00341     return varimp;
00342   }
00343   template <class VarImp>
00344   forceinline unsigned int
00345   VarViewBase<VarImp>::degree(void) const {
00346     return varimp->degree();
00347   }
00348   template <class VarImp>
00349   forceinline void
00350   VarViewBase<VarImp>::subscribe(Space* home, Propagator* p, PropCond pc,
00351                                  bool process) {
00352     varimp->subscribe(home,p,pc,process);
00353   }
00354   template <class VarImp>
00355   forceinline void
00356   VarViewBase<VarImp>::cancel(Space* home, Propagator* p, PropCond pc) {
00357     varimp->cancel(home,p,pc);
00358   }
00359   template <class VarImp>
00360   forceinline void
00361   VarViewBase<VarImp>::subscribe(Space* home, Advisor* a) {
00362     varimp->subscribe(home,a);
00363   }
00364   template <class VarImp>
00365   forceinline void
00366   VarViewBase<VarImp>::cancel(Space* home, Advisor* a) {
00367     varimp->cancel(home,a);
00368   }
00369   template <class VarImp>
00370   forceinline void
00371   VarViewBase<VarImp>::schedule(Space* home, Propagator* p, ModEvent me) {
00372     return VarImp::schedule(home,p,me);
00373   }
00374   template <class VarImp>
00375   forceinline ModEvent
00376   VarViewBase<VarImp>::me(ModEventDelta med) {
00377     return VarImp::me(med);
00378   }
00379   template <class VarImp>
00380   forceinline ModEventDelta
00381   VarViewBase<VarImp>::med(ModEvent me) {
00382     return VarImp::med(me);
00383   }
00384   template <class VarImp>
00385   forceinline ModEvent
00386   VarViewBase<VarImp>::me_combine(ModEvent me1, ModEvent me2) {
00387     return VarImp::me_combine(me1,me2);
00388   }
00389 
00390   template <class VarImp>
00391   forceinline bool
00392   same(const VarViewBase<VarImp>& x, const VarViewBase<VarImp>& y) {
00393     return x.var() == y.var();
00394   }
00395   template <class VarImp>
00396   forceinline bool
00397   before(const VarViewBase<VarImp>& x, const VarViewBase<VarImp>& y) {
00398     return x.var() < y.var();
00399   }
00400 
00401   /*
00402    * Derived view: contain the base view from which they are derived
00403    *
00404    */
00405 
00406   template <class View>
00407   forceinline
00408   DerivedViewBase<View>::DerivedViewBase(void) {}
00409 
00410   template <class View>
00411   forceinline
00412   DerivedViewBase<View>::DerivedViewBase(const View& x)
00413     : view(x) {}
00414 
00415   template <class View>
00416   forceinline bool
00417   DerivedViewBase<View>::varderived(void) {
00418     return View::varderived();
00419   }
00420 
00421   template <class View>
00422   forceinline typename ViewVarImpTraits<View>::VarImp*
00423   DerivedViewBase<View>::var(void) const {
00424     return view.var();
00425   }
00426 
00427   template <class View>
00428   forceinline View
00429   DerivedViewBase<View>::base(void) const {
00430     return view;
00431   }
00432 
00433   template <class View>
00434   forceinline unsigned int
00435   DerivedViewBase<View>::degree(void) const {
00436     return view.degree();
00437   }
00438 
00439 
00440   /*
00441    * Testing whether two views share the same variable
00442    *
00443    */
00444 
00445   forceinline bool
00446   shared(const ConstViewBase&, const ConstViewBase&) {
00447     return false;
00448   }
00449 
00450   template <class VarImp>
00451   forceinline bool
00452   shared(const VarViewBase<VarImp>&, const ConstViewBase&) {
00453     return false;
00454   }
00455 
00456   template <class View>
00457   forceinline bool
00458   shared(const DerivedViewBase<View>&, const ConstViewBase&) {
00459     return false;
00460   }
00461 
00462   template <class VarImp>
00463   forceinline bool
00464   shared(const ConstViewBase&, const VarViewBase<VarImp>&) {
00465     return false;
00466   }
00467 
00468   template <class View>
00469   forceinline bool
00470   shared(const ConstViewBase&, const DerivedViewBase<View>&) {
00471     return false;
00472   }
00473 
00474   template <class VarImpA, class VarImpB>
00475   forceinline bool
00476   shared(const VarViewBase<VarImpA>& x, const VarViewBase<VarImpB>& y) {
00477     return (static_cast<VarImpBase*>(x.var()) ==
00478             static_cast<VarImpBase*>(y.var()));
00479   }
00480   template <class VarImpA, class ViewB>
00481   forceinline bool
00482   shared(const VarViewBase<VarImpA>& x, const DerivedViewBase<ViewB>& y) {
00483     return (ViewB::varderived() &&
00484             static_cast<VarImpBase*>(x.var()) ==
00485             static_cast<VarImpBase*>(y.var()));
00486   }
00487   template <class ViewA, class VarImpB>
00488   forceinline bool
00489   shared(const DerivedViewBase<ViewA>& x, const VarViewBase<VarImpB>& y) {
00490     return (ViewA::varderived() &&
00491             static_cast<VarImpBase*>(x.var()) ==
00492             static_cast<VarImpBase*>(y.var()));
00493   }
00494   template <class ViewA, class ViewB>
00495   forceinline bool
00496   shared(const DerivedViewBase<ViewA>& x, const DerivedViewBase<ViewB>& y) {
00497     return (ViewA::varderived() && ViewB::varderived() &&
00498             static_cast<VarImpBase*>(x.var()) ==
00499             static_cast<VarImpBase*>(y.var()));
00500   }
00501 
00502 
00503   /*
00504    * Tuple of views
00505    *
00506    */
00507   template <class View, unsigned int n>
00508   forceinline
00509   ViewTuple<View,n>::ViewTuple(void) {}
00510 
00511   template <class View, unsigned int n>
00512   forceinline
00513   ViewTuple<View,n>::ViewTuple(Space* home, const Reflection::VarMap& vars,
00514                                Reflection::Arg* arg) {
00515     Reflection::ArrayArg* a = arg->toArray();
00516     for (int i=n; i--; )
00517       x[i] = View(home, vars, (*a)[i]);
00518   }
00519 
00520   template <class View, unsigned int n>
00521   forceinline const View&
00522   ViewTuple<View,n>::operator[](unsigned int i) const {
00523     assert(i<n);
00524     return x[i];
00525   }
00526   template <class View, unsigned int n>
00527   forceinline View&
00528   ViewTuple<View,n>::operator[](unsigned int i) {
00529     assert(i<n);
00530     return x[i];
00531   }
00532 
00533   template <class View, unsigned int n>
00534   forceinline void
00535   ViewTuple<View,n>::subscribe(Space* home, Propagator* p, PropCond pc,
00536                                bool process) {
00537     for (unsigned int i=0; i<n; i++)
00538       x[i].subscribe(home,p,pc,process);
00539   }
00540   template <class View, unsigned int n>
00541   forceinline void
00542   ViewTuple<View,n>::cancel(Space* home, Propagator* p, PropCond pc) {
00543     for (unsigned int i=0; i<n; i++)
00544       x[i].cancel(home,p,pc);
00545   }
00546   template <class View, unsigned int n>
00547   forceinline void
00548   ViewTuple<View,n>::subscribe(Space* home, Advisor* a) {
00549     for (unsigned int i=0; i<n; i++)
00550       x[i].subscribe(home,a);
00551   }
00552   template <class View, unsigned int n>
00553   forceinline void
00554   ViewTuple<View,n>::cancel(Space* home, Advisor* a) {
00555     for (unsigned int i=0; i<n; i++)
00556       x[i].cancel(home,a);
00557   }
00558   template <class View, unsigned int n>
00559   forceinline void
00560   ViewTuple<View,n>::update(Space* home, bool share, ViewTuple& xs) {
00561     for (unsigned int i=0; i<n; i++)
00562       x[i].update(home,share,xs.x[i]);
00563   }
00564   template <class View, unsigned int n>
00565   forceinline Reflection::Arg*
00566   ViewTuple<View,n>::spec(const Space* home, Reflection::VarMap& vm) const {
00567     Reflection::ArrayArg* a = Reflection::Arg::newArray(n);
00568     for (int i=n; i--; )
00569       (*a)[i] = x[i].spec(home, vm);
00570     return a;
00571   }
00572   template <class View, unsigned int n>
00573   inline Support::Symbol
00574   ViewTuple<View,n>::type(void) {
00576     return "<"+View::type()+","+">";
00577   }
00578 
00579   template <class View, unsigned int n>
00580   forceinline bool
00581   same(const ViewTuple<View,n>& x, const ViewTuple<View,n>& y) {
00582     for (unsigned int i=0; i<n; i++)
00583       if (!same(x[i],y[i]))
00584         return false;
00585     return true;
00586   }
00587   template <class View, unsigned int n>
00588   forceinline bool
00589   before(const ViewTuple<View,n>& x, const ViewTuple<View,n>& y){
00590     for (unsigned int i=0; i<n; i++)
00591       if (before(x[i],y[i])) {
00592         return true;
00593       } else if (before(y[i],x[i])) {
00594         return false;
00595       }
00596     return false;
00597   }
00598 
00599 }
00600 
00601 template <class View, unsigned int n>
00602 inline std::ostream&
00603 operator<<(std::ostream& os, const Gecode::ViewTuple<View,n>& xs) {
00604   os << "[";
00605   for (unsigned int i=0; i<n-1; i++)
00606     os << xs[i] << ",";
00607   return os << xs[n-1] << "]";
00608 }
00609 
00610 
00611 // STATISTICS: kernel-other