main page
modules
namespaces
classes
files
Gecode home
Generated on Sat Nov 9 2013 19:18:29 for Gecode by
doxygen
1.8.4
gecode
iter
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
;
53
RangeList
*
next
;
54
};
56
class
RLIO
:
public
Support::BlockAllocator
<RangeList,Region> {
57
public
:
59
unsigned
int
use_cnt
;
61
RLIO
(
Region
&
r
);
62
};
64
RLIO
*
rlio
;
66
RangeList
*
h
;
68
RangeList
*
c
;
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
);
92
RangeListIter
(
const
RangeListIter
&
i
);
94
RangeListIter
(
Region
&
r
);
96
void
init
(
Region
&
r
);
98
RangeListIter
&
operator =
(
const
RangeListIter
&
i
);
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
126
forceinline
127
RangeListIter::RLIO::RLIO
(
Region
&
r
)
128
: Support::BlockAllocator<
RangeList
,
Region
>(r), use_cnt(1) {}
129
130
131
forceinline
132
RangeListIter::RangeListIter
(
void
)
133
:
rlio
(NULL),
h
(NULL),
c
(NULL) {}
134
135
forceinline
136
RangeListIter::RangeListIter
(
Region
&
r
)
137
: rlio(new (r.ralloc(sizeof(
RLIO
)))
RLIO
(r)), h(NULL),
c
(NULL) {}
138
139
forceinline
void
140
RangeListIter::init
(
Region
&
r
) {
141
rlio
=
new
(r.
ralloc
(
sizeof
(
RLIO
)))
RLIO
(r);
142
h
=
c
= NULL;
143
}
144
145
forceinline
146
RangeListIter::RangeListIter
(
const
RangeListIter
&
i
)
147
: rlio(i.rlio), h(i.h),
c
(i.
c
) {
148
if
(
rlio
!= NULL)
149
rlio
->
use_cnt
++;
150
}
151
152
forceinline
RangeListIter
&
153
RangeListIter::operator =
(
const
RangeListIter
&
i
) {
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
168
forceinline
169
RangeListIter::~RangeListIter
(
void
) {
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
179
RangeListIter::set
(
RangeList
*
l
) {
180
h
=
c
=
l
;
181
}
182
183
forceinline
RangeListIter::RangeList
*
184
RangeListIter::get
(
void
)
const
{
185
return
h
;
186
}
187
188
forceinline
RangeListIter::RangeList
*
189
RangeListIter::range
(
int
min
,
int
max
,
RangeList
*& f) {
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
201
forceinline
RangeListIter::RangeList
*
202
RangeListIter::range
(
int
min
,
int
max
) {
203
RangeList
*
t
=
new
(*rlio)
RangeList
;
204
t->
min
=
min
; t->
max
=
max
;
205
return
t
;
206
}
207
208
template
<
class
I>
209
forceinline
RangeListIter::RangeList
*
210
RangeListIter::range
(I&
i
,
RangeList
*& f) {
211
return
range
(i.min(),i.max(),f);
212
}
213
214
template
<
class
I>
215
forceinline
RangeListIter::RangeList
*
216
RangeListIter::range
(I&
i
) {
217
return
range
(i.min(),i.max());
218
}
219
220
template
<
class
I>
221
inline
RangeListIter::RangeList
*
222
RangeListIter::copy
(I&
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
234
RangeListIter::operator ()
(
void
)
const
{
235
return
c
!= NULL;
236
}
237
238
forceinline
void
239
RangeListIter::operator ++
(
void
) {
240
c
=
c
->
next
;
241
}
242
243
forceinline
void
244
RangeListIter::reset
(
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