/* * Main authors: * Christian Schulte * * Copyright: * Christian Schulte, 2004 * * Last modified: * $Date: 2006-08-04 16:05:50 +0200 (Fri, 04 Aug 2006) $ by $Author: schulte $ * $Revision: 3515 $ * * This file is part of Gecode, the generic constraint * development environment: * http://www.gecode.org * * See the file "LICENSE" for information on usage and * redistribution of this file, and for a * DISCLAIMER OF ALL WARRANTIES. * */ #include namespace Gecode { namespace Iter { namespace Ranges { /** * \brief Range iterator for computing intersection (binary) * * Requires \code #include "gecode/iter.hh" \endcode * \ingroup FuncIterRanges */ template class Inter : public MinMax { protected: /// First iterator I i; /// Second iterator J j; public: /// \name Constructors and initialization //@{ /// Default constructor Inter(void); /// Initialize with iterator \a i and \a j Inter(I& i, J& j); /// Initialize with iterator \a i and \a j void init(I& i, J& j); //@} /// \name Iteration control //@{ /// Move iterator to next range (if possible) void operator++(void); //@} }; /** * \brief Range iterator for intersection for any number of iterators * * Requires \code #include "gecode/iter.hh" \endcode * \ingroup FuncIterRanges */ template class NaryInter : public MinMax { protected: /// Array of iterators I* is; /// Number of iterators int n; public: /// \name Constructors and initialization //@{ /// Default constructor NaryInter(void); /// Initialize with \a n iterators in \a i NaryInter(I* i, int n); /// Initialize with \a n iterators in \a i void init(I* i, int n); //@} /// \name Iteration control //@{ /// Move iterator to next range (if possible) void operator++(void); //@} }; /* * Binary intersection * */ template inline void Inter::operator++(void) { if (!i() || !j()) goto done; do { while (i() && (i.max() < j.min())) ++i; if (!i()) goto done; while (j() && (j.max() < i.min())) ++j; if (!j()) goto done; } while (i.max() < j.min()); // Now the intervals overlap: consume the smaller interval ma = std::min(i.max(),j.max()); mi = std::max(i.min(),j.min()); if (i.max() < j.max()) ++i; else ++j; return; done: finish(); } template forceinline Inter::Inter(void) {} template forceinline Inter::Inter(I& i0, J& j0) : i(i0), j(j0) { operator++(); } template forceinline void Inter::init(I& i0, J& j0) { i = i0; j = j0; operator++(); } /* * Nary intersection * */ template inline void NaryInter::operator++(void) { // The next interval to be returned must have a hole // between it and the previously returned range. mi = ma+2; ma = is[0].max(); // Intersect with all other intervals restart: for (int i = n; i--;) { // Skip intervals that are too small while (is[i]() && (is[i].max() < mi)) ++is[i]; if (!is[i]()) goto done; if (is[i].min() > ma) { mi=is[i].min(); ma=is[i].max(); goto restart; } // Now the intervals overlap if (mi < is[i].min()) mi = is[i].min(); if (ma > is[i].max()) { ma = is[i].max(); } } return; done: finish(); } template forceinline NaryInter::NaryInter(void) {} template inline NaryInter::NaryInter(I* is0, int n0) : is(is0), n(n0) { if (!is[0]()) { finish(); } else { ma=is[0].min()-2; operator++(); } } template inline void NaryInter::init(I* is0, int n0) { is = is0; n = n0; if (!is[0]()) { finish(); } else { ma=is[0].min()-2; operator++(); } } }}} // STATISTICS: iter-any