Generated on Sat Feb 7 2015 02:01:26 for Gecode by doxygen 1.8.9.1
ranges-list.hpp
Go to the documentation of this file.
1 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /*
3  * Main authors:
4  * Christian Schulte <schulte@gecode.org>
5  *
6  * Copyright:
7  * Christian Schulte, 2010
8  *
9  * Last modified:
10  * $Date: 2013-07-11 19:23:04 +0200 (Thu, 11 Jul 2013) $ by $Author: schulte $
11  * $Revision: 13866 $
12  *
13  * This file is part of Gecode, the generic constraint
14  * development environment:
15  * http://www.gecode.org
16  *
17  * Permission is hereby granted, free of charge, to any person obtaining
18  * a copy of this software and associated documentation files (the
19  * "Software"), to deal in the Software without restriction, including
20  * without limitation the rights to use, copy, modify, merge, publish,
21  * distribute, sublicense, and/or sell copies of the Software, and to
22  * permit persons to whom the Software is furnished to do so, subject to
23  * the following conditions:
24  *
25  * The above copyright notice and this permission notice shall be
26  * included in all copies or substantial portions of the Software.
27  *
28  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35  *
36  */
37 
38 namespace Gecode { namespace Iter { namespace Ranges {
39 
45  class RangeListIter {
46  protected:
48  class RangeList : public Support::BlockClient<RangeList,Region> {
49  public:
51  int min, max;
54  };
56  class RLIO : public Support::BlockAllocator<RangeList,Region> {
57  public:
59  unsigned int use_cnt;
61  RLIO(Region& r);
62  };
70  void set(RangeList* l);
72  RangeList* get(void) const;
74  RangeList* range(int min, int max, RangeList*& f);
76  RangeList* range(int min, int max);
78  template<class I>
79  RangeList* range(I& i, RangeList*& f);
81  template<class I>
82  RangeList* range(I& i);
84  template<class I>
85  RangeList* copy(I& i);
86  public:
88 
89  RangeListIter(void);
96  void init(Region& r);
100 
102 
103  bool operator ()(void) const;
106  void operator ++(void);
108  void reset(void);
110 
112 
113  int min(void) const;
116  int max(void) const;
118  unsigned int width(void) const;
120 
122  ~RangeListIter(void);
123  };
124 
125 
128  : Support::BlockAllocator<RangeList,Region>(r), use_cnt(1) {}
129 
130 
133  : rlio(NULL), h(NULL), c(NULL) {}
134 
137  : rlio(new (r.ralloc(sizeof(RLIO))) RLIO(r)), h(NULL), c(NULL) {}
138 
139  forceinline void
141  rlio = new (r.ralloc(sizeof(RLIO))) RLIO(r);
142  h = c = NULL;
143  }
144 
147  : rlio(i.rlio), h(i.h), c(i.c) {
148  if (rlio != NULL)
149  rlio->use_cnt++;
150  }
151 
154  if (&i != this) {
155  if ((rlio != NULL) && (--rlio->use_cnt == 0)) {
156  Region& r = rlio->allocator();
157  rlio->~RLIO();
158  r.rfree(rlio,sizeof(RLIO));
159  }
160  rlio = i.rlio;
161  if (rlio != NULL)
162  rlio->use_cnt++;
163  c=i.c; h=i.h;
164  }
165  return *this;
166  }
167 
170  if ((rlio != NULL) && (--rlio->use_cnt == 0)) {
171  Region& r = rlio->allocator();
172  rlio->~RLIO();
173  r.rfree(rlio,sizeof(RLIO));
174  }
175  }
176 
177 
178  forceinline void
180  h = c = l;
181  }
182 
184  RangeListIter::get(void) const {
185  return h;
186  }
187 
190  RangeList* t;
191  // Take element from freelist if possible
192  if (f != NULL) {
193  t = f; f = f->next;
194  } else {
195  t = new (*rlio) RangeList;
196  }
197  t->min = min; t->max = max;
198  return t;
199  }
200 
203  RangeList* t = new (*rlio) RangeList;
204  t->min = min; t->max = max;
205  return t;
206  }
207 
208  template<class I>
211  return range(i.min(),i.max(),f);
212  }
213 
214  template<class I>
217  return range(i.min(),i.max());
218  }
219 
220  template<class I>
223  RangeList* h;
224  RangeList** c = &h;
225  for ( ; i(); ++i) {
226  RangeList* t = range(i);
227  *c = t; c = &t->next;
228  }
229  *c = NULL;
230  return h;
231  }
232 
233  forceinline bool
235  return c != NULL;
236  }
237 
238  forceinline void
240  c = c->next;
241  }
242 
243  forceinline void
245  c = h;
246  }
247 
248  forceinline int
249  RangeListIter::min(void) const {
250  return c->min;
251  }
252  forceinline int
253  RangeListIter::max(void) const {
254  return c->max;
255  }
256  forceinline unsigned int
257  RangeListIter::width(void) const {
258  return static_cast<unsigned int>(c->max-c->min)+1;
259  }
260 
261 }}}
262 
263 // STATISTICS: iter-any
264 
void rfree(void *p, size_t s)
Free memory previously allocated.
Definition: region.hpp:310
NodeType t
Type of node.
Definition: bool-expr.cpp:234
NNF * l
Left subtree.
Definition: bool-expr.cpp:244
A & allocator(void)
Return allocator used.
RangeList * range(int min, int max, RangeList *&f)
Create new range possibly from freelist f and init.
int min(void) const
Return smallest value of range.
bool operator()(void) const
Test whether iterator is still at a range or done.
void max(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Definition: arithmetic.cpp:57
Shared object for allocation.
Definition: ranges-list.hpp:56
RangeListIter & operator=(const RangeListIter &i)
Assignment operator.
Handle to region.
Definition: region.hpp:61
RangeList * get(void) const
Get head of current range list.
Gecode::FloatVal c(-8, 8)
Manage memory organized into block lists (allocator)
Gecode::IntArgs i(4, 1, 2, 3, 4)
void reset(void)
Reset iterator to start.
NNF * r
Right subtree.
Definition: bool-expr.cpp:246
RLIO * rlio
Reference to shared object.
Definition: ranges-list.hpp:64
RangeList * copy(I &i)
Copy the iterator i to a range list.
int min
Minimum and maximum of a range.
Definition: ranges-list.hpp:51
unsigned int use_cnt
Counter used for reference counting.
Definition: ranges-list.hpp:59
void min(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Definition: arithmetic.cpp:75
void set(RangeList *l)
Set range lists.
Client for block allocator of type T.
void init(Region &r)
Initialize.
#define forceinline
Definition: config.hpp:132
int max(void) const
Return largest value of range.
unsigned int width(void) const
Return width of range (distance between minimum and maximum)
Gecode toplevel namespace
void operator++(void)
Move iterator to next range (if possible)
RangeList * h
Head of range list.
Definition: ranges-list.hpp:66
Iterator over range lists.
Definition: ranges-list.hpp:45
RangeList * c
Current list element.
Definition: ranges-list.hpp:68
void * ralloc(size_t s)
Allocate memory from region.
Definition: region.hpp:302
RangeListIter(void)
Default constructor.