this repo has no description
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
36namespace Gecode { namespace Iter { namespace Ranges {
37
38 /**
39 * \brief Range iterator for pointwise product with a positive integer
40 *
41 * Note that this iterator has a different interface as it can be used
42 * for both integer precision as well as double precision (depending
43 * on the type \a Val (\c int or \c double) and
44 * on the type \a UnsVal (\c unsigned \c int or \c double).
45 *
46 * \ingroup FuncIterRanges
47 */
48 template<class Val, class UnsVal, class I>
49 class ScaleUp {
50 protected:
51 /// Iterator to be scaled
52 I i;
53 /// Scale-factor
54 int a;
55 /// Current value of range
56 Val cur;
57 /// Last value of scaled range of \a i
58 Val end;
59 public:
60 /// \name Constructors and initialization
61 //@{
62 /// Default constructor
63 ScaleUp(void);
64 /// Initialize with ranges from \a i and scale factor \a a
65 ScaleUp(I& i, int a);
66 /// Initialize with ranges from \a i and scale factor \a a
67 void init(I& i, int a);
68 //@}
69
70 /// \name Iteration control
71 //@{
72 /// Test whether iterator is still at a range or done
73 bool operator ()(void) const;
74 /// Move iterator to next range (if possible)
75 void operator ++(void);
76 //@}
77
78 /// \name Range access
79 //@{
80 /// Return smallest value of range
81 Val min(void) const;
82 /// Return largest value of range
83 Val max(void) const;
84 /// Return width of range (distance between minimum and maximum)
85 UnsVal width(void) const;
86 //@}
87 };
88
89 /**
90 * \brief Range iterator for pointwise division by a positive integer
91 *
92 * \ingroup FuncIterRanges
93 */
94 template<class I>
95 class ScaleDown : public MinMax {
96 protected:
97 /// Iterator to be scaled down
98 I i;
99 /// Divide by this factor
100 int a;
101 public:
102 /// \name Constructors and initialization
103 //@{
104 /// Default constructor
105 ScaleDown(void);
106 /// Initialize with ranges from \a i and scale factor \a a
107 ScaleDown(I& i, int a);
108 /// Initialize with ranges from \a i and scale factor \a a
109 void init(I& i, int a);
110 //@}
111
112 /// \name Iteration control
113 //@{
114 /// Move iterator to next range (if possible)
115 void operator ++(void);
116 //@}
117 };
118
119
120
121 template<class Val, class UnsVal, class I>
122 forceinline
123 ScaleUp<Val,UnsVal,I>::ScaleUp(void) {}
124
125 template<class Val, class UnsVal, class I>
126 inline void
127 ScaleUp<Val,UnsVal,I>::init(I& i0, int a0) {
128 i = i0; a = a0;
129 if (i()) {
130 cur = static_cast<Val>(a) * static_cast<Val>(i.min());
131 end = static_cast<Val>(a) * static_cast<Val>(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 = static_cast<Val>(a) * static_cast<Val>(i.min());
143 end = static_cast<Val>(a) * static_cast<Val>(i.max());
144 } else {
145 cur = 1;
146 end = 0;
147 }
148 }
149
150 template<class Val, class UnsVal, class I>
151 forceinline void
152 ScaleUp<Val,UnsVal,I>::operator ++(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
168 ScaleUp<Val,UnsVal,I>::operator ()(void) const {
169 return (a == 1) ? i() : (cur <= end);
170 }
171
172 template<class Val, class UnsVal, class I>
173 forceinline Val
174 ScaleUp<Val,UnsVal,I>::min(void) const {
175 return (a == 1) ? static_cast<Val>(i.min()) : cur;
176 }
177 template<class Val, class UnsVal, class I>
178 forceinline Val
179 ScaleUp<Val,UnsVal,I>::max(void) const {
180 return (a == 1) ? static_cast<Val>(i.max()) : cur;
181 }
182 template<class Val, class UnsVal, class I>
183 forceinline UnsVal
184 ScaleUp<Val,UnsVal,I>::width(void) const {
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
194 ScaleDown<I>::operator ++(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>
214 forceinline
215 ScaleDown<I>::ScaleDown(void) {}
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