/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ /* * Main authors: * Christopher Mears * * Contributing authors: * Christian Schulte * * Copyright: * Christopher Mears, 2011 * Christian Schulte, 2011 * * Last modified: * $Date: 2011-07-01 00:20:54 +1000 (Fri, 01 Jul 2011) $ by $Author: schulte $ * $Revision: 12138 $ * * 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 { namespace Precede { /// Whether \a x is assigned to value \a v template forceinline bool assigned(View x, int v) { return x.assigned() && (x.val() == v); } template forceinline Single::Index::Index(Space& home, Propagator& p, Council& c, int i0) : Advisor(home,p,c), i(i0) {} template forceinline Single::Index::Index(Space& home, bool share, Index& a) : Advisor(home,share,a), i(a.i) {} template forceinline ExecStatus Single::updateAlpha(Space& home) { int n = x.size(); while ((alpha < n) && !x[alpha].in(s)) GECODE_ME_CHECK(x[alpha++].nq(home, t)); if (alpha < n) GECODE_ME_CHECK(x[alpha].nq(home, t)); return ES_OK; } template forceinline ExecStatus Single::updateBeta(Space& home) { int n = x.size(); do { beta++; } while ((beta < n) && !x[beta].in(s)); if (beta > gamma) GECODE_ME_CHECK(x[alpha].eq(home, s)); return ES_OK; } template forceinline Single::Single(Home home, ViewArray& x0, int s0, int t0, int b, int g) : NaryPropagator(home,x0), c(home), s(s0), t(t0), alpha(0), beta(b), gamma(g) { for (int i=x.size(); i--; ) if (!x[i].assigned()) x[i].subscribe(home,*new (home) Index(home,*this,c,i)); View::schedule(home, *this, ME_INT_BND); } template inline ExecStatus Single::post(Home home, ViewArray& x, int s, int t) { { int alpha = 0; while ((alpha < x.size()) && !x[alpha].in(s)) GECODE_ME_CHECK(x[alpha++].nq(home,t)); x.drop_fst(alpha); if (x.size() == 0) return ES_OK; } // alpha has been normalized to 0 int beta = 0, gamma = 0; GECODE_ME_CHECK(x[0].nq(home,t)); do { gamma++; } while ((gamma < x.size()) && !assigned(x[gamma],t)); do { beta++; } while ((beta < x.size()) && !x[beta].in(s)); if (beta > gamma) { GECODE_ME_CHECK(x[0].eq(home, s)); return ES_OK; } if (gamma < x.size()) x.drop_lst(gamma); (void) new (home) Single(home, x, s, t, beta, gamma); return ES_OK; } template forceinline Single::Single(Space& home, bool share, Single& p) : NaryPropagator(home, share, p), s(p.s), t(p.t), alpha(p.alpha), beta(p.beta), gamma(p.gamma) { c.update(home, share, p.c); } template Propagator* Single::copy(Space& home, bool share) { // Try to eliminate assigned views at the beginning if (alpha > 0) { int i = 0; while ((i < alpha) && x[i].assigned()) i++; x.drop_fst(i); for (Advisors as(c); as(); ++as) as.advisor().i -= i; alpha -= i; beta -= i; gamma -= i; } // Try to eliminate assigned views at the end if (gamma < x.size()) { int i = x.size()-1; while ((i > gamma) && x[i].assigned()) i--; x.drop_lst(i); } return new (home) Single(home, share, *this); } template inline size_t Single::dispose(Space& home) { // Cancel remaining advisors for (Advisors as(c); as(); ++as) x[as.advisor().i].cancel(home,as.advisor()); c.dispose(home); (void) NaryPropagator::dispose(home); return sizeof(*this); } template PropCost Single::cost(const Space&, const ModEventDelta&) const { return PropCost::linear(PropCost::LO, x.size()); } template ExecStatus Single::advise(Space& home, Advisor& a0, const Delta& d) { Index& a(static_cast(a0)); int i = a.i; // Check for gamma if ((beta <= gamma) && (i < gamma) && assigned(x[i],t)) gamma = i; if (x[i].assigned()) { a.dispose(home,c); if (c.empty()) return ES_NOFIX; } else if ((i < alpha) || (i > gamma)) { x[i].cancel(home,a); a.dispose(home,c); return (c.empty()) ? ES_NOFIX : ES_FIX; } if (beta > gamma) return ES_NOFIX; if ((alpha == i) || (beta == i)) { if (x[i].any(d) && !x[i].in(s)) return ES_NOFIX; if ((x[i].min(d) <= s) && (s <= x[i].max(d))) return ES_NOFIX; } return ES_FIX; } template ExecStatus Single::propagate(Space& home, const ModEventDelta&) { int n = x.size(); if (beta > gamma) { GECODE_ME_CHECK(x[alpha].eq(home, s)); return home.ES_SUBSUMED(*this); } if ((alpha < n) && !x[alpha].in(s)) { alpha++; while (alpha < beta) GECODE_ME_CHECK(x[alpha++].nq(home, t)); GECODE_ES_CHECK(updateAlpha(home)); beta = alpha; if (alpha < n) GECODE_ES_CHECK(updateBeta(home)); } else if ((beta < n) && !x[beta].in(s)) { GECODE_ES_CHECK(updateBeta(home)); } return (c.empty()) ? home.ES_SUBSUMED(*this) : ES_FIX; } }}} // STATISTICS: int-prop