Something went wrong. Try again.
This repository has no description
Something went wrong. Try again.
25 kB · 867 lines
C++
at develop
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- *//* * Main authors: * Christian Schulte <schulte@gecode.org> * * Contributing authors: * Samuel Gagnon <samuel.gagnon92@gmail.com> * * 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 View> 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 Var> 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<Var>& 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 View> 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<View>& y); //@}
/// \name Ordering //@{ /// Whether this view comes before view \a y (arbitray order) bool operator <(const DerivedView<View>& y) const; //@} };
/** * \brief Test whether views share same variable * \ingroup TaskVarView */ template<class ViewA, class ViewB> bool shared(const ConstView<ViewA>&, const ConstView<ViewB>&); /** * \brief Test whether views share same variable * \ingroup TaskVarView */ template<class Var, class View> bool shared(const VarImpView<Var>&, const ConstView<View>&); /** * \brief Test whether views share same variable * \ingroup TaskVarView */ template<class ViewA, class ViewB> bool shared(const DerivedView<ViewA>&, const ConstView<ViewB>&); /** * \brief Test whether views share same variable * \ingroup TaskVarView */ template<class View, class Var> bool shared(const ConstView<View>&, const VarImpView<Var>&); /** * \brief Test whether views share same variable * \ingroup TaskVarView */ template<class ViewA, class ViewB> bool shared(const ConstView<ViewA>&, const DerivedView<ViewB>&); /** * \brief Test whether views share same variable * \ingroup TaskVarView */ template<class VarA, class VarB> bool shared(const VarImpView<VarA>&, const VarImpView<VarB>&); /** * \brief Test whether views share same variable * \ingroup TaskVarView */ template<class Var, class View> bool shared(const VarImpView<Var>&, const DerivedView<View>&); /** * \brief Test whether views share same variable * \ingroup TaskVarView */ template<class View, class Var> bool shared(const DerivedView<View>&, const VarImpView<Var>&); /** * \brief Test whether views share same variable * \ingroup TaskVarView */ template<class ViewA, class ViewB> bool shared(const DerivedView<ViewA>&, const DerivedView<ViewB>&);
/* * Constant view: has no variable implementation * */ template<class View> forceinline unsigned int ConstView<View>::degree(void) const { return 0; } template<class View> forceinline double ConstView<View>::afc(void) const { return 0.0; } template<class View> forceinline bool ConstView<View>::varderived(void) { return false; } template<class View> forceinline typename View::VarImpType* ConstView<View>::varimp(void) const { return nullptr; }#ifdef GECODE_HAS_CBS template<class View> forceinline unsigned int ConstView<View>::id(void) const { return 0; }#endif template<class View> forceinline bool ConstView<View>::assigned(void) const { return true; } template<class View> forceinline void ConstView<View>::subscribe(Space& home, Propagator& p, PropCond, bool schedule) { if (schedule) View::schedule(home,p,ME_GEN_ASSIGNED); } template<class View> forceinline void ConstView<View>::cancel(Space&, Propagator&, PropCond) { } template<class View> forceinline void ConstView<View>::reschedule(Space& home, Propagator& p, PropCond) { View::schedule(home,p,ME_GEN_ASSIGNED); } template<class View> forceinline void ConstView<View>::subscribe(Space&, Advisor&, bool) { } template<class View> forceinline void ConstView<View>::cancel(Space&, Advisor&, bool) { } template<class View> forceinline void ConstView<View>::schedule(Space& home, Propagator& p, ModEvent me) { View::schedule(home,p,me); } template<class View> forceinline ModEvent ConstView<View>::me(const ModEventDelta& med) { return View::me(med); } template<class View> forceinline ModEventDelta ConstView<View>::med(ModEvent me) { return View::med(me); } template<class View> forceinline ModEvent ConstView<View>::modevent(const Delta& d) { (void) d; return ME_GEN_NONE; } template<class View> forceinline void ConstView<View>::update(Space&, ConstView<View>&) { } template<class View> forceinline bool ConstView<View>::operator <(const ConstView<View>&) const { return true; }
/* * Variable view: contains a pointer to a variable implementation * */ template<class Var> forceinline VarImpView<Var>::VarImpView(void) : x(nullptr) {} template<class Var> forceinline VarImpView<Var>::VarImpView(VarImpType* y) : x(y) {} template<class Var> forceinline void VarImpView<Var>::varimp(VarImpType* y) { x=y; } template<class Var> forceinline bool VarImpView<Var>::varderived(void) { return true; } template<class Var> forceinline typename Var::VarImpType* VarImpView<Var>::varimp(void) const { return x; } template<class Var> forceinline unsigned int VarImpView<Var>::degree(void) const { return x->degree(); } template<class Var> forceinline double VarImpView<Var>::afc(void) const { return x->afc(); }#ifdef GECODE_HAS_CBS template<class Var> forceinline unsigned int VarImpView<Var>::id(void) const { return x->id(); }#endif template<class Var> forceinline bool VarImpView<Var>::assigned(void) const { return x->assigned(); } template<class Var> forceinline void VarImpView<Var>::subscribe(Space& home, Propagator& p, PropCond pc, bool schedule) { x->subscribe(home,p,pc,schedule); } template<class Var> forceinline void VarImpView<Var>::cancel(Space& home, Propagator& p, PropCond pc) { x->cancel(home,p,pc); } template<class Var> forceinline void VarImpView<Var>::reschedule(Space& home, Propagator& p, PropCond pc) { x->reschedule(home,p,pc); } template<class Var> forceinline void VarImpView<Var>::subscribe(Space& home, Advisor& a, bool fail) { x->subscribe(home,a,fail); } template<class Var> forceinline void VarImpView<Var>::cancel(Space& home, Advisor& a, bool fail) { x->cancel(home,a,fail); } template<class Var> forceinline void VarImpView<Var>::schedule(Space& home, Propagator& p, ModEvent me) { VarImpType::schedule(home,p,me); } template<class Var> forceinline ModEvent VarImpView<Var>::me(const ModEventDelta& med) { return VarImpType::me(med); } template<class Var> forceinline ModEventDelta VarImpView<Var>::med(ModEvent me) { return VarImpType::med(me); } template<class Var> forceinline ModEvent VarImpView<Var>::modevent(const Delta& d) { return VarImpType::modevent(d); } template<class Var> forceinline void VarImpView<Var>::update(Space& home, VarImpView<Var>& y) { x = y.x->copy(home); } template<class Var> forceinline bool VarImpView<Var>::operator <(const VarImpView<Var>& y) const { return this->varimp() < y.varimp(); }
/* * Derived view: contain the base view from which they are derived * */
template<class View> forceinline DerivedView<View>::DerivedView(void) {}
template<class View> forceinline DerivedView<View>::DerivedView(const View& y) : x(y) {}
template<class View> forceinline bool DerivedView<View>::varderived(void) { return View::varderived(); }
template<class View> forceinline typename View::VarImpType* DerivedView<View>::varimp(void) const { return x.varimp(); }
template<class View> forceinline View DerivedView<View>::base(void) const { return x; }
template<class View> forceinline unsigned int DerivedView<View>::degree(void) const { return x.degree(); } template<class View> forceinline double DerivedView<View>::afc(void) const { return x.afc(); }#ifdef GECODE_HAS_CBS template<class View> forceinline unsigned int DerivedView<View>::id(void) const { return x.id(); }#endif template<class View> forceinline bool DerivedView<View>::assigned(void) const { return x.assigned(); }
template<class View> forceinline void DerivedView<View>::schedule(Space& home, Propagator& p, ModEvent me) { return View::schedule(home,p,me); } template<class View> forceinline ModEvent DerivedView<View>::me(const ModEventDelta& med) { return View::me(med); } template<class View> forceinline ModEventDelta DerivedView<View>::med(ModEvent me) { return View::med(me); }
template<class View> forceinline void DerivedView<View>::subscribe(Space& home, Propagator& p, PropCond pc, bool schedule) { x.subscribe(home,p,pc,schedule); } template<class View> forceinline void DerivedView<View>::cancel(Space& home, Propagator& p, PropCond pc) { x.cancel(home,p,pc); } template<class View> forceinline void DerivedView<View>::reschedule(Space& home, Propagator& p, PropCond pc) { x.reschedule(home,p,pc); } template<class View> forceinline void DerivedView<View>::subscribe(Space& home, Advisor& a, bool fail) { x.subscribe(home,a,fail); } template<class View> forceinline void DerivedView<View>::cancel(Space& home, Advisor& a, bool fail) { x.cancel(home,a,fail); } template<class View> forceinline ModEvent DerivedView<View>::modevent(const Delta& d) { return View::modevent(d); } template<class View> forceinline void DerivedView<View>::update(Space& home, DerivedView<View>& y) { x.update(home,y.x); } template<class View> forceinline bool DerivedView<View>::operator <(const DerivedView<View>& y) const { return base() < y.base(); }
/* * Tests whether two views are the same * */
/// Test whether two views are the same template<class ViewA, class ViewB> forceinline bool operator ==(const ConstView<ViewA>&, const ConstView<ViewB>&) { return false; } /// Test whether two views are the same template<class Var, class View> forceinline bool operator ==(const ConstView<View>&, const VarImpView<Var>&) { return false; } /// Test whether two views are the same template<class ViewA, class ViewB> forceinline bool operator ==(const ConstView<ViewA>&, const DerivedView<ViewB>&) { return false; } /// Test whether two views are the same template<class Var, class View> forceinline bool operator ==(const VarImpView<Var>&, const ConstView<View>&) { return false; } /// Test whether two views are the same template<class Var, class View> forceinline bool operator ==(const VarImpView<Var>&, const DerivedView<View>&) { return false; } /// Test whether two views are the same template<class ViewX, class ViewY> forceinline bool operator ==(const DerivedView<ViewX>&, const ConstView<ViewY>&) { return false; } /// Test whether two views are the same template<class View, class Var> forceinline bool operator ==(const DerivedView<View>&, const VarImpView<Var>&) { return false; } /// Test whether two views are not the same template<class ViewX, class ViewY> forceinline bool operator ==(const DerivedView<ViewX>&, const DerivedView<ViewY>&) { return false; } /// Test whether two views are the same template<class VarX, class VarY> forceinline bool operator ==(const VarImpView<VarX>& x, const VarImpView<VarY>& y) { return x.varimp() == y.varimp(); }
/// Test whether two views are not the same template<class ViewA, class ViewB> forceinline bool operator !=(const ConstView<ViewA>&, const ConstView<ViewB>&) { return true; } /// Test whether two views are not the same template<class Var, class View> forceinline bool operator !=(const ConstView<View>&, const VarImpView<Var>&) { return true; } /// Test whether two views are not the same template<class ViewA, class ViewB> forceinline bool operator !=(const ConstView<ViewA>&, const DerivedView<ViewB>&) { return true; } /// Test whether two views are not the same template<class Var, class View> forceinline bool operator !=(const VarImpView<Var>&, const ConstView<View>&) { return true; } /// Test whether two views are not the same template<class Var, class View> forceinline bool operator !=(const VarImpView<Var>&, const DerivedView<View>&) { return true; } /// Test whether two views are the same template<class ViewX, class ViewY> forceinline bool operator !=(const DerivedView<ViewX>&, const ConstView<ViewY>&) { return true; } /// Test whether two views are not the same template<class View, class Var> forceinline bool operator !=(const DerivedView<View>&, const VarImpView<Var>&) { return true; } /// Test whether two views are not the same template<class ViewX, class ViewY> forceinline bool operator !=(const DerivedView<ViewX>&, const DerivedView<ViewY>&) { return true; } /// Test whether two views are not the same template<class VarX, class VarY> forceinline bool operator !=(const VarImpView<VarX>& x, const VarImpView<VarY>& y) { return x.varimp() != y.varimp(); }
/* * Testing whether two views share the same variable * */
template<class ViewA, class ViewB> forceinline bool shared(const ConstView<ViewA>&, const ConstView<ViewB>&) { return false; } template<class Var, class View> forceinline bool shared(const VarImpView<Var>&, const ConstView<View>&) { return false; } template<class ViewA, class ViewB> forceinline bool shared(const DerivedView<ViewA>&, const ConstView<ViewB>&) { return false; } template<class View, class Var> forceinline bool shared(const ConstView<View>&, const VarImpView<Var>&) { return false; } template<class ViewA, class ViewB> forceinline bool shared(const ConstView<ViewA>&, const DerivedView<ViewB>&) { return false; } template<class VarA, class VarB> forceinline bool shared(const VarImpView<VarA>& x, const VarImpView<VarB>& y) { return (static_cast<VarImpBase*>(x.varimp()) == static_cast<VarImpBase*>(y.varimp())); } template<class Var, class View> forceinline bool shared(const VarImpView<Var>& x, const DerivedView<View>& y) { return (View::varderived() && static_cast<VarImpBase*>(x.varimp()) == static_cast<VarImpBase*>(y.varimp())); } template<class View, class Var> forceinline bool shared(const DerivedView<View>& x, const VarImpView<Var>& y) { return (View::varderived() && static_cast<VarImpBase*>(x.varimp()) == static_cast<VarImpBase*>(y.varimp())); } template<class ViewA, class ViewB> forceinline bool shared(const DerivedView<ViewA>& x, const DerivedView<ViewB>& y) { return (ViewA::varderived() && ViewB::varderived() && static_cast<VarImpBase*>(x.varimp()) == static_cast<VarImpBase*>(y.varimp())); }
}
// STATISTICS: kernel-var