Generated on Thu Mar 7 2013 10:21:43 for Gecode by doxygen 1.8.3.1
singleton.hpp
Go to the documentation of this file.
1 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /*
3  * Main authors:
4  * Guido Tack <tack@gecode.org>
5  *
6  * Contributing authors:
7  * Christian Schulte <schulte@gecode.org>
8  *
9  * Copyright:
10  * Guido Tack, 2004
11  * Christian Schulte, 2004
12  *
13  * Last modified:
14  * $Date: 2011-08-20 00:47:28 +1000 (Sat, 20 Aug 2011) $ by $Author: tack $
15  * $Revision: 12318 $
16  *
17  * This file is part of Gecode, the generic constraint
18  * development environment:
19  * http://www.gecode.org
20  *
21  * Permission is hereby granted, free of charge, to any person obtaining
22  * a copy of this software and associated documentation files (the
23  * "Software"), to deal in the Software without restriction, including
24  * without limitation the rights to use, copy, modify, merge, publish,
25  * distribute, sublicense, and/or sell copies of the Software, and to
26  * permit persons to whom the Software is furnished to do so, subject to
27  * the following conditions:
28  *
29  * The above copyright notice and this permission notice shall be
30  * included in all copies or substantial portions of the Software.
31  *
32  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
33  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
34  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
35  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
36  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
37  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
38  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
39  *
40  */
41 
42 namespace Gecode { namespace Set {
43 
46 
49  : DerivedView<Gecode::Int::IntView>(y) {}
50 
53  : DerivedView<Gecode::Int::IntView>(y) {}
54 
57  switch(pc) {
58  case PC_SET_VAL:
59  case PC_SET_CGLB:
60  case PC_SET_CARD:
62  default:
64  }
65  }
66 
69  switch(me) {
71  return ME_SET_FAILED;
73  return ME_SET_NONE;
75  return ME_SET_VAL;
77  return ME_SET_LUB;
78  default:
79  return ME_SET_LUB;
80  }
81  }
82 
85  switch(me) {
86  case ME_SET_FAILED:
88  case ME_SET_NONE:
90  case ME_SET_VAL:
92  default:
94  }
95  }
96 
97  forceinline unsigned int
98  SingletonView::glbSize(void) const {
99  return x.assigned() ? 1U : 0U;
100  }
101 
102  forceinline unsigned int
103  SingletonView::lubSize(void) const { return x.size(); }
104 
105  forceinline unsigned int
107  return lubSize() - glbSize();
108  }
109 
110  forceinline bool
111  SingletonView::contains(int n) const { return x.assigned() ?
112  (x.val()==n) : false; }
113 
114  forceinline bool
115  SingletonView::notContains(int n) const { return !x.in(n); }
116 
117  forceinline unsigned int
118  SingletonView::cardMin() const { return 1; }
119 
120  forceinline unsigned int
121  SingletonView::cardMax() const { return 1; }
122 
123  forceinline int
124  SingletonView::lubMin() const { return x.min(); }
125 
126  forceinline int
127  SingletonView::lubMax() const { return x.max(); }
128 
129  forceinline int
130  SingletonView::glbMin() const { return x.assigned() ?
131  x.val() : BndSet::MIN_OF_EMPTY; }
132 
133  forceinline int
134  SingletonView::glbMax() const { return x.assigned() ?
135  x.val() : BndSet::MAX_OF_EMPTY; }
136 
138  SingletonView::cardMin(Space&, unsigned int c) {
139  return c<=1 ? ME_SET_NONE : ME_SET_FAILED;
140  }
141 
143  SingletonView::cardMax(Space&, unsigned int c) {
144  return c<1 ? ME_SET_FAILED : ME_SET_NONE;
145  }
146 
149  return me_inttoset(x.eq(home,c));
150  }
151 
154  return me_inttoset(x.eq(home,c));
155  }
156 
158  SingletonView::intersect(Space& home,int i, int j) {
159  ModEvent me1 = me_inttoset(x.gq(home,i));
160  ModEvent me2 = me_inttoset(x.lq(home,j));
161  if (me_failed(me1) || me_failed(me2))
162  return ME_SET_FAILED;
163  switch (me1) {
164  case ME_SET_NONE:
165  case ME_SET_LUB:
166  return me2;
167  case ME_SET_VAL:
168  return ME_SET_VAL;
169  default:
170  GECODE_NEVER;
171  return ME_SET_VAL;
172  }
173  }
174 
177  return me_inttoset(x.nq(home,c));
178  }
179 
181  SingletonView::include(Space& home, int j, int k) {
182  return j==k ? me_inttoset(x.eq(home,j)) : ME_SET_FAILED ;
183  }
184 
186  SingletonView::exclude(Space& home, int j, int k) {
187  ModEvent me1 = me_inttoset(x.gr(home,j));
188  ModEvent me2 = me_inttoset(x.le(home,k));
189  if (me_failed(me1) || me_failed(me2))
190  return ME_SET_FAILED;
191  switch (me1) {
192  case ME_SET_NONE:
193  case ME_SET_LUB:
194  return me2;
195  case ME_SET_VAL:
196  return ME_SET_VAL;
197  default:
198  GECODE_NEVER;
199  return ME_SET_VAL;
200  }
201  }
202 
203  template<class I> ModEvent
204  SingletonView::excludeI(Space& home, I& iter) {
205  return me_inttoset(x.minus_r(home,iter));
206  }
207 
208  template<class I> ModEvent
209  SingletonView::includeI(Space& home, I& iter) {
210  if (!iter())
211  return ME_SET_NONE;
212 
213  if (iter.min()!=iter.max())
214  return ME_SET_FAILED;
215 
216  int val = iter.min();
217  ++iter;
218  if ( iter() )
219  return ME_SET_FAILED;
220 
221  return me_inttoset(x.eq(home, val));
222  }
223 
224  template<class I> ModEvent
226  return me_inttoset(x.inter_r(home,iter));
227  }
228 
229  forceinline void
231  bool schedule) {
232  x.subscribe(home,p,pc_settoint(pc),schedule);
233  }
234  forceinline void
236  x.cancel(home,p,pc_settoint(pc));
237  }
238 
239  forceinline void
241  x.subscribe(home,a);
242  }
243  forceinline void
245  x.cancel(home,a);
246  }
247 
248 
249  forceinline void
251  return Gecode::Int::IntView::schedule(home,p,me_settoint(me));
252  }
255  return me_inttoset(Int::IntView::me(med));
256  }
259  return SetView::med(me_settoint(me));
260  }
261 
262 
263  /*
264  * Delta information for advisors
265  *
266  * For SingletonViews, a glb change means that the view is assigned.
267  * Thus, the delta for the glb is always equal to the delta for the lub.
268  *
269  */
270 
274  }
275 
276  forceinline int
277  SingletonView::glbMin(const Delta& d) const { return x.min(d); }
278 
279  forceinline int
280  SingletonView::glbMax(const Delta& d) const { return x.max(d); }
281 
282  forceinline bool
283  SingletonView::glbAny(const Delta& d) const { return x.any(d); }
284 
285  forceinline int
286  SingletonView::lubMin(const Delta& d) const { return x.min(d); }
287 
288  forceinline int
289  SingletonView::lubMax(const Delta& d) const { return x.max(d); }
290 
291  forceinline bool
292  SingletonView::lubAny(const Delta& d) const { return x.any(d); }
293 
294  /*
295  * Iterators
296  *
297  */
298 
303  template<>
305  public:
307 
308 
309  LubRanges(void);
311  LubRanges(const SingletonView& x);
313  void init(const SingletonView& x);
315  };
316 
319 
322  Gecode::Int::IntVarImpFwd(s.base().varimp()) {}
323 
324  forceinline void
327  }
328 
333  template<>
335  private:
336  int val;
337  bool flag;
338  public:
340 
341 
342  GlbRanges(void);
344  GlbRanges(const SingletonView& x);
346  void init(const SingletonView& x);
347 
349 
350 
351  bool operator ()(void) const;
353  void operator ++(void);
355 
357 
358 
359  int min(void) const;
361  int max(void) const;
363  unsigned int width(void) const;
365  };
366 
369 
370  forceinline void
372  if (s.base().assigned()) {
373  val = s.base().val();
374  flag = true;
375  } else {
376  val = 0;
377  flag = false;
378  }
379  }
380 
383  init(s);
384  }
385 
386  forceinline bool
387  GlbRanges<SingletonView>::operator ()(void) const { return flag; }
388 
389  forceinline void
391 
392  forceinline int
393  GlbRanges<SingletonView>::min(void) const { return val; }
394  forceinline int
395  GlbRanges<SingletonView>::max(void) const { return val; }
396  forceinline unsigned int
397  GlbRanges<SingletonView>::width(void) const { return 1; }
398 
399 }}
400 
401 // STATISTICS: set-var
402