Generated on Wed Jul 21 2021 00:00:00 for Gecode by doxygen 1.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  * This file is part of Gecode, the generic constraint
10  * development environment:
11  * http://www.gecode.org
12  *
13  * Permission is hereby granted, free of charge, to any person obtaining
14  * a copy of this software and associated documentation files (the
15  * "Software"), to deal in the Software without restriction, including
16  * without limitation the rights to use, copy, modify, merge, publish,
17  * distribute, sublicense, and/or sell copies of the Software, and to
18  * permit persons to whom the Software is furnished to do so, subject to
19  * the following conditions:
20  *
21  * The above copyright notice and this permission notice shall be
22  * included in all copies or substantial portions of the Software.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31  *
32  */
33 
34 #include <cmath>
35 
36 namespace Gecode { namespace Iter { namespace Ranges {
37 
48  template<class Val, class UnsVal, class I>
49  class ScaleUp {
50  protected:
52  I i;
54  int a;
56  Val cur;
58  Val end;
59  public:
61 
62  ScaleUp(void);
65  ScaleUp(I& i, int a);
67  void init(I& i, int a);
69 
71 
72  bool operator ()(void) const;
75  void operator ++(void);
77 
79 
80  Val min(void) const;
83  Val max(void) const;
85  UnsVal width(void) const;
87  };
88 
94  template<class I>
95  class ScaleDown : public MinMax {
96  protected:
98  I i;
100  int a;
101  public:
103 
104  ScaleDown(void);
107  ScaleDown(I& i, int a);
109  void init(I& i, int a);
111 
113 
114  void operator ++(void);
117  };
118 
119 
120 
121  template<class Val, class UnsVal, class I>
124 
125  template<class Val, class UnsVal, class I>
126  inline void
128  i = i0; a = a0;
129  if (i()) {
130  cur = a * i.min();
131  end = a * i.max();
132  } else {
133  cur = 1;
134  end = 0;
135  }
136  }
137 
138  template<class Val, class UnsVal, class I>
139  inline
140  ScaleUp<Val,UnsVal,I>::ScaleUp(I& i0, int a0) : i(i0), a(a0) {
141  if (i()) {
142  cur = a * i.min();
143  end = a * i.max();
144  } else {
145  cur = 1;
146  end = 0;
147  }
148  }
149 
150  template<class Val, class UnsVal, class I>
151  forceinline void
153  if (a == 1) {
154  ++i;
155  } else {
156  cur += a;
157  if (cur > end) {
158  ++i;
159  if (i()) {
160  cur = a * i.min();
161  end = a * i.max();
162  }
163  }
164  }
165  }
166  template<class Val, class UnsVal, class I>
167  forceinline bool
169  return (a == 1) ? i() : (cur <= end);
170  }
171 
172  template<class Val, class UnsVal, class I>
173  forceinline Val
175  return (a == 1) ? static_cast<Val>(i.min()) : cur;
176  }
177  template<class Val, class UnsVal, class I>
178  forceinline Val
180  return (a == 1) ? static_cast<Val>(i.max()) : cur;
181  }
182  template<class Val, class UnsVal, class I>
183  forceinline UnsVal
185  return (a == 1) ?
186  static_cast<UnsVal>(i.width()) :
187  static_cast<UnsVal>(1);
188  }
189 
190 
191 
192  template<class I>
193  forceinline void
195  finish();
196  while ((mi > ma) && i()) {
197  mi = static_cast<int>(ceil(static_cast<double>(i.min())/a));
198  ma = static_cast<int>(floor(static_cast<double>(i.max())/a));
199  ++i;
200  }
201  while (i()) {
202  int n_mi = static_cast<int>(ceil(static_cast<double>(i.min())/a));
203  if (n_mi-ma > 1)
204  break;
205  int n_ma = static_cast<int>(floor(static_cast<double>(i.max())/a));
206  if (n_mi <= n_ma) {
207  ma = n_ma;
208  }
209  ++i;
210  }
211  }
212 
213  template<class I>
216 
217  template<class I>
218  inline void
219  ScaleDown<I>::init(I& i0, int a0) {
220  i = i0; a = a0;
221  operator ++();
222  }
223 
224  template<class I>
225  inline
226  ScaleDown<I>::ScaleDown(I& i0, int a0) : i(i0), a(a0) {
227  i = i0; a = a0;
228  operator ++();
229  }
230 
231 }}}
232 
233 // STATISTICS: iter-any
234 
struct Gecode::@602::NNF::@65::@67 a
For atomic nodes.
Base for range iterators with explicit min and max.
Range iterator for pointwise division by a positive integer.
void operator++(void)
Move iterator to next range (if possible)
int a
Divide by this factor.
I i
Iterator to be scaled down.
void init(I &i, int a)
Initialize with ranges from i and scale factor a.
ScaleDown(void)
Default constructor.
Range iterator for pointwise product with a positive integer.
void operator++(void)
Move iterator to next range (if possible)
bool operator()(void) const
Test whether iterator is still at a range or done.
Val max(void) const
Return largest value of range.
ScaleUp(void)
Default constructor.
UnsVal width(void) const
Return width of range (distance between minimum and maximum)
Val end
Last value of scaled range of i.
ScaleUp(I &i, int a)
Initialize with ranges from i and scale factor a.
Val min(void) const
Return smallest value of range.
Val cur
Current value of range.
I i
Iterator to be scaled.
void init(I &i, int a)
Initialize with ranges from i and scale factor a.
Gecode::IntArgs i({1, 2, 3, 4})
#define forceinline
Definition: config.hpp:192