/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ /* * Main authors: * Christian Schulte * * Copyright: * Christian Schulte, 2005 * * Last modified: * $Date: 2008-07-11 09:33:32 +0200 (Fri, 11 Jul 2008) $ by $Author: tack $ * $Revision: 7290 $ * * This file is part of Gecode, the generic constraint * development environment: * http://www.gecode.org * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * */ namespace Gecode { /** * \brief Base-class for constant views * \ingroup TaskVarView */ class ConstViewBase { public: /// \name Generic view information //@{ /// Return degree (number of subscribed propagators and advisors) unsigned int degree(void) const; /// Return whether this view is derived from a VarViewBase static bool varderived(void); /// Return dummy variable implementation of view VarImpBase* var(void) const; //@} }; /** * \brief Base-class for variable views * \ingroup TaskVarView */ template class VarViewBase { protected: /// Pointer to variable implementation VarImp* varimp; /// Default constructor VarViewBase(void); /// Initialize with variable implementation x VarViewBase(VarImp* x); public: /// \name Generic view information //@{ /// Return whether this view is derived from a VarViewBase static bool varderived(void); /// Return variable implementation of view VarImp* var(void) const; /// Return degree (number of subscribed propagators and advisors) unsigned int degree(void) const; //@} /// \name View-dependent propagator support //@{ /// Schedule propagator \a p with modification event \a me static void schedule(Space* home, Propagator* p, ModEvent me); /// Return modification event for view type in \a med static ModEvent me(ModEventDelta med); /// Translate modification event \a me to modification event delta for view static ModEventDelta med(ModEvent me); /// Combine modifications events \a me1 and \a me2 static ModEvent me_combine(ModEvent me1, ModEvent me2); //@} /// \name Dependencies //@{ /** * \brief Subscribe propagator \a p with propagation condition \a pc to view * * In case \a process is false, the propagator is just subscribed but * not processed for execution (this must be used when creating * subscriptions during propagation). */ void subscribe(Space* home, Propagator* p, PropCond pc, bool process=true); /// Cancel subscription of propagator \a p with propagation condition \a pc to view void cancel(Space* home, Propagator* p, PropCond pc); /// Subscribe advisor \a a to view void subscribe(Space* home, Advisor* a); /// Cancel subscription of advisor \a a void cancel(Space* home, Advisor* a); //@} }; /** \name View comparison * \relates VarViewBase */ //@{ /// Test whether views \a x and \a y are the same template bool same(const VarViewBase& x, const VarViewBase& y); /// Test whether view \a x comes before \a y (arbitrary order) template bool before(const VarViewBase& x, const VarViewBase& y); //@} /** * \brief Base-class for derived views * \ingroup TaskVarView */ template class DerivedViewBase { protected: /// View from which this view is derived View view; /// Default constructor DerivedViewBase(void); /// Initialize with view x DerivedViewBase(const View& x); public: /// The variable type belonging to the \a View typedef typename ViewVarImpTraits::VarImp VarImp; /// \name Generic view information //@{ /// Return whether this view is derived from a VarViewBase static bool varderived(void); /// Return variable implementation of view VarImp* var(void) const; /// Return view from which this view is derived View base(void) const; /// Return degree (number of subscribed propagators) unsigned int degree(void) const; //@} }; /** * \brief Test whether views share same variable * \ingroup TaskVarView */ bool shared(const ConstViewBase&, const ConstViewBase&); /** * \brief Test whether views share same variable * \ingroup TaskVarView */ template bool shared(const VarViewBase&, const ConstViewBase&); /** * \brief Test whether views share same variable * \ingroup TaskVarView */ template bool shared(const DerivedViewBase&, const ConstViewBase&); /** * \brief Test whether views share same variable * \ingroup TaskVarView */ template bool shared(const ConstViewBase&, const VarViewBase&); /** * \brief Test whether views share same variable * \ingroup TaskVarView */ template bool shared(const ConstViewBase&, const DerivedViewBase&); /** * \brief Test whether views share same variable * \ingroup TaskVarView */ template bool shared(const VarViewBase&, const VarViewBase&); /** * \brief Test whether views share same variable * \ingroup TaskVarView */ template bool shared(const VarViewBase&, const DerivedViewBase&); /** * \brief Test whether views share same variable * \ingroup TaskVarView */ template bool shared(const DerivedViewBase&, const VarViewBase&); /** * \brief Test whether views share same variable * \ingroup TaskVarView */ template bool shared(const DerivedViewBase&, const DerivedViewBase&); /* * Constant view: has no variable implementation * */ forceinline unsigned int ConstViewBase::degree(void) const { return 0; } forceinline bool ConstViewBase::varderived(void) { return false; } forceinline VarImpBase* ConstViewBase::var(void) const { return NULL; } /* * Variable view: contains a pointer to a variable implementation * */ template forceinline VarViewBase::VarViewBase(void) {} template forceinline VarViewBase::VarViewBase(VarImp* x) : varimp(x) {} template forceinline bool VarViewBase::varderived(void) { return true; } template forceinline VarImp* VarViewBase::var(void) const { return varimp; } template forceinline unsigned int VarViewBase::degree(void) const { return varimp->degree(); } template forceinline void VarViewBase::subscribe(Space* home, Propagator* p, PropCond pc, bool process) { varimp->subscribe(home,p,pc,process); } template forceinline void VarViewBase::cancel(Space* home, Propagator* p, PropCond pc) { varimp->cancel(home,p,pc); } template forceinline void VarViewBase::subscribe(Space* home, Advisor* a) { varimp->subscribe(home,a); } template forceinline void VarViewBase::cancel(Space* home, Advisor* a) { varimp->cancel(home,a); } template forceinline void VarViewBase::schedule(Space* home, Propagator* p, ModEvent me) { return VarImp::schedule(home,p,me); } template forceinline ModEvent VarViewBase::me(ModEventDelta med) { return VarImp::me(med); } template forceinline ModEventDelta VarViewBase::med(ModEvent me) { return VarImp::med(me); } template forceinline ModEvent VarViewBase::me_combine(ModEvent me1, ModEvent me2) { return VarImp::me_combine(me1,me2); } template forceinline bool same(const VarViewBase& x, const VarViewBase& y) { return x.var() == y.var(); } template forceinline bool before(const VarViewBase& x, const VarViewBase& y) { return x.var() < y.var(); } /* * Derived view: contain the base view from which they are derived * */ template forceinline DerivedViewBase::DerivedViewBase(void) {} template forceinline DerivedViewBase::DerivedViewBase(const View& x) : view(x) {} template forceinline bool DerivedViewBase::varderived(void) { return View::varderived(); } template forceinline typename ViewVarImpTraits::VarImp* DerivedViewBase::var(void) const { return view.var(); } template forceinline View DerivedViewBase::base(void) const { return view; } template forceinline unsigned int DerivedViewBase::degree(void) const { return view.degree(); } /* * Testing whether two views share the same variable * */ forceinline bool shared(const ConstViewBase&, const ConstViewBase&) { return false; } template forceinline bool shared(const VarViewBase&, const ConstViewBase&) { return false; } template forceinline bool shared(const DerivedViewBase&, const ConstViewBase&) { return false; } template forceinline bool shared(const ConstViewBase&, const VarViewBase&) { return false; } template forceinline bool shared(const ConstViewBase&, const DerivedViewBase&) { return false; } template forceinline bool shared(const VarViewBase& x, const VarViewBase& y) { return (static_cast(x.var()) == static_cast(y.var())); } template forceinline bool shared(const VarViewBase& x, const DerivedViewBase& y) { return (ViewB::varderived() && static_cast(x.var()) == static_cast(y.var())); } template forceinline bool shared(const DerivedViewBase& x, const VarViewBase& y) { return (ViewA::varderived() && static_cast(x.var()) == static_cast(y.var())); } template forceinline bool shared(const DerivedViewBase& x, const DerivedViewBase& y) { return (ViewA::varderived() && ViewB::varderived() && static_cast(x.var()) == static_cast(y.var())); } } // STATISTICS: kernel-other