this repo has no description
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, 2011
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
34#include <gecode/int/nvalues.hh>
35#include <gecode/int/rel.hh>
36
37namespace Gecode {
38
39 void
40 nvalues(Home home, const IntVarArgs& x, IntRelType irt, int y,
41 IntPropLevel) {
42 using namespace Int;
43 Limits::check(y,"Int::nvalues");
44 // Due to the quadratic Boolean matrix used in propagation
45 long long int n = x.size();
46 Limits::check(n*n,"Int::nvalues");
47
48 GECODE_POST;
49
50 ViewArray<IntView> xv(home,x);
51
52 switch (irt) {
53 case IRT_EQ:
54 {
55 ConstIntView yv(y);
56 GECODE_ES_FAIL(NValues::EqInt<ConstIntView>::post(home,xv,yv));
57 }
58 break;
59 case IRT_NQ:
60 {
61 IntVar z(home,0,x.size());
62 GECODE_ME_FAIL(IntView(z).nq(home,y));
63 GECODE_ES_FAIL(NValues::EqInt<IntView>::post(home,xv,z));
64 }
65 break;
66 case IRT_LE:
67 y--;
68 // Fall through
69 case IRT_LQ:
70 {
71 ConstIntView yv(y);
72 GECODE_ES_FAIL(NValues::LqInt<ConstIntView>::post(home,xv,yv));
73 }
74 break;
75 case IRT_GR:
76 y++;
77 // Fall through
78 case IRT_GQ:
79 {
80 ConstIntView yv(y);
81 GECODE_ES_FAIL(NValues::GqInt<ConstIntView>::post(home,xv,yv));
82 }
83 break;
84 default:
85 throw UnknownRelation("Int::nvalues");
86 }
87 }
88
89 void
90 nvalues(Home home, const IntVarArgs& x, IntRelType irt, IntVar y,
91 IntPropLevel) {
92 using namespace Int;
93 // Due to the quadratic Boolean matrix used in propagation
94 long long int n = x.size();
95 Limits::check(n*n,"Int::nvalues");
96
97 GECODE_POST;
98
99 if (y.assigned()) {
100 nvalues(home, x, irt, y.val());
101 return;
102 }
103
104 ViewArray<IntView> xv(home,x);
105
106 switch (irt) {
107 case IRT_EQ:
108 GECODE_ES_FAIL(NValues::EqInt<IntView>::post(home,xv,y));
109 break;
110 case IRT_NQ:
111 {
112 IntVar z(home,0,x.size());
113 GECODE_ES_FAIL((Rel::Nq<IntView,IntView>::post(home,y,z)));
114 GECODE_ES_FAIL(NValues::EqInt<IntView>::post(home,xv,z));
115 }
116 break;
117 case IRT_LE:
118 {
119 OffsetView z(y,-1);
120 GECODE_ES_FAIL(NValues::LqInt<OffsetView>::post(home,xv,z));
121 }
122 break;
123 case IRT_LQ:
124 GECODE_ES_FAIL(NValues::LqInt<IntView>::post(home,xv,y));
125 break;
126 case IRT_GR:
127 {
128 OffsetView z(y,1);
129 GECODE_ES_FAIL(NValues::GqInt<OffsetView>::post(home,xv,z));
130 }
131 break;
132 case IRT_GQ:
133 GECODE_ES_FAIL(NValues::GqInt<IntView>::post(home,xv,y));
134 break;
135 default:
136 throw UnknownRelation("Int::nvalues");
137 }
138 }
139
140 void
141 nvalues(Home home, const BoolVarArgs& x, IntRelType irt, int y,
142 IntPropLevel) {
143 using namespace Int;
144 Limits::check(y,"Int::nvalues");
145
146 GECODE_POST;
147
148 Region region;
149 ViewArray<BoolView> xv(region,x);
150
151 switch (irt) {
152 case IRT_EQ:
153 {
154 ConstIntView yv(y);
155 GECODE_ES_FAIL(NValues::EqBool<ConstIntView>::post(home,xv,yv));
156 }
157 break;
158 case IRT_NQ:
159 {
160 IntVar z(home,0,2);
161 GECODE_ME_FAIL(IntView(z).nq(home,y));
162 GECODE_ES_FAIL(NValues::EqBool<IntView>::post(home,xv,z));
163 }
164 break;
165 case IRT_LE:
166 y--;
167 // Fall through
168 case IRT_LQ:
169 {
170 ConstIntView yv(y);
171 GECODE_ES_FAIL(NValues::LqBool<ConstIntView>::post(home,xv,yv));
172 }
173 break;
174 case IRT_GR:
175 y++;
176 // Fall through
177 case IRT_GQ:
178 {
179 ConstIntView yv(y);
180 GECODE_ES_FAIL(NValues::GqBool<ConstIntView>::post(home,xv,yv));
181 }
182 break;
183 default:
184 throw UnknownRelation("Int::nvalues");
185 }
186 }
187
188 void
189 nvalues(Home home, const BoolVarArgs& x, IntRelType irt, IntVar y,
190 IntPropLevel) {
191 using namespace Int;
192
193 GECODE_POST;
194
195 if (y.assigned()) {
196 nvalues(home, x, irt, y.val());
197 return;
198 }
199
200 Region region;
201 ViewArray<BoolView> xv(region,x);
202
203 switch (irt) {
204 case IRT_EQ:
205 GECODE_ES_FAIL(NValues::EqBool<IntView>::post(home,xv,y));
206 break;
207 case IRT_NQ:
208 {
209 IntVar z(home,0,2);
210 GECODE_ES_FAIL((Rel::Nq<IntView,IntView>::post(home,y,z)));
211 GECODE_ES_FAIL(NValues::EqBool<IntView>::post(home,xv,z));
212 }
213 break;
214 case IRT_LE:
215 {
216 OffsetView z(y,-1);
217 GECODE_ES_FAIL(NValues::LqBool<OffsetView>::post(home,xv,z));
218 }
219 break;
220 case IRT_LQ:
221 GECODE_ES_FAIL(NValues::LqBool<IntView>::post(home,xv,y));
222 break;
223 case IRT_GR:
224 {
225 OffsetView z(y,1);
226 GECODE_ES_FAIL(NValues::GqBool<OffsetView>::post(home,xv,z));
227 }
228 break;
229 case IRT_GQ:
230 GECODE_ES_FAIL(NValues::GqBool<IntView>::post(home,xv,y));
231 break;
232 default:
233 throw UnknownRelation("Int::nvalues");
234 }
235 }
236
237}
238
239// STATISTICS: int-post
240