main page
modules
namespaces
classes
files
Gecode home
Generated on Fri Aug 24 2012 04:52:14 for Gecode by
doxygen
1.8.1.2
gecode
set
view
singleton.hpp
Go to the documentation of this file.
1
/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2
/*
3
* Main authors:
4
* Guido Tack <tack@gecode.org>
5
*
6
* Contributing authors:
7
* Christian Schulte <schulte@gecode.org>
8
*
9
* Copyright:
10
* Guido Tack, 2004
11
* Christian Schulte, 2004
12
*
13
* Last modified:
14
* $Date: 2011-08-20 00:47:28 +1000 (Sat, 20 Aug 2011) $ by $Author: tack $
15
* $Revision: 12318 $
16
*
17
* This file is part of Gecode, the generic constraint
18
* development environment:
19
* http://www.gecode.org
20
*
21
* Permission is hereby granted, free of charge, to any person obtaining
22
* a copy of this software and associated documentation files (the
23
* "Software"), to deal in the Software without restriction, including
24
* without limitation the rights to use, copy, modify, merge, publish,
25
* distribute, sublicense, and/or sell copies of the Software, and to
26
* permit persons to whom the Software is furnished to do so, subject to
27
* the following conditions:
28
*
29
* The above copyright notice and this permission notice shall be
30
* included in all copies or substantial portions of the Software.
31
*
32
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
33
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
34
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
35
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
36
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
37
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
38
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
39
*
40
*/
41
42
namespace
Gecode {
namespace
Set {
43
44
forceinline
45
SingletonView::SingletonView
(
void
) {}
46
47
forceinline
48
SingletonView::SingletonView
(
Gecode::Int::IntView
& y)
49
:
DerivedView
<Gecode::Int::
IntView
>(y) {}
50
51
forceinline
52
SingletonView::SingletonView
(
const
Gecode::IntVar
& y)
53
:
DerivedView
<Gecode::Int::
IntView
>(y) {}
54
55
forceinline
PropCond
56
SingletonView::pc_settoint
(
PropCond
pc) {
57
switch
(pc) {
58
case
PC_SET_VAL
:
59
case
PC_SET_CGLB
:
60
case
PC_SET_CARD
:
61
return
Gecode::Int::PC_INT_VAL
;
62
default
:
63
return
Gecode::Int::PC_INT_DOM
;
64
}
65
}
66
67
forceinline
ModEvent
68
SingletonView::me_inttoset
(
ModEvent
me) {
69
switch
(me) {
70
case
Gecode::Int::ME_INT_FAILED
:
71
return
ME_SET_FAILED
;
72
case
Gecode::Int::ME_INT_NONE
:
73
return
ME_SET_NONE
;
74
case
Gecode::Int::ME_INT_VAL
:
75
return
ME_SET_VAL
;
76
case
Gecode::Int::ME_INT_DOM
:
77
return
ME_SET_LUB
;
78
default
:
79
return
ME_SET_LUB
;
80
}
81
}
82
83
forceinline
ModEvent
84
SingletonView::me_settoint
(
ModEvent
me) {
85
switch
(me) {
86
case
ME_SET_FAILED
:
87
return
Gecode::Int::ME_INT_FAILED
;
88
case
ME_SET_NONE
:
89
return
Gecode::Int::ME_INT_NONE
;
90
case
ME_SET_VAL
:
91
return
Gecode::Int::ME_INT_VAL
;
92
default
:
93
return
Gecode::Int::ME_INT_DOM
;
94
}
95
}
96
97
forceinline
unsigned
int
98
SingletonView::glbSize
(
void
)
const
{
99
return
x
.
assigned
() ? 1U : 0U;
100
}
101
102
forceinline
unsigned
int
103
SingletonView::lubSize
(
void
)
const
{
return
x
.
size
(); }
104
105
forceinline
unsigned
int
106
SingletonView::unknownSize
(
void
)
const
{
107
return
lubSize
() -
glbSize
();
108
}
109
110
forceinline
bool
111
SingletonView::contains
(
int
n)
const
{
return
x
.
assigned
() ?
112
(
x
.
val
()==n) :
false
; }
113
114
forceinline
bool
115
SingletonView::notContains
(
int
n)
const
{
return
!
x
.
in
(n); }
116
117
forceinline
unsigned
int
118
SingletonView::cardMin
()
const
{
return
1; }
119
120
forceinline
unsigned
int
121
SingletonView::cardMax
()
const
{
return
1; }
122
123
forceinline
int
124
SingletonView::lubMin
()
const
{
return
x
.
min
(); }
125
126
forceinline
int
127
SingletonView::lubMax
()
const
{
return
x
.
max
(); }
128
129
forceinline
int
130
SingletonView::glbMin
()
const
{
return
x
.
assigned
() ?
131
x
.
val
() :
BndSet::MIN_OF_EMPTY
; }
132
133
forceinline
int
134
SingletonView::glbMax
()
const
{
return
x
.
assigned
() ?
135
x
.
val
() :
BndSet::MAX_OF_EMPTY
; }
136
137
forceinline
ModEvent
138
SingletonView::cardMin
(
Space
&,
unsigned
int
c
) {
139
return
c<=1 ?
ME_SET_NONE
:
ME_SET_FAILED
;
140
}
141
142
forceinline
ModEvent
143
SingletonView::cardMax
(
Space
&,
unsigned
int
c
) {
144
return
c<1 ?
ME_SET_FAILED
:
ME_SET_NONE
;
145
}
146
147
forceinline
ModEvent
148
SingletonView::include
(
Space
& home,
int
c
) {
149
return
me_inttoset
(
x
.
eq
(home,c));
150
}
151
152
forceinline
ModEvent
153
SingletonView::intersect
(
Space
& home,
int
c
) {
154
return
me_inttoset
(
x
.
eq
(home,c));
155
}
156
157
forceinline
ModEvent
158
SingletonView::intersect
(
Space
& home,
int
i
,
int
j) {
159
ModEvent
me1 =
me_inttoset
(
x
.
gq
(home,i));
160
ModEvent
me2 =
me_inttoset
(
x
.
lq
(home,j));
161
if
(
me_failed
(me1) ||
me_failed
(me2))
162
return
ME_SET_FAILED
;
163
switch
(me1) {
164
case
ME_SET_NONE
:
165
case
ME_SET_LUB
:
166
return
me2;
167
case
ME_SET_VAL
:
168
return
ME_SET_VAL
;
169
default
:
170
GECODE_NEVER
;
171
return
ME_SET_VAL
;
172
}
173
}
174
175
forceinline
ModEvent
176
SingletonView::exclude
(
Space
& home,
int
c
) {
177
return
me_inttoset
(
x
.
nq
(home,c));
178
}
179
180
forceinline
ModEvent
181
SingletonView::include
(
Space
& home,
int
j,
int
k) {
182
return
j==k ?
me_inttoset
(
x
.
eq
(home,j)) :
ME_SET_FAILED
;
183
}
184
185
forceinline
ModEvent
186
SingletonView::exclude
(
Space
& home,
int
j,
int
k) {
187
ModEvent
me1 =
me_inttoset
(
x
.
gr
(home,j));
188
ModEvent
me2 =
me_inttoset
(
x
.
le
(home,k));
189
if
(
me_failed
(me1) ||
me_failed
(me2))
190
return
ME_SET_FAILED
;
191
switch
(me1) {
192
case
ME_SET_NONE
:
193
case
ME_SET_LUB
:
194
return
me2;
195
case
ME_SET_VAL
:
196
return
ME_SET_VAL
;
197
default
:
198
GECODE_NEVER
;
199
return
ME_SET_VAL
;
200
}
201
}
202
203
template
<
class
I>
ModEvent
204
SingletonView::excludeI
(
Space
& home, I& iter) {
205
return
me_inttoset
(
x
.
minus_r
(home,iter));
206
}
207
208
template
<
class
I>
ModEvent
209
SingletonView::includeI
(
Space
& home, I& iter) {
210
if
(!iter())
211
return
ME_SET_NONE
;
212
213
if
(iter.min()!=iter.max())
214
return
ME_SET_FAILED
;
215
216
int
val = iter.min();
217
++iter;
218
if
( iter() )
219
return
ME_SET_FAILED
;
220
221
return
me_inttoset
(
x
.
eq
(home, val));
222
}
223
224
template
<
class
I>
ModEvent
225
SingletonView::intersectI
(
Space
& home, I& iter) {
226
return
me_inttoset
(
x
.
inter_r
(home,iter));
227
}
228
229
forceinline
void
230
SingletonView::subscribe
(
Space
& home,
Propagator
& p,
PropCond
pc,
231
bool
schedule) {
232
x
.
subscribe
(home,p,
pc_settoint
(pc),schedule);
233
}
234
forceinline
void
235
SingletonView::cancel
(
Space
& home,
Propagator
& p,
PropCond
pc) {
236
x
.
cancel
(home,p,
pc_settoint
(pc));
237
}
238
239
forceinline
void
240
SingletonView::subscribe
(
Space
& home,
Advisor
&
a
) {
241
x
.
subscribe
(home,a);
242
}
243
forceinline
void
244
SingletonView::cancel
(
Space
& home,
Advisor
&
a
) {
245
x
.
cancel
(home,a);
246
}
247
248
249
forceinline
void
250
SingletonView::schedule
(
Space
& home,
Propagator
& p,
ModEvent
me) {
251
return
Gecode::Int::IntView::schedule
(home,p,
me_settoint
(me));
252
}
253
forceinline
ModEvent
254
SingletonView::me
(
const
ModEventDelta
& med) {
255
return
me_inttoset
(
Int::IntView::me
(med));
256
}
257
forceinline
ModEventDelta
258
SingletonView::med
(
ModEvent
me) {
259
return
SetView::med
(
me_settoint
(me));
260
}
261
262
263
/*
264
* Delta information for advisors
265
*
266
* For SingletonViews, a glb change means that the view is assigned.
267
* Thus, the delta for the glb is always equal to the delta for the lub.
268
*
269
*/
270
271
forceinline
ModEvent
272
SingletonView::modevent
(
const
Delta
&
d
) {
273
return
me_inttoset
(
Int::IntView::modevent
(d));
274
}
275
276
forceinline
int
277
SingletonView::glbMin
(
const
Delta
&
d
)
const
{
return
x
.
min
(d); }
278
279
forceinline
int
280
SingletonView::glbMax
(
const
Delta
&
d
)
const
{
return
x
.
max
(d); }
281
282
forceinline
bool
283
SingletonView::glbAny
(
const
Delta
&
d
)
const
{
return
x
.
any
(d); }
284
285
forceinline
int
286
SingletonView::lubMin
(
const
Delta
&
d
)
const
{
return
x
.
min
(d); }
287
288
forceinline
int
289
SingletonView::lubMax
(
const
Delta
&
d
)
const
{
return
x
.
max
(d); }
290
291
forceinline
bool
292
SingletonView::lubAny
(
const
Delta
&
d
)
const
{
return
x
.
any
(d); }
293
294
/*
295
* Iterators
296
*
297
*/
298
303
template
<>
304
class
LubRanges
<
SingletonView
> :
public
Gecode::Int::IntVarImpFwd
{
305
public
:
307
308
309
LubRanges
(
void
);
311
LubRanges
(
const
SingletonView
& x);
313
void
init
(
const
SingletonView
& x);
315
};
316
317
forceinline
318
LubRanges<SingletonView>::LubRanges
(
void
) {}
319
320
forceinline
321
LubRanges<SingletonView>::LubRanges
(
const
SingletonView
& s) :
322
Gecode::Int::
IntVarImpFwd
(s.base().varimp()) {}
323
324
forceinline
void
325
LubRanges<SingletonView>::init
(
const
SingletonView
& s) {
326
Gecode::Int::IntVarImpFwd::init
(s.
base
().
varimp
());
327
}
328
333
template
<>
334
class
GlbRanges
<
SingletonView
> {
335
private
:
336
int
val;
337
bool
flag;
338
public
:
340
341
342
GlbRanges
(
void
);
344
GlbRanges
(
const
SingletonView
& x);
346
void
init
(
const
SingletonView
& x);
347
349
350
351
bool
operator ()
(
void
)
const
;
353
void
operator ++
(
void
);
355
357
358
359
int
min
(
void
)
const
;
361
int
max
(
void
)
const
;
363
unsigned
int
width
(
void
)
const
;
365
};
366
367
forceinline
368
GlbRanges<SingletonView>::GlbRanges
(
void
) {}
369
370
forceinline
void
371
GlbRanges<SingletonView>::init
(
const
SingletonView
& s) {
372
if
(s.
base
().
assigned
()) {
373
val = s.
base
().
val
();
374
flag =
true
;
375
}
else
{
376
val = 0;
377
flag =
false
;
378
}
379
}
380
381
forceinline
382
GlbRanges<SingletonView>::GlbRanges
(
const
SingletonView
& s) {
383
init
(s);
384
}
385
386
forceinline
bool
387
GlbRanges<SingletonView>::operator ()
(
void
)
const
{
return
flag; }
388
389
forceinline
void
390
GlbRanges<SingletonView>::operator ++
(
void
) { flag=
false
; }
391
392
forceinline
int
393
GlbRanges<SingletonView>::min
(
void
)
const
{
return
val; }
394
forceinline
int
395
GlbRanges<SingletonView>::max
(
void
)
const
{
return
val; }
396
forceinline
unsigned
int
397
GlbRanges<SingletonView>::width
(
void
)
const
{
return
1; }
398
399
}}
400
401
// STATISTICS: set-var
402