Generated on Tue May 22 09:40:06 2018 for Gecode by doxygen 1.6.3

ranges-list.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, 2010
00008  *
00009  *  This file is part of Gecode, the generic constraint
00010  *  development environment:
00011  *     http://www.gecode.org
00012  *
00013  *  Permission is hereby granted, free of charge, to any person obtaining
00014  *  a copy of this software and associated documentation files (the
00015  *  "Software"), to deal in the Software without restriction, including
00016  *  without limitation the rights to use, copy, modify, merge, publish,
00017  *  distribute, sublicense, and/or sell copies of the Software, and to
00018  *  permit persons to whom the Software is furnished to do so, subject to
00019  *  the following conditions:
00020  *
00021  *  The above copyright notice and this permission notice shall be
00022  *  included in all copies or substantial portions of the Software.
00023  *
00024  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00025  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00026  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00027  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00028  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00029  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00030  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00031  *
00032  */
00033 
00034 namespace Gecode { namespace Iter { namespace Ranges {
00035 
00041   class RangeListIter {
00042   protected:
00044     class RangeList : public Support::BlockClient<RangeList,Region> {
00045     public:
00047       int min, max;
00049       RangeList* next;
00050     };
00052     class RLIO : public Support::BlockAllocator<RangeList,Region> {
00053     public:
00055       unsigned int use_cnt;
00057       RLIO(Region& r);
00058     };
00060     RLIO* rlio;
00062     RangeList* h;
00064     RangeList* c;
00066     void set(RangeList* l);
00068     RangeList* get(void) const;
00070     RangeList* range(int min, int max, RangeList*& f);
00072     RangeList* range(int min, int max);
00074     template<class I>
00075     RangeList* range(I& i, RangeList*& f);
00077     template<class I>
00078     RangeList* range(I& i);
00080     template<class I>
00081     RangeList* copy(I& i);
00082   public:
00084 
00085 
00086     RangeListIter(void);
00088     RangeListIter(const RangeListIter& i);
00090     RangeListIter(Region& r);
00092     void init(Region& r);
00094     RangeListIter& operator =(const RangeListIter& i);
00096 
00098 
00099 
00100     bool operator ()(void) const;
00102     void operator ++(void);
00104     void reset(void);
00106 
00108 
00109 
00110     int min(void) const;
00112     int max(void) const;
00114     unsigned int width(void) const;
00116 
00118     ~RangeListIter(void);
00119   };
00120 
00121 
00122   forceinline
00123   RangeListIter::RLIO::RLIO(Region& r)
00124     : Support::BlockAllocator<RangeList,Region>(r), use_cnt(1) {}
00125 
00126 
00127   forceinline
00128   RangeListIter::RangeListIter(void)
00129     : rlio(NULL), h(NULL), c(NULL) {}
00130 
00131   forceinline
00132   RangeListIter::RangeListIter(Region& r)
00133     : rlio(new (r.ralloc(sizeof(RLIO))) RLIO(r)), h(NULL), c(NULL) {}
00134 
00135   forceinline void
00136   RangeListIter::init(Region& r) {
00137     rlio = new (r.ralloc(sizeof(RLIO))) RLIO(r);
00138     h = c = NULL;
00139   }
00140 
00141   forceinline
00142   RangeListIter::RangeListIter(const RangeListIter& i)
00143     : rlio(i.rlio), h(i.h), c(i.c)  {
00144     if (rlio != NULL)
00145       rlio->use_cnt++;
00146   }
00147 
00148   forceinline RangeListIter&
00149   RangeListIter::operator =(const RangeListIter& i) {
00150     if (&i != this) {
00151       if ((rlio != NULL) && (--rlio->use_cnt == 0)) {
00152         Region& r = rlio->allocator();
00153         rlio->~RLIO();
00154         r.rfree(rlio,sizeof(RLIO));
00155       }
00156       rlio = i.rlio;
00157       if (rlio != NULL)
00158         rlio->use_cnt++;
00159       c=i.c; h=i.h;
00160     }
00161     return *this;
00162   }
00163 
00164   forceinline
00165   RangeListIter::~RangeListIter(void) {
00166     if ((rlio != NULL) && (--rlio->use_cnt == 0)) {
00167       Region& r = rlio->allocator();
00168       rlio->~RLIO();
00169       r.rfree(rlio,sizeof(RLIO));
00170     }
00171   }
00172 
00173 
00174   forceinline void
00175   RangeListIter::set(RangeList* l) {
00176     h = c = l;
00177   }
00178 
00179   forceinline RangeListIter::RangeList*
00180   RangeListIter::get(void) const {
00181     return h;
00182   }
00183 
00184   forceinline RangeListIter::RangeList*
00185   RangeListIter::range(int min, int max, RangeList*& f) {
00186     RangeList* t;
00187     // Take element from freelist if possible
00188     if (f != NULL) {
00189       t = f; f = f->next;
00190     } else {
00191       t = new (*rlio) RangeList;
00192     }
00193     t->min = min; t->max = max;
00194     return t;
00195   }
00196 
00197   forceinline RangeListIter::RangeList*
00198   RangeListIter::range(int min, int max) {
00199     RangeList* t = new (*rlio) RangeList;
00200     t->min = min; t->max = max;
00201     return t;
00202   }
00203 
00204   template<class I>
00205   forceinline RangeListIter::RangeList*
00206   RangeListIter::range(I& i, RangeList*& f) {
00207     return range(i.min(),i.max(),f);
00208   }
00209 
00210   template<class I>
00211   forceinline RangeListIter::RangeList*
00212   RangeListIter::range(I& i) {
00213     return range(i.min(),i.max());
00214   }
00215 
00216   template<class I>
00217   inline RangeListIter::RangeList*
00218   RangeListIter::copy(I& i) {
00219     RangeList*  h;
00220     RangeList** c = &h;
00221     for ( ; i(); ++i) {
00222       RangeList* t = range(i);
00223       *c = t; c = &t->next;
00224     }
00225     *c = NULL;
00226     return h;
00227   }
00228 
00229   forceinline bool
00230   RangeListIter::operator ()(void) const {
00231     return c != NULL;
00232   }
00233 
00234   forceinline void
00235   RangeListIter::operator ++(void) {
00236     c = c->next;
00237   }
00238 
00239   forceinline void
00240   RangeListIter::reset(void) {
00241     c = h;
00242   }
00243 
00244   forceinline int
00245   RangeListIter::min(void) const {
00246     return c->min;
00247   }
00248   forceinline int
00249   RangeListIter::max(void) const {
00250     return c->max;
00251   }
00252   forceinline unsigned int
00253   RangeListIter::width(void) const {
00254     return static_cast<unsigned int>(c->max-c->min)+1;
00255   }
00256 
00257 }}}
00258 
00259 // STATISTICS: iter-any
00260