main page
modules
namespaces
classes
files
Gecode home
Generated on Tue Jan 28 2020 00:00:00 for Gecode by
doxygen
1.8.17
gecode
int
int-set-1.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, 2003
8
*
9
* Last modified:
10
* $Date: 2013-03-11 14:47:11 +0100 (Mon, 11 Mar 2013) $ by $Author: schulte $
11
* $Revision: 13490 $
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
#include <sstream>
39
40
namespace
Gecode
{
41
42
/*
43
* Integer sets
44
*
45
*/
46
forceinline
47
IntSet::IntSet
(
void
) {}
48
56
template
<
class
I>
57
class
IntSetInit
{
58
public
:
60
static
void
init
(
IntSet
& s, I&
i
) {
61
Support::DynamicArray<IntSet::Range,Heap>
d
(
heap
);
62
int
n
=0;
63
unsigned
int
size
= 0;
64
while
(
i
()) {
65
d
[
n
].
min
=
i
.min();
d
[
n
].
max
=
i
.max();
size
+=
i
.width();
66
++
n
; ++
i
;
67
}
68
if
(
n
> 0) {
69
IntSet::IntSetObject* o = IntSet::IntSetObject::allocate(
n
);
70
for
(
int
j=
n
; j--; )
71
o->r[j]=
d
[j];
72
o->
size
=
size
;
73
s.
object
(o);
74
}
75
}
76
};
77
79
template
<>
80
class
IntSetInit
<
IntSet
> {
81
public
:
82
static
void
init
(
IntSet
& s,
const
IntSet
&
i
) {
83
s.
object
(
i
.object());
84
}
85
};
86
88
template
<
class
I>
89
IntSet::IntSet
(I&
i
) {
90
IntSetInit<I>::init
(*
this
,
i
);
91
}
92
94
template
<
class
I>
95
IntSet::IntSet
(
const
I&
i
) {
96
IntSetInit<I>::init
(*
this
,
i
);
97
}
98
99
forceinline
100
IntSet::IntSet
(
const
int
r
[][2],
int
n
) {
101
init(
r
,
n
);
102
}
103
104
forceinline
105
IntSet::IntSet
(
const
int
r
[],
int
n
) {
106
init(
r
,
n
);
107
}
108
109
forceinline
110
IntSet::IntSet
(
int
n
,
int
m) {
111
init(
n
,m);
112
}
113
114
forceinline
int
115
IntSet::min
(
int
i
)
const
{
116
assert(
object
() != NULL);
117
return
static_cast<
IntSetObject*
>
(
object
())->
r
[
i
].
min
;
118
}
119
120
forceinline
int
121
IntSet::max
(
int
i
)
const
{
122
assert(
object
() != NULL);
123
return
static_cast<
IntSetObject*
>
(
object
())->
r
[
i
].
max
;
124
}
125
126
forceinline
unsigned
int
127
IntSet::width
(
int
i
)
const
{
128
assert(
object
() != NULL);
129
IntSetObject* o =
static_cast<
IntSetObject*
>
(
object
());
130
return
static_cast<
unsigned
int
>
(o->r[
i
].max-o->r[
i
].min)+1;
131
}
132
133
forceinline
int
134
IntSet::ranges
(
void
)
const
{
135
IntSetObject* o =
static_cast<
IntSetObject*
>
(
object
());
136
return
(o == NULL) ? 0 : o->n;
137
}
138
139
forceinline
bool
140
IntSet::in
(
int
n
)
const
{
141
IntSetObject* o =
static_cast<
IntSetObject*
>
(
object
());
142
if
((o == NULL) || (
n
< o->r[0].min) || (
n
> o->r[o->n-1].max))
143
return
false
;
144
else
145
return
o->in(
n
);
146
}
147
148
forceinline
int
149
IntSet::min
(
void
)
const
{
150
IntSetObject* o =
static_cast<
IntSetObject*
>
(
object
());
151
return
(o == NULL) ?
Int::Limits::max
: o->r[0].min;
152
}
153
154
forceinline
int
155
IntSet::max
(
void
)
const
{
156
IntSetObject* o =
static_cast<
IntSetObject*
>
(
object
());
157
return
(o == NULL) ?
Int::Limits::min
: o->r[o->n-1].max;
158
}
159
160
forceinline
unsigned
int
161
IntSet::size
(
void
)
const
{
162
IntSetObject* o =
static_cast<
IntSetObject*
>
(
object
());
163
return
(o == NULL) ? 0 : o->size;
164
}
165
166
forceinline
unsigned
int
167
IntSet::width
(
void
)
const
{
168
return
static_cast<
unsigned
int
>
(
max
()-
min
()+1);
169
}
170
171
172
/*
173
* Range iterator for integer sets
174
*
175
*/
176
177
forceinline
178
IntSetRanges::IntSetRanges
(
void
) {}
179
forceinline
180
void
181
IntSetRanges::init
(
const
IntSet
& s) {
182
int
n
= s.
ranges
();
183
if
(
n
> 0) {
184
i = &
static_cast<
IntSet::IntSetObject*
>
(s.
object
())->
r
[0]; e = i+
n
;
185
}
else
{
186
i = e = NULL;
187
}
188
}
189
forceinline
190
IntSetRanges::IntSetRanges
(
const
IntSet
& s) {
init
(s); }
191
192
193
forceinline
void
194
IntSetRanges::operator ++
(
void
) {
195
i++;
196
}
197
forceinline
bool
198
IntSetRanges::operator ()
(
void
)
const
{
199
return
i<e;
200
}
201
202
forceinline
int
203
IntSetRanges::min
(
void
)
const
{
204
return
i->min;
205
}
206
forceinline
int
207
IntSetRanges::max
(
void
)
const
{
208
return
i->max;
209
}
210
forceinline
unsigned
int
211
IntSetRanges::width
(
void
)
const
{
212
return
static_cast<
unsigned
int
>
(i->max - i->min) + 1;
213
}
214
215
/*
216
* Value iterator for integer sets
217
*
218
*/
219
forceinline
220
IntSetValues::IntSetValues
(
void
) {}
221
222
forceinline
223
IntSetValues::IntSetValues
(
const
IntSet
& s) {
224
IntSetRanges
r
(s);
225
Iter::Ranges::ToValues<IntSetRanges>::init
(
r
);
226
}
227
228
forceinline
void
229
IntSetValues::init
(
const
IntSet
& s) {
230
IntSetRanges
r
(s);
231
Iter::Ranges::ToValues<IntSetRanges>::init
(
r
);
232
}
233
234
template
<
class
Char,
class
Traits>
235
std::basic_ostream<Char,Traits>&
236
operator <<
(std::basic_ostream<Char,Traits>& os,
const
IntSet
& is) {
237
std::basic_ostringstream<Char,Traits> s;
238
s.copyfmt(os); s.width(0);
239
s <<
'{'
;
240
for
(
int
i
= 0;
i
< is.
ranges
(); ) {
241
int
min
= is.
min
(
i
);
242
int
max
= is.
max
(
i
);
243
if
(
min
==
max
)
244
s <<
min
;
245
else
246
s <<
min
<<
".."
<<
max
;
247
i
++;
248
if
(
i
< is.
ranges
())
249
s <<
','
;
250
}
251
s <<
'}'
;
252
return
os << s.str();
253
}
254
255
}
256
257
// STATISTICS: int-var
258
Gecode::IntSetInit::init
static void init(IntSet &s, I &i)
Initialize s with iterator i.
Definition:
int-set-1.hpp:60
Gecode::IntSet::in
bool in(int n) const
Return whether n is included in the set.
Definition:
int-set-1.hpp:140
Gecode::IntSetRanges::width
unsigned int width(void) const
Return width of range (distance between minimum and maximum)
Definition:
int-set-1.hpp:211
Gecode::IntSet::min
int min(int i) const
Return minimum of range at position i.
Definition:
int-set-1.hpp:115
forceinline
#define forceinline
Definition:
config.hpp:173
Gecode::IntSetInit
Integer set initialization.
Definition:
int-set-1.hpp:57
Gecode::max
void max(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Definition:
arithmetic.cpp:53
Gecode::IntSet::max
int max(int i) const
Return maximum of range at position i.
Definition:
int-set-1.hpp:121
Gecode::IntSet::width
unsigned int width(void) const
Return width of set (distance between maximum and minimum)
Definition:
int-set-1.hpp:167
Gecode::Iter::Ranges::size
unsigned int size(I &i)
Size of all ranges of range iterator i.
Definition:
ranges-operations.hpp:78
Gecode::IntSet::IntSet
IntSet(void)
Initialize as empty set.
Definition:
int-set-1.hpp:47
Gecode::Support::DynamicArray
Array with arbitrary number of elements.
Definition:
dynamic-array.hpp:48
Test::Int::Basic::i
Gecode::IntArgs i(4, 1, 2, 3, 4)
Gecode::IntSetRanges::min
int min(void) const
Return smallest value of range.
Definition:
int-set-1.hpp:203
Gecode::SharedHandle::object
SharedHandle::Object * object(void) const
Access to the shared object.
Definition:
core.hpp:3090
Gecode::IntSetInit< IntSet >::init
static void init(IntSet &s, const IntSet &i)
Definition:
int-set-1.hpp:82
Gecode::IntSetRanges
Range iterator for integer sets.
Definition:
int.hh:274
Gecode::IntSet::size
unsigned int size(void) const
Return size (cardinality) of set.
Definition:
int-set-1.hpp:161
Gecode::IntSet::min
int min(void) const
Return minimum of entire set.
Definition:
int-set-1.hpp:149
Gecode::IntSetRanges::IntSetRanges
IntSetRanges(void)
Default constructor.
Definition:
int-set-1.hpp:178
Gecode
Gecode toplevel namespace
Gecode::IntSetRanges::max
int max(void) const
Return largest value of range.
Definition:
int-set-1.hpp:207
Gecode::IntSet
Integer sets.
Definition:
int.hh:174
Gecode::r
Post propagator for SetVar SetOpType SetVar SetRelType r
Definition:
set.hh:784
Gecode::IntSetValues::init
void init(const IntSet &s)
Initialize with values for s.
Definition:
int-set-1.hpp:229
Gecode::Int::Limits::max
const int max
Largest allowed integer value.
Definition:
int.hh:116
Gecode::IntSetValues::IntSetValues
IntSetValues(void)
Default constructor.
Definition:
int-set-1.hpp:220
Gecode::IntSetRanges::operator++
void operator++(void)
Move iterator to next range (if possible)
Definition:
int-set-1.hpp:194
Gecode::Iter::Ranges::ToValues::init
void init(I &i)
Initialize with values from range iterator i.
Definition:
ranges-values.hpp:107
Gecode::heap
Heap heap
The single global heap.
Definition:
heap.cpp:48
Gecode::IntSet::max
int max(void) const
Return maximum of entire set.
Definition:
int-set-1.hpp:155
Test::Int::Distinct::d
Gecode::IntSet d(v, 7)
Gecode::IntSetRanges::init
void init(const IntSet &s)
Initialize with ranges for set s.
Definition:
int-set-1.hpp:181
Gecode::IntSetRanges::operator()
bool operator()(void) const
Test whether iterator is still at a range or done.
Definition:
int-set-1.hpp:198
Gecode::Int::Limits::min
const int min
Smallest allowed integer value.
Definition:
int.hh:118
Gecode::min
void min(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Definition:
arithmetic.cpp:71
n
int n
Number of negative literals for node type.
Definition:
bool-expr.cpp:238
Gecode::operator<<
Archive & operator<<(Archive &e, FloatNumBranch nl)
Definition:
val-sel.hpp:43
Gecode::IntSet::ranges
int ranges(void) const
Return number of ranges of the specification.
Definition:
int-set-1.hpp:134