/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ /* * Main authors: * Christian Schulte * * Contributing authors: * Guido Tack * * Copyright: * Christian Schulte, 2004 * Guido Tack, 2004 * * Last modified: * $Date: 2008-07-11 09:31:51 +0200 (Fri, 11 Jul 2008) $ by $Author: tack $ * $Revision: 7288 $ * * 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 Int { namespace Element { /// Class to get VarArg type for view template class ViewToVarArg {}; /// VarArg type for integer views template<> class ViewToVarArg { public: typedef IntVarArgs argtype; }; /// VarArg type for Boolean views template<> class ViewToVarArg { public: typedef BoolVarArgs argtype; }; /** * \brief Class for pair of index and view * */ template class IdxView { public: int idx; View view; static IdxView* allocate(Space*, int); static IdxView* init(Space*, const typename ViewToVarArg::argtype&); }; template forceinline IdxView* IdxView::allocate(Space* home, int n) { return static_cast*> (home->alloc(sizeof(IdxView)*n)); } template forceinline IdxView* IdxView::init(Space* home, const typename ViewToVarArg::argtype& x) { IdxView* iv = allocate(home,x.size()); for (int i = x.size(); i--; ) { iv[i].idx = i; iv[i].view = x[i]; } return iv; } /** * \brief Class for bounds-equality test * */ template class RelTestBnd { public: RelTest operator()(VA,VC); }; /** * \brief Class for bounds-equality test (specialized) * */ template class RelTestBnd { public: RelTest operator()(VA,ConstIntView); }; /** * \brief Class for domain-equality test * */ template class RelTestDom { public: RelTest operator()(VA,VC); }; /** * \brief Class for domain-equality test (specialized) * */ template class RelTestDom { public: RelTest operator()(VA,ConstIntView); }; template forceinline RelTest RelTestBnd::operator()(VA x, VC y) { return rtest_eq_bnd(x,y); } template forceinline RelTest RelTestBnd::operator()(VA x, ConstIntView y) { return rtest_eq_bnd(x,y.val()); } template forceinline RelTest RelTestDom::operator()(VA x, VC y) { return rtest_eq_dom(x,y); } template forceinline RelTest RelTestDom::operator()(VA x, ConstIntView y) { return rtest_eq_dom(x,y.val()); } /* * Base class * */ template View::View(Space* home, IdxView* iv0, int n0, VB y0, VC y1) : Propagator(home), iv(iv0), n(n0), x0(y0), x1(y1) { x0.subscribe(home,this,PC_INT_DOM); x1.subscribe(home,this,pc_ac); for (int i=n; i--; ) iv[i].view.subscribe(home,this,pc_ac); } template forceinline View::View(Space* home, bool share, View& p) : Propagator(home,share,p), n(p.n) { x0.update(home,share,p.x0); x1.update(home,share,p.x1); iv = IdxView::allocate(home,n); for (int i=n; i--; ) { iv[i].idx = p.iv[i].idx; iv[i].view.update(home,share,p.iv[i].view); } } template PropCost View::cost(ModEventDelta) const { // This is required for subscribing to variables in the // above constructor, but this is then the only time this // virtual function is ever used! return PC_LINEAR_LO; } template Reflection::ActorSpec View::spec(const Space* home, Reflection::VarMap& m, const Support::Symbol& ati) const { Reflection::ActorSpec s(ati); Reflection::IntArrayArg* ai = Reflection::Arg::newIntArray(n); for (int i=n; i--;) (*ai)[i] = iv[i].idx; Reflection::ArrayArg* a = Reflection::Arg::newArray(n); for (int i=n; i--;) (*a)[i] = iv[i].view.spec(home, m); return s << x0.spec(home, m) << x1.spec(home, m) << ai << a; } template forceinline size_t View::dispose(Space* home) { x0.cancel(home,this,PC_INT_DOM); x1.cancel(home,this,pc_ac); for (int i=n; i--;) iv[i].view.cancel(home,this,pc_ac); (void) Propagator::dispose(home); return sizeof(*this); } /** * \brief Value iterator for indices in index-view map * */ template class IterIdxView { private: const IdxView *cur, *end; public: IterIdxView(void); IterIdxView(const IdxView*, const IdxView*); void init(const IdxView*, const IdxView*); bool operator()(void) const; void operator++(void); int val(void) const; }; template forceinline IterIdxView::IterIdxView(void) {} template forceinline IterIdxView::IterIdxView(const IdxView* b, const IdxView* e) : cur(b), end(e) {} template forceinline void IterIdxView::init(const IdxView* b, const IdxView* e) { cur=b; end=e; } template forceinline bool IterIdxView::operator()(void) const { return cur < end; } template forceinline void IterIdxView::operator++(void) { cur++; } template forceinline int IterIdxView::val(void) const { return cur->idx; } /* * Generic scanning: does all but computing new domain for result * (which is specific to bounds/domain version) * */ template ExecStatus scan(Space* home, IdxView* iv, int& n, VB x0, VC x1, Propagator* p, RelTest rt) { assert(n > 1); /* * Prunes pairs of index, variable * - checks for idx value removed * - checks for disequal variables * */ ViewValues vx0(x0); int i = 0; int j = 0; while (vx0() && (i < n)) { if (iv[i].idx < vx0.val()) { iv[i].view.cancel(home,p,pc_ac); ++i; } else if (iv[i].idx > vx0.val()) { ++vx0; } else { assert(iv[i].idx == vx0.val()); switch (rt(iv[i].view,x1)) { case RT_FALSE: iv[i].view.cancel(home,p,pc_ac); break; case RT_TRUE: case RT_MAYBE: iv[j++] = iv[i]; break; default: GECODE_NEVER; } ++vx0; ++i; } } while (i < n) iv[i++].view.cancel(home,p,pc_ac); bool adjust = (j v(&iv[0],&iv[n]); GECODE_ME_CHECK(x0.narrow_v(home,v,false)); assert(x0.size() == static_cast(n)); } return ES_OK; } /* * Bounds consistent propagator * */ template forceinline ViewBnd::ViewBnd(Space* home, IdxView* iv, int n, VB x0, VC x1) : View(home,iv,n,x0,x1) {} template ExecStatus ViewBnd::post(Space* home, IdxView* iv, int n, VB x0, VC x1) { GECODE_ME_CHECK(x0.gq(home,0)); GECODE_ME_CHECK(x0.le(home,n)); if (x0.assigned()) { (void) new (home) Rel::EqBnd(home,iv[x0.val()].view,x1); return ES_OK; } else { assert(n>1); (void) new (home) ViewBnd(home,iv,n,x0,x1); } return ES_OK; } template forceinline ViewBnd::ViewBnd(Space* home, bool share, ViewBnd& p) : View(home,share,p) {} template Actor* ViewBnd::copy(Space* home, bool share) { return new (home) ViewBnd(home,share,*this); } template inline Support::Symbol ViewBnd::ati(void) { return Reflection::mangle("Gecode::Int::Element::ViewBnd"); } template Reflection::ActorSpec ViewBnd::spec(const Space* home, Reflection::VarMap& m) const { return View::spec(home, m, ati()); } template void ViewBnd::post(Space* home, Reflection::VarMap& vars, const Reflection::ActorSpec& spec) { spec.checkArity(4); VB x0(home, vars, spec[0]); VC x1(home, vars, spec[1]); Reflection::IntArrayArg* ia = spec[2]->toIntArray(); GECODE_AUTOARRAY(int,idx,ia->size()); for (int i=ia->size(); i--; ) idx[i] = (*ia)[i]; ViewArray y(home, vars, spec[3]); IdxView* iv = IdxView::allocate(home, ia->size()); for (int i=ia->size(); i--; ) { iv[i].view = y[i]; iv[i].idx = idx[i]; } (void) new (home) ViewBnd(home, iv, ia->size(), x0, x1); } template ExecStatus ViewBnd::propagate(Space* home, ModEventDelta) { assert(n > 1); RelTestBnd rt; GECODE_ME_CHECK((scan > (home,iv,n,x0,x1,this,rt))); if (n == 1) { size_t s = this->dispose(home); (void) new (home) Rel::EqBnd(home,iv[0].view,x1); return ES_SUBSUMED(this,s); } assert(n > 1); // Compute new result int min = iv[n-1].view.min(); int max = iv[n-1].view.max(); for (int i=n-1; i--; ) { min = std::min(iv[i].view.min(),min); max = std::max(iv[i].view.max(),max); } ExecStatus es = shared(x0,x1) ? ES_NOFIX : ES_FIX; { ModEvent me = x1.lq(home,max); if (me_failed(me)) return ES_FAILED; if (me_modified(me) && (x1.max() != max)) es = ES_NOFIX; } { ModEvent me = x1.gq(home,min); if (me_failed(me)) return ES_FAILED; if (me_modified(me) && (x1.min() != min)) es = ES_NOFIX; } return (x1.assigned() && (min == max)) ? ES_SUBSUMED(this,home) : es; } /* * Domain consistent propagator * */ template forceinline ViewDom::ViewDom(Space* home, IdxView* iv, int n, VB x0, VC x1) : View(home,iv,n,x0,x1) {} template ExecStatus ViewDom::post(Space* home, IdxView* iv, int n, VB x0, VC x1){ GECODE_ME_CHECK(x0.gq(home,0)); GECODE_ME_CHECK(x0.le(home,n)); if (x0.assigned()) { (void) new (home) Rel::EqDom(home,iv[x0.val()].view,x1); return ES_OK; } else { assert(n>1); (void) new (home) ViewDom(home,iv,n,x0,x1); } return ES_OK; } template forceinline ViewDom::ViewDom(Space* home, bool share, ViewDom& p) : View(home,share,p) {} template Actor* ViewDom::copy(Space* home, bool share) { return new (home) ViewDom(home,share,*this); } template PropCost ViewDom::cost(ModEventDelta med) const { if (VA::me(med) != ME_INT_DOM) return PC_LINEAR_LO; else return PC_LINEAR_HI; } template inline Support::Symbol ViewDom::ati(void) { return Reflection::mangle("Gecode::Int::Element::ViewDom"); } template Reflection::ActorSpec ViewDom::spec(const Space* home, Reflection::VarMap& m) const { return View::spec(home, m, ati()); } template void ViewDom::post(Space* home, Reflection::VarMap& vars, const Reflection::ActorSpec& spec) { spec.checkArity(4); VB x0(home, vars, spec[0]); VC x1(home, vars, spec[1]); Reflection::IntArrayArg* ia = spec[2]->toIntArray(); GECODE_AUTOARRAY(int,idx,ia->size()); for (int i=ia->size(); i--; ) idx[i] = (*ia)[i]; ViewArray y(home, vars, spec[3]); IdxView* iv = IdxView::allocate(home, ia->size()); for (int i=ia->size(); i--; ) { iv[i].view = y[i]; iv[i].idx = idx[i]; } (void) new (home) ViewDom(home, iv, ia->size(), x0, x1); } template ExecStatus ViewDom::propagate(Space* home, ModEventDelta med) { assert(n > 1); if (VA::me(med) != ME_INT_DOM) { RelTestBnd rt; GECODE_ME_CHECK((scan > (home,iv,n,x0,x1,this,rt))); if (n == 1) { size_t s = this->dispose(home); (void) new (home) Rel::EqDom(home,iv[0].view,x1); return ES_SUBSUMED(this,s); } // Compute new result int min = iv[n-1].view.min(); int max = iv[n-1].view.max(); for (int i=n-1; i--; ) { min = std::min(iv[i].view.min(),min); max = std::max(iv[i].view.max(),max); } GECODE_ME_CHECK(x1.lq(home,max)); GECODE_ME_CHECK(x1.gq(home,min)); return (x1.assigned() && (min == max)) ? ES_SUBSUMED(this,home) : ES_NOFIX_PARTIAL(this,VA::med(ME_INT_DOM)); } RelTestDom rt; GECODE_ME_CHECK((scan > (home,iv,n,x0,x1,this,rt))); if (n == 1) { size_t s = this->dispose(home); (void) new (home) Rel::EqDom(home,iv[0].view,x1); return ES_SUBSUMED(this,s); } assert(n > 1); GECODE_AUTOARRAY(ViewRanges,i_view,n); for (int i = n; i--; ) i_view[i].init(iv[i].view); Iter::Ranges::NaryUnion > i_val(i_view, n); ModEvent me = x1.inter_r(home,i_val); GECODE_ME_CHECK(me); return (shared(x0,x1) || me_modified(me)) ? ES_NOFIX : ES_FIX; } }}} // STATISTICS: int-prop