ranges-list.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, 2010
*
* 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 Iter { namespace Ranges {
class RangeListIter {
protected:
class RangeList : public Support::BlockClient<RangeList,Region> {
public:
int min, max;
RangeList* next;
};
class RLIO : public Support::BlockAllocator<RangeList,Region> {
public:
unsigned int use_cnt;
RLIO(Region& r);
};
RLIO* rlio;
RangeList* h;
RangeList* c;
void set(RangeList* l);
RangeList* get(void) const;
RangeList* range(int min, int max, RangeList*& f);
RangeList* range(int min, int max);
template<class I>
RangeList* range(I& i, RangeList*& f);
template<class I>
RangeList* range(I& i);
template<class I>
RangeList* copy(I& i);
public:
RangeListIter(void);
RangeListIter(const RangeListIter& i);
RangeListIter(Region& r);
void init(Region& r);
RangeListIter& operator =(const RangeListIter& i);
bool operator ()(void) const;
void operator ++(void);
void reset(void);
int min(void) const;
int max(void) const;
unsigned int width(void) const;
~RangeListIter(void);
};
forceinline
RangeListIter::RLIO::RLIO(Region& r)
: Support::BlockAllocator<RangeList,Region>(r), use_cnt(1) {}
forceinline
RangeListIter::RangeListIter(void)
: rlio(NULL), h(NULL), c(NULL) {}
forceinline
RangeListIter::RangeListIter(Region& r)
: rlio(new (r.ralloc(sizeof(RLIO))) RLIO(r)), h(NULL), c(NULL) {}
forceinline void
RangeListIter::init(Region& r) {
rlio = new (r.ralloc(sizeof(RLIO))) RLIO(r);
h = c = NULL;
}
forceinline
RangeListIter::RangeListIter(const RangeListIter& i)
: rlio(i.rlio), h(i.h), c(i.c) {
if (rlio != NULL)
rlio->use_cnt++;
}
forceinline RangeListIter&
RangeListIter::operator =(const RangeListIter& i) {
if (&i != this) {
if ((rlio != NULL) && (--rlio->use_cnt == 0)) {
Region& r = rlio->allocator();
rlio->~RLIO();
r.rfree(rlio,sizeof(RLIO));
}
rlio = i.rlio;
if (rlio != NULL)
rlio->use_cnt++;
c=i.c; h=i.h;
}
return *this;
}
forceinline
RangeListIter::~RangeListIter(void) {
if ((rlio != NULL) && (--rlio->use_cnt == 0)) {
Region& r = rlio->allocator();
rlio->~RLIO();
r.rfree(rlio,sizeof(RLIO));
}
}
forceinline void
RangeListIter::set(RangeList* l) {
h = c = l;
}
forceinline RangeListIter::RangeList*
RangeListIter::get(void) const {
return h;
}
forceinline RangeListIter::RangeList*
RangeListIter::range(int min, int max, RangeList*& f) {
RangeList* t;
// Take element from freelist if possible
if (f != NULL) {
t = f; f = f->next;
} else {
t = new (*rlio) RangeList;
}
t->min = min; t->max = max;
return t;
}
forceinline RangeListIter::RangeList*
RangeListIter::range(int min, int max) {
RangeList* t = new (*rlio) RangeList;
t->min = min; t->max = max;
return t;
}
template<class I>
forceinline RangeListIter::RangeList*
RangeListIter::range(I& i, RangeList*& f) {
return range(i.min(),i.max(),f);
}
template<class I>
forceinline RangeListIter::RangeList*
RangeListIter::range(I& i) {
return range(i.min(),i.max());
}
template<class I>
inline RangeListIter::RangeList*
RangeListIter::copy(I& i) {
RangeList* h;
RangeList** c = &h;
for ( ; i(); ++i) {
RangeList* t = range(i);
*c = t; c = &t->next;
}
*c = NULL;
return h;
}
forceinline bool
RangeListIter::operator ()(void) const {
return c != NULL;
}
forceinline void
RangeListIter::operator ++(void) {
c = c->next;
}
forceinline void
RangeListIter::reset(void) {
c = h;
}
forceinline int
RangeListIter::min(void) const {
return c->min;
}
forceinline int
RangeListIter::max(void) const {
return c->max;
}
forceinline unsigned int
RangeListIter::width(void) const {
return static_cast<unsigned int>(c->max-c->min)+1;
}
}}}
// STATISTICS: iter-any
Gecode::Iter::Ranges::RangeListIter::RLIO
Shared object for allocation.
Definition ranges-list.hpp:52
Gecode::Iter::Ranges::RangeListIter::RLIO::use_cnt
unsigned int use_cnt
Counter used for reference counting.
Definition ranges-list.hpp:55
Gecode::Iter::Ranges::RangeListIter::RLIO::RLIO
RLIO(Region &r)
Initialize.
Definition ranges-list.hpp:123
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
Iterator over range lists.
Definition ranges-list.hpp:41
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::width
unsigned int width(void) const
Return width of range (distance between minimum and maximum).
Definition ranges-list.hpp:253
Gecode::Iter::Ranges::RangeListIter::init
void init(Region &r)
Initialize.
Definition ranges-list.hpp:136
Gecode::Iter::Ranges::RangeListIter::operator++
void operator++(void)
Move iterator to next range (if possible).
Definition ranges-list.hpp:235
Gecode::Iter::Ranges::RangeListIter::~RangeListIter
~RangeListIter(void)
Destructor.
Definition ranges-list.hpp:165
Gecode::Iter::Ranges::RangeListIter::RangeListIter
RangeListIter(void)
Default constructor.
Definition ranges-list.hpp:128
Gecode::Iter::Ranges::RangeListIter::rlio
RLIO * rlio
Reference to shared object.
Definition ranges-list.hpp:60
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::operator()
bool operator()(void) const
Test whether iterator is still at a range or done.
Definition ranges-list.hpp:230
Gecode::Iter::Ranges::RangeListIter::reset
void reset(void)
Reset iterator to start.
Definition ranges-list.hpp:240
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::Support::BlockAllocator
Manage memory organized into block lists (allocator).
Definition block-allocator.hpp:45
Gecode::Support::BlockAllocator< RangeList, Region >::BlockAllocator
BlockAllocator(Region &a)
Definition block-allocator.hpp:98
Gecode::Support::BlockClient
Client for block allocator of type T.
Definition block-allocator.hpp:84
Test::Base::next
Base * next(void) const
Return next test.
Definition test.hpp:58
Gecode::Iter::Ranges
Range iterators.
Definition iter.hh:43
Gecode::Iter
Range and value iterators.
Definition iter.hh:41
Gecode::Support
Support algorithms and datastructures
Gecode
Gecode toplevel namespace
forceinline
#define forceinline
Definition config.hpp:187