/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ /* * Main authors: * Christian Schulte * * Contributing authors: * Samuel Gagnon * * Copyright: * Christian Schulte, 2005 * Samuel Gagnon, 2018 * * 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 */ template class ConstView { public: /// The variable implementation type corresponding to the constant view typedef typename View::VarImpType VarImpType; /// The variable type corresponding to the constant view typedef typename View::VarType VarType; /// \name Generic view information //@{ /// Return degree (number of subscribed propagators and advisors) unsigned int degree(void) const; /// Return accumulated failure count double afc(void) const; /// Return whether this view is derived from a VarImpView static bool varderived(void); /// Return dummy variable implementation of view VarImpType* varimp(void) const; #ifdef GECODE_HAS_CBS /// Return dummy id unsigned int id(void) const; #endif //@} /// \name Domain tests //@{ /// Test whether view is assigned bool assigned(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(const ModEventDelta& med); /// Translate modification event \a me to modification event delta for view static ModEventDelta med(ModEvent me); //@} /// \name Dependencies //@{ /** * \brief Subscribe propagator \a p with propagation condition \a pc to view * * In case \a schedule is false, the propagator is just subscribed but * not scheduled for execution (this must be used when creating * subscriptions during propagation). */ void subscribe(Space& home, Propagator& p, PropCond pc, bool schedule=true); /// Cancel subscription of propagator \a p with propagation condition \a pc to view void cancel(Space& home, Propagator& p, PropCond pc); /// Re-schedule propagator \a p with propagation condition \a pc void reschedule(Space& home, Propagator& p, PropCond pc); /** \brief Subscribe advisor \a a to view * * If \a fail is true, run the advisor also on failure. This * feature is undocumented. */ void subscribe(Space& home, Advisor& a, bool fail=false); /// Cancel subscription of advisor \a a void cancel(Space& home, Advisor& a, bool fail=false); //@} /// \name Delta information for advisors //@{ /// Return modification event static ModEvent modevent(const Delta& d); //@} /// \name Cloning //@{ /// Update this view to be a clone of view \a y void update(Space& home, ConstView& y); //@} /// \name Ordering //@{ /// Whether this view comes before view \a y (arbitray order) bool operator <(const ConstView& y) const; //@} }; /** * \brief Base-class for variable implementation views * \ingroup TaskVarView */ template class VarImpView { public: /// The variable type corresponding to the view typedef Var VarType; /// The variable implementation type corresponding to the view typedef typename Var::VarImpType VarImpType; protected: /// Pointer to variable implementation VarImpType* x; /// Default constructor VarImpView(void); /// Initialize with variable implementation \a y VarImpView(VarImpType* y); /// Set variable implementation to \a y void varimp(VarImpType* y); public: /// \name Generic view information //@{ /// Return whether this view is derived from a VarImpView static bool varderived(void); /// Return variable implementation of view VarImpType* varimp(void) const; /// Return degree (number of subscribed propagators and advisors) unsigned int degree(void) const; /// Return accumulated failure count double afc(void) const; #ifdef GECODE_HAS_CBS /// Return id of implementation of view unsigned int id(void) const; #endif //@} /// \name Domain tests //@{ /// Test whether view is assigned bool assigned(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(const ModEventDelta& med); /// Translate modification event \a me to modification event delta for view static ModEventDelta med(ModEvent me); //@} /// \name Dependencies //@{ /** * \brief Subscribe propagator \a p with propagation condition \a pc to view * * In case \a schedule is false, the propagator is just subscribed but * not scheduled for execution (this must be used when creating * subscriptions during propagation). */ void subscribe(Space& home, Propagator& p, PropCond pc, bool schedule=true); /// Cancel subscription of propagator \a p with propagation condition \a pc to view void cancel(Space& home, Propagator& p, PropCond pc); /// Re-schedule propagator \a p with propagation condition \a pc void reschedule(Space& home, Propagator& p, PropCond pc); /** \brief Subscribe advisor \a a to view * * If \a fail is true, run the advisor also on failure. This * feature is undocumented. */ void subscribe(Space& home, Advisor& a, bool fail=false); /// Cancel subscription of advisor \a a void cancel(Space& home, Advisor& a, bool fail=false); //@} /// \name Delta information for advisors //@{ /// Return modification event static ModEvent modevent(const Delta& d); //@} /// \name Cloning //@{ /// Update this view to be a clone of view \a y void update(Space& home, VarImpView& y); //@} /// \name Ordering //@{ /// Whether this view comes before view \a y (arbitray order) bool operator <(const VarImpView& y) const; //@} }; /** * \brief Base-class for derived views * \ingroup TaskVarView */ template class DerivedView { public: /// The variable implementation type belonging to the \a View typedef typename View::VarImpType VarImpType; /// The variable type belonging to the \a View typedef typename View::VarType VarType; protected: /// View from which this view is derived View x; /// Default constructor DerivedView(void); public: /// Initialize with view \a y DerivedView(const View& y); /// \name Generic view information //@{ /// Return whether this view is derived from a VarImpView static bool varderived(void); /// Return variable implementation of view VarImpType* varimp(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; /// Return accumulated failure count double afc(void) const; #ifdef GECODE_HAS_CBS /// Return id of implementation of view unsigned int id(void) const; #endif //@} /// \name Domain tests //@{ /// Test whether view is assigned bool assigned(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(const ModEventDelta& med); /// Translate modification event \a me to modification event delta for view static ModEventDelta med(ModEvent); //@} /// \name Dependencies //@{ /** * \brief Subscribe propagator \a p with propagation condition \a pc to view * * In case \a schedule is false, the propagator is just subscribed but * not scheduled for execution (this must be used when creating * subscriptions during propagation). */ void subscribe(Space& home, Propagator& p, PropCond pc, bool schedule=true); /// Cancel subscription of propagator \a p with propagation condition \a pc to view void cancel(Space& home, Propagator& p, PropCond pc); /// Re-schedule propagator \a p with propagation condition \a pc void reschedule(Space& home, Propagator& p, PropCond pc); /** \brief Subscribe advisor \a a to view * * If \a fail is true, run the advisor also on failure. This * feature is undocumented. */ void subscribe(Space& home, Advisor& a, bool fail=false); /// Cancel subscription of advisor \a a void cancel(Space& home, Advisor& a, bool fail=false); //@} /// \name Delta information for advisors //@{ /// Return modification event static ModEvent modevent(const Delta& d); //@} /// \name Cloning //@{ /// Update this view to be a clone of view \a y void update(Space& home, DerivedView& y); //@} /// \name Ordering //@{ /// Whether this view comes before view \a y (arbitray order) bool operator <(const DerivedView& y) const; //@} }; /** * \brief Test whether views share same variable * \ingroup TaskVarView */ template bool shared(const ConstView&, const ConstView&); /** * \brief Test whether views share same variable * \ingroup TaskVarView */ template bool shared(const VarImpView&, const ConstView&); /** * \brief Test whether views share same variable * \ingroup TaskVarView */ template bool shared(const DerivedView&, const ConstView&); /** * \brief Test whether views share same variable * \ingroup TaskVarView */ template bool shared(const ConstView&, const VarImpView&); /** * \brief Test whether views share same variable * \ingroup TaskVarView */ template bool shared(const ConstView&, const DerivedView&); /** * \brief Test whether views share same variable * \ingroup TaskVarView */ template bool shared(const VarImpView&, const VarImpView&); /** * \brief Test whether views share same variable * \ingroup TaskVarView */ template bool shared(const VarImpView&, const DerivedView&); /** * \brief Test whether views share same variable * \ingroup TaskVarView */ template bool shared(const DerivedView&, const VarImpView&); /** * \brief Test whether views share same variable * \ingroup TaskVarView */ template bool shared(const DerivedView&, const DerivedView&); /* * Constant view: has no variable implementation * */ template forceinline unsigned int ConstView::degree(void) const { return 0; } template forceinline double ConstView::afc(void) const { return 0.0; } template forceinline bool ConstView::varderived(void) { return false; } template forceinline typename View::VarImpType* ConstView::varimp(void) const { return nullptr; } #ifdef GECODE_HAS_CBS template forceinline unsigned int ConstView::id(void) const { return 0; } #endif template forceinline bool ConstView::assigned(void) const { return true; } template forceinline void ConstView::subscribe(Space& home, Propagator& p, PropCond, bool schedule) { if (schedule) View::schedule(home,p,ME_GEN_ASSIGNED); } template forceinline void ConstView::cancel(Space&, Propagator&, PropCond) { } template forceinline void ConstView::reschedule(Space& home, Propagator& p, PropCond) { View::schedule(home,p,ME_GEN_ASSIGNED); } template forceinline void ConstView::subscribe(Space&, Advisor&, bool) { } template forceinline void ConstView::cancel(Space&, Advisor&, bool) { } template forceinline void ConstView::schedule(Space& home, Propagator& p, ModEvent me) { View::schedule(home,p,me); } template forceinline ModEvent ConstView::me(const ModEventDelta& med) { return View::me(med); } template forceinline ModEventDelta ConstView::med(ModEvent me) { return View::med(me); } template forceinline ModEvent ConstView::modevent(const Delta& d) { (void) d; return ME_GEN_NONE; } template forceinline void ConstView::update(Space&, ConstView&) { } template forceinline bool ConstView::operator <(const ConstView&) const { return true; } /* * Variable view: contains a pointer to a variable implementation * */ template forceinline VarImpView::VarImpView(void) : x(nullptr) {} template forceinline VarImpView::VarImpView(VarImpType* y) : x(y) {} template forceinline void VarImpView::varimp(VarImpType* y) { x=y; } template forceinline bool VarImpView::varderived(void) { return true; } template forceinline typename Var::VarImpType* VarImpView::varimp(void) const { return x; } template forceinline unsigned int VarImpView::degree(void) const { return x->degree(); } template forceinline double VarImpView::afc(void) const { return x->afc(); } #ifdef GECODE_HAS_CBS template forceinline unsigned int VarImpView::id(void) const { return x->id(); } #endif template forceinline bool VarImpView::assigned(void) const { return x->assigned(); } template forceinline void VarImpView::subscribe(Space& home, Propagator& p, PropCond pc, bool schedule) { x->subscribe(home,p,pc,schedule); } template forceinline void VarImpView::cancel(Space& home, Propagator& p, PropCond pc) { x->cancel(home,p,pc); } template forceinline void VarImpView::reschedule(Space& home, Propagator& p, PropCond pc) { x->reschedule(home,p,pc); } template forceinline void VarImpView::subscribe(Space& home, Advisor& a, bool fail) { x->subscribe(home,a,fail); } template forceinline void VarImpView::cancel(Space& home, Advisor& a, bool fail) { x->cancel(home,a,fail); } template forceinline void VarImpView::schedule(Space& home, Propagator& p, ModEvent me) { VarImpType::schedule(home,p,me); } template forceinline ModEvent VarImpView::me(const ModEventDelta& med) { return VarImpType::me(med); } template forceinline ModEventDelta VarImpView::med(ModEvent me) { return VarImpType::med(me); } template forceinline ModEvent VarImpView::modevent(const Delta& d) { return VarImpType::modevent(d); } template forceinline void VarImpView::update(Space& home, VarImpView& y) { x = y.x->copy(home); } template forceinline bool VarImpView::operator <(const VarImpView& y) const { return this->varimp() < y.varimp(); } /* * Derived view: contain the base view from which they are derived * */ template forceinline DerivedView::DerivedView(void) {} template forceinline DerivedView::DerivedView(const View& y) : x(y) {} template forceinline bool DerivedView::varderived(void) { return View::varderived(); } template forceinline typename View::VarImpType* DerivedView::varimp(void) const { return x.varimp(); } template forceinline View DerivedView::base(void) const { return x; } template forceinline unsigned int DerivedView::degree(void) const { return x.degree(); } template forceinline double DerivedView::afc(void) const { return x.afc(); } #ifdef GECODE_HAS_CBS template forceinline unsigned int DerivedView::id(void) const { return x.id(); } #endif template forceinline bool DerivedView::assigned(void) const { return x.assigned(); } template forceinline void DerivedView::schedule(Space& home, Propagator& p, ModEvent me) { return View::schedule(home,p,me); } template forceinline ModEvent DerivedView::me(const ModEventDelta& med) { return View::me(med); } template forceinline ModEventDelta DerivedView::med(ModEvent me) { return View::med(me); } template forceinline void DerivedView::subscribe(Space& home, Propagator& p, PropCond pc, bool schedule) { x.subscribe(home,p,pc,schedule); } template forceinline void DerivedView::cancel(Space& home, Propagator& p, PropCond pc) { x.cancel(home,p,pc); } template forceinline void DerivedView::reschedule(Space& home, Propagator& p, PropCond pc) { x.reschedule(home,p,pc); } template forceinline void DerivedView::subscribe(Space& home, Advisor& a, bool fail) { x.subscribe(home,a,fail); } template forceinline void DerivedView::cancel(Space& home, Advisor& a, bool fail) { x.cancel(home,a,fail); } template forceinline ModEvent DerivedView::modevent(const Delta& d) { return View::modevent(d); } template forceinline void DerivedView::update(Space& home, DerivedView& y) { x.update(home,y.x); } template forceinline bool DerivedView::operator <(const DerivedView& y) const { return base() < y.base(); } /* * Tests whether two views are the same * */ /// Test whether two views are the same template forceinline bool operator ==(const ConstView&, const ConstView&) { return false; } /// Test whether two views are the same template forceinline bool operator ==(const ConstView&, const VarImpView&) { return false; } /// Test whether two views are the same template forceinline bool operator ==(const ConstView&, const DerivedView&) { return false; } /// Test whether two views are the same template forceinline bool operator ==(const VarImpView&, const ConstView&) { return false; } /// Test whether two views are the same template forceinline bool operator ==(const VarImpView&, const DerivedView&) { return false; } /// Test whether two views are the same template forceinline bool operator ==(const DerivedView&, const ConstView&) { return false; } /// Test whether two views are the same template forceinline bool operator ==(const DerivedView&, const VarImpView&) { return false; } /// Test whether two views are not the same template forceinline bool operator ==(const DerivedView&, const DerivedView&) { return false; } /// Test whether two views are the same template forceinline bool operator ==(const VarImpView& x, const VarImpView& y) { return x.varimp() == y.varimp(); } /// Test whether two views are not the same template forceinline bool operator !=(const ConstView&, const ConstView&) { return true; } /// Test whether two views are not the same template forceinline bool operator !=(const ConstView&, const VarImpView&) { return true; } /// Test whether two views are not the same template forceinline bool operator !=(const ConstView&, const DerivedView&) { return true; } /// Test whether two views are not the same template forceinline bool operator !=(const VarImpView&, const ConstView&) { return true; } /// Test whether two views are not the same template forceinline bool operator !=(const VarImpView&, const DerivedView&) { return true; } /// Test whether two views are the same template forceinline bool operator !=(const DerivedView&, const ConstView&) { return true; } /// Test whether two views are not the same template forceinline bool operator !=(const DerivedView&, const VarImpView&) { return true; } /// Test whether two views are not the same template forceinline bool operator !=(const DerivedView&, const DerivedView&) { return true; } /// Test whether two views are not the same template forceinline bool operator !=(const VarImpView& x, const VarImpView& y) { return x.varimp() != y.varimp(); } /* * Testing whether two views share the same variable * */ template forceinline bool shared(const ConstView&, const ConstView&) { return false; } template forceinline bool shared(const VarImpView&, const ConstView&) { return false; } template forceinline bool shared(const DerivedView&, const ConstView&) { return false; } template forceinline bool shared(const ConstView&, const VarImpView&) { return false; } template forceinline bool shared(const ConstView&, const DerivedView&) { return false; } template forceinline bool shared(const VarImpView& x, const VarImpView& y) { return (static_cast(x.varimp()) == static_cast(y.varimp())); } template forceinline bool shared(const VarImpView& x, const DerivedView& y) { return (View::varderived() && static_cast(x.varimp()) == static_cast(y.varimp())); } template forceinline bool shared(const DerivedView& x, const VarImpView& y) { return (View::varderived() && static_cast(x.varimp()) == static_cast(y.varimp())); } template forceinline bool shared(const DerivedView& x, const DerivedView& y) { return (ViewA::varderived() && ViewB::varderived() && static_cast(x.varimp()) == static_cast(y.varimp())); } } // STATISTICS: kernel-var