Generated on Sat Feb 7 2015 02:01:26 for Gecode by doxygen 1.8.9.1
ranges-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, 2005
8  *
9  * Last modified:
10  * $Date: 2012-02-17 02:33:03 +0100 (Fri, 17 Feb 2012) $ by $Author: tack $
11  * $Revision: 12528 $
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 <cmath>
39 
40 namespace Gecode { namespace Iter { namespace Ranges {
41 
52  template<class Val, class UnsVal, class I>
53  class ScaleUp {
54  protected:
56  I i;
58  int a;
60  Val cur;
62  Val end;
63  public:
65 
66  ScaleUp(void);
69  ScaleUp(I& i, int a);
71  void init(I& i, int a);
73 
75 
76  bool operator ()(void) const;
79  void operator ++(void);
81 
83 
84  Val min(void) const;
87  Val max(void) const;
89  UnsVal width(void) const;
91  };
92 
98  template<class I>
99  class ScaleDown : public MinMax {
100  protected:
102  I i;
104  int a;
105  public:
107 
108  ScaleDown(void);
111  ScaleDown(I& i, int a);
113  void init(I& i, int a);
115 
117 
118  void operator ++(void);
121  };
122 
123 
124 
125  template<class Val, class UnsVal, class I>
128 
129  template<class Val, class UnsVal, class I>
130  inline void
132  i = i0; a = a0;
133  if (i()) {
134  cur = a * i.min();
135  end = a * i.max();
136  } else {
137  cur = 1;
138  end = 0;
139  }
140  }
141 
142  template<class Val, class UnsVal, class I>
143  inline
144  ScaleUp<Val,UnsVal,I>::ScaleUp(I& i0, int a0) : i(i0), a(a0) {
145  if (i()) {
146  cur = a * i.min();
147  end = a * i.max();
148  } else {
149  cur = 1;
150  end = 0;
151  }
152  }
153 
154  template<class Val, class UnsVal, class I>
155  forceinline void
157  if (a == 1) {
158  ++i;
159  } else {
160  cur += a;
161  if (cur > end) {
162  ++i;
163  if (i()) {
164  cur = a * i.min();
165  end = a * i.max();
166  }
167  }
168  }
169  }
170  template<class Val, class UnsVal, class I>
171  forceinline bool
173  return (a == 1) ? i() : (cur <= end);
174  }
175 
176  template<class Val, class UnsVal, class I>
177  forceinline Val
179  return (a == 1) ? static_cast<Val>(i.min()) : cur;
180  }
181  template<class Val, class UnsVal, class I>
182  forceinline Val
184  return (a == 1) ? static_cast<Val>(i.max()) : cur;
185  }
186  template<class Val, class UnsVal, class I>
187  forceinline UnsVal
189  return (a == 1) ?
190  static_cast<UnsVal>(i.width()) :
191  static_cast<UnsVal>(1);
192  }
193 
194 
195 
196  template<class I>
197  forceinline void
199  finish();
200  while ((mi > ma) && i()) {
201  mi = static_cast<int>(ceil(static_cast<double>(i.min())/a));
202  ma = static_cast<int>(floor(static_cast<double>(i.max())/a));
203  ++i;
204  }
205  while (i()) {
206  int n_mi = static_cast<int>(ceil(static_cast<double>(i.min())/a));
207  if (n_mi-ma > 1)
208  break;
209  int n_ma = static_cast<int>(floor(static_cast<double>(i.max())/a));
210  if (n_mi <= n_ma) {
211  ma = n_ma;
212  }
213  ++i;
214  }
215  }
216 
217  template<class I>
220 
221  template<class I>
222  inline void
223  ScaleDown<I>::init(I& i0, int a0) {
224  i = i0; a = a0;
225  operator ++();
226  }
227 
228  template<class I>
229  inline
230  ScaleDown<I>::ScaleDown(I& i0, int a0) : i(i0), a(a0) {
231  i = i0; a = a0;
232  operator ++();
233  }
234 
235 }}}
236 
237 // STATISTICS: iter-any
238 
UnsVal width(void) const
Return width of range (distance between minimum and maximum)
Val max(void) const
Return largest value of range.
Base for range iterators with explicit min and max.
Val cur
Current value of range.
Val end
Last value of scaled range of i.
Gecode::IntArgs i(4, 1, 2, 3, 4)
Range iterator for pointwise product with a positive integer.
Range iterator for pointwise division by a positive integer.
ScaleUp(void)
Default constructor.
void init(I &i, int a)
Initialize with ranges from i and scale factor a.
void init(I &i, int a)
Initialize with ranges from i and scale factor a.
bool operator()(void) const
Test whether iterator is still at a range or done.
#define forceinline
Definition: config.hpp:132
I i
Iterator to be scaled.
int a
Divide by this factor.
Gecode toplevel namespace
ScaleDown(void)
Default constructor.
Val min(void) const
Return smallest value of range.
I i
Iterator to be scaled down.
void operator++(void)
Move iterator to next range (if possible)
struct Gecode::@518::NNF::@57::@59 a
For atomic nodes.
void operator++(void)
Move iterator to next range (if possible)