Generated on Sat Feb 7 2015 02:01:21 for Gecode by doxygen 1.8.9.1
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  * Last modified:
10  * $Date: 2011-11-29 14:03:56 +0100 (Tue, 29 Nov 2011) $ by $Author: schulte $
11  * $Revision: 12485 $
12  *
13  * This file is part of Gecode, the generic constraint
14  * development environment:
15  * http://www.gecode.org
16  *
17  * Permission is hereby granted, free of charge, to any person obtaining
18  * a copy of this software and associated documentation files (the
19  * "Software"), to deal in the Software without restriction, including
20  * without limitation the rights to use, copy, modify, merge, publish,
21  * distribute, sublicense, and/or sell copies of the Software, and to
22  * permit persons to whom the Software is furnished to do so, subject to
23  * the following conditions:
24  *
25  * The above copyright notice and this permission notice shall be
26  * included in all copies or substantial portions of the Software.
27  *
28  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35  *
36  */
37 
38 #include <gecode/int/count.hh>
39 #include <gecode/int/rel.hh>
40 
41 namespace Gecode {
42 
43  void
44  count(Home home, const IntVarArgs& x, int n,
45  IntRelType irt, int m, IntConLevel) {
46  using namespace Int;
47  Limits::check(n,"Int::count");
48  Limits::check(m,"Int::count");
49 
50  if (home.failed()) return;
51 
52  ViewArray<IntView> xv(home,x);
53  ConstIntView y(n);
54 
55  switch (irt) {
56  case IRT_EQ:
58  ::post(home,xv,y,m)));
59  break;
60  case IRT_NQ:
61  {
62  IntVar z(home,0,x.size());
63  GECODE_ME_FAIL(IntView(z).nq(home,m));
65  ::post(home,xv,y,z,0)));
66  }
67  break;
68  case IRT_LE:
69  m--; // FALL THROUGH
70  case IRT_LQ:
72  ::post(home,xv,y,m)));
73  break;
74  case IRT_GR:
75  m++; // FALL THROUGH
76  case IRT_GQ:
78  ::post(home,xv,y,m)));
79  break;
80  default:
81  throw UnknownRelation("Int::count");
82  }
83  }
84 
85  void
86  count(Home home, const IntVarArgs& x, IntVar y,
87  IntRelType irt, int m, IntConLevel icl) {
88  using namespace Int;
89  Limits::check(m,"Int::count");
90  if (home.failed()) return;
91  ViewArray<IntView> xv(home,x);
92 
93  switch (irt) {
94  case IRT_EQ:
95  {
96  ConstIntView z(m);
97  if ((icl == ICL_DOM) || (icl == ICL_DEF))
99  ::post(home,xv,y,z,0)));
100  else
102  ::post(home,xv,y,z,0)));
103  }
104  break;
105  case IRT_NQ:
106  {
107  IntVar z(home,0,x.size());
108  GECODE_ME_FAIL(IntView(z).nq(home,m));
110  ::post(home,xv,y,z,0)));
111  }
112  break;
113  case IRT_LE:
114  m--; // FALL THROUGH
115  case IRT_LQ:
117  ::post(home,xv,y,m)));
118  break;
119  case IRT_GR:
120  m++; // FALL THROUGH
121  case IRT_GQ:
122  {
123  ConstIntView z(m);
124  if ((icl == ICL_DOM) || (icl == ICL_DEF))
126  ::post(home,xv,y,z,0)));
127  else
129  ::post(home,xv,y,z,0)));
130  }
131  break;
132  default:
133  throw UnknownRelation("Int::count");
134  }
135  }
136 
137  void
138  count(Home home, const IntVarArgs& x, const IntSet& y,
139  IntRelType irt, int m, IntConLevel) {
140  using namespace Int;
141 
142  if (y.size() == 1) {
143  count(home,x,y.min(),irt,m);
144  return;
145  }
146 
147  Limits::check(y.min(),"Int::count");
148  Limits::check(y.max(),"Int::count");
149  Limits::check(m,"Int::count");
150 
151  if (home.failed()) return;
152 
153  ViewArray<IntView> xv(home,x);
154  switch (irt) {
155  case IRT_EQ:
157  break;
158  case IRT_NQ:
159  {
160  IntVar z(home,0,x.size());
161  GECODE_ME_FAIL(IntView(z).nq(home,m));
163  ::post(home,xv,y,z,0)));
164  }
165  break;
166  case IRT_LE:
167  m--; // FALL THROUGH
168  case IRT_LQ:
170  break;
171  case IRT_GR:
172  m++; // FALL THROUGH
173  case IRT_GQ:
175  break;
176  default:
177  throw UnknownRelation("Int::count");
178  }
179  }
180 
181  void
182  count(Home home, const IntVarArgs& x, const IntArgs& y,
183  IntRelType irt, int m, IntConLevel) {
184  using namespace Int;
185  if (x.size() != y.size())
186  throw ArgumentSizeMismatch("Int::count");
187  Limits::check(m,"Int::count");
188  if (home.failed()) return;
189 
190  ViewArray<OffsetView> xy(home,x.size());
191  for (int i=x.size(); i--; )
192  xy[i] = OffsetView(x[i],-y[i]);
193 
194  ZeroIntView zero;
195  switch (irt) {
196  case IRT_EQ:
198  ::post(home,xy,zero,m)));
199  break;
200  case IRT_NQ:
201  {
202  IntVar z(home,0,x.size());
203  GECODE_ME_FAIL(IntView(z).nq(home,m));
205  ::post(home,xy,zero,z,0)));
206  }
207  break;
208  case IRT_LE:
209  m--; // FALL THROUGH
210  case IRT_LQ:
212  ::post(home,xy,zero,m)));
213  break;
214  case IRT_GR:
215  m++; // FALL THROUGH
216  case IRT_GQ:
218  ::post(home,xy,zero,m)));
219  break;
220  default:
221  throw UnknownRelation("Int::count");
222  }
223  }
224 
225  void
226  count(Home home, const IntVarArgs& x, int n,
227  IntRelType irt, IntVar z, IntConLevel) {
228  using namespace Int;
229  Limits::check(n,"Int::count");
230  if (home.failed()) return;
231  ViewArray<IntView> xv(home,x);
232  ConstIntView yv(n);
233  switch (irt) {
234  case IRT_EQ:
236  ::post(home,xv,yv,z,0)));
237  break;
238  case IRT_NQ:
239  {
240  IntVar nz(home,0,x.size());
243  ::post(home,xv,yv,nz,0)));
244  }
245  break;
246  case IRT_LE:
248  ::post(home,xv,yv,z,-1)));
249  break;
250  case IRT_LQ:
252  ::post(home,xv,yv,z,0)));
253  break;
254  case IRT_GR:
256  ::post(home,xv,yv,z,1)));
257  break;
258  case IRT_GQ:
260  ::post(home,xv,yv,z,0)));
261  break;
262  default:
263  throw UnknownRelation("Int::count");
264  }
265  }
266 
267  void
268  count(Home home, const IntVarArgs& x, IntVar y,
269  IntRelType irt, IntVar z, IntConLevel icl) {
270  using namespace Int;
271  if (home.failed()) return;
272  ViewArray<IntView> xv(home,x);
273  switch (irt) {
274  case IRT_EQ:
275  if ((icl == ICL_DOM) || (icl == ICL_DEF))
277  ::post(home,xv,y,z,0)));
278  else
280  ::post(home,xv,y,z,0)));
281  break;
282  case IRT_NQ:
283  {
284  IntVar nz(home,0,x.size());
287  ::post(home,xv,y,nz,0)));
288  }
289  break;
290  case IRT_LE:
292  ::post(home,xv,y,z,-1)));
293  break;
294  case IRT_LQ:
296  ::post(home,xv,y,z,0)));
297  break;
298  case IRT_GR:
299  if ((icl == ICL_DOM) || (icl == ICL_DEF))
301  ::post(home,xv,y,z,1)));
302  else
304  ::post(home,xv,y,z,0)));
305  break;
306  case IRT_GQ:
307  if ((icl == ICL_DOM) || (icl == ICL_DEF))
309  ::post(home,xv,y,z,0)));
310  else
312  ::post(home,xv,y,z,0)));
313  break;
314  default:
315  throw UnknownRelation("Int::count");
316  }
317  }
318 
319  void
320  count(Home home, const IntVarArgs& x, const IntSet& y,
321  IntRelType irt, IntVar z, IntConLevel) {
322  using namespace Int;
323 
324  if (y.size() == 1) {
325  count(home,x,y.min(),irt,z);
326  return;
327  }
328 
329  Limits::check(y.min(),"Int::count");
330  Limits::check(y.max(),"Int::count");
331 
332  if (home.failed()) return;
333  ViewArray<IntView> xv(home,x);
334  switch (irt) {
335  case IRT_EQ:
337  ::post(home,xv,y,z,0)));
338  break;
339  case IRT_NQ:
340  {
341  IntVar nz(home,0,x.size());
344  ::post(home,xv,y,nz,0)));
345  }
346  break;
347  case IRT_LE:
349  ::post(home,xv,y,z,-1)));
350  break;
351  case IRT_LQ:
353  ::post(home,xv,y,z,0)));
354  break;
355  case IRT_GR:
357  ::post(home,xv,y,z,1)));
358  break;
359  case IRT_GQ:
361  ::post(home,xv,y,z,0)));
362  break;
363  default:
364  throw UnknownRelation("Int::count");
365  }
366  }
367 
368  void
369  count(Home home, const IntVarArgs& x, const IntArgs& y,
370  IntRelType irt, IntVar z, IntConLevel) {
371  using namespace Int;
372  if (x.size() != y.size())
373  throw ArgumentSizeMismatch("Int::count");
374  if (home.failed()) return;
375 
376  ViewArray<OffsetView> xy(home,x.size());
377  for (int i=x.size(); i--; )
378  xy[i] = OffsetView(x[i],-y[i]);
379 
380  ZeroIntView u;
381  switch (irt) {
382  case IRT_EQ:
384  ::post(home,xy,u,z,0)));
385  break;
386  case IRT_NQ:
387  {
388  IntVar nz(home,0,x.size());
391  ::post(home,xy,u,nz,0)));
392  }
393  break;
394  case IRT_LE:
396  ::post(home,xy,u,z,-1)));
397  break;
398  case IRT_LQ:
400  ::post(home,xy,u,z,0)));
401  break;
402  case IRT_GR:
404  ::post(home,xy,u,z,1)));
405  break;
406  case IRT_GQ:
408  ::post(home,xy,u,z,0)));
409  break;
410  default:
411  throw UnknownRelation("Int::count");
412  }
413  }
414 
415 }
416 
417 // STATISTICS: int-post
bool failed(void) const
Check whether corresponding space is failed.
Definition: core.hpp:3446
IntConLevel
Consistency levels for integer propagators.
Definition: int.hh:937
void post(Home home, Term *t, int n, FloatRelType frt, FloatVal c)
Post propagator for linear constraint over floats.
Definition: post.cpp:228
int size(void) const
Return size of array (number of elements)
Definition: array.hpp:1662
Less or equal ( )
Definition: int.hh:906
Propagator for counting views (equal to number of equal views)
Definition: count.hh:306
Greater ( )
Definition: int.hh:909
Greater or equal ( )
Definition: int.hh:908
Propagator for counting views (less or equal integer to number of equal views)
Definition: count.hh:231
Propagator for counting views (greater or equal to number of equal views)
Definition: count.hh:376
Exception: Unknown relation passed as argument
Definition: exception.hpp:91
Gecode::IntArgs i(4, 1, 2, 3, 4)
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:238
Equality ( )
Definition: int.hh:904
Propagator for counting views (equal integer to number of equal views)
Definition: count.hh:171
IntRelType
Relation types for integers.
Definition: int.hh:903
Propagator for counting views (less or equal to number of equal views)
Definition: count.hh:341
Less ( )
Definition: int.hh:907
Integer sets.
Definition: int.hh:171
Offset integer view.
Definition: view.hpp:422
unsigned int size(void) const
Return size (cardinality) of set.
Definition: int-set-1.hpp:161
Passing integer variables.
Definition: int.hh:636
Passing integer arguments.
Definition: int.hh:607
Propagator for counting views (greater or equal integer to number of equal views) ...
Definition: count.hh:201
Zero integer view.
Definition: view.hpp:959
union Gecode::@518::NNF::@57 u
Union depending on nodetype t.
Constant integer view.
Definition: view.hpp:804
Integer view for integer variables.
Definition: view.hpp:129
Node * x
Pointer to corresponding Boolean expression node.
Definition: bool-expr.cpp:253
void count(Home home, const IntVarArgs &x, int n, IntRelType irt, int m, IntConLevel)
Post propagator for .
Definition: count.cpp:44
Integer variables.
Definition: int.hh:350
The default consistency for a constraint.
Definition: int.hh:941
Binary disequality propagator.
Definition: rel.hh:432
#define GECODE_ME_FAIL(me)
Check whether modification event me is failed, and fail space home.
Definition: macros.hpp:70
Gecode toplevel namespace
Disequality ( )
Definition: int.hh:905
void check(int n, const char *l)
Check whether n is in range, otherwise throw out of limits with information l.
Definition: limits.hpp:50
Home class for posting propagators
Definition: core.hpp:717
Exception: Arguments are of different size
Definition: exception.hpp:77
#define GECODE_ES_FAIL(es)
Check whether execution status es is failed, and fail space home.
Definition: macros.hpp:96
Domain propagation or consistency.
Definition: int.hh:940
int max(int i) const
Return maximum of range at position i.
Definition: int-set-1.hpp:121
int min(int i) const
Return minimum of range at position i.
Definition: int-set-1.hpp:115