/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ /* * Main authors: * Christian Schulte * * Copyright: * Christian Schulte, 2002 * * Last modified: * $Date: 2008-01-31 18:29:16 +0100 (Thu, 31 Jan 2008) $ by $Author: tack $ * $Revision: 6017 $ * * 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 { namespace Int { /* * Support functions for division * */ template forceinline int ScaleView::floor_div(double x) const { return static_cast(floor(x / a)); } template forceinline int ScaleView::ceil_div(double x) const { return static_cast(ceil(x / a)); } template forceinline int ScaleView::exact_div(double x, bool& exact) const { double xa = x / a; if (ceil(xa) == xa) { exact = true; return static_cast(xa); } else { exact = false; return 0; } } #if GECODE_INT_RND_TWDS_ZERO template forceinline int ScaleView::floor_div(int x) const { return ((x >= 0) ? x : (x-a+1))/a; } template forceinline int ScaleView::ceil_div(int x) const { return ((x >= 0) ? (x+a-1) : x)/a; } template forceinline int ScaleView::exact_div(int x, bool& exact) const { int xa = x / a; if (a * xa == x) { exact = true; return xa; } else { exact = false; return 0; } } #endif /* * Constructors and initialization * */ template forceinline ScaleView::ScaleView(void) {} template forceinline ScaleView::ScaleView(int b, const IntView& x) : DerivedViewBase(x), a(b) {} template forceinline ScaleView::ScaleView(Space* home, const Reflection::VarMap& vars, Reflection::Arg* arg) : DerivedViewBase(IntView(home, vars, arg->second())), a(arg->first()->toInt()) {} template forceinline void ScaleView::init(int b, const IntView& x) { view=x; a=b; } template forceinline int ScaleView::scale(void) const { return a; } /* * Value access * */ template forceinline Val ScaleView::min(void) const { Val c = view.min(); c *= a; return c; } template forceinline Val ScaleView::max(void) const { Val c = view.max(); c *= a; return c; } template forceinline Val ScaleView::med(void) const { Val c = view.med(); c *= a; return c; } template forceinline Val ScaleView::val(void) const { Val c = view.val(); c *= a; return c; } template forceinline UnsVal ScaleView::size(void) const { return static_cast(view.size()); } template forceinline UnsVal ScaleView::width(void) const { UnsVal c = view.width(); c *= a; return c; } template forceinline UnsVal ScaleView::regret_min(void) const { UnsVal c = view.regret_min(); c *= a; return c; } template forceinline UnsVal ScaleView::regret_max(void) const { UnsVal c = view.regret_max(); c *= a; return c; } /* * Domain tests * */ template forceinline bool ScaleView::range(void) const { return view.range(); } template forceinline bool ScaleView::assigned(void) const { return view.assigned(); } template forceinline bool ScaleView::in(Val n) const { bool exact; int nda = exact_div(n, exact); return exact && view.in(nda); } /* * Domain update by value * */ template forceinline ModEvent ScaleView::lq(Space* home, Val n) { return (n >= max()) ? ME_INT_NONE : view.lq(home,floor_div(n)); } template forceinline ModEvent ScaleView::le(Space* home, Val n) { return (n > max()) ? ME_INT_NONE : view.le(home,floor_div(n)); } template forceinline ModEvent ScaleView::gq(Space* home, Val n) { return (n <= min()) ? ME_INT_NONE : view.gq(home,ceil_div(n)); } template forceinline ModEvent ScaleView::gr(Space* home, Val n) { return (n < min()) ? ME_INT_NONE : view.gr(home,ceil_div(n)); } template forceinline ModEvent ScaleView::nq(Space* home, Val n) { bool exact; int nda = exact_div(n,exact); return exact ? view.nq(home,nda) : ME_INT_NONE; } template forceinline ModEvent ScaleView::eq(Space* home, Val n) { bool exact; int nda = exact_div(n,exact); return exact ? view.eq(home,nda) : ME_INT_FAILED; } /* * Propagator modification events * */ template forceinline void ScaleView::schedule(Space* home, Propagator* p, ModEvent me) { return IntView::schedule(home,p,me); } template forceinline ModEvent ScaleView::me(ModEventDelta med) { return IntView::me(med); } template forceinline ModEventDelta ScaleView::med(ModEvent me) { return IntView::med(me); } /* * Dependencies * */ template forceinline void ScaleView::subscribe(Space* home, Propagator* p, PropCond pc, bool process) { view.subscribe(home,p,pc,process); } template forceinline void ScaleView::cancel(Space* home, Propagator* p, PropCond pc) { view.cancel(home,p,pc); } template forceinline void ScaleView::subscribe(Space* home, Advisor* a) { view.subscribe(home,a); } template forceinline void ScaleView::cancel(Space* home, Advisor* a) { view.cancel(home,a); } /* * Delta information for advisors * */ template forceinline ModEvent ScaleView::modevent(const Delta* d) { return IntView::modevent(d); } template forceinline Val ScaleView::min(const Delta* d) const { Val c = view.min(d); c *= a; return c; } template forceinline Val ScaleView::max(const Delta* d) const { Val c = view.max(d); c *= a; return c; } template forceinline bool ScaleView::any(const Delta* d) const { return view.any(d); } /* * Cloning * */ template forceinline void ScaleView::update(Space* home, bool share, ScaleView& x) { a=x.a; view.update(home,share,x.view); } /* * Serialization * */ template forceinline Reflection::Arg* ScaleView::spec(const Space* home, Reflection::VarMap& m) const { return Reflection::Arg::newPair(Reflection::Arg::newInt(a), view.spec(home, m)); } template inline Support::Symbol ScaleView::type(void) { return Support::Symbol("Gecode::Int::ScaleView"); } /** * \brief %Range iterator for integer-precision scale integer views * \ingroup TaskActorIntView */ template <> class ViewRanges : public Iter::Ranges::ScaleUp > { public: /// \name Constructors and initialization //@{ /// Default constructor ViewRanges(void); /// Initialize with ranges for view \a x ViewRanges(const IntScaleView& x); /// Initialize with ranges for view \a x void init(const IntScaleView& x); //@} }; forceinline ViewRanges::ViewRanges(void) {} forceinline ViewRanges::ViewRanges(const IntScaleView& x) { ViewRanges xi(x.base()); Iter::Ranges::ScaleUp >::init (xi,x.scale()); } forceinline void ViewRanges::init(const IntScaleView& x) { ViewRanges xi(x.base()); Iter::Ranges::ScaleUp >::init (xi,x.scale()); } /** * \brief %Range iterator for double-precision scale integer views * \ingroup TaskActorIntView */ template <> class ViewRanges : public Iter::Ranges::ScaleUp > { public: /// \name Constructors and initialization //@{ /// Default constructor ViewRanges(void); /// Initialize with ranges for view \a x ViewRanges(const DoubleScaleView& x); /// Initialize with ranges for view \a x void init(const DoubleScaleView& x); //@} }; forceinline ViewRanges::ViewRanges(void) {} forceinline ViewRanges::ViewRanges(const DoubleScaleView& x) { ViewRanges xi(x.base()); Iter::Ranges::ScaleUp >::init (xi,x.scale()); } forceinline void ViewRanges::init(const DoubleScaleView& x) { ViewRanges xi(x.base()); Iter::Ranges::ScaleUp >::init (xi,x.scale()); } } /* * View comparison * */ template forceinline bool same(const Int::ScaleView& x, const Int::ScaleView& y) { return same(x.base(),y.base()) && (x.scale() == y.scale()); } template forceinline bool before(const Int::ScaleView& x, const Int::ScaleView& y) { return before(x.base(),y.base()) || (same(x.base(),y.base()) && (x.scale() < y.scale())); } } // STATISTICS: int-var