main page
modules
namespaces
classes
files
Gecode home
Generated on Tue Mar 5 2013 22:37:16 for Gecode by
doxygen
1.8.3.1
gecode
int
bool
eq.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, 2004
8
*
9
* Last modified:
10
* $Date: 2010-03-04 03:32:21 +1100 (Thu, 04 Mar 2010) $ by $Author: schulte $
11
* $Revision: 10364 $
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
Int {
namespace
Bool {
39
40
template
<
class
BVA,
class
BVB>
41
forceinline
42
Eq<BVA,BVB>::Eq
(
Home
home, BVA b0, BVB
b1
)
43
:
BoolBinary
<BVA,BVB>(home,b0,b1) {}
44
45
template
<
class
BVA,
class
BVB>
46
forceinline
47
Eq<BVA,BVB>::Eq
(
Space
& home,
bool
share,
Eq<BVA,BVB>
& p)
48
:
BoolBinary
<BVA,BVB>(home,share,p) {}
49
50
template
<
class
BVA,
class
BVB>
51
forceinline
52
Eq<BVA,BVB>::Eq
(
Space
& home,
bool
share,
Propagator
& p,
53
BVA b0, BVB
b1
)
54
:
BoolBinary
<BVA,BVB>(home,share,p,b0,b1) {}
55
56
template
<
class
BVA,
class
BVB>
57
Actor
*
58
Eq<BVA,BVB>::copy
(
Space
& home,
bool
share) {
59
return
new
(home)
Eq<BVA,BVB>
(home,share,*
this
);
60
}
61
62
template
<
class
BVA,
class
BVB>
63
inline
ExecStatus
64
Eq<BVA,BVB>::post
(
Home
home, BVA b0, BVB
b1
) {
65
switch
(
bool_test
(b0,b1)) {
66
case
BT_SAME
:
return
ES_OK
;
67
case
BT_COMP
:
return
ES_FAILED
;
68
case
BT_NONE
:
69
if
(b0.zero()) {
70
GECODE_ME_CHECK
(b1.zero(home));
71
}
else
if
(b0.one()) {
72
GECODE_ME_CHECK
(b1.one(home));
73
}
else
if
(b1.zero()) {
74
GECODE_ME_CHECK
(b0.zero(home));
75
}
else
if
(b1.one()) {
76
GECODE_ME_CHECK
(b0.one(home));
77
}
else
{
78
(void)
new
(home)
Eq<BVA,BVB>
(home,b0,
b1
);
79
}
80
break
;
81
default
:
GECODE_NEVER
;
82
}
83
return
ES_OK
;
84
}
85
86
template
<
class
BVA,
class
BVB>
87
ExecStatus
88
Eq<BVA,BVB>::propagate
(
Space
& home,
const
ModEventDelta
&) {
89
#define GECODE_INT_STATUS(S0,S1) \
90
((BVA::S0<<(1*BVA::BITS))|(BVB::S1<<(0*BVB::BITS)))
91
switch
((x0.status() << (1*BVA::BITS)) | (x1.status() << (0*BVB::BITS))) {
92
case
GECODE_INT_STATUS
(NONE,NONE):
93
GECODE_NEVER
;
94
case
GECODE_INT_STATUS
(NONE,ZERO):
95
GECODE_ME_CHECK
(x0.zero_none(home));
break
;
96
case
GECODE_INT_STATUS
(NONE,ONE):
97
GECODE_ME_CHECK
(x0.one_none(home));
break
;
98
case
GECODE_INT_STATUS
(ZERO,NONE):
99
GECODE_ME_CHECK
(x1.zero_none(home));
break
;
100
case
GECODE_INT_STATUS
(ZERO,ZERO):
101
break
;
102
case
GECODE_INT_STATUS
(ZERO,ONE):
103
return
ES_FAILED
;
104
case
GECODE_INT_STATUS
(ONE,NONE):
105
GECODE_ME_CHECK
(x1.one_none(home));
break
;
106
case
GECODE_INT_STATUS
(ONE,ZERO):
107
return
ES_FAILED
;
108
case
GECODE_INT_STATUS
(ONE,ONE):
109
break
;
110
default
:
111
GECODE_NEVER
;
112
}
113
return
home.
ES_SUBSUMED
(*
this
);
114
#undef GECODE_INT_STATUS
115
}
116
117
template
<
class
BV>
118
forceinline
119
NaryEq<BV>::NaryEq
(
Home
home,
ViewArray<BV>
& x)
120
:
NaryPropagator
<BV,
PC_BOOL_VAL
>(home,x) {}
121
122
template
<
class
BV>
123
forceinline
124
NaryEq<BV>::NaryEq
(
Space
& home,
bool
share,
NaryEq<BV>
& p)
125
:
NaryPropagator
<BV,
PC_BOOL_VAL
>(home,share,p) {}
126
127
template
<
class
BV>
128
Actor
*
129
NaryEq<BV>::copy
(
Space
& home,
bool
share) {
130
return
new
(home)
NaryEq<BV>
(home,share,*
this
);
131
}
132
133
template
<
class
BV>
134
inline
ExecStatus
135
NaryEq<BV>::post
(
Home
home,
ViewArray<BV>
& x) {
136
x.
unique
(home);
137
int
n = x.
size
();
138
if
(n < 2)
139
return
ES_OK
;
140
if
(n == 2)
141
return
Eq<BV,BV>::post
(home,x[0],x[1]);
142
for
(
int
i
=n;
i
--; )
143
if
(x[
i
].
assigned
()) {
144
if
(x[
i
].
one
()) {
145
for
(
int
j=
i
; j--; )
146
GECODE_ME_CHECK
(x[j].
one
(home));
147
for
(
int
j=
i
+1; j<n; j++)
148
GECODE_ME_CHECK
(x[j].one_none(home));
149
}
else
{
150
for
(
int
j=
i
; j--; )
151
GECODE_ME_CHECK
(x[j].zero(home));
152
for
(
int
j=
i
+1; j<n; j++)
153
GECODE_ME_CHECK
(x[j].zero_none(home));
154
}
155
return
ES_OK
;
156
}
157
(void)
new
(home)
NaryEq<BV>
(home,x);
158
return
ES_OK
;
159
}
160
161
template
<
class
BV>
162
PropCost
163
NaryEq<BV>::cost
(
const
Space
&,
const
ModEventDelta
&)
const
{
164
return
PropCost::unary
(
PropCost::LO
);
165
}
166
167
template
<
class
BV>
168
ExecStatus
169
NaryEq<BV>::propagate
(
Space
& home,
const
ModEventDelta
&) {
170
int
n=x.size();
171
int
i
=0;
172
while
(
true
) {
173
if
(x[i].
assigned
()) {
174
if
(x[i].
one
()) {
175
for
(
int
j=0; j<
i
; j++)
176
GECODE_ME_CHECK
(x[j].one_none(home));
177
for
(
int
j=i+1; j<n; j++)
178
GECODE_ME_CHECK
(x[j].
one
(home));
179
}
else
{
180
for
(
int
j=0; j<
i
; j++)
181
GECODE_ME_CHECK
(x[j].zero_none(home));
182
for
(
int
j=i+1; j<n; j++)
183
GECODE_ME_CHECK
(x[j].zero(home));
184
}
185
return
home.
ES_SUBSUMED
(*
this
);
186
}
187
i++;
188
}
189
GECODE_NEVER
;
190
return
ES_FIX
;
191
}
192
193
}}}
194
195
// STATISTICS: int-prop
196