Generated on Thu Jan 16 2025 00:00:00 for Gecode by doxygen 1.14.0
count.cpp
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, 2002
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/count.hh>
35#include <gecode/int/rel.hh>
36
37namespace Gecode {
38
39 void
40 count(Home home, const IntVarArgs& x, int n,
41 IntRelType irt, int m, IntPropLevel) {
42 using namespace Int;
43 Limits::check(n,"Int::count");
44 Limits::check(m,"Int::count");
45
47
48 ViewArray<IntView> xv(home,x);
49 ConstIntView y(n);
50
51 switch (irt) {
52 case IRT_EQ:
54 ::post(home,xv,y,m)));
55 break;
56 case IRT_NQ:
57 {
58 IntVar z(home,0,x.size());
59 GECODE_ME_FAIL(IntView(z).nq(home,m));
61 ::post(home,xv,y,z,0)));
62 }
63 break;
64 case IRT_LE:
65 m--; // FALL THROUGH
66 case IRT_LQ:
68 ::post(home,xv,y,m)));
69 break;
70 case IRT_GR:
71 m++; // FALL THROUGH
72 case IRT_GQ:
74 ::post(home,xv,y,m)));
75 break;
76 default:
77 throw UnknownRelation("Int::count");
78 }
79 }
80
81 void
82 count(Home home, const IntVarArgs& x, IntVar y,
83 IntRelType irt, int m, IntPropLevel ipl) {
84 using namespace Int;
85 Limits::check(m,"Int::count");
87 ViewArray<IntView> xv(home,x);
88
89 switch (irt) {
90 case IRT_EQ:
91 {
92 ConstIntView z(m);
93 if ((vbd(ipl) == IPL_DOM) || (vbd(ipl) == IPL_DEF))
95 ::post(home,xv,y,z,0)));
96 else
98 ::post(home,xv,y,z,0)));
99 }
100 break;
101 case IRT_NQ:
102 {
103 IntVar z(home,0,x.size());
104 GECODE_ME_FAIL(IntView(z).nq(home,m));
106 ::post(home,xv,y,z,0)));
107 }
108 break;
109 case IRT_LE:
110 m--; // FALL THROUGH
111 case IRT_LQ:
113 ::post(home,xv,y,m)));
114 break;
115 case IRT_GR:
116 m++; // FALL THROUGH
117 case IRT_GQ:
118 {
119 ConstIntView z(m);
120 if ((vbd(ipl) == IPL_DOM) || (vbd(ipl) == IPL_DEF))
122 ::post(home,xv,y,z,0)));
123 else
125 ::post(home,xv,y,z,0)));
126 }
127 break;
128 default:
129 throw UnknownRelation("Int::count");
130 }
131 }
132
133 void
134 count(Home home, const IntVarArgs& x, const IntSet& y,
135 IntRelType irt, int m, IntPropLevel) {
136 using namespace Int;
137
138 if (y.size() == 1) {
139 count(home,x,y.min(),irt,m);
140 return;
141 }
142
143 Limits::check(y.min(),"Int::count");
144 Limits::check(y.max(),"Int::count");
145 Limits::check(m,"Int::count");
146
148
149 ViewArray<IntView> xv(home,x);
150 switch (irt) {
151 case IRT_EQ:
153 break;
154 case IRT_NQ:
155 {
156 IntVar z(home,0,x.size());
157 GECODE_ME_FAIL(IntView(z).nq(home,m));
159 ::post(home,xv,y,z,0)));
160 }
161 break;
162 case IRT_LE:
163 m--; // FALL THROUGH
164 case IRT_LQ:
166 break;
167 case IRT_GR:
168 m++; // FALL THROUGH
169 case IRT_GQ:
171 break;
172 default:
173 throw UnknownRelation("Int::count");
174 }
175 }
176
177 void
178 count(Home home, const IntVarArgs& x, const IntArgs& y,
179 IntRelType irt, int m, IntPropLevel) {
180 using namespace Int;
181 if (x.size() != y.size())
182 throw ArgumentSizeMismatch("Int::count");
183 Limits::check(m,"Int::count");
185
186 ViewArray<OffsetView> xy(home,x.size());
187 for (int i=0; i<x.size(); i++)
188 xy[i] = OffsetView(x[i],-y[i]);
189
190 ZeroIntView zero;
191 switch (irt) {
192 case IRT_EQ:
194 ::post(home,xy,zero,m)));
195 break;
196 case IRT_NQ:
197 {
198 IntVar z(home,0,x.size());
199 GECODE_ME_FAIL(IntView(z).nq(home,m));
201 ::post(home,xy,zero,z,0)));
202 }
203 break;
204 case IRT_LE:
205 m--; // FALL THROUGH
206 case IRT_LQ:
208 ::post(home,xy,zero,m)));
209 break;
210 case IRT_GR:
211 m++; // FALL THROUGH
212 case IRT_GQ:
214 ::post(home,xy,zero,m)));
215 break;
216 default:
217 throw UnknownRelation("Int::count");
218 }
219 }
220
221 void
222 count(Home home, const IntVarArgs& x, int n,
224 using namespace Int;
225 Limits::check(n,"Int::count");
227 ViewArray<IntView> xv(home,x);
228 ConstIntView yv(n);
229 switch (irt) {
230 case IRT_EQ:
232 ::post(home,xv,yv,z,0)));
233 break;
234 case IRT_NQ:
235 {
236 IntVar nz(home,0,x.size());
239 ::post(home,xv,yv,nz,0)));
240 }
241 break;
242 case IRT_LE:
244 ::post(home,xv,yv,z,-1)));
245 break;
246 case IRT_LQ:
248 ::post(home,xv,yv,z,0)));
249 break;
250 case IRT_GR:
252 ::post(home,xv,yv,z,1)));
253 break;
254 case IRT_GQ:
256 ::post(home,xv,yv,z,0)));
257 break;
258 default:
259 throw UnknownRelation("Int::count");
260 }
261 }
262
263 void
264 count(Home home, const IntVarArgs& x, IntVar y,
265 IntRelType irt, IntVar z, IntPropLevel ipl) {
266 using namespace Int;
268 ViewArray<IntView> xv(home,x);
269 switch (irt) {
270 case IRT_EQ:
271 if ((vbd(ipl) == IPL_DOM) || (vbd(ipl) == IPL_DEF))
273 ::post(home,xv,y,z,0)));
274 else
276 ::post(home,xv,y,z,0)));
277 break;
278 case IRT_NQ:
279 {
280 IntVar nz(home,0,x.size());
283 ::post(home,xv,y,nz,0)));
284 }
285 break;
286 case IRT_LE:
288 ::post(home,xv,y,z,-1)));
289 break;
290 case IRT_LQ:
292 ::post(home,xv,y,z,0)));
293 break;
294 case IRT_GR:
295 if ((vbd(ipl) == IPL_DOM) || (vbd(ipl) == IPL_DEF))
297 ::post(home,xv,y,z,1)));
298 else
300 ::post(home,xv,y,z,1)));
301 break;
302 case IRT_GQ:
303 if ((vbd(ipl) == IPL_DOM) || (vbd(ipl) == IPL_DEF))
305 ::post(home,xv,y,z,0)));
306 else
308 ::post(home,xv,y,z,0)));
309 break;
310 default:
311 throw UnknownRelation("Int::count");
312 }
313 }
314
315 void
316 count(Home home, const IntVarArgs& x, const IntSet& y,
318 using namespace Int;
319
320 if (y.size() == 1) {
321 count(home,x,y.min(),irt,z);
322 return;
323 }
324
325 Limits::check(y.min(),"Int::count");
326 Limits::check(y.max(),"Int::count");
327
329 ViewArray<IntView> xv(home,x);
330 switch (irt) {
331 case IRT_EQ:
333 ::post(home,xv,y,z,0)));
334 break;
335 case IRT_NQ:
336 {
337 IntVar nz(home,0,x.size());
340 ::post(home,xv,y,nz,0)));
341 }
342 break;
343 case IRT_LE:
345 ::post(home,xv,y,z,-1)));
346 break;
347 case IRT_LQ:
349 ::post(home,xv,y,z,0)));
350 break;
351 case IRT_GR:
353 ::post(home,xv,y,z,1)));
354 break;
355 case IRT_GQ:
357 ::post(home,xv,y,z,0)));
358 break;
359 default:
360 throw UnknownRelation("Int::count");
361 }
362 }
363
364 void
365 count(Home home, const IntVarArgs& x, const IntArgs& y,
367 using namespace Int;
368 if (x.size() != y.size())
369 throw ArgumentSizeMismatch("Int::count");
371
372 ViewArray<OffsetView> xy(home,x.size());
373 for (int i=0; i<x.size(); i++)
374 xy[i] = OffsetView(x[i],-y[i]);
375
376 ZeroIntView u;
377 switch (irt) {
378 case IRT_EQ:
380 ::post(home,xy,u,z,0)));
381 break;
382 case IRT_NQ:
383 {
384 IntVar nz(home,0,x.size());
387 ::post(home,xy,u,nz,0)));
388 }
389 break;
390 case IRT_LE:
392 ::post(home,xy,u,z,-1)));
393 break;
394 case IRT_LQ:
396 ::post(home,xy,u,z,0)));
397 break;
398 case IRT_GR:
400 ::post(home,xy,u,z,1)));
401 break;
402 case IRT_GQ:
404 ::post(home,xy,u,z,0)));
405 break;
406 default:
407 throw UnknownRelation("Int::count");
408 }
409 }
410
411}
412
413// STATISTICS: int-post
Home class for posting propagators
Definition core.hpp:856
Passing integer arguments.
Definition int.hh:634
Integer sets.
Definition int.hh:174
Passing integer variables.
Definition int.hh:662
Integer variables.
Definition int.hh:371
Exception: Arguments are of different size
Definition exception.hpp:73
Constant integer view.
Definition view.hpp:851
Propagator for counting views (equal integer to number of equal views)
Definition count.hh:172
static ExecStatus post(Home home, ViewArray< VX > &x, VY y, int c)
Post propagator for .
Definition int-eq.hpp:43
Propagator for counting views (equal to number of equal views)
Definition count.hh:309
Propagator for counting views (greater or equal integer to number of equal views)
Definition count.hh:202
static ExecStatus post(Home home, ViewArray< VX > &x, VY y, int c)
Post propagator for .
Definition int-gq.hpp:43
Propagator for counting views (greater or equal to number of equal views)
Definition count.hh:379
Propagator for counting views (less or equal integer to number of equal views)
Definition count.hh:232
static ExecStatus post(Home home, ViewArray< VX > &x, VY y, int c)
Post propagator for .
Definition int-lq.hpp:43
Propagator for counting views (less or equal to number of equal views)
Definition count.hh:344
Integer view for integer variables.
Definition view.hpp:129
Offset integer view.
Definition view.hpp:443
static ExecStatus post(Home home, V0 x0, V1 x1)
Post propagator .
Definition nq.hpp:49
Exception: Unknown relation passed as argument
Definition exception.hpp:87
Zero integer view.
Definition view.hpp:1014
View arrays.
Definition array.hpp:253
#define GECODE_POST
Check for failure in a constraint post function.
Definition macros.hpp:40
#define GECODE_ES_FAIL(es)
Check whether execution status es is failed, and fail space home.
Definition macros.hpp:103
#define GECODE_ME_FAIL(me)
Check whether modification event me is failed, and fail space home.
Definition macros.hpp:77
IntRelType
Relation types for integers.
Definition int.hh:940
IntPropLevel
Propagation levels for integer propagators.
Definition int.hh:989
@ IRT_EQ
Equality ( )
Definition int.hh:941
@ IRT_NQ
Disequality ( )
Definition int.hh:942
@ IRT_GQ
Greater or equal ( )
Definition int.hh:945
@ IRT_LE
Less ( )
Definition int.hh:944
@ IRT_GR
Greater ( )
Definition int.hh:946
@ IRT_LQ
Less or equal ( )
Definition int.hh:943
@ IPL_DOM
Domain propagation Options: basic versus advanced propagation.
Definition int.hh:994
@ IPL_DEF
Simple propagation levels.
Definition int.hh:991
void check(int n, const char *l)
Check whether n is in range, otherwise throw out of limits with information l.
Definition limits.hpp:46
Finite domain integers.
Gecode toplevel namespace
void count(Home home, const IntVarArgs &x, int n, IntRelType irt, int m, IntPropLevel ipl=IPL_DEF)
Post propagator for .
Definition count.cpp:40
IntPropLevel vbd(IntPropLevel ipl)
Extract value, bounds, or domain propagation from propagation level.
Definition ipl.hpp:37
Post propagator for SetVar SetOpType SetVar SetRelType SetVar z
Definition set.hh:773
Post propagator for SetVar SetOpType SetVar y
Definition set.hh:773
TFE post(PropagatorGroup g)
Only post functions (but not propagators) from g are considered.
Definition filter.cpp:138
Post propagator for SetVar x
Definition set.hh:773