/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ /* * Main authors: * Guido Tack * Christian Schulte * Gabor Szokoli * * Copyright: * Guido Tack, 2004 * Christian Schulte, 2004 * Gabor Szokoli, 2004 * * Last modified: * $Date: 2008-02-06 18:48:22 +0100 (Wed, 06 Feb 2008) $ by $Author: schulte $ * $Revision: 6102 $ * * 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 "gecode/set.hh" #include "gecode/int.hh" #include "gecode/set/rel.hh" namespace Gecode { namespace Set { namespace Int { template forceinline Match::Match(Space* home, View y0, ViewArray< Gecode::Int::IntView >& ys) : Propagator(home), x0(y0), xs(ys) { x0.subscribe(home,this, PC_SET_ANY); xs.subscribe(home,this, Gecode::Int::PC_INT_BND); } template forceinline Match::Match(Space* home, bool share, Match& p) : Propagator(home,share,p) { x0.update(home,share,p.x0); xs.update(home,share,p.xs); } template forceinline ExecStatus Match::post(Space* home, View x0, ViewArray& xs) { unsigned int xs_size = xs.size(); GECODE_ME_CHECK(x0.cardMin(home,xs_size)); GECODE_ME_CHECK(x0.cardMax(home,xs_size)); if (xs_size==1) { SingletonView sv(xs[0]); GECODE_ES_CHECK((Rel::Eq::post(home,x0, sv))); } else { // Sharing in xs is handled correctly in the propagator: // if two views in xs are shared, this leads to failure. (void) new (home) Match(home,x0,xs); } return ES_OK; } template PropCost Match::cost(ModEventDelta) const { return PC_LINEAR_LO; } template size_t Match::dispose(Space* home) { assert(!home->failed()); x0.cancel(home,this, PC_SET_ANY); xs.cancel(home,this, Gecode::Int::PC_INT_BND); (void) Propagator::dispose(home); return sizeof(*this); } template Actor* Match::copy(Space* home, bool share) { return new (home) Match(home,share,*this); } template ExecStatus Match::propagate(Space* home, ModEventDelta) { int xs_size = xs.size(); bool loopFlag; do { loopFlag = false; // Order int vars in xs GECODE_ME_CHECK(xs[0].gq(home,x0.lubMin())); for (int i=xs_size-1; i--; ) { GECODE_ME_CHECK_MODIFIED(loopFlag, xs[i+1].gq(home,xs[i].min() + 1)); } GECODE_ME_CHECK_MODIFIED(loopFlag, xs[xs_size-1].lq(home,x0.lubMax())); for (int i=xs_size-2; i--; ) { GECODE_ME_CHECK_MODIFIED(loopFlag, xs[i].lq(home,xs[i+1].max() - 1)); } // if y from xs is assigned, add to glb(x0) for (int i=xs_size; i--; ) { if (xs[i].assigned()) { GECODE_ME_CHECK_MODIFIED(loopFlag, x0.include(home,xs[i].val())); } } // intersect every y in xs with lub(x0) for (int i=xs_size; i--; ) { LubRanges ub(x0); GECODE_ME_CHECK_MODIFIED(loopFlag, xs[i].inter_r(home,ub,false)); } // remove gaps between vars in xs from lub(x0) GECODE_ME_CHECK_MODIFIED(loopFlag, x0.exclude(home,Limits::min,xs[0].min()-1)); GECODE_ME_CHECK_MODIFIED(loopFlag, x0.exclude(home,xs[xs_size-1].max()+1, Limits::max)); for (int i=xs_size-1; i--; ) { int start = xs[i].max() + 1; int end = xs[i+1].min() - 1; if (start<=end) { GECODE_ME_CHECK_MODIFIED(loopFlag, x0.exclude(home,start,end)); } } // try to assign vars in xs from glb(x0) if (x0.glbSize()>0) { LubRanges ub(x0); Iter::Ranges::ToValues > ubv(ub); GlbRanges lb(x0); Iter::Ranges::ToValues > lbv(lb); int i=0; for (; ubv() && lbv() && ubv.val()==lbv.val(); ++ubv, ++lbv, i++) { GECODE_ME_CHECK_MODIFIED(loopFlag, xs[i].eq(home,lbv.val())); } if (i lbx0(x0); GlbRanges ubx0(x0); Iter::Ranges::Inter,GlbRanges > inter(lbx0, ubx0); int to = x0.glbMax(); int from = to; while (inter()) { from = inter.min(); ++inter; } int i=xs_size-1; for (int j=to; j>=from;j--,i--) { GECODE_ME_CHECK_MODIFIED(loopFlag, xs[i].eq(home,j)); } } } } while (loopFlag); for (int i=xs_size; i--; ) if (!xs[i].assigned()) return ES_FIX; return ES_SUBSUMED(this,home); } template Support::Symbol Match::ati(void) { return Reflection::mangle("Gecode::Set::Int::Match"); } template Reflection::ActorSpec Match::spec(const Space* home, Reflection::VarMap& m) const { Reflection::ActorSpec s(ati()); return s << x0.spec(home, m) << xs.spec(home, m); } template void Match::post(Space* home, Reflection::VarMap& vars, const Reflection::ActorSpec& spec) { spec.checkArity(2); View x0(home, vars, spec[0]); ViewArray x1(home, vars, spec[1]); (void) new (home) Match(home,x0,x1); } }}} // STATISTICS: set-prop