ranges-union.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 Union : public MinMax {
protected:
I i;
J j;
public:
Union(void);
Union(I& i, J& j);
void init(I& i, J& j);
void operator ++(void);
};
class NaryUnion : public RangeListIter {
protected:
RangeList* f;
template<class I, class J>
RangeList* two(I& i, J& j);
template<class I>
void insert(I& i, RangeList*& u);
public:
NaryUnion(void);
template<class I>
NaryUnion(Region& r, I& i);
template<class I, class J>
NaryUnion(Region& r, I& i, J& j);
template<class I>
NaryUnion(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);
NaryUnion& operator =(const NaryUnion& m);
};
/*
* Binary union
*
*/
template<class I, class J>
inline void
Union<I,J>::operator ++(void) {
if (!i() && !j()) {
finish(); return;
}
if (!i() || (j() && (j.max()+1 < i.min()))) {
mi = j.min(); ma = j.max(); ++j; return;
}
if (!j() || (i() && (i.max()+1 < j.min()))) {
mi = i.min(); ma = i.max(); ++i; return;
}
mi = std::min(i.min(),j.min());
ma = std::max(i.max(),j.max());
++i; ++j;
next:
if (i() && (i.min() <= ma+1)) {
ma = std::max(ma,i.max()); ++i;
goto next;
}
if (j() && (j.min() <= ma+1)) {
ma = std::max(ma,j.max()); ++j;
goto next;
}
}
template<class I, class J>
forceinline
Union<I,J>::Union(void) {}
template<class I, class J>
forceinline
Union<I,J>::Union(I& i0, J& j0)
: i(i0), j(j0) {
operator ++();
}
template<class I, class J>
forceinline void
Union<I,J>::init(I& i0, J& j0) {
i = i0; j = j0;
operator ++();
}
/*
* Nary union
*
*/
template<class I, class J>
RangeListIter::RangeList*
NaryUnion::two(I& i, J& j) {
RangeList* h;
RangeList** c = &h;
while (i() && j())
if (i.max()+1 < j.min()) {
RangeList* t = range(i); ++i;
*c = t; c = &t->next;
} else if (j.max()+1 < i.min()) {
RangeList* t = range(j); ++j;
*c = t; c = &t->next;
} else {
int min = std::min(i.min(),j.min());
int max = std::max(i.max(),j.max());
++i; ++j;
nexta:
if (i() && (i.min() <= max+1)) {
max = std::max(max,i.max()); ++i;
goto nexta;
}
if (j() && (j.min() <= max+1)) {
max = std::max(max,j.max()); ++j;
goto nexta;
}
RangeList* t = range(min,max);
*c = t; c = &t->next;
}
for ( ; i(); ++i) {
RangeList* t = range(i);
*c = t; c = &t->next;
}
for ( ; j(); ++j) {
RangeList* t = range(j);
*c = t; c = &t->next;
}
*c = NULL;
return h;
}
template<class I>
void
NaryUnion::insert(I& i, RangeList*& u) {
// The current rangelist
RangeList** c = &u;
while ((*c != NULL) && i())
if ((*c)->max+1 < i.min()) {
// Keep range from union
c = &(*c)->next;
} else if (i.max()+1 < (*c)->min) {
// Copy range from iterator
RangeList* t = range(i,f); ++i;
// Insert
t->next = *c; *c = t; c = &t->next;
} else {
// Ranges overlap
// Compute new minimum
(*c)->min = std::min((*c)->min,i.min());
// Compute new maximum
int max = std::max((*c)->max,i.max());
// Scan from the next range in the union
RangeList* s = (*c)->next;
++i;
nextb:
if ((s != NULL) && (s->min <= max+1)) {
max = std::max(max,s->max);
RangeList* t = s;
s = s->next;
// Put deleted element into freelist
t->next = f; f = t;
goto nextb;
}
if (i() && (i.min() <= max+1)) {
max = std::max(max,i.max()); ++i;
goto nextb;
}
// Store computed max and shunt skipped ranges from union
(*c)->max = max; (*c)->next = s;
}
if (*c == NULL) {
// Copy remaining ranges from iterator
for ( ; i(); ++i) {
RangeList* t = range(i,f);
*c = t; c = &t->next;
}
*c = NULL;
}
}
forceinline
NaryUnion::NaryUnion(void)
: f(NULL) {}
template<class I>
forceinline void
NaryUnion::init(Region& r, I& i) {
RangeListIter::init(r);
f = NULL;
set(copy(i));
}
template<class I, class J>
forceinline void
NaryUnion::init(Region& r, I& i, J& j) {
RangeListIter::init(r);
f = NULL;
set(two(i,j));
}
template<class I>
forceinline void
NaryUnion::init(Region& r, I* i, int n) {
f = NULL;
RangeListIter::init(r);
int m = 0;
while ((m < n) && !i[m]())
m++;
// Union is empty
if (m >= n)
return;
n--;
while (!i[n]())
n--;
if (m == n) {
// Union is just a single iterator
set(copy(i[m]));
} else {
// At least two iterators
RangeList* u = two(i[m++],i[n--]);
// Insert the remaining iterators
for ( ; m<=n; m++)
insert(i[m], u);
set(u);
}
}
template<class I>
forceinline
NaryUnion::NaryUnion(Region& r, I& i) {
init(r, i);
}
template<class I, class J>
forceinline
NaryUnion::NaryUnion(Region& r, I& i, J& j) {
init(r, i, j);
}
template<class I>
forceinline
NaryUnion::NaryUnion(Region& r, I* i, int n) {
init(r, i, n);
}
template<class I>
forceinline void
NaryUnion::operator |=(I& i) {
RangeList* u = get();
insert(i, u);
set(u);
}
forceinline NaryUnion&
NaryUnion::operator =(const NaryUnion& m) {
f = NULL;
return static_cast<NaryUnion&>(RangeListIter::operator =(m));
}
}}}
// STATISTICS: iter-any
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::NaryUnion
Range iterator for union of iterators.
Definition ranges-union.hpp:74
Gecode::Iter::Ranges::NaryUnion::f
RangeList * f
Freelist used for allocation.
Definition ranges-union.hpp:77
Gecode::Iter::Ranges::NaryUnion::operator=
NaryUnion & operator=(const NaryUnion &m)
Assignment operator (both iterators must be allocated from the same region).
Definition ranges-union.hpp:350
Gecode::Iter::Ranges::NaryUnion::insert
void insert(I &i, RangeList *&u)
Insert ranges from i into u.
Definition ranges-union.hpp:223
Gecode::Iter::Ranges::NaryUnion::two
RangeList * two(I &i, J &j)
Return range list for union of two iterators.
Gecode::Iter::Ranges::NaryUnion::NaryUnion
NaryUnion(void)
Default constructor.
Definition ranges-union.hpp:275
Gecode::Iter::Ranges::NaryUnion::operator|=
void operator|=(I &i)
Add iterator i.
Definition ranges-union.hpp:343
Gecode::Iter::Ranges::NaryUnion::init
void init(Region &r, I &i)
Initialize with single iterator i.
Definition ranges-union.hpp:280
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::Iter::Ranges::Union::Union
Union(void)
Default constructor.
Definition ranges-union.hpp:155
Gecode::Iter::Ranges::Union::j
J j
Second iterator.
Definition ranges-union.hpp:49
Gecode::Iter::Ranges::Union::i
I i
First iterator.
Definition ranges-union.hpp:47
Gecode::Iter::Ranges::Union::operator++
void operator++(void)
Move iterator to next range (if possible).
Definition ranges-union.hpp:124
Gecode::Iter::Ranges::Union::init
void init(I &i, J &j)
Initialize with iterator i and j.
Definition ranges-union.hpp:166
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