/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ /* * Main authors: * Christian Schulte * * Copyright: * Christian Schulte, 2002 * * Last modified: * $Date: 2011-09-28 22:50:49 +1000 (Wed, 28 Sep 2011) $ by $Author: tack $ * $Revision: 12418 $ * * 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 Val ScaleView::floor_div(double y) const { return static_cast(floor(y / a)); } template forceinline Val ScaleView::ceil_div(double y) const { return static_cast(ceil(y / a)); } template forceinline Val ScaleView::exact_div(double y, bool& exact) const { double ya = y / a; if (ceil(ya) == ya) { exact = true; return static_cast(ya); } else { exact = false; return 0; } } #if GECODE_INT_RND_TWDS_ZERO template forceinline int ScaleView::floor_div(int y) const { return ((y >= 0) ? y : (y-a+1))/a; } template forceinline int ScaleView::ceil_div(int y) const { return ((y >= 0) ? (y+a-1) : y)/a; } template forceinline int ScaleView::exact_div(int y, bool& exact) const { int ya = y / a; if (a * ya == y) { exact = true; return ya; } else { exact = false; return 0; } } #endif /* * Constructors and initialization * */ template forceinline ScaleView::ScaleView(void) {} template forceinline ScaleView::ScaleView(int b, const IntView& y) : DerivedView(y), a(b) {} /* * Value access * */ template forceinline int ScaleView::scale(void) const { return a; } template forceinline Val ScaleView::min(void) const { Val c = x.min(); c *= a; return c; } template forceinline Val ScaleView::max(void) const { Val c = x.max(); c *= a; return c; } template forceinline Val ScaleView::med(void) const { Val c = x.med(); c *= a; return c; } template forceinline Val ScaleView::val(void) const { Val c = x.val(); c *= a; return c; } template forceinline UnsVal ScaleView::size(void) const { return static_cast(x.size()); } template forceinline UnsVal ScaleView::width(void) const { UnsVal c = x.width(); c *= a; return c; } template forceinline UnsVal ScaleView::regret_min(void) const { UnsVal c = x.regret_min(); c *= a; return c; } template forceinline UnsVal ScaleView::regret_max(void) const { UnsVal c = x.regret_max(); c *= a; return c; } /* * Domain tests * */ template forceinline bool ScaleView::range(void) const { return x.range(); } template forceinline bool ScaleView::in(Val n) const { bool exact; int nda = exact_div(n, exact); return exact && x.in(nda); } /* * Domain update by value * */ template forceinline ModEvent ScaleView::lq(Space& home, Val n) { return (n >= max()) ? ME_INT_NONE : x.lq(home,floor_div(n)); } template forceinline ModEvent ScaleView::le(Space& home, Val n) { return (n > max()) ? ME_INT_NONE : x.le(home,floor_div(n)); } template forceinline ModEvent ScaleView::gq(Space& home, Val n) { return (n <= min()) ? ME_INT_NONE : x.gq(home,ceil_div(n)); } template forceinline ModEvent ScaleView::gr(Space& home, Val n) { return (n < min()) ? ME_INT_NONE : x.gr(home,ceil_div(n)); } template forceinline ModEvent ScaleView::nq(Space& home, Val n) { bool exact; int nda = exact_div(n,exact); return exact ? x.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 ? x.eq(home,nda) : ME_INT_FAILED; } /* * Propagator modification events * */ template forceinline ModEventDelta ScaleView::med(ModEvent me) { return IntView::med(me); } /* * Delta information for advisors * */ template forceinline Val ScaleView::min(const Delta& d) const { Val c = x.min(d); c *= a; return c; } template forceinline Val ScaleView::max(const Delta& d) const { Val c = x.max(d); c *= a; return c; } template forceinline bool ScaleView::any(const Delta& d) const { return x.any(d); } /* * Cloning * */ template forceinline void ScaleView::update(Space& home, bool share, ScaleView& y) { DerivedView::update(home,share,y); a=y.a; } /** * \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 ScaleView& x, const ScaleView& y) { return same(x.base(),y.base()) && (x.scale() == y.scale()); } template forceinline bool before(const ScaleView& x, const ScaleView& y) { return before(x.base(),y.base()) || (same(x.base(),y.base()) && (x.scale() < y.scale())); } }} // STATISTICS: int-var