this repo has no description
1/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2/*
3 * Main authors:
4 * Guido Tack <tack@gecode.org>
5 * Christian Schulte <schulte@gecode.org>
6 *
7 * Contributing authors:
8 * Gabor Szokoli <szokoli@gecode.org>
9 *
10 * Copyright:
11 * Guido Tack, 2004
12 * Christian Schulte, 2004
13 * Gabor Szokoli, 2004
14 *
15 * This file is part of Gecode, the generic constraint
16 * development environment:
17 * http://www.gecode.org
18 *
19 * Permission is hereby granted, free of charge, to any person obtaining
20 * a copy of this software and associated documentation files (the
21 * "Software"), to deal in the Software without restriction, including
22 * without limitation the rights to use, copy, modify, merge, publish,
23 * distribute, sublicense, and/or sell copies of the Software, and to
24 * permit persons to whom the Software is furnished to do so, subject to
25 * the following conditions:
26 *
27 * The above copyright notice and this permission notice shall be
28 * included in all copies or substantial portions of the Software.
29 *
30 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
31 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
32 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
33 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
34 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
35 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
36 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37 *
38 */
39
40namespace Gecode { namespace Set { namespace Rel {
41
42 template<class View0, class View1>
43 forceinline
44 Distinct<View0,View1>::Distinct(Home home, View0 x, View1 y)
45 : MixBinaryPropagator<View0, PC_SET_VAL, View1, PC_SET_VAL>(home,x,y) {}
46
47 template<class View0, class View1>
48 forceinline
49 Distinct<View0,View1>::Distinct(Space& home, Distinct& p)
50 : MixBinaryPropagator<View0, PC_SET_VAL, View1, PC_SET_VAL>
51 (home,p) {}
52
53 template<class View0, class View1>
54 ExecStatus
55 Distinct<View0,View1>::post(Home home, View0 x, View1 y) {
56 if (same(x,y))
57 return ES_FAILED;
58 if (x.assigned()) {
59 GlbRanges<View0> xr(x);
60 IntSet xs(xr);
61 ConstSetView cv(home, xs);
62 GECODE_ES_CHECK((DistinctDoit<View1>::post(home,y,cv)));
63 }
64 if (y.assigned()) {
65 GlbRanges<View1> yr(y);
66 IntSet ys(yr);
67 ConstSetView cv(home, ys);
68 GECODE_ES_CHECK((DistinctDoit<View0>::post(home,x,cv)));
69 }
70 (void) new (home) Distinct<View0,View1>(home,x,y);
71 return ES_OK;
72 }
73
74 template<class View0, class View1>
75 Actor*
76 Distinct<View0,View1>::copy(Space& home) {
77 return new (home) Distinct<View0,View1>(home,*this);
78 }
79
80 template<class View0, class View1>
81 ExecStatus
82 Distinct<View0,View1>::propagate(Space& home, const ModEventDelta&) {
83 assert(x0.assigned()||x1.assigned());
84 if (x0.assigned()) {
85 GlbRanges<View0> xr(x0);
86 IntSet xs(xr);
87 ConstSetView cv(home, xs);
88 GECODE_REWRITE(*this,(DistinctDoit<View1>::post(home(*this),x1,cv)));
89 } else {
90 GlbRanges<View1> yr(x1);
91 IntSet ys(yr);
92 ConstSetView cv(home, ys);
93 GECODE_REWRITE(*this,(DistinctDoit<View0>::post(home(*this),x0,cv)));
94 }
95 }
96
97 template<class View0>
98 ExecStatus
99 DistinctDoit<View0>::post(Home home, View0 x, ConstSetView y) {
100 (void) new (home) DistinctDoit<View0>(home,x,y);
101 return ES_OK;
102 }
103
104 template<class View0>
105 Actor*
106 DistinctDoit<View0>::copy(Space& home) {
107 return new (home) DistinctDoit<View0>(home,*this);
108 }
109
110 template<class View0>
111 ExecStatus
112 DistinctDoit<View0>::propagate(Space& home, const ModEventDelta&) {
113 if (x0.assigned()) {
114 GlbRanges<View0> xi(x0);
115 GlbRanges<ConstSetView> yi(y);
116 if (Iter::Ranges::equal(xi,yi)) { return ES_FAILED; }
117 else { return home.ES_SUBSUMED(*this); }
118 }
119 assert(x0.lubSize()-x0.glbSize() >0);
120 if (x0.cardMin()>y.cardMax()) { return home.ES_SUBSUMED(*this); }
121 if (x0.cardMax()<y.cardMin()) { return home.ES_SUBSUMED(*this); }
122 //These tests are too expensive, we should only do them
123 //in the 1 unknown left case.
124 GlbRanges<View0> xi1(x0);
125 LubRanges<ConstSetView> yi1(y);
126 if (!Iter::Ranges::subset(xi1,yi1)) { return home.ES_SUBSUMED(*this); }
127 LubRanges<View0> xi2(x0);
128 GlbRanges<ConstSetView> yi2(y);
129 if (!Iter::Ranges::subset(yi2,xi2)) { return home.ES_SUBSUMED(*this); }
130 // from here, we know y\subseteq lub(x) and glb(x)\subseteq y
131
132 if (x0.lubSize() == y.cardMin() && x0.lubSize() > 0) {
133 GECODE_ME_CHECK(x0.cardMax(home, x0.lubSize() - 1));
134 return home.ES_SUBSUMED(*this);
135 }
136 if (x0.glbSize() == y.cardMin()) {
137 GECODE_ME_CHECK(x0.cardMin(home, x0.glbSize() + 1));
138 return home.ES_SUBSUMED(*this);
139 }
140 return ES_FIX;
141 }
142
143 template<class View0>
144 forceinline
145 DistinctDoit<View0>::DistinctDoit(Home home, View0 _x, ConstSetView _y)
146 : UnaryPropagator<View0, PC_SET_ANY>(home,_x), y(_y) {}
147
148 template<class View0>
149 forceinline
150 DistinctDoit<View0>::DistinctDoit(Space& home, DistinctDoit<View0>& p)
151 : UnaryPropagator<View0, PC_SET_ANY>(home,p) {
152 y.update(home,p.y);
153 }
154
155}}}
156
157// STATISTICS: set-prop