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, 2008 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 34namespace Gecode { namespace Iter { namespace Ranges { 35 36 /** 37 * \brief Range iterator for mapping ranges 38 * 39 * If \a strict is true, then mapped ranges are not allowed to 40 * be adjacent or overlap. 41 * 42 * \ingroup FuncIterRanges 43 */ 44 template<class I, class M, bool strict=true> 45 class Map { 46 public: 47 /// \name Constructors and initialization 48 //@{ 49 /// Default constructor 50 Map(void); 51 /// Initialize with ranges from \a i 52 Map(I& i); 53 /// Initialize with ranges from \a i and map \a m 54 Map(I& i, const M& m); 55 /// Initialize with ranges from \a i 56 void init(I& i); 57 /// Initialize with ranges from \a i and map \a m 58 void init(I& i, const M& m); 59 //@} 60 61 /// \name Iteration control 62 //@{ 63 /// Test whether iterator is still at a range or done 64 bool operator ()(void) const; 65 /// Move iterator to next range (if possible) 66 void operator ++(void); 67 //@} 68 69 /// \name Range access 70 //@{ 71 /// Return smallest value of range 72 int min(void) const; 73 /// Return largest value of range 74 int max(void) const; 75 /// Return width of range (distance between minimum and maximum) 76 unsigned int width(void) const; 77 //@} 78 }; 79 80 /// Specialized mapping of ranges for non-strict maps 81 template<class I, class M> 82 class Map<I,M,false> : public MinMax { 83 protected: 84 /// Input range 85 I i; 86 /// Map for ranges 87 M m; 88 /// Find next mapped range 89 void next(void); 90 public: 91 /// \name Constructors and initialization 92 //@{ 93 /// Default constructor 94 Map(void); 95 /// Initialize with ranges from \a i 96 Map(I& i); 97 /// Initialize with ranges from \a i and map \a m 98 Map(I& i, const M& m); 99 /// Initialize with ranges from \a i 100 void init(I& i); 101 /// Initialize with ranges from \a i and map \a m 102 void init(I& i, const M& m); 103 //@} 104 105 /// \name Iteration control 106 //@{ 107 /// Move iterator to next range (if possible) 108 void operator ++(void); 109 //@} 110 }; 111 112 /// Specialized mapping of ranges for strict maps 113 template<class I, class M> 114 class Map<I,M,true> { 115 protected: 116 /// Input range iterator 117 I i; 118 /// Map object 119 M m; 120 public: 121 /// \name Constructors and initialization 122 //@{ 123 /// Default constructor 124 Map(void); 125 /// Initialize with ranges from \a i 126 Map(I& i); 127 /// Initialize with ranges from \a i and map \a m 128 Map(I& i, const M& m); 129 /// Initialize with ranges from \a i 130 void init(I& i); 131 /// Initialize with ranges from \a i and map \a m 132 void init(I& i, const M& m); 133 //@} 134 135 /// \name Iteration control 136 //@{ 137 /// Test whether iterator is still at a range or done 138 bool operator ()(void) const; 139 /// Move iterator to next range (if possible) 140 void operator ++(void); 141 //@} 142 143 /// \name Range access 144 //@{ 145 /// Return smallest value of range 146 int min(void) const; 147 /// Return largest value of range 148 int max(void) const; 149 /// Return width of range (distance between minimum and maximum) 150 unsigned int width(void) const; 151 //@} 152 }; 153 154 155 template<class I, class M> 156 forceinline 157 Map<I,M,false>::Map(void) {} 158 159 template<class I, class M> 160 forceinline void 161 Map<I,M,false>::next(void) { 162 if (i()) { 163 mi = m.min(i.min()); 164 ma = m.max(i.max()); 165 ++i; 166 while (i() && (ma+1 >= m.min(i.min()))) { 167 ma = m.max(i.max()); ++i; 168 } 169 } else { 170 finish(); 171 } 172 } 173 174 template<class I, class M> 175 forceinline void 176 Map<I,M,false>::init(I& i0) { 177 i=i0; next(); 178 } 179 template<class I, class M> 180 forceinline void 181 Map<I,M,false>::init(I& i0, const M& m0) { 182 i=i0; m=m0; next(); 183 } 184 185 template<class I, class M> 186 forceinline 187 Map<I,M,false>::Map(I& i0) : i(i0) { 188 next(); 189 } 190 template<class I, class M> 191 forceinline 192 Map<I,M,false>::Map(I& i0, const M& m0) : i(i0), m(m0) { 193 next(); 194 } 195 196 template<class I, class M> 197 forceinline void 198 Map<I,M,false>::operator ++(void) { 199 next(); 200 } 201 202 203 204 template<class I, class M> 205 forceinline 206 Map<I,M,true>::Map(void) {} 207 208 template<class I, class M> 209 forceinline void 210 Map<I,M,true>::init(I& i0) { 211 i=i0; 212 } 213 template<class I, class M> 214 forceinline void 215 Map<I,M,true>::init(I& i0, const M& m0) { 216 i=i0; m=m0; 217 } 218 219 template<class I, class M> 220 forceinline 221 Map<I,M,true>::Map(I& i0) : i(i0) {} 222 template<class I, class M> 223 forceinline 224 Map<I,M,true>::Map(I& i0, const M& m0) : i(i0), m(m0) {} 225 226 template<class I, class M> 227 forceinline bool 228 Map<I,M,true>::operator ()(void) const { 229 return i(); 230 } 231 template<class I, class M> 232 forceinline void 233 Map<I,M,true>::operator ++(void) { 234 ++i; 235 } 236 237 template<class I, class M> 238 forceinline int 239 Map<I,M,true>::min(void) const { 240 return m.min(i.min()); 241 } 242 template<class I, class M> 243 forceinline int 244 Map<I,M,true>::max(void) const { 245 return m.max(i.max()); 246 } 247 template<class I, class M> 248 forceinline unsigned int 249 Map<I,M,true>::width(void) const { 250 return static_cast<unsigned int>(max()-min())+1; 251 } 252 253}}} 254 255// STATISTICS: iter-any 256