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, 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
34#include <gecode/int/element.hh>
35
36namespace Gecode {
37
38 void
39 element(Home home, IntSharedArray c, IntVar x0, IntVar x1,
40 IntPropLevel) {
41 using namespace Int;
42 if (c.size() == 0)
43 throw TooFewArguments("Int::element");
44 GECODE_POST;
45 for (int i=0; i<c.size(); i++)
46 Limits::check(c[i],"Int::element");
47 GECODE_ES_FAIL((Element::post_int<IntView,IntView>(home,c,x0,x1)));
48 }
49
50 void
51 element(Home home, IntSharedArray c, IntVar x0, int offset, IntVar x1,
52 IntPropLevel) {
53 using namespace Int;
54 if (c.size() == 0)
55 throw TooFewArguments("Int::element");
56 GECODE_POST;
57 for (int i=0; i<c.size(); i++)
58 Limits::check(c[i],"Int::element");
59 if (offset==0) {
60 GECODE_ES_FAIL((Element::post_int<IntView,IntView>(home,c,x0,x1)));
61 } else {
62 OffsetView ov(x0,offset);
63 GECODE_ES_FAIL((Element::post_int<OffsetView,IntView>(home,c,ov,x1)));
64 }
65 }
66
67 void
68 element(Home home, IntSharedArray c, IntVar x0, BoolVar x1,
69 IntPropLevel) {
70 using namespace Int;
71 if (c.size() == 0)
72 throw TooFewArguments("Int::element");
73 GECODE_POST;
74 for (int i=0; i<c.size(); i++)
75 Limits::check(c[i],"Int::element");
76 GECODE_ES_FAIL((Element::post_int<IntView,BoolView>(home,c,x0,x1)));
77 }
78
79 void
80 element(Home home, IntSharedArray c, IntVar x0, int offset, BoolVar x1,
81 IntPropLevel) {
82 using namespace Int;
83 if (c.size() == 0)
84 throw TooFewArguments("Int::element");
85 GECODE_POST;
86 for (int i=0; i<c.size(); i++)
87 Limits::check(c[i],"Int::element");
88 if (offset==0) {
89 GECODE_ES_FAIL((Element::post_int<IntView,BoolView>(home,c,x0,x1)));
90 } else {
91 OffsetView ov(x0,offset);
92 GECODE_ES_FAIL((Element::post_int<OffsetView,BoolView>(home,c,ov,x1)));
93 }
94 }
95
96 void
97 element(Home home, IntSharedArray c, IntVar x0, int x1,
98 IntPropLevel) {
99 using namespace Int;
100 if (c.size() == 0)
101 throw TooFewArguments("Int::element");
102 Limits::check(x1,"Int::element");
103 GECODE_POST;
104 for (int i=0; i<c.size(); i++)
105 Limits::check(c[i],"Int::element");
106 ConstIntView cx1(x1);
107 GECODE_ES_FAIL((Element::post_int<IntView,ConstIntView>(home,c,x0,cx1)));
108 }
109
110 void
111 element(Home home, IntSharedArray c, IntVar x0, int offset, int x1,
112 IntPropLevel) {
113 using namespace Int;
114 if (c.size() == 0)
115 throw TooFewArguments("Int::element");
116 Limits::check(x1,"Int::element");
117 GECODE_POST;
118 for (int i=0; i<c.size(); i++)
119 Limits::check(c[i],"Int::element");
120 ConstIntView cx1(x1);
121 if (offset==0) {
122 GECODE_ES_FAIL((Element::post_int<IntView,ConstIntView>(home,c,x0,cx1)));
123 } else {
124 OffsetView ov(x0,offset);
125 GECODE_ES_FAIL((Element::post_int<OffsetView,ConstIntView>(home,c,ov,cx1)));
126 }
127 }
128
129 void
130 element(Home home, const IntVarArgs& c, IntVar x0, IntVar x1,
131 IntPropLevel ipl) {
132 using namespace Int;
133 if (c.size() == 0)
134 throw TooFewArguments("Int::element");
135 GECODE_POST;
136 IdxViewArray<IntView> iv(home,c);
137 if ((vbd(ipl) == IPL_DOM) || (vbd(ipl) == IPL_DEF)) {
138 GECODE_ES_FAIL((Element::ViewDom<IntView,IntView,IntView>
139 ::post(home,iv,x0,x1)));
140 } else {
141 GECODE_ES_FAIL((Element::ViewBnd<IntView,IntView,IntView>
142 ::post(home,iv,x0,x1)));
143 }
144 }
145
146 void
147 element(Home home, const IntVarArgs& c, IntVar x0, int offset, IntVar x1,
148 IntPropLevel ipl) {
149 using namespace Int;
150 if (c.size() == 0)
151 throw TooFewArguments("Int::element");
152 GECODE_POST;
153 IdxViewArray<IntView> iv(home,c);
154 if ((vbd(ipl) == IPL_DOM) || (vbd(ipl) == IPL_DEF)) {
155 if (offset==0) {
156 GECODE_ES_FAIL((Element::ViewDom<IntView,IntView,IntView>
157 ::post(home,iv,x0,x1)));
158 } else {
159 OffsetView ov(x0,offset);
160 GECODE_ES_FAIL((Element::ViewDom<IntView,OffsetView,IntView>
161 ::post(home,iv,ov,x1)));
162 }
163 } else {
164 if (offset==0) {
165 GECODE_ES_FAIL((Element::ViewBnd<IntView,IntView,IntView>
166 ::post(home,iv,x0,x1)));
167 } else {
168 OffsetView ov(x0,offset);
169 GECODE_ES_FAIL((Element::ViewBnd<IntView,OffsetView,IntView>
170 ::post(home,iv,ov,x1)));
171 }
172 }
173 }
174
175 void
176 element(Home home, const IntVarArgs& c, IntVar x0, int x1,
177 IntPropLevel ipl) {
178 using namespace Int;
179 if (c.size() == 0)
180 throw TooFewArguments("Int::element");
181 Limits::check(x1,"Int::element");
182 GECODE_POST;
183 IdxViewArray<IntView> iv(home,c);
184 ConstIntView v1(x1);
185 if ((vbd(ipl) == IPL_DOM) || (vbd(ipl) == IPL_DEF)) {
186 GECODE_ES_FAIL((Element::ViewDom<IntView,IntView,ConstIntView>
187 ::post(home,iv,x0,v1)));
188 } else {
189 GECODE_ES_FAIL((Element::ViewBnd<IntView,IntView,ConstIntView>
190 ::post(home,iv,x0,v1)));
191 }
192 }
193
194 void
195 element(Home home, const IntVarArgs& c, IntVar x0, int offset, int x1,
196 IntPropLevel ipl) {
197 using namespace Int;
198 if (c.size() == 0)
199 throw TooFewArguments("Int::element");
200 Limits::check(x1,"Int::element");
201 GECODE_POST;
202 IdxViewArray<IntView> iv(home,c);
203 ConstIntView v1(x1);
204 if ((vbd(ipl) == IPL_DOM) || (vbd(ipl) == IPL_DEF)) {
205 if (offset==0) {
206 GECODE_ES_FAIL((Element::ViewDom<IntView,IntView,ConstIntView>
207 ::post(home,iv,x0,v1)));
208 } else {
209 OffsetView ov(x0,offset);
210 GECODE_ES_FAIL((Element::ViewDom<IntView,OffsetView,ConstIntView>
211 ::post(home,iv,ov,v1)));
212 }
213 } else {
214 if (offset==0) {
215 GECODE_ES_FAIL((Element::ViewBnd<IntView,IntView,ConstIntView>
216 ::post(home,iv,x0,v1)));
217 } else {
218 OffsetView ov(x0,offset);
219 GECODE_ES_FAIL((Element::ViewBnd<IntView,OffsetView,ConstIntView>
220 ::post(home,iv,ov,v1)));
221 }
222 }
223 }
224
225 void
226 element(Home home, const BoolVarArgs& c, IntVar x0, BoolVar x1,
227 IntPropLevel) {
228 using namespace Int;
229 if (c.size() == 0)
230 throw TooFewArguments("Int::element");
231 GECODE_POST;
232 IdxViewArray<BoolView> iv(home,c);
233 GECODE_ES_FAIL((Element::ViewBnd<BoolView,IntView,BoolView>
234 ::post(home,iv,x0,x1)));
235 }
236
237 void
238 element(Home home, const BoolVarArgs& c, IntVar x0, int offset, BoolVar x1,
239 IntPropLevel) {
240 using namespace Int;
241 if (c.size() == 0)
242 throw TooFewArguments("Int::element");
243 GECODE_POST;
244 IdxViewArray<BoolView> iv(home,c);
245 if (offset==0) {
246 GECODE_ES_FAIL((Element::ViewBnd<BoolView,IntView,BoolView>
247 ::post(home,iv,x0,x1)));
248 } else {
249 OffsetView ov(x0,offset);
250 GECODE_ES_FAIL((Element::ViewBnd<BoolView,OffsetView,BoolView>
251 ::post(home,iv,ov,x1)));
252 }
253 }
254
255 void
256 element(Home home, const BoolVarArgs& c, IntVar x0, int x1,
257 IntPropLevel) {
258 using namespace Int;
259 if (c.size() == 0)
260 throw TooFewArguments("Int::element");
261 Limits::check(x1,"Int::element");
262 GECODE_POST;
263 IdxViewArray<BoolView> iv(home,c);
264 ConstIntView v1(x1);
265 GECODE_ES_FAIL((Element::ViewBnd<BoolView,IntView,ConstIntView>
266 ::post(home,iv,x0,v1)));
267 }
268
269 void
270 element(Home home, const BoolVarArgs& c, IntVar x0, int offset, int x1,
271 IntPropLevel) {
272 using namespace Int;
273 if (c.size() == 0)
274 throw TooFewArguments("Int::element");
275 Limits::check(x1,"Int::element");
276 GECODE_POST;
277 IdxViewArray<BoolView> iv(home,c);
278 ConstIntView v1(x1);
279 if (offset==0) {
280 GECODE_ES_FAIL((Element::ViewBnd<BoolView,IntView,ConstIntView>
281 ::post(home,iv,x0,v1)));
282 } else {
283 OffsetView ov(x0,offset);
284 GECODE_ES_FAIL((Element::ViewBnd<BoolView,OffsetView,ConstIntView>
285 ::post(home,iv,ov,v1)));
286
287 }
288 }
289
290 namespace {
291 IntVar
292 pair(Home home, IntVar x, int w, IntVar y, int h) {
293 IntVar xy(home,0,w*h-1);
294 if (Int::Element::Pair::post(home,x,y,xy,w,h) != ES_OK)
295 home.fail();
296 return xy;
297 }
298
299 IntVar
300 pairWithOffsets(Home home, IntVar x, int xoff, int w, IntVar y, int yoff, int h) {
301 IntVar xy(home,0,w*h-1);
302 if (xoff==0 && yoff==0) {
303 if (Int::Element::Pair::post(home,x,y,xy,w,h) != ES_OK)
304 home.fail();
305 } else {
306 Int::OffsetView xo(x,xoff);
307 Int::OffsetView yo(y,yoff);
308 if (Int::Element::PairWithOffsets::post(home,xo,yo,xy,w,h) != ES_OK)
309 home.fail();
310 }
311 return xy;
312 }
313 }
314
315 void
316 element(Home home, IntSharedArray a,
317 IntVar x, int w, IntVar y, int h, IntVar z,
318 IntPropLevel ipl) {
319 using namespace Int;
320 if (a.size() != w*h)
321 throw Int::ArgumentSizeMismatch("Int::element");
322 GECODE_POST;
323 element(home, a, pair(home,x,w,y,h), z, ipl);
324 }
325
326 void
327 element(Home home, IntSharedArray a,
328 IntVar x, int xoff, int w,
329 IntVar y, int yoff, int h,
330 IntVar z, IntPropLevel ipl) {
331 using namespace Int;
332 if (a.size() != w*h)
333 throw Int::ArgumentSizeMismatch("Int::element");
334 GECODE_POST;
335 element(home, a, pairWithOffsets(home,x,xoff,w,y,yoff,h), z, ipl);
336 }
337
338 void
339 element(Home home, IntSharedArray a,
340 IntVar x, int w, IntVar y, int h, BoolVar z,
341 IntPropLevel ipl) {
342 using namespace Int;
343 if (a.size() != w*h)
344 throw Int::ArgumentSizeMismatch("Int::element");
345 GECODE_POST;
346 element(home, a, pair(home,x,w,y,h), z, ipl);
347 }
348
349 void
350 element(Home home, IntSharedArray a,
351 IntVar x, int xoff, int w, IntVar y, int yoff, int h,
352 BoolVar z, IntPropLevel ipl) {
353 using namespace Int;
354 if (a.size() != w*h)
355 throw Int::ArgumentSizeMismatch("Int::element");
356 GECODE_POST;
357 element(home, a, pairWithOffsets(home,x,xoff,w,y,yoff,h), z, ipl);
358 }
359
360 void
361 element(Home home, const IntVarArgs& a,
362 IntVar x, int w, IntVar y, int h, IntVar z,
363 IntPropLevel ipl) {
364 using namespace Int;
365 if (a.size() != w*h)
366 throw Int::ArgumentSizeMismatch("Int::element");
367 GECODE_POST;
368 element(home, a, pair(home,x,w,y,h), z, ipl);
369 }
370
371 void
372 element(Home home, const IntVarArgs& a,
373 IntVar x, int xoff, int w,
374 IntVar y, int yoff, int h,
375 IntVar z, IntPropLevel ipl) {
376 using namespace Int;
377 if (a.size() != w*h)
378 throw Int::ArgumentSizeMismatch("Int::element");
379 GECODE_POST;
380 element(home, a, pairWithOffsets(home,x,xoff,w,y,yoff,h), z, ipl);
381 }
382
383 void
384 element(Home home, const BoolVarArgs& a,
385 IntVar x, int w, IntVar y, int h, BoolVar z,
386 IntPropLevel ipl) {
387 using namespace Int;
388 if (a.size() != w*h)
389 throw Int::ArgumentSizeMismatch("Int::element");
390 GECODE_POST;
391 element(home, a, pair(home,x,w,y,h), z, ipl);
392 }
393
394 void
395 element(Home home, const BoolVarArgs& a,
396 IntVar x, int xoff, int w,
397 IntVar y, int yoff, int h,
398 BoolVar z, IntPropLevel ipl) {
399 using namespace Int;
400 if (a.size() != w*h)
401 throw Int::ArgumentSizeMismatch("Int::element");
402 GECODE_POST;
403 element(home, a, pairWithOffsets(home,x,xoff,w,y,yoff,h), z, ipl);
404 }
405
406}
407
408// STATISTICS: int-post