Generated on Tue Feb 11 2025 17:33:26 for Gecode by doxygen 1.12.0
eqv.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 * 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 Int { namespace Bool {
35
36 template<class BVA, class BVB, class BVC>
38 Eqv<BVA,BVB,BVC>::Eqv(Home home, BVA b0, BVB b1, BVC b2)
39 : BoolTernary<BVA,BVB,BVC>(home,b0,b1,b2) {}
40
41 template<class BVA, class BVB, class BVC>
44 : BoolTernary<BVA,BVB,BVC>(home,p) {}
45
46 template<class BVA, class BVB, class BVC>
47 inline ExecStatus
48 Eqv<BVA,BVB,BVC>::post(Home home, BVA b0, BVB b1, BVC b2){
49 switch (bool_test(b0,b1)) {
50 case BT_SAME:
51 GECODE_ME_CHECK(b2.one(home)); break;
52 case BT_COMP:
53 GECODE_ME_CHECK(b2.zero(home)); break;
54 case BT_NONE:
55 if (b2.one())
56 return Eq<BVA,BVB>::post(home,b0,b1);
57 if (b0.one()) {
58 if (b1.one()) {
59 GECODE_ME_CHECK(b2.one(home)); return ES_OK;
60 } else if (b1.zero()) {
61 GECODE_ME_CHECK(b2.zero(home)); return ES_OK;
62 }
63 }
64 if (b0.zero()) {
65 if (b1.one()) {
66 GECODE_ME_CHECK(b2.zero(home)); return ES_OK;
67 } else if (b1.zero()) {
68 GECODE_ME_CHECK(b2.one(home)); return ES_OK;
69 }
70 }
71 (void) new (home) Eqv(home,b0,b1,b2);
72 break;
73 default:
75 }
76 return ES_OK;
77 }
78
79 template<class BVA, class BVB, class BVC>
80 Actor*
82 return new (home) Eqv<BVA,BVB,BVC>(home,*this);
83 }
84
85 template<class BVA, class BVB, class BVC>
88#define GECODE_INT_STATUS(S0,S1,S2) \
89 ((BVA::S0<<(2*BVA::BITS))|(BVB::S1<<(1*BVB::BITS))|(BVC::S2<<(0*BVC::BITS)))
90 switch ((x0.status() << (2*BVA::BITS)) | (x1.status() << (1*BVB::BITS)) |
91 (x2.status() << (0*BVC::BITS))) {
92 case GECODE_INT_STATUS(NONE,NONE,NONE):
94 case GECODE_INT_STATUS(NONE,NONE,ZERO):
95 case GECODE_INT_STATUS(NONE,NONE,ONE):
96 case GECODE_INT_STATUS(NONE,ZERO,NONE):
97 return ES_FIX;
98 case GECODE_INT_STATUS(NONE,ZERO,ZERO):
99 GECODE_ME_CHECK(x0.one_none(home)); break;
100 case GECODE_INT_STATUS(NONE,ZERO,ONE):
101 GECODE_ME_CHECK(x0.zero_none(home)); break;
102 case GECODE_INT_STATUS(NONE,ONE,NONE):
103 return ES_FIX;
104 case GECODE_INT_STATUS(NONE,ONE,ZERO):
105 GECODE_ME_CHECK(x0.zero_none(home)); break;
106 case GECODE_INT_STATUS(NONE,ONE,ONE):
107 GECODE_ME_CHECK(x0.one_none(home)); break;
108 case GECODE_INT_STATUS(ZERO,NONE,NONE):
109 return ES_FIX;
110 case GECODE_INT_STATUS(ZERO,NONE,ZERO):
111 GECODE_ME_CHECK(x1.one_none(home)); break;
112 case GECODE_INT_STATUS(ZERO,NONE,ONE):
113 GECODE_ME_CHECK(x1.zero_none(home)); break;
114 case GECODE_INT_STATUS(ZERO,ZERO,NONE):
115 GECODE_ME_CHECK(x2.one_none(home)); break;
116 case GECODE_INT_STATUS(ZERO,ZERO,ZERO):
117 return ES_FAILED;
118 case GECODE_INT_STATUS(ZERO,ZERO,ONE):
119 break;
120 case GECODE_INT_STATUS(ZERO,ONE,NONE):
121 GECODE_ME_CHECK(x2.zero_none(home)); break;
122 case GECODE_INT_STATUS(ZERO,ONE,ZERO):
123 break;
124 case GECODE_INT_STATUS(ZERO,ONE,ONE):
125 return ES_FAILED;
126 case GECODE_INT_STATUS(ONE,NONE,NONE):
127 return ES_FIX;
128 case GECODE_INT_STATUS(ONE,NONE,ZERO):
129 GECODE_ME_CHECK(x1.zero_none(home)); break;
130 case GECODE_INT_STATUS(ONE,NONE,ONE):
131 GECODE_ME_CHECK(x1.one_none(home)); break;
132 case GECODE_INT_STATUS(ONE,ZERO,NONE):
133 GECODE_ME_CHECK(x2.zero_none(home)); break;
134 case GECODE_INT_STATUS(ONE,ZERO,ZERO):
135 break;
136 case GECODE_INT_STATUS(ONE,ZERO,ONE):
137 return ES_FAILED;
138 case GECODE_INT_STATUS(ONE,ONE,NONE):
139 GECODE_ME_CHECK(x2.one_none(home)); break;
140 case GECODE_INT_STATUS(ONE,ONE,ZERO):
141 return ES_FAILED;
142 case GECODE_INT_STATUS(ONE,ONE,ONE):
143 break;
144 default:
146 }
147 return home.ES_SUBSUMED(*this);
148#undef GECODE_INT_STATUS
149 }
150
151
152 /*
153 * N-ary Boolean equivalence propagator
154 *
155 */
156
159 : BinaryPropagator<BoolView,PC_BOOL_VAL>(home,x0[0],x0[1]),
160 x(x0), pm2(pm20) {
161 assert(x.size() >= 2);
162 x.drop_fst(2);
163 }
164
167 : BinaryPropagator<BoolView,PC_BOOL_VAL>(home,p), pm2(p.pm2) {
168 x.update(home,p.x);
169 }
170
171 forceinline size_t
174 return sizeof(*this);
175 }
176
177 forceinline void
179 if (x0.assigned()) {
180 pm2 ^= x0.val();
181 int n = x.size();
182 for (int i=n; i--; )
183 if (x[i].assigned()) {
184 pm2 ^= x[i].val();
185 x[i] = x[--n];
186 } else {
187 // Move to x0 and subscribe
188 x0=x[i]; x[i]=x[--n];
189 x0.subscribe(home,*this,PC_BOOL_VAL,false);
190 break;
191 }
192 x.size(n);
193 }
194 }
195
196}}}
197
198// STATISTICS: int-prop
int p
Number of positive literals for node type.
int n
Number of negative literals for node type.
Base-class for both propagators and branchers.
Definition core.hpp:628
Binary propagator.
Definition pattern.hpp:84
Home class for posting propagators
Definition core.hpp:856
Boolean view for Boolean variables.
Definition view.hpp:1380
int val(void) const
Return assigned value (only if assigned)
Definition bool.hpp:74
Base-class for ternary Boolean propagators.
Definition bool.hh:77
static ExecStatus post(Home home, BVA x0, BVB x1)
Post propagator .
Definition eq.hpp:60
Boolean equivalence propagator.
Definition bool.hh:426
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition eqv.hpp:87
static ExecStatus post(Home home, BVA b0, BVB b1, BVC b2)
Post propagator (equivalence)
Definition eqv.hpp:48
Eqv(Space &home, Eqv &p)
Constructor for cloning p.
Definition eqv.hpp:43
virtual Actor * copy(Space &home)
Copy propagator during cloning.
Definition eqv.hpp:81
Boolean n-ary equivalence propagator.
Definition bool.hh:453
void resubscribe(Space &home, BoolView &x0)
Update subscription.
Definition eqv.hpp:178
NaryEqv(Home home, ViewArray< BoolView > &x, int pm2)
Constructor for posting.
Definition eqv.hpp:158
virtual size_t dispose(Space &home)
Delete propagator and return its size.
Definition eqv.hpp:172
int pm2
Parity information mod 2.
Definition bool.hh:460
ViewArray< BoolView > x
Views not yet subscribed to.
Definition bool.hh:458
Computation spaces.
Definition core.hpp:1742
bool assigned(void) const
Test whether view is assigned.
Definition view.hpp:516
void subscribe(Space &home, Propagator &p, PropCond pc, bool schedule=true)
Subscribe propagator p with propagation condition pc to view.
Definition view.hpp:521
View arrays.
Definition array.hpp:253
void update(Space &home, ViewArray< View > &a)
Update array to be a clone of array a.
Definition array.hpp:1328
void drop_fst(int i)
Drop views from positions 0 to i-1 from array.
Definition array.hpp:1242
int size(void) const
Return size of array (number of elements)
Definition array.hpp:1156
ExecStatus ES_SUBSUMED(Propagator &p)
Definition core.hpp:3563
int ModEventDelta
Modification event deltas.
Definition core.hpp:89
#define GECODE_ME_CHECK(me)
Check whether modification event me is failed, and forward failure.
Definition macros.hpp:52
#define GECODE_INT_STATUS(S0, S1)
const Gecode::PropCond PC_BOOL_VAL
Propagate when a view becomes assigned (single value)
Definition var-type.hpp:126
BoolTest bool_test(const BoolView &b0, const BoolView &b1)
Definition bool-test.hpp:41
@ BT_NONE
No sharing.
Definition view.hpp:1785
@ BT_COMP
Same variable but complement.
Definition view.hpp:1787
@ BT_SAME
Same variable.
Definition view.hpp:1786
Gecode toplevel namespace
ExecStatus
Definition core.hpp:472
@ ES_OK
Execution is okay.
Definition core.hpp:476
@ ES_FIX
Propagation has computed fixpoint.
Definition core.hpp:477
@ ES_FAILED
Execution has resulted in failure.
Definition core.hpp:474
Post propagator for SetVar x
Definition set.hh:767
#define forceinline
Definition config.hpp:187
#define GECODE_NEVER
Assert that this command is never executed.
Definition macros.hpp:56