Generated on Sun Aug 26 2012 08:42:48 for Gecode by doxygen 1.8.1.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, 2005
8  *
9  * Last modified:
10  * $Date: 2011-08-22 21:08:29 +1000 (Mon, 22 Aug 2011) $ by $Author: schulte $
11  * $Revision: 12326 $
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 "test/int.hh"
39 
40 namespace Test { namespace Int {
41 
43  namespace Count {
44 
50 
51  class IntInt : public Test {
52  protected:
55  public:
58  : Test("Count::Int::Int::"+str(irt0),4,-2,2), irt(irt0) {}
60  virtual bool solution(const Assignment& x) const {
61  int m = 0;
62  for (int i=x.size(); i--; )
63  if (x[i] == 0)
64  m++;
65  return cmp(m,irt,2);
66  }
68  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
69  Gecode::count(home, x, 0, irt, 2);
70  }
71  };
72 
74  class SetInt : public Test {
75  protected:
78  public:
81  : Test("Count::Set::Int::"+str(irt0),4,-2,2), irt(irt0) {}
83  virtual bool solution(const Assignment& x) const {
84  int m = 0;
85  for (int i=x.size(); i--; )
86  if ((x[i] >= -1) && (x[i] <= 1))
87  m++;
88  return cmp(m,irt,2);
89  }
91  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
92  Gecode::IntSet s(-1,1);
93  Gecode::count(home, x, s, irt, 2);
94  }
95  };
96 
98  class IntIntDup : public Test {
99  protected:
102  public:
105  : Test("Count::Int::Int::Dup::"+str(irt0),4,-2,2), irt(irt0) {}
107  virtual bool solution(const Assignment& x) const {
108  int m = 0;
109  for (int i=x.size(); i--; )
110  if (x[i] == 0)
111  m += 2;
112  return cmp(m,irt,4);
113  }
115  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
116  Gecode::IntVarArgs y(8);
117  for (int i=x.size(); i--; )
118  y[i]=y[4+i]=x[i];
119  Gecode::count(home, y, 0, irt, 4);
120  }
121  };
122 
124  class IntVar : public Test {
125  protected:
128  public:
131  : Test("Count::Int::Var::"+str(irt0),5,-2,2), irt(irt0) {}
133  virtual bool solution(const Assignment& x) const {
134  int m = 0;
135  for (int i=0; i<4; i++)
136  if (x[i] == 0)
137  m++;
138  return cmp(m,irt,x[4]);
139  }
141  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
142  Gecode::IntVarArgs y(4);
143  for (int i=0; i<4; i++)
144  y[i]=x[i];
145  Gecode::count(home, y, 0, irt, x[4]);
146  }
147  };
148 
150  class SetVar : public Test {
151  protected:
154  public:
157  : Test("Count::Set::Var::"+str(irt0),5,-2,2), irt(irt0) {}
159  virtual bool solution(const Assignment& x) const {
160  int m = 0;
161  for (int i=0; i<4; i++)
162  if ((x[i] >= -1) && (x[i] <= 1))
163  m++;
164  return cmp(m,irt,x[4]);
165  }
167  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
168  Gecode::IntVarArgs y(4);
169  for (int i=0; i<4; i++)
170  y[i]=x[i];
171  Gecode::IntSet s(-1,1);
172  Gecode::count(home, y, s, irt, x[4]);
173  }
174  };
175 
176  Gecode::IntArgs ints(4, 1,0,3,2);
177 
179  class IntArrayInt : public Test {
180  protected:
183  public:
186  : Test("Count::IntArray::Int::"+str(irt0),5,-2,2), irt(irt0) {}
188  virtual bool solution(const Assignment& x) const {
189  int m = 0;
190  for (int i=0; i<4; i++)
191  if (x[i] == ints[i])
192  m++;
193  return cmp(m,irt,2);
194  }
196  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
197  Gecode::IntVarArgs y(4);
198  for (int i=0; i<4; i++)
199  y[i]=x[i];
200  Gecode::count(home, y, ints, irt, 2);
201  }
202  };
203 
205  class IntArrayVar : public Test {
206  protected:
209  public:
212  : Test("Count::IntArray::Var::"+str(irt0),5,-2,2), irt(irt0) {}
214  virtual bool solution(const Assignment& x) const {
215  int m = 0;
216  for (int i=0; i<4; i++)
217  if (x[i] == ints[i])
218  m++;
219  return cmp(m,irt,x[4]);
220  }
222  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
223  Gecode::IntVarArgs y(4);
224  for (int i=0; i<4; i++)
225  y[i]=x[i];
226  Gecode::count(home, y, ints, irt, x[4]);
227  }
228  };
229 
231  class IntVarShared : public Test {
232  protected:
235  public:
238  : Test("Count::Int::Var::Shared::"+str(irt0),4,-2,2), irt(irt0) {}
240  virtual bool solution(const Assignment& x) const {
241  int m = 0;
242  for (int i=0; i<4; i++)
243  if (x[i] == 0)
244  m++;
245  return cmp(m,irt,x[2]);
246  }
248  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
249  Gecode::count(home, x, 0, irt, x[2]);
250  }
251  };
252 
254  class VarVar : public Test {
255  protected:
258  public:
261  : Test("Count::Var::Var::"+str(irt0),5,-2,2), irt(irt0) {}
263  virtual bool solution(const Assignment& x) const {
264  int m = 0;
265  for (int i=0; i<3; i++)
266  if (x[i] == x[3])
267  m++;
268  return cmp(m,irt,x[4]);
269  }
271  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
272  Gecode::IntVarArgs y(3);
273  for (int i=0; i<3; i++)
274  y[i]=x[i];
275  Gecode::count(home, y, x[3], irt, x[4]);
276  }
277  };
278 
280  class VarInt : public Test {
281  protected:
284  public:
287  : Test("Count::Var::Int::"+str(irt0),4,-2,2), irt(irt0) {}
289  virtual bool solution(const Assignment& x) const {
290  int m = 0;
291  for (int i=0; i<3; i++)
292  if (x[i] == x[3])
293  m++;
294  return cmp(m,irt,2);
295  }
297  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
298  Gecode::IntVarArgs y(3);
299  for (int i=0; i<3; i++)
300  y[i]=x[i];
301  Gecode::count(home, y, x[3], irt, 2);
302  }
303  };
304 
306  class VarVarSharedA : public Test {
307  protected:
310  public:
313  : Test("Count::Var::Var::Shared::A::"+str(irt0),5,-2,2), irt(irt0) {}
315  virtual bool solution(const Assignment& x) const {
316  int m = 0;
317  for (int i=0; i<4; i++)
318  if (x[i] == x[1])
319  m++;
320  return cmp(m,irt,x[4]);
321  }
323  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
324  Gecode::IntVarArgs y(4);
325  for (int i=0; i<4; i++)
326  y[i]=x[i];
327  Gecode::count(home, y, x[1], irt, x[4]);
328  }
329  };
330 
332  class VarVarSharedB : public Test {
333  protected:
336  public:
339  : Test("Count::Var::Var::Shared::B::"+str(irt0),5,-2,2), irt(irt0) {}
341  virtual bool solution(const Assignment& x) const {
342  int m = 0;
343  for (int i=0; i<4; i++)
344  if (x[i] == x[4])
345  m++;
346  return cmp(m,irt,x[3]);
347  }
349  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
350  Gecode::IntVarArgs y(4);
351  for (int i=0; i<4; i++)
352  y[i]=x[i];
353  Gecode::count(home, y, x[4], irt, x[3]);
354  }
355  };
356 
358  class VarVarSharedC : public Test {
359  protected:
362  public:
365  : Test("Count::Var::Var::Shared::C::"+str(irt0),4,-2,2), irt(irt0) {}
367  virtual bool solution(const Assignment& x) const {
368  int m = 0;
369  for (int i=0; i<4; i++)
370  if (x[i] == x[1])
371  m++;
372  return cmp(m,irt,x[3]);
373  }
375  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
376  Gecode::count(home, x, x[1], irt, x[3]);
377  }
378  };
379 
381  class Create {
382  public:
384  Create(void) {
385  for (IntRelTypes irts; irts(); ++irts) {
386  (void) new IntInt(irts.irt());
387  (void) new SetInt(irts.irt());
388  (void) new IntIntDup(irts.irt());
389  (void) new IntVar(irts.irt());
390  (void) new SetVar(irts.irt());
391  (void) new IntArrayInt(irts.irt());
392  (void) new IntArrayVar(irts.irt());
393  (void) new IntVarShared(irts.irt());
394  (void) new VarVar(irts.irt());
395  (void) new VarInt(irts.irt());
396  (void) new VarVarSharedA(irts.irt());
397  (void) new VarVarSharedB(irts.irt());
398  (void) new VarVarSharedC(irts.irt());
399  }
400  }
401  };
402 
405 
406  }
407 }}
408 
409 // STATISTICS: test-int
410