ranges-inter.hpp
Go to the documentation of this file.
/* -*- 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 {
template<class I, class J>
class Inter : public MinMax {
protected:
I i;
J j;
public:
Inter(void);
Inter(I& i, J& j);
void init(I& i, J& j);
void operator ++(void);
};
class NaryInter : public RangeListIter {
protected:
RangeList* f;
public:
NaryInter(void);
template<class I>
NaryInter(Region& r, I& i);
template<class I, class J>
NaryInter(Region& r, I& i, J& j);
template<class I>
NaryInter(Region& r, I* i, int n);
template<class I>
void init(Region& r, I& i);
template<class I, class J>
void init(Region& r, I& i, J& j);
template<class I>
void init(Region& r, I* i, int n);
template<class I>
void operator &=(I& i);
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 = NULL;
set(copy(i));
}
template<class I, class J>
forceinline void
NaryInter::init(Region& r, I& i, J& j) {
RangeListIter::init(r);
f = NULL;
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 = NULL;
set(h);
}
template<class I>
forceinline void
NaryInter::init(Region& r, I* i, int n) {
RangeListIter::init(r);
f = NULL;
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 = NULL;
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 != NULL)) {
do {
while (i() && (i.max() < j->min))
++i;
if (!i()) goto done;
while ((j != NULL) && (j->max < i.min())) {
RangeList* t = j->next;
j->next = f; f = j;
j = t;
}
if (j == NULL) 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 != NULL) {
RangeList* t = j->next;
j->next = f; f = j;
j = t;
}
*c = NULL;
set(h);
}
forceinline NaryInter&
NaryInter::operator =(const NaryInter& m) {
f = NULL;
return static_cast<NaryInter&>(RangeListIter::operator =(m));
}
}}}
// STATISTICS: iter-any
Gecode::Iter::Ranges::Inter::operator++
void operator++(void)
Move iterator to next range (if possible).
Definition ranges-inter.hpp:118
Gecode::Iter::Ranges::Inter::init
void init(I &i, J &j)
Initialize with iterator i and j.
Definition ranges-inter.hpp:148
Gecode::Iter::Ranges::Inter::j
J j
Second iterator.
Definition ranges-inter.hpp:49
Gecode::Iter::Ranges::Inter::Inter
Inter(void)
Default constructor.
Definition ranges-inter.hpp:137
Gecode::Iter::Ranges::Inter::i
I i
First iterator.
Definition ranges-inter.hpp:47
Gecode::Iter::Ranges::MinMax::ma
int ma
Maximum of current range.
Definition ranges-minmax.hpp:52
Gecode::Iter::Ranges::MinMax::mi
int mi
Minimum of current range.
Definition ranges-minmax.hpp:50
Gecode::Iter::Ranges::MinMax::MinMax
MinMax(void)
Default constructor.
Definition ranges-minmax.hpp:87
Gecode::Iter::Ranges::MinMax::finish
void finish(void)
Set range such that iteration stops
Definition ranges-minmax.hpp:82
Gecode::Iter::Ranges::NaryInter
Range iterator for intersection of iterators.
Definition ranges-inter.hpp:74
Gecode::Iter::Ranges::NaryInter::operator=
NaryInter & operator=(const NaryInter &m)
Assignment operator (both iterators must be allocated from the same region).
Definition ranges-inter.hpp:297
Gecode::Iter::Ranges::NaryInter::NaryInter
NaryInter(void)
Default constructor.
Definition ranges-inter.hpp:160
Gecode::Iter::Ranges::NaryInter::init
void init(Region &r, I &i)
Initialize with single iterator i.
Definition ranges-inter.hpp:164
Gecode::Iter::Ranges::NaryInter::f
RangeList * f
Freelist.
Definition ranges-inter.hpp:77
Gecode::Iter::Ranges::NaryInter::operator&=
void operator&=(I &i)
Add iterator i.
Definition ranges-inter.hpp:256
Gecode::Iter::Ranges::RangeListIter::RangeList
Range list class.
Definition ranges-list.hpp:44
Gecode::Iter::Ranges::RangeListIter::RangeList::max
int max
Definition ranges-list.hpp:47
Gecode::Iter::Ranges::RangeListIter::RangeList::min
int min
Minimum and maximum of a range.
Definition ranges-list.hpp:47
Gecode::Iter::Ranges::RangeListIter::RangeList::next
RangeList * next
Next element.
Definition ranges-list.hpp:49
Gecode::Iter::Ranges::RangeListIter::copy
RangeList * copy(I &i)
Copy the iterator i to a range list.
Definition ranges-list.hpp:218
Gecode::Iter::Ranges::RangeListIter::max
int max(void) const
Return largest value of range.
Definition ranges-list.hpp:249
Gecode::Iter::Ranges::RangeListIter::get
RangeList * get(void) const
Get head of current range list.
Definition ranges-list.hpp:180
Gecode::Iter::Ranges::RangeListIter::init
void init(Region &r)
Initialize.
Definition ranges-list.hpp:136
Gecode::Iter::Ranges::RangeListIter::RangeListIter
RangeListIter(void)
Default constructor.
Definition ranges-list.hpp:128
Gecode::Iter::Ranges::RangeListIter::operator=
RangeListIter & operator=(const RangeListIter &i)
Assignment operator.
Definition ranges-list.hpp:149
Gecode::Iter::Ranges::RangeListIter::range
RangeList * range(int min, int max, RangeList *&f)
Create new range possibly from freelist f and init.
Definition ranges-list.hpp:185
Gecode::Iter::Ranges::RangeListIter::set
void set(RangeList *l)
Set range lists.
Definition ranges-list.hpp:175
Gecode::Iter::Ranges::RangeListIter::c
RangeList * c
Current list element.
Definition ranges-list.hpp:64
Gecode::Iter::Ranges::RangeListIter::h
RangeList * h
Head of range list.
Definition ranges-list.hpp:62
Gecode::Iter::Ranges::RangeListIter::min
int min(void) const
Return smallest value of range.
Definition ranges-list.hpp:245
Gecode::Region
Handle to region.
Definition region.hpp:55
Gecode::Iter::Ranges
Range iterators.
Definition iter.hh:43
Gecode::Iter
Range and value iterators.
Definition iter.hh:41
Gecode
Gecode toplevel namespace
forceinline
#define forceinline
Definition config.hpp:187