/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ /* * Main authors: * Christian Schulte * * Copyright: * Christian Schulte, 2008 * * Last modified: * $Date: 2011-05-11 20:44:17 +1000 (Wed, 11 May 2011) $ by $Author: tack $ * $Revision: 12001 $ * * 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 Branch { /** * \brief %Choice storing position and values * * The maximal number of alternatives is defined by \a alt. */ template class PosValuesChoice : public PosChoice { private: /// Information about position and minimum class PosMin { public: /// Start position of range unsigned int pos; /// Minmum of range int min; }; /// Number of ranges unsigned int n; /// Values to assign to PosMin* pm; public: /// Initialize choice for brancher \a b, position \a p, view choice \a viewc, and view \a x PosValuesChoice(const Brancher& b, const Pos& p, const typename ViewSel::Choice& viewc, View x); /// Initialize choice for brancher \a b from \a e PosValuesChoice(const Brancher& b, unsigned int alt, Pos p, const typename ViewSel::Choice& viewc, Archive& e); /// Return value to branch with for alternative \a a int val(unsigned int a) const; /// Report size occupied virtual size_t size(void) const; /// Deallocate virtual ~PosValuesChoice(void); /// Archive into \a e virtual void archive(Archive& e) const; }; template forceinline PosValuesChoice:: PosValuesChoice(const Brancher& b, const Pos& p, const typename ViewSel::Choice& viewc, View x) : PosChoice(b,x.size(),p,viewc), n(0) { for (ViewRanges r(x); r(); ++r) n++; pm = heap.alloc(n+1); unsigned int w=0; int i=0; for (ViewRanges r(x); r(); ++r) { pm[i].min = r.min(); pm[i].pos = w; w += r.width(); i++; } pm[i].pos = w; } template forceinline PosValuesChoice:: PosValuesChoice(const Brancher& b, unsigned int alt, Pos p, const typename ViewSel::Choice& viewc, Archive& e) : PosChoice(b,alt,p,viewc) { e >> n; pm = heap.alloc(n+1); for (unsigned int i=0; i> pm[i].pos; e >> pm[i].min; } } template forceinline int PosValuesChoice::val(unsigned int a) const { PosMin* l = &pm[0]; PosMin* r = &pm[n-1]; while (true) { PosMin* m = l + (r-l)/2; if (a < m->pos) { r=m-1; } else if (a >= (m+1)->pos) { l=m+1; } else { return m->min + static_cast(a - m->pos); } } GECODE_NEVER; return 0; } template size_t PosValuesChoice::size(void) const { return sizeof(PosValuesChoice)+(n+1)*sizeof(PosMin); } template PosValuesChoice::~PosValuesChoice(void) { heap.free(pm,n+1); } template forceinline void PosValuesChoice::archive(Archive& e) const { PosChoice::archive(e); e << this->alternatives() << n; for (unsigned int i=0; i forceinline ViewValuesBrancher:: ViewValuesBrancher(Home home, ViewArray& x, ViewSel& vi_s, BranchFilter bf) : ViewBrancher(home,x,vi_s,bf) {} template void ViewValuesBrancher:: post(Home home, ViewArray& x, ViewSel& vi_s, BranchFilter bf) { (void) new (home) ViewValuesBrancher(home,x,vi_s,bf); } template forceinline ViewValuesBrancher:: ViewValuesBrancher(Space& home, bool share, ViewValuesBrancher& b) : ViewBrancher(home,share,b) {} template Actor* ViewValuesBrancher::copy(Space& home, bool share) { return new (home) ViewValuesBrancher(home,share,*this); } template const Choice* ViewValuesBrancher::choice(Space& home) { Pos p = ViewBrancher::pos(home); View v(ViewBrancher::view(p).varimp()); return new PosValuesChoice (*this,p,viewsel.choice(home),v); } template const Choice* ViewValuesBrancher::choice(const Space& home, Archive& e) { int p; unsigned int alt; e >> p >> alt; return new PosValuesChoice (*this,alt,p,viewsel.choice(home,e),e); } template ExecStatus ViewValuesBrancher ::commit(Space& home, const Choice& c, unsigned int a) { const PosValuesChoice& pvc = static_cast&>(c); View v(ViewBrancher::view(pvc.pos()).varimp()); viewsel.commit(home, pvc.viewchoice(), a); return me_failed(v.eq(home,pvc.val(a))) ? ES_FAILED : ES_OK; } }}} // STATISTICS: int-branch