/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ /* * Main authors: * Guido Tack * * Contributing authors: * Christian Schulte * * Copyright: * Guido Tack, 2004 * Christian Schulte, 2004 * * Last modified: * $Date: 2011-05-02 08:24:42 +1000 (Mon, 02 May 2011) $ by $Author: tack $ * $Revision: 11973 $ * * 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. * */ #include namespace Gecode { namespace Set { template forceinline ComplementView::ComplementView(void) {} template forceinline ComplementView::ComplementView(View& y) : DerivedView(y) {} template forceinline ModEvent ComplementView::me_negateset(ModEvent me) { switch(me) { case ME_SET_LUB : return ME_SET_GLB; case ME_SET_GLB : return ME_SET_LUB; case ME_SET_CLUB : return ME_SET_CGLB; case ME_SET_CGLB : return ME_SET_CLUB; default: return me; } } template forceinline PropCond ComplementView::pc_negateset(PropCond pc) { switch(pc) { case PC_SET_CLUB : return PC_SET_CGLB; case PC_SET_CGLB : return PC_SET_CLUB; default: return pc; } } template forceinline unsigned int ComplementView::glbSize(void) const { return Limits::card - x.lubSize(); } template forceinline unsigned int ComplementView::lubSize(void) const { return Limits::card - x.glbSize(); } template forceinline unsigned int ComplementView::unknownSize(void) const { return lubSize() - glbSize(); } template forceinline bool ComplementView::contains(int n) const { return x.notContains(n); } template forceinline bool ComplementView::notContains(int n) const { return x.contains(n); } template forceinline unsigned int ComplementView::cardMin(void) const { return Limits::card - x.cardMax(); } template forceinline unsigned int ComplementView::cardMax(void) const { return Limits::card - x.cardMin(); } template forceinline int ComplementView::lubMin(void) const { GlbRanges lb(x); RangesCompl > lbc(lb); if (lbc()) { return lbc.min(); } else { return BndSet::MIN_OF_EMPTY; } } template forceinline int ComplementView::lubMax(void) const { GlbRanges lb(x); RangesCompl > lbc(lb); if (lbc()) { while(lbc()) ++lbc; return lbc.max(); } else { return BndSet::MAX_OF_EMPTY; } } template forceinline int ComplementView::glbMin(void) const { LubRanges ub(x); RangesCompl > ubc(ub); if (ubc()) { return ubc.min(); } else { return BndSet::MIN_OF_EMPTY; } } template forceinline int ComplementView::glbMax(void) const { LubRanges ub(x); RangesCompl > ubc(ub); if (ubc()) { while (ubc()) ++ubc; return ubc.max(); } else { return BndSet::MAX_OF_EMPTY; } } template forceinline ModEvent ComplementView::cardMin(Space& home, unsigned int c) { if (c < Limits::card) return me_negateset(x.cardMax(home, Limits::card - c)); return ME_SET_NONE; } template forceinline ModEvent ComplementView::cardMax(Space& home, unsigned int c) { if (c < Limits::card) return me_negateset(x.cardMin(home, Limits::card - c)); return ME_SET_NONE; } template forceinline ModEvent ComplementView::include(Space& home, int c) { return me_negateset((x.exclude(home, c))); } template forceinline ModEvent ComplementView::exclude(Space& home, int c) { return me_negateset((x.include(home, c))); } template forceinline ModEvent ComplementView::intersect(Space& home, int c) { Iter::Ranges::Singleton si(c,c); RangesCompl csi(si); return me_negateset((x.includeI(home, csi))); } template forceinline ModEvent ComplementView::intersect(Space& home, int i, int j) { Iter::Ranges::Singleton si(i,j); RangesCompl csi(si); return me_negateset((x.includeI(home, csi))); } template forceinline ModEvent ComplementView::include(Space& home, int j, int k) { return me_negateset(x.exclude(home,j,k)); } template forceinline ModEvent ComplementView::exclude(Space& home, int j, int k) { return me_negateset(x.include(home,j,k)); } template template ModEvent ComplementView::excludeI(Space& home,I& iter) { return me_negateset(x.includeI(home,iter)); } template template ModEvent ComplementView::includeI(Space& home,I& iter) { return me_negateset(x.excludeI(home,iter)); } template template ModEvent ComplementView::intersectI(Space& home,I& iter) { RangesCompl c(iter); return me_negateset(x.includeI(home,c)); } template forceinline void ComplementView::subscribe(Space& home, Propagator& p, PropCond pc, bool schedule) { x.subscribe(home,p, pc_negateset(pc),schedule); } template forceinline void ComplementView::cancel(Space& home, Propagator& p, PropCond pc) { x.cancel(home,p, pc_negateset(pc)); } template forceinline void ComplementView::subscribe(Space& home, Advisor& a) { x.subscribe(home,a); } template forceinline void ComplementView::cancel(Space& home, Advisor& a) { x.cancel(home,a); } template forceinline void ComplementView::schedule(Space& home, Propagator& p, ModEvent me) { return View::schedule(home,p,me_negateset(me)); } template forceinline ModEvent ComplementView::me(const ModEventDelta& med) { return me_negateset(View::me(med)); } template forceinline ModEventDelta ComplementView::med(ModEvent me) { return me_negateset(View::med(me)); } /* * Delta information for advisors * */ template forceinline ModEvent ComplementView::modevent(const Delta& d) { return me_negateset(View::modevent(d)); } template forceinline int ComplementView::glbMin(const Delta& d) const { return x.lubMin(d); } template forceinline int ComplementView::glbMax(const Delta& d) const { return x.lubMax(d); } template forceinline bool ComplementView::glbAny(const Delta& d) const { return x.lubAny(d); } template forceinline int ComplementView::lubMin(const Delta& d) const { return x.glbMin(d); } template forceinline int ComplementView::lubMax(const Delta& d) const { return x.glbMax(d); } template forceinline bool ComplementView::lubAny(const Delta& d) const { return x.glbAny(d); } /** * \brief %Range iterator for least upper bound of complement set views * \ingroup TaskActorSetView */ template class LubRanges > { private: GlbRanges lb; RangesCompl > lbc; public: /// \name Constructors and initialization //@{ /// Default constructor LubRanges(void) {} /// Initialize with ranges for view \a x LubRanges(const ComplementView& x); /// Initialize with ranges for view \a x void init(const ComplementView& x); //@} /// \name Iteration control //@{ /// Test whether iterator is still at a range or done bool operator ()(void) const; /// Move iterator to next range (if possible) void operator ++(void); //@} /// \name Range access //@{ /// Return smallest value of range int min(void) const; /// Return largest value of range int max(void) const; /// Return width of ranges (distance between minimum and maximum) unsigned int width(void) const; //@} }; template forceinline LubRanges >::LubRanges(const ComplementView& s) : lb(s.base()), lbc(lb) {} template forceinline void LubRanges >::init(const ComplementView& s) { lb.init(s.base()); lbc.init(lb); } template forceinline bool LubRanges >::operator ()(void) const { return lbc(); } template forceinline void LubRanges >::operator ++(void) { return ++lbc; } template forceinline int LubRanges >::min(void) const { return lbc.min(); } template forceinline int LubRanges >::max(void) const { return lbc.max(); } template forceinline unsigned int LubRanges >::width(void) const { return lbc.width(); } /** * \brief Range iterator for the least upper bound of double-complement-views * * This class provides (by specialization) a range iterator * for the least upper bounds of complements of complement set views. * * \ingroup TaskActorSet */ template class LubRanges > > : public LubRanges { public: /// \name Constructors and initialization //@{ /// Default constructor LubRanges(void) {} /// Initialize with ranges for view \a x LubRanges(const ComplementView >& x); /// Initialize with ranges for view \a x void init(const ComplementView >& x); //@} }; template forceinline LubRanges > >:: LubRanges(const ComplementView >& x) : LubRanges(x) {} template forceinline void LubRanges > >:: init(const ComplementView >& x) { LubRanges::init(x); } /** * \brief %Range iterator for greatest lower bound of complement set views * \ingroup TaskActorSetView */ template class GlbRanges > { private: LubRanges ub; RangesCompl > ubc; public: /// \name Constructors and initialization //@{ /// Default constructor GlbRanges(void) {} /// Initialize with ranges for view \a x GlbRanges(const ComplementView & x); /// Initialize with ranges for view \a x void init(const ComplementView & x); //@} /// \name Iteration control //@{ /// Test whether iterator is still at a range or done bool operator ()(void) const; /// Move iterator to next range (if possible) void operator ++(void); //@} /// \name Range access //@{ /// Return smallest value of range int min(void) const; /// Return largest value of range int max(void) const; /// Return width of ranges (distance between minimum and maximum) unsigned int width(void) const; //@} }; template forceinline GlbRanges >::GlbRanges(const ComplementView & s) : ub(s.base()), ubc(ub) {} template forceinline void GlbRanges >::init(const ComplementView & s) { ub.init(s.base()); ubc.init(ub); } template forceinline bool GlbRanges >::operator ()(void) const { return ubc(); } template forceinline void GlbRanges >::operator ++(void) { return ++ubc; } template forceinline int GlbRanges >::min(void) const { return ubc.min(); } template forceinline int GlbRanges >::max(void) const { return ubc.max(); } template forceinline unsigned int GlbRanges >::width(void) const { return ubc.width(); } /** * \brief Range iterator for the greatest lower bound of double-complement-views * * This class provides (by specialization) a range iterator * for the greatest lower bounds of complements of complement set views. * * \ingroup TaskActorSet */ template class GlbRanges > > : public GlbRanges { public: /// \name Constructors and initialization //@{ /// Default constructor GlbRanges(void) {} /// Initialize with ranges for view \a x GlbRanges(const ComplementView >& x); /// Initialize with ranges for view \a x void init(const ComplementView >& x); //@} }; template forceinline GlbRanges > >:: GlbRanges(const ComplementView >& x) : GlbRanges(x) {} template forceinline void GlbRanges > >:: init(const ComplementView >& x) { GlbRanges::init(x); } template std::basic_ostream& operator <<(std::basic_ostream& os, const ComplementView& x) { std::basic_ostringstream s; s.copyfmt(os); s.width(0); s << "(" << x.base() << ")^C"; return os << s.str(); } }} // STATISTICS: set-var