/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ /* * Main authors: * Christian Schulte * * Copyright: * Christian Schulte, 2004 * * Last modified: * $Date: 2007-11-11 22:09:47 +0100 (Sun, 11 Nov 2007) $ by $Author: tack $ * $Revision: 5262 $ * * This file is part of Gecode, the generic constraint * development environment: * http://www.gecode.org * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * */ namespace Gecode { namespace Iter { namespace Ranges { /** * \brief %Range iterator cache * * Allows to iterate the ranges as defined by the input iterator * several times provided the Cache is %reset by the reset member * function. * * \ingroup FuncIterRanges */ template class Cache { protected: /// %Ranges stored in cache class Range { public: int min; int max; }; /// Array for ranges SharedArray r; /// Current range int c; public: /// \name Constructors and initialization //@{ /// Default constructor Cache(void); /// Initialize with ranges from \a i Cache(I& i); /// Initialize with ranges from \a i void init(I& i); //@} /// \name Iteration control //@{ /// Test whether iterator is still at a range or done bool operator()(void) const; /// Move iterator to next range (if possible) void operator++(void); /// Reset iterator to start from beginning void reset(void); //@} /// \name %Range access //@{ /// Return smallest value of range int min(void) const; /// Return largest value of range int max(void) const; /// Return width of range (distance between minimum and maximum) unsigned int width(void) const; //@} }; template forceinline Cache::Cache(void) {} template inline void Cache::init(I& i) { Support::DynamicArray d; int n=0; while (i()) { d[n].min = i.min(); d[n].max = i.max(); ++n; ++i; } r.init(n); for (int j=n; j--; ) r[j]=d[j]; c = 0; } template inline Cache::Cache(I& i) { init(i); } template forceinline void Cache::operator++(void) { c++; } template forceinline bool Cache::operator()(void) const { return c < r.size(); } template forceinline void Cache::reset(void) { c = 0; } template forceinline int Cache::min(void) const { return r[c].min; } template forceinline int Cache::max(void) const { return r[c].max; } template forceinline unsigned int Cache::width(void) const { return r[c].max-r[c].min+1; } // VALUECACHE /** * \brief %Range iterator cache * * Allows to iterate the ranges as defined by the input iterator * several times provided the ValCache is %reset by the reset member * function. * * \ingroup FuncIterRanges */ template class ValCache { protected: /// Array for ranges SharedArray r; /// Current range int c; /// Number of ranges in cache int n; /// Number of elements in cache int s; public: /// \name Constructors and initialization //@{ /// Default constructor ValCache(void); /// Initialize with ranges from \a i ValCache(I& i); /// Initialize with ranges from \a i void init(I& i); //@} /// \name Iteration control //@{ /// Test whether iterator is still at a range or done bool operator()(void) const; /// Move iterator to next range (if possible) void operator++(void); /// Move iterator to previous range (if possible) void operator--(void); /// Reset iterator to start from beginning void reset(void); /// Start iteration from end void last(void); /// Stop iteration void finish(void); //@} /// \name %Range access //@{ /// Always returns val int min(void) const; /// Always returns val int max(void) const; /// Return value int val(void) const; /// Return width of range (distance between minimum and maximum) unsigned int width(void) const; /// Return size of the union over all ranges unsigned int size(void) const; //@} /// \name %Index acces //@{ /// Start iteration from ith range in cache void index(unsigned int i); /// Get the index of the current range unsigned int index(void); //@} }; template forceinline ValCache::ValCache(void) {} template inline void ValCache::init(I& i) { Support::DynamicArray d; int j = 0; s = 0; while (i()) { d[j] = i.val(); ++j; ++i; s++; } c = 0; n = j; r.init(n); for (int j = n; j--; ) r[j] = d[j]; c = 0; } template inline ValCache::ValCache(I& i) { init(i); } template forceinline void ValCache::operator++(void) { c++; } template forceinline void ValCache::operator--(void) { c--; } template forceinline bool ValCache::operator()(void) const { return -1 < c && c < n; } template forceinline void ValCache::reset(void) { c = 0; } template forceinline void ValCache::last(void) { c = n - 1; } template forceinline void ValCache::finish(void) { c = -1; } template forceinline int ValCache::min(void) const { return r[c]; } template forceinline int ValCache::max(void) const { return r[c]; } template forceinline int ValCache::val(void) const { return r[c]; } template forceinline unsigned int ValCache::width(void) const { return 1; } template forceinline unsigned int ValCache::size(void) const { return s; } template forceinline void ValCache::index(unsigned int i) { // maybe add an exception here c = i; } template forceinline unsigned int ValCache::index(void) { return c; } }}} // STATISTICS: iter-any