Generated on Mon Sep 17 2012 22:20:42 for Gecode by doxygen 1.8.1.1
scale.hpp
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-09-28 22:50:49 +1000 (Wed, 28 Sep 2011) $ by $Author: tack $
11  * $Revision: 12418 $
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 namespace Gecode { namespace Int {
39 
40  /*
41  * Support functions for division
42  *
43  */
44  template<class Val, class UnsVal>
45  forceinline Val
47  return static_cast<Val>(floor(y / a));
48  }
49 
50  template<class Val, class UnsVal>
51  forceinline Val
53  return static_cast<Val>(ceil(y / a));
54  }
55 
56  template<class Val, class UnsVal>
57  forceinline Val
58  ScaleView<Val,UnsVal>::exact_div(double y, bool& exact) const {
59  double ya = y / a;
60  if (ceil(ya) == ya) {
61  exact = true; return static_cast<Val>(ya);
62  } else {
63  exact = false; return 0;
64  }
65  }
66 
67 #if GECODE_INT_RND_TWDS_ZERO
68 
69  template<class Val, class UnsVal>
70  forceinline int
72  return ((y >= 0) ? y : (y-a+1))/a;
73  }
74 
75  template<class Val, class UnsVal>
76  forceinline int
77  ScaleView<Val,UnsVal>::ceil_div(int y) const {
78  return ((y >= 0) ? (y+a-1) : y)/a;
79  }
80 
81  template<class Val, class UnsVal>
82  forceinline int
83  ScaleView<Val,UnsVal>::exact_div(int y, bool& exact) const {
84  int ya = y / a;
85  if (a * ya == y) {
86  exact = true; return ya;
87  } else {
88  exact = false; return 0;
89  }
90  }
91 
92 #endif
93 
94 
95  /*
96  * Constructors and initialization
97  *
98  */
99  template<class Val, class UnsVal>
102 
103  template<class Val, class UnsVal>
106  : DerivedView<IntView>(y), a(b) {}
107 
108 
109  /*
110  * Value access
111  *
112  */
113  template<class Val, class UnsVal>
114  forceinline int
116  return a;
117  }
118  template<class Val, class UnsVal>
119  forceinline Val
121  Val c = x.min(); c *= a; return c;
122  }
123 
124  template<class Val, class UnsVal>
125  forceinline Val
127  Val c = x.max(); c *= a; return c;
128  }
129 
130  template<class Val, class UnsVal>
131  forceinline Val
133  Val c = x.med(); c *= a; return c;
134  }
135 
136  template<class Val, class UnsVal>
137  forceinline Val
139  Val c = x.val(); c *= a; return c;
140  }
141 
142  template<class Val, class UnsVal>
143  forceinline UnsVal
145  return static_cast<UnsVal>(x.size());
146  }
147 
148  template<class Val, class UnsVal>
149  forceinline UnsVal
151  UnsVal c = x.width(); c *= a; return c;
152  }
153 
154  template<class Val, class UnsVal>
155  forceinline UnsVal
157  UnsVal c = x.regret_min(); c *= a; return c;
158  }
159 
160  template<class Val, class UnsVal>
161  forceinline UnsVal
163  UnsVal c = x.regret_max(); c *= a; return c;
164  }
165 
166 
167  /*
168  * Domain tests
169  *
170  */
171  template<class Val, class UnsVal>
172  forceinline bool
174  return x.range();
175  }
176  template<class Val, class UnsVal>
177  forceinline bool
179  bool exact;
180  int nda = exact_div(n, exact);
181  return exact && x.in(nda);
182  }
183 
184 
185 
186 
187  /*
188  * Domain update by value
189  *
190  */
191  template<class Val, class UnsVal>
194  return (n >= max()) ? ME_INT_NONE : x.lq(home,floor_div(n));
195  }
196 
197  template<class Val, class UnsVal>
200  return (n > max()) ? ME_INT_NONE : x.le(home,floor_div(n));
201  }
202 
203  template<class Val, class UnsVal>
206  return (n <= min()) ? ME_INT_NONE : x.gq(home,ceil_div(n));
207  }
208  template<class Val, class UnsVal>
211  return (n < min()) ? ME_INT_NONE : x.gr(home,ceil_div(n));
212  }
213 
214  template<class Val, class UnsVal>
217  bool exact;
218  int nda = exact_div(n,exact);
219  return exact ? x.nq(home,nda) : ME_INT_NONE;
220  }
221 
222  template<class Val, class UnsVal>
225  bool exact;
226  int nda = exact_div(n,exact);
227  return exact ? x.eq(home,nda) : ME_INT_FAILED;
228  }
229 
230 
231  /*
232  * Propagator modification events
233  *
234  */
235  template<class Val, class UnsVal>
238  return IntView::med(me);
239  }
240 
241 
242 
243  /*
244  * Delta information for advisors
245  *
246  */
247  template<class Val, class UnsVal>
248  forceinline Val
250  Val c = x.min(d); c *= a; return c;
251  }
252  template<class Val, class UnsVal>
253  forceinline Val
255  Val c = x.max(d); c *= a; return c;
256  }
257  template<class Val, class UnsVal>
258  forceinline bool
260  return x.any(d);
261  }
262 
263 
264 
265  /*
266  * Cloning
267  *
268  */
269  template<class Val, class UnsVal>
270  forceinline void
273  DerivedView<IntView>::update(home,share,y);
274  a=y.a;
275  }
276 
277 
278 
283  template<>
285  : public Iter::Ranges::ScaleUp<int,unsigned int,ViewRanges<IntView> > {
286  public:
288 
289 
290  ViewRanges(void);
292  ViewRanges(const IntScaleView& x);
294  void init(const IntScaleView& x);
296  };
297 
302  ViewRanges<IntView> xi(x.base());
304  (xi,x.scale());
305  }
306  forceinline void
308  ViewRanges<IntView> xi(x.base());
310  (xi,x.scale());
311  }
312 
313 
318  template<>
320  : public Iter::Ranges::ScaleUp<double,double,ViewRanges<IntView> > {
321  public:
323 
324 
325  ViewRanges(void);
327  ViewRanges(const DoubleScaleView& x);
329  void init(const DoubleScaleView& x);
331  };
332 
337  ViewRanges<IntView> xi(x.base());
339  (xi,x.scale());
340  }
341  forceinline void
343  ViewRanges<IntView> xi(x.base());
345  (xi,x.scale());
346  }
347 
348 
349  /*
350  * View comparison
351  *
352  */
353  template<class Val, class UnsVal>
354  forceinline bool
356  return same(x.base(),y.base()) && (x.scale() == y.scale());
357  }
358  template<class Val, class UnsVal>
359  forceinline bool
361  return before(x.base(),y.base())
362  || (same(x.base(),y.base()) && (x.scale() < y.scale()));
363  }
364 
365 }}
366 
367 // STATISTICS: int-var
368