Something went wrong. Try again.
This repository has no description
Something went wrong. Try again.
7.8 kB · 305 lines
C++
at develop
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- *//* * Main authors: * Christian Schulte <schulte@gecode.org> * * Copyright: * Christian Schulte, 2004 * * 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 <algorithm>
namespace Gecode { namespace Iter { namespace Ranges {
/** * \brief Range iterator for computing intersection (binary) * * \ingroup FuncIterRanges */ template<class I, class J> 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 of iterators * * \ingroup FuncIterRanges */ class NaryInter : public RangeListIter { protected: /// Freelist RangeList* f; public: /// \name Constructors and initialization //@{ /// Default constructor NaryInter(void); /// Initialize with single iterator \a i template<class I> NaryInter(Region& r, I& i); /// Initialize with iterators \a i and \a j template<class I, class J> NaryInter(Region& r, I& i, J& j); /// Initialize with \a n iterators in \a i template<class I> NaryInter(Region& r, I* i, int n); /// Initialize with single iterator \a i template<class I> void init(Region& r, I& i); /// Initialize with iterators \a i and \a j template<class I, class J> void init(Region& r, I& i, J& j); /// Initialize with \a n iterators in \a i template<class I> void init(Region& r, I* i, int n); /// Add iterator \a i template<class I> void operator &=(I& i); /// Assignment operator (both iterators must be allocated from the same region) NaryInter& operator =(const NaryInter& m); //@} };
/* * Binary intersection * */
template<class I, class J> inline void Inter<I,J>::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<class I, class J> forceinline Inter<I,J>::Inter(void) {}
template<class I, class J> forceinline Inter<I,J>::Inter(I& i0, J& j0) : i(i0), j(j0) { operator ++(); }
template<class I, class J> forceinline void Inter<I,J>::init(I& i0, J& j0) { i = i0; j = j0; operator ++(); }
/* * Nary intersection * */
forceinline NaryInter::NaryInter(void) {}
template<class I> forceinline void NaryInter::init(Region& r, I& i) { RangeListIter::init(r); f = nullptr; set(copy(i)); }
template<class I, class J> forceinline void NaryInter::init(Region& r, I& i, J& j) { RangeListIter::init(r); f = nullptr; RangeList* h; RangeList** c = &h; while (i() && j()) { 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 RangeList* t = range(std::max(i.min(),j.min()), std::min(i.max(),j.max())); *c = t; c = &t->next; if (i.max() < j.max()) ++i; else ++j; } done: *c = nullptr; set(h); }
template<class I> forceinline void NaryInter::init(Region& r, I* i, int n) { RangeListIter::init(r); f = nullptr; if ((n > 0) && i[0]()) { RangeList* h; RangeList** c = &h;
int min = i[0].min(); while (i[0]()) { // Initialize with last interval int max = i[0].max(); // Intersect with all other intervals restart: for (int j=n; j--;) { // Skip intervals that are too small while (i[j]() && (i[j].max() < min)) ++i[j]; if (!i[j]()) goto done; if (i[j].min() > max) { min=i[j].min(); max=i[j].max(); goto restart; } // Now the intervals overlap if (min < i[j].min()) min = i[j].min(); if (max > i[j].max()) max = i[j].max(); } RangeList* t = range(min,max); *c = t; c = &t->next; // The next interval must be at least two elements away min = max + 2; } done: *c = nullptr; set(h); } }
template<class I> forceinline NaryInter::NaryInter(Region& r, I& i) { init(r, i); } template<class I, class J> forceinline NaryInter::NaryInter(Region& r, I& i, J& j) { init(r, i, j); } template<class I> forceinline NaryInter::NaryInter(Region& r, I* i, int n) { init(r, i, n); }
template<class I> forceinline void NaryInter::operator &=(I& i) { RangeList* j = get(); // The new rangelist RangeList* h; RangeList** c = &h; while (i() && (j != nullptr)) { do { while (i() && (i.max() < j->min)) ++i; if (!i()) goto done; while ((j != nullptr) && (j->max < i.min())) { RangeList* t = j->next; j->next = f; f = j; j = t; } if (j == nullptr) goto done; } while (i.max() < j->min); // Now the intervals overlap: consume the smaller interval RangeList* t = range(std::max(i.min(),j->min), std::min(i.max(),j->max),f); *c = t; c = &t->next; if (i.max() < j->max) { ++i; } else { RangeList* tn = j->next; j->next = f; f = j; j = tn; } } done: // Put remaining elements into freelist while (j != nullptr) { RangeList* t = j->next; j->next = f; f = j; j = t; } *c = nullptr; set(h); }
forceinline NaryInter& NaryInter::operator =(const NaryInter& m) { f = nullptr; return static_cast<NaryInter&>(RangeListIter::operator =(m)); }
}}}
// STATISTICS: iter-any