ranges-cache.icc
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, 2004 00008 * 00009 * Last modified: 00010 * $Date: 2007-11-11 22:09:47 +0100 (Sun, 11 Nov 2007) $ by $Author: tack $ 00011 * $Revision: 5262 $ 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 00049 template <class I> 00050 class Cache { 00051 protected: 00053 class Range { 00054 public: 00055 int min; int max; 00056 }; 00058 SharedArray<Range> r; 00060 int c; 00061 public: 00063 00064 00065 Cache(void); 00067 Cache(I& i); 00069 void init(I& i); 00071 00073 00074 00075 bool operator()(void) const; 00077 void operator++(void); 00079 void reset(void); 00081 00083 00084 00085 int min(void) const; 00087 int max(void) const; 00089 unsigned int width(void) const; 00091 }; 00092 00093 00094 template <class I> 00095 forceinline 00096 Cache<I>::Cache(void) {} 00097 00098 template <class I> 00099 inline void 00100 Cache<I>::init(I& i) { 00101 Support::DynamicArray<Range> d; 00102 int n=0; 00103 while (i()) { 00104 d[n].min = i.min(); d[n].max = i.max(); 00105 ++n; ++i; 00106 } 00107 r.init(n); 00108 for (int j=n; j--; ) 00109 r[j]=d[j]; 00110 c = 0; 00111 } 00112 00113 template <class I> 00114 inline 00115 Cache<I>::Cache(I& i) { 00116 init(i); 00117 } 00118 00119 template <class I> 00120 forceinline void 00121 Cache<I>::operator++(void) { 00122 c++; 00123 } 00124 template <class I> 00125 forceinline bool 00126 Cache<I>::operator()(void) const { 00127 return c < r.size(); 00128 } 00129 00130 template <class I> 00131 forceinline void 00132 Cache<I>::reset(void) { 00133 c = 0; 00134 } 00135 00136 template <class I> 00137 forceinline int 00138 Cache<I>::min(void) const { 00139 return r[c].min; 00140 } 00141 template <class I> 00142 forceinline int 00143 Cache<I>::max(void) const { 00144 return r[c].max; 00145 } 00146 template <class I> 00147 forceinline unsigned int 00148 Cache<I>::width(void) const { 00149 return r[c].max-r[c].min+1; 00150 } 00151 00152 // VALUECACHE 00163 template <class I> 00164 class ValCache { 00165 protected: 00167 SharedArray<int> r; 00169 int c; 00171 int n; 00173 int s; 00174 public: 00176 00177 00178 ValCache(void); 00180 ValCache(I& i); 00182 void init(I& i); 00184 00186 00187 00188 bool operator()(void) const; 00190 void operator++(void); 00192 void operator--(void); 00194 void reset(void); 00196 void last(void); 00198 void finish(void); 00200 00202 00203 00204 int min(void) const; 00206 int max(void) const; 00208 int val(void) const; 00210 unsigned int width(void) const; 00212 unsigned int size(void) const; 00214 00216 00217 00218 void index(unsigned int i); 00220 unsigned int index(void); 00222 00223 }; 00224 00225 template <class I> 00226 forceinline 00227 ValCache<I>::ValCache(void) {} 00228 00229 template <class I> 00230 inline void 00231 ValCache<I>::init(I& i) { 00232 Support::DynamicArray<int> d; 00233 int j = 0; 00234 s = 0; 00235 while (i()) { 00236 d[j] = i.val(); 00237 ++j; ++i; 00238 s++; 00239 } 00240 c = 0; 00241 n = j; 00242 00243 r.init(n); 00244 for (int j = n; j--; ) 00245 r[j] = d[j]; 00246 c = 0; 00247 } 00248 00249 template <class I> 00250 inline 00251 ValCache<I>::ValCache(I& i) { 00252 init(i); 00253 } 00254 00255 template <class I> 00256 forceinline void 00257 ValCache<I>::operator++(void) { 00258 c++; 00259 } 00260 00261 template <class I> 00262 forceinline void 00263 ValCache<I>::operator--(void) { 00264 c--; 00265 } 00266 00267 template <class I> 00268 forceinline bool 00269 ValCache<I>::operator()(void) const { 00270 return -1 < c && c < n; 00271 } 00272 00273 template <class I> 00274 forceinline void 00275 ValCache<I>::reset(void) { 00276 c = 0; 00277 } 00278 00279 template <class I> 00280 forceinline void 00281 ValCache<I>::last(void) { 00282 c = n - 1; 00283 } 00284 00285 template <class I> 00286 forceinline void 00287 ValCache<I>::finish(void) { 00288 c = -1; 00289 } 00290 00291 template <class I> 00292 forceinline int 00293 ValCache<I>::min(void) const { 00294 return r[c]; 00295 } 00296 00297 template <class I> 00298 forceinline int 00299 ValCache<I>::max(void) const { 00300 return r[c]; 00301 } 00302 00303 template <class I> 00304 forceinline int 00305 ValCache<I>::val(void) const { 00306 return r[c]; 00307 } 00308 00309 template <class I> 00310 forceinline unsigned int 00311 ValCache<I>::width(void) const { 00312 return 1; 00313 } 00314 00315 template <class I> 00316 forceinline unsigned int 00317 ValCache<I>::size(void) const { 00318 return s; 00319 } 00320 00321 template <class I> 00322 forceinline void 00323 ValCache<I>::index(unsigned int i) { 00324 // maybe add an exception here 00325 c = i; 00326 } 00327 00328 template <class I> 00329 forceinline unsigned int 00330 ValCache<I>::index(void) { 00331 return c; 00332 } 00333 00334 }}} 00335 00336 // STATISTICS: iter-any 00337