Generated on Tue Feb 11 2025 17:33:26 for Gecode by doxygen 1.12.0
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 * This file is part of Gecode, the generic constraint
10 * development environment:
11 * http://www.gecode.org
12 *
13 * Permission is hereby granted, free of charge, to any person obtaining
14 * a copy of this software and associated documentation files (the
15 * "Software"), to deal in the Software without restriction, including
16 * without limitation the rights to use, copy, modify, merge, publish,
17 * distribute, sublicense, and/or sell copies of the Software, and to
18 * permit persons to whom the Software is furnished to do so, subject to
19 * the following conditions:
20 *
21 * The above copyright notice and this permission notice shall be
22 * included in all copies or substantial portions of the Software.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 *
32 */
33
34namespace Gecode { namespace Iter { namespace Ranges {
35
42 protected:
44 class RangeList : public Support::BlockClient<RangeList,Region> {
45 public:
47 int min, max;
50 };
52 class RLIO : public Support::BlockAllocator<RangeList,Region> {
53 public:
55 unsigned int use_cnt;
57 RLIO(Region& r);
58 };
66 void set(RangeList* l);
68 RangeList* get(void) const;
70 RangeList* range(int min, int max, RangeList*& f);
72 RangeList* range(int min, int max);
74 template<class I>
75 RangeList* range(I& i, RangeList*& f);
77 template<class I>
78 RangeList* range(I& i);
80 template<class I>
81 RangeList* copy(I& i);
82 public:
84
85
86 RangeListIter(void);
92 void init(Region& r);
96
98
99
100 bool operator ()(void) const;
102 void operator ++(void);
104 void reset(void);
106
108
109
110 int min(void) const;
112 int max(void) const;
114 unsigned int width(void) const;
116
118 ~RangeListIter(void);
119 };
120
121
124 : Support::BlockAllocator<RangeList,Region>(r), use_cnt(1) {}
125
126
129 : rlio(NULL), h(NULL), c(NULL) {}
130
133 : rlio(new (r.ralloc(sizeof(RLIO))) RLIO(r)), h(NULL), c(NULL) {}
134
135 forceinline void
137 rlio = new (r.ralloc(sizeof(RLIO))) RLIO(r);
138 h = c = NULL;
139 }
140
143 : rlio(i.rlio), h(i.h), c(i.c) {
144 if (rlio != NULL)
145 rlio->use_cnt++;
146 }
147
150 if (&i != this) {
151 if ((rlio != NULL) && (--rlio->use_cnt == 0)) {
152 Region& r = rlio->allocator();
153 rlio->~RLIO();
154 r.rfree(rlio,sizeof(RLIO));
155 }
156 rlio = i.rlio;
157 if (rlio != NULL)
158 rlio->use_cnt++;
159 c=i.c; h=i.h;
160 }
161 return *this;
162 }
163
166 if ((rlio != NULL) && (--rlio->use_cnt == 0)) {
167 Region& r = rlio->allocator();
168 rlio->~RLIO();
169 r.rfree(rlio,sizeof(RLIO));
170 }
171 }
172
173
174 forceinline void
176 h = c = l;
177 }
178
180 RangeListIter::get(void) const {
181 return h;
182 }
183
186 RangeList* t;
187 // Take element from freelist if possible
188 if (f != NULL) {
189 t = f; f = f->next;
190 } else {
191 t = new (*rlio) RangeList;
192 }
193 t->min = min; t->max = max;
194 return t;
195 }
196
199 RangeList* t = new (*rlio) RangeList;
200 t->min = min; t->max = max;
201 return t;
202 }
203
204 template<class I>
207 return range(i.min(),i.max(),f);
208 }
209
210 template<class I>
213 return range(i.min(),i.max());
214 }
215
216 template<class I>
219 RangeList* h;
220 RangeList** c = &h;
221 for ( ; i(); ++i) {
222 RangeList* t = range(i);
223 *c = t; c = &t->next;
224 }
225 *c = NULL;
226 return h;
227 }
228
229 forceinline bool
231 return c != NULL;
232 }
233
234 forceinline void
236 c = c->next;
237 }
238
239 forceinline void
241 c = h;
242 }
243
244 forceinline int
245 RangeListIter::min(void) const {
246 return c->min;
247 }
248 forceinline int
249 RangeListIter::max(void) const {
250 return c->max;
251 }
252 forceinline unsigned int
254 return static_cast<unsigned int>(c->max-c->min)+1;
255 }
256
257}}}
258
259// STATISTICS: iter-any
260
NNF * l
Left subtree.
NodeType t
Type of node.
Shared object for allocation.
unsigned int use_cnt
Counter used for reference counting.
int min
Minimum and maximum of a range.
Iterator over range lists.
RangeList * copy(I &i)
Copy the iterator i to a range list.
int max(void) const
Return largest value of range.
RangeList * get(void) const
Get head of current range list.
unsigned int width(void) const
Return width of range (distance between minimum and maximum)
void init(Region &r)
Initialize.
void operator++(void)
Move iterator to next range (if possible)
RangeListIter(void)
Default constructor.
RLIO * rlio
Reference to shared object.
RangeListIter & operator=(const RangeListIter &i)
Assignment operator.
RangeList * range(int min, int max, RangeList *&f)
Create new range possibly from freelist f and init.
void set(RangeList *l)
Set range lists.
RangeList * c
Current list element.
bool operator()(void) const
Test whether iterator is still at a range or done.
void reset(void)
Reset iterator to start.
RangeList * h
Head of range list.
int min(void) const
Return smallest value of range.
Handle to region.
Definition region.hpp:55
Manage memory organized into block lists (allocator)
A & allocator(void)
Return allocator used.
Client for block allocator of type T.
Base * next(void) const
Return next test.
Definition test.hpp:58
Gecode toplevel namespace
Post propagator for SetVar SetOpType SetVar SetRelType r
Definition set.hh:767
void min(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
void max(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
#define forceinline
Definition config.hpp:187