Generated on Tue Jul 27 2010 21:59:16 for Gecode by doxygen 1.7.1

ranges-cache.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, 2004
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 
00049   template<class I>
00050   class Cache {
00051   private:
00053     IsRangeIter<I> _checkI;
00054   protected:
00056     class Range {
00057     public:
00058       int min; int max;
00059     };
00061     SharedArray<Range> r;
00063     int c;
00064   public:
00066 
00067 
00068     Cache(void);
00070     Cache(I& i);
00072     void init(I& i);
00074 
00076 
00077 
00078     bool operator ()(void) const;
00080     void operator ++(void);
00082     void reset(void);
00084 
00086 
00087 
00088     int min(void) const;
00090     int max(void) const;
00092     unsigned int width(void) const;
00094   };
00095 
00096 
00097   template<class I>
00098   forceinline
00099   Cache<I>::Cache(void) {}
00100 
00101   template<class I>
00102   inline void
00103   Cache<I>::init(I& i) {
00104     Support::DynamicArray<Range,Heap> d(heap);
00105     int n=0;
00106     while (i()) {
00107       d[n].min = i.min(); d[n].max = i.max();
00108       ++n; ++i;
00109     }
00110     r.init(n);
00111     for (int j=n; j--; )
00112       r[j]=d[j];
00113     c = 0;
00114   }
00115 
00116   template<class I>
00117   inline
00118   Cache<I>::Cache(I& i) {
00119     init(i);
00120   }
00121 
00122   template<class I>
00123   forceinline void
00124   Cache<I>::operator ++(void) {
00125     c++;
00126   }
00127   template<class I>
00128   forceinline bool
00129   Cache<I>::operator ()(void) const {
00130     return c < r.size();
00131   }
00132 
00133   template<class I>
00134   forceinline void
00135   Cache<I>::reset(void) {
00136     c = 0;
00137   }
00138 
00139   template<class I>
00140   forceinline int
00141   Cache<I>::min(void) const {
00142     return r[c].min;
00143   }
00144   template<class I>
00145   forceinline int
00146   Cache<I>::max(void) const {
00147     return r[c].max;
00148   }
00149   template<class I>
00150   forceinline unsigned int
00151   Cache<I>::width(void) const {
00152     return static_cast<unsigned int>(r[c].max-r[c].min+1);
00153   }
00154 
00155 }}}
00156 
00157 // STATISTICS: iter-any
00158