val-set.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, 2011
*
* 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 {
/*
* Value sets
*
*/
forceinline
ValSet::ValSet(void)
: fst(NULL), lst(NULL), n(0) {}
forceinline void
ValSet::add(Space& home, int v) {
RangeList* c = fst;
RangeList** p = &fst;
while (c != NULL) {
if (v < c->min()) {
if (v+1 == c->min()) {
c->min(v); n++;
return;
} else {
*p = new (home) RangeList(v,v,c); n++;
return;
}
} else if (v <= c->max()) {
// Value already included
return;
} else if (v == c->max()+1) {
if ((c->next() != NULL) && (v+1 == c->next()->min())) {
c->next()->min(c->min());
*p = c->next();
c->dispose(home,c);
} else {
c->max(v);
}
n++;
return;
} else {
// FIXME: HOW TO CAST HERE?
p = reinterpret_cast<RangeList**>(c->nextRef());
c = *p;
}
}
*p = new (home) RangeList(v,v,NULL); n++;
lst = *p;
}
forceinline int
ValSet::size(void) const {
return n;
}
forceinline bool
ValSet::empty(void) const {
return n == 0;
}
forceinline int
ValSet::min(void) const {
return fst->min();
}
forceinline int
ValSet::max(void) const {
return lst->max();
}
forceinline void
ValSet::update(Space& home, ValSet& vs) {
if (vs.n > 0) {
n = vs.n;
// Count number of ranges
int m = 0;
for (RangeList* c = vs.fst; c != NULL; c = c->next())
m++;
fst = home.alloc<RangeList>(m);
lst = fst + (m-1);
int i=0;
for (RangeList* c = vs.fst; c != NULL; c = c->next()) {
fst[i].min(c->min()); fst[i].max(c->max());
fst[i].next(fst+i+1);
i++;
}
lst->next(NULL);
}
}
forceinline void
ValSet::flush(void) {
fst = lst = NULL;
}
forceinline void
ValSet::dispose(Space& home) {
if (fst != NULL)
fst->dispose(home,lst);
}
forceinline
ValSet::Ranges::Ranges(const ValSet& vs)
: c(vs.fst) {}
forceinline bool
ValSet::Ranges::operator ()(void) const {
return c != NULL;
}
forceinline void
ValSet::Ranges::operator ++(void) {
c = c->next();
}
forceinline int
ValSet::Ranges::min(void) const {
return c->min();
}
forceinline int
ValSet::Ranges::max(void) const {
return c->max();
}
forceinline unsigned int
ValSet::Ranges::width(void) const {
return c->width();
}
template<class View>
forceinline Iter::Ranges::CompareStatus
ValSet::compare(View x) const {
if (empty() || (x.max() < min()) || (x.min() > max()))
return Iter::Ranges::CS_DISJOINT;
ValSet::Ranges vsr(*this);
ViewRanges<View> xr(x);
return Iter::Ranges::compare(xr,vsr);
}
template<class View>
forceinline bool
ValSet::subset(View x) const {
if (empty() || (x.min() < min()) || (x.max() > max()))
return false;
ValSet::Ranges vsr(*this);
ViewRanges<View> xr(x);
return Iter::Ranges::subset(xr,vsr);
}
}}
// STATISTICS: int-other
Gecode::FloatVal::max
friend FloatVal max(const FloatVal &x, const FloatVal &y)
Definition val.hpp:386
Gecode::FloatVal::min
friend FloatVal min(const FloatVal &x, const FloatVal &y)
Definition val.hpp:398
Gecode::Int::ValSet::Ranges
Iterator over ranges.
Definition val-set.hh:78
Gecode::Int::ValSet::Ranges::width
unsigned int width(void) const
Return width of range (distance between minimum and maximum).
Definition val-set.hpp:155
Gecode::Int::ValSet::Ranges::max
int max(void) const
Return largest value of range.
Definition val-set.hpp:150
Gecode::Int::ValSet::Ranges::operator()
bool operator()(void) const
Test whether iterator is still at a range or done.
Definition val-set.hpp:136
Gecode::Int::ValSet::Ranges::operator++
void operator++(void)
Move iterator to next range (if possible).
Definition val-set.hpp:141
Gecode::Int::ValSet::Ranges::Ranges
Ranges(const ValSet &vs)
Initialize.
Definition val-set.hpp:132
Gecode::Int::ValSet::Ranges::min
int min(void) const
Return smallest value of range.
Definition val-set.hpp:146
Gecode::Int::ValSet::size
int size(void) const
Return size (number of values).
Definition val-set.hpp:81
Gecode::Int::ValSet::fst
RangeList * fst
First element of range list.
Definition val-set.hh:47
Gecode::Int::ValSet::subset
bool subset(View x) const
Whether all values of x are included in the value set.
Definition val-set.hpp:171
Gecode::Int::ValSet::ValSet
ValSet(void)
Initialize.
Definition val-set.hpp:41
Gecode::Int::ValSet::empty
bool empty(void) const
Test whether set is empty.
Definition val-set.hpp:86
Gecode::Int::ValSet::max
int max(void) const
Return largest value (provided the set is not empty).
Definition val-set.hpp:96
Gecode::Int::ValSet::compare
Iter::Ranges::CompareStatus compare(View x) const
Compare view x with value set.
Definition val-set.hpp:161
Gecode::Int::ValSet::flush
void flush(void)
Flush entries.
Definition val-set.hpp:121
Gecode::Int::ValSet::dispose
void dispose(Space &home)
Dispose value set.
Definition val-set.hpp:126
Gecode::Int::ValSet::update
void update(Space &home, ValSet &vs)
Update value set during cloning.
Definition val-set.hpp:101
Gecode::Int::ValSet::n
int n
Number of stored values (integer precision is sufficient).
Definition val-set.hh:51
Gecode::Int::ValSet::lst
RangeList * lst
Last element of range list.
Definition val-set.hh:49
Gecode::Int::ValSet::min
int min(void) const
Return smallest value (provided the set is not empty).
Definition val-set.hpp:91
Gecode::Int::ValSet::add
void add(Space &home, int v)
Add value v to value set.
Definition val-set.hpp:45
Gecode::Int::ViewRanges
Range iterator for integer views.
Definition view.hpp:54
Gecode::RangeList
Lists of ranges (intervals).
Definition range-list.hpp:49
Gecode::Space
Computation spaces.
Definition core.hpp:1742
Gecode::Space::alloc
T * alloc(long unsigned int n)
Allocate block of n objects of type T from space heap.
Definition core.hpp:2837
Gecode::View
Gecode::Int
Finite domain integers.
Gecode::Iter::Ranges::CompareStatus
CompareStatus
Comapre two iterators with each other.
Definition ranges-operations.hpp:60
Gecode::Iter::Ranges::CS_DISJOINT
@ CS_DISJOINT
Intersection is empty.
Definition ranges-operations.hpp:62
Gecode::Iter::Ranges::subset
bool subset(I &i, J &j)
Check whether range iterator i is subset of range iterator j.
Definition ranges-operations.hpp:97
Gecode::Iter::Ranges::compare
CompareStatus compare(I &i, J &j)
Check whether range iterator i is a subset of j, or whether they are disjoint.
Definition ranges-operations.hpp:127
Gecode
Gecode toplevel namespace
forceinline
#define forceinline
Definition config.hpp:187