ranges-map.hpp
Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ 00002 /* 00003 * Main authors: 00004 * Christian Schulte <schulte@gecode.org> 00005 * 00006 * Copyright: 00007 * Christian Schulte, 2008 00008 * 00009 * Last modified: 00010 * $Date: 2010-07-28 17:35:33 +0200 (Wed, 28 Jul 2010) $ by $Author: schulte $ 00011 * $Revision: 11294 $ 00012 * 00013 * This file is part of Gecode, the generic constraint 00014 * development environment: 00015 * http://www.gecode.org 00016 * 00017 * Permission is hereby granted, free of charge, to any person obtaining 00018 * a copy of this software and associated documentation files (the 00019 * "Software"), to deal in the Software without restriction, including 00020 * without limitation the rights to use, copy, modify, merge, publish, 00021 * distribute, sublicense, and/or sell copies of the Software, and to 00022 * permit persons to whom the Software is furnished to do so, subject to 00023 * the following conditions: 00024 * 00025 * The above copyright notice and this permission notice shall be 00026 * included in all copies or substantial portions of the Software. 00027 * 00028 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 00029 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00030 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00031 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 00032 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 00033 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 00034 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00035 * 00036 */ 00037 00038 namespace Gecode { namespace Iter { namespace Ranges { 00039 00048 template<class I, class M, bool strict=true> 00049 class Map { 00050 public: 00052 00053 00054 Map(void); 00056 Map(I& i); 00058 Map(I& i, const M& m); 00060 void init(I& i); 00062 void init(I& i, const M& m); 00064 00066 00067 00068 bool operator ()(void) const; 00070 void operator ++(void); 00072 00074 00075 00076 int min(void) const; 00078 int max(void) const; 00080 unsigned int width(void) const; 00082 }; 00083 00085 template<class I, class M> 00086 class Map<I,M,false> : public MinMax { 00087 protected: 00089 I i; 00091 M m; 00093 void next(void); 00094 public: 00096 00097 00098 Map(void); 00100 Map(I& i); 00102 Map(I& i, const M& m); 00104 void init(I& i); 00106 void init(I& i, const M& m); 00108 00110 00111 00112 void operator ++(void); 00114 }; 00115 00117 template<class I, class M> 00118 class Map<I,M,true> { 00119 protected: 00121 I i; 00123 M m; 00124 public: 00126 00127 00128 Map(void); 00130 Map(I& i); 00132 Map(I& i, const M& m); 00134 void init(I& i); 00136 void init(I& i, const M& m); 00138 00140 00141 00142 bool operator ()(void) const; 00144 void operator ++(void); 00146 00148 00149 00150 int min(void) const; 00152 int max(void) const; 00154 unsigned int width(void) const; 00156 }; 00157 00158 00159 template<class I, class M> 00160 forceinline 00161 Map<I,M,false>::Map(void) {} 00162 00163 template<class I, class M> 00164 forceinline void 00165 Map<I,M,false>::next(void) { 00166 if (i()) { 00167 mi = m.min(i.min()); 00168 ma = m.max(i.max()); 00169 ++i; 00170 while (i() && (ma+1 >= m.min(i.min()))) { 00171 ma = m.max(i.max()); ++i; 00172 } 00173 } else { 00174 finish(); 00175 } 00176 } 00177 00178 template<class I, class M> 00179 forceinline void 00180 Map<I,M,false>::init(I& i0) { 00181 i=i0; next(); 00182 } 00183 template<class I, class M> 00184 forceinline void 00185 Map<I,M,false>::init(I& i0, const M& m0) { 00186 i=i0; m=m0; next(); 00187 } 00188 00189 template<class I, class M> 00190 forceinline 00191 Map<I,M,false>::Map(I& i0) : i(i0) { 00192 next(); 00193 } 00194 template<class I, class M> 00195 forceinline 00196 Map<I,M,false>::Map(I& i0, const M& m0) : i(i0), m(m0) { 00197 next(); 00198 } 00199 00200 template<class I, class M> 00201 forceinline void 00202 Map<I,M,false>::operator ++(void) { 00203 next(); 00204 } 00205 00206 00207 00208 template<class I, class M> 00209 forceinline 00210 Map<I,M,true>::Map(void) {} 00211 00212 template<class I, class M> 00213 forceinline void 00214 Map<I,M,true>::init(I& i0) { 00215 i=i0; 00216 } 00217 template<class I, class M> 00218 forceinline void 00219 Map<I,M,true>::init(I& i0, const M& m0) { 00220 i=i0; m=m0; 00221 } 00222 00223 template<class I, class M> 00224 forceinline 00225 Map<I,M,true>::Map(I& i0) : i(i0) {} 00226 template<class I, class M> 00227 forceinline 00228 Map<I,M,true>::Map(I& i0, const M& m0) : i(i0), m(m0) {} 00229 00230 template<class I, class M> 00231 forceinline bool 00232 Map<I,M,true>::operator ()(void) const { 00233 return i(); 00234 } 00235 template<class I, class M> 00236 forceinline void 00237 Map<I,M,true>::operator ++(void) { 00238 ++i; 00239 } 00240 00241 template<class I, class M> 00242 forceinline int 00243 Map<I,M,true>::min(void) const { 00244 return m.min(i.min()); 00245 } 00246 template<class I, class M> 00247 forceinline int 00248 Map<I,M,true>::max(void) const { 00249 return m.max(i.max()); 00250 } 00251 template<class I, class M> 00252 forceinline unsigned int 00253 Map<I,M,true>::width(void) const { 00254 return static_cast<unsigned int>(max()-min())+1; 00255 } 00256 00257 }}} 00258 00259 // STATISTICS: iter-any 00260