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: 2009-09-08 21:10:29 +0200 (Tue, 08 Sep 2009) $ by $Author: schulte $ 00011 * $Revision: 9692 $ 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 private: 00052 IsRangeIter<I> _checkI; 00053 public: 00055 00056 00057 Map(void); 00059 Map(I& i); 00061 Map(I& i, const M& m); 00063 void init(I& i); 00065 void init(I& i, const M& m); 00067 00069 00070 00071 bool operator ()(void) const; 00073 void operator ++(void); 00075 00077 00078 00079 int min(void) const; 00081 int max(void) const; 00083 unsigned int width(void) const; 00085 }; 00086 00088 template<class I, class M> 00089 class Map<I,M,false> : public MinMax { 00090 private: 00092 IsRangeIter<I> _checkI; 00093 protected: 00095 I i; 00097 M m; 00099 void next(void); 00100 public: 00102 00103 00104 Map(void); 00106 Map(I& i); 00108 Map(I& i, const M& m); 00110 void init(I& i); 00112 void init(I& i, const M& m); 00114 00116 00117 00118 void operator ++(void); 00120 }; 00121 00123 template<class I, class M> 00124 class Map<I,M,true> { 00125 private: 00127 IsRangeIter<I> _checkI; 00128 protected: 00130 I i; 00132 M m; 00133 public: 00135 00136 00137 Map(void); 00139 Map(I& i); 00141 Map(I& i, const M& m); 00143 void init(I& i); 00145 void init(I& i, const M& m); 00147 00149 00150 00151 bool operator ()(void) const; 00153 void operator ++(void); 00155 00157 00158 00159 int min(void) const; 00161 int max(void) const; 00163 unsigned int width(void) const; 00165 }; 00166 00167 00168 template<class I, class M> 00169 forceinline 00170 Map<I,M,false>::Map(void) {} 00171 00172 template<class I, class M> 00173 forceinline void 00174 Map<I,M,false>::next(void) { 00175 if (i()) { 00176 mi = m.min(i.min()); 00177 ma = m.max(i.max()); 00178 ++i; 00179 while (i() && (ma+1 >= m.min(i.min()))) { 00180 ma = m.max(i.max()); ++i; 00181 } 00182 } else { 00183 finish(); 00184 } 00185 } 00186 00187 template<class I, class M> 00188 forceinline void 00189 Map<I,M,false>::init(I& i0) { 00190 i=i0; next(); 00191 } 00192 template<class I, class M> 00193 forceinline void 00194 Map<I,M,false>::init(I& i0, const M& m0) { 00195 i=i0; m=m0; next(); 00196 } 00197 00198 template<class I, class M> 00199 forceinline 00200 Map<I,M,false>::Map(I& i0) : i(i0) { 00201 next(); 00202 } 00203 template<class I, class M> 00204 forceinline 00205 Map<I,M,false>::Map(I& i0, const M& m0) : i(i0), m(m0) { 00206 next(); 00207 } 00208 00209 template<class I, class M> 00210 forceinline void 00211 Map<I,M,false>::operator ++(void) { 00212 next(); 00213 } 00214 00215 00216 00217 template<class I, class M> 00218 forceinline 00219 Map<I,M,true>::Map(void) {} 00220 00221 template<class I, class M> 00222 forceinline void 00223 Map<I,M,true>::init(I& i0) { 00224 i=i0; 00225 } 00226 template<class I, class M> 00227 forceinline void 00228 Map<I,M,true>::init(I& i0, const M& m0) { 00229 i=i0; m=m0; 00230 } 00231 00232 template<class I, class M> 00233 forceinline 00234 Map<I,M,true>::Map(I& i0) : i(i0) {} 00235 template<class I, class M> 00236 forceinline 00237 Map<I,M,true>::Map(I& i0, const M& m0) : i(i0), m(m0) {} 00238 00239 template<class I, class M> 00240 forceinline bool 00241 Map<I,M,true>::operator ()(void) const { 00242 return i(); 00243 } 00244 template<class I, class M> 00245 forceinline void 00246 Map<I,M,true>::operator ++(void) { 00247 ++i; 00248 } 00249 00250 template<class I, class M> 00251 forceinline int 00252 Map<I,M,true>::min(void) const { 00253 return m.min(i.min()); 00254 } 00255 template<class I, class M> 00256 forceinline int 00257 Map<I,M,true>::max(void) const { 00258 return m.max(i.max()); 00259 } 00260 template<class I, class M> 00261 forceinline unsigned int 00262 Map<I,M,true>::width(void) const { 00263 return static_cast<unsigned int>(max()-min())+1; 00264 } 00265 00266 }}} 00267 00268 // STATISTICS: iter-any 00269