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 * Last modified: 00010 * $Date: 2010-07-28 17:58:23 +0200 (Wed, 28 Jul 2010) $ by $Author: tack $ 00011 * $Revision: 11296 $ 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 00045 class RangeListIter { 00046 protected: 00048 class RangeList : public Support::BlockClient<RangeList,Region> { 00049 public: 00051 int min, max; 00053 RangeList* next; 00054 }; 00056 class RLIO : public Support::BlockAllocator<RangeList,Region> { 00057 public: 00059 unsigned int use_cnt; 00061 RLIO(Region& r); 00062 }; 00064 RLIO* rlio; 00066 RangeList* h; 00068 RangeList* c; 00070 void set(RangeList* l); 00071 public: 00073 00074 00075 RangeListIter(void); 00077 RangeListIter(const RangeListIter& i); 00079 RangeListIter(Region& r); 00081 void init(Region& r); 00083 RangeListIter& operator =(const RangeListIter& i); 00085 00087 00088 00089 bool operator ()(void) const; 00091 void operator ++(void); 00093 void reset(void); 00095 00097 00098 00099 int min(void) const; 00101 int max(void) const; 00103 unsigned int width(void) const; 00105 00107 ~RangeListIter(void); 00108 }; 00109 00110 00111 forceinline 00112 RangeListIter::RLIO::RLIO(Region& r) 00113 : Support::BlockAllocator<RangeList,Region>(r), use_cnt(1) {} 00114 00115 00116 forceinline 00117 RangeListIter::RangeListIter(void) 00118 : rlio(NULL) {} 00119 00120 forceinline 00121 RangeListIter::RangeListIter(Region& r) 00122 : rlio(new (r.ralloc(sizeof(RLIO))) RLIO(r)), 00123 h(NULL), c(NULL) {} 00124 00125 forceinline void 00126 RangeListIter::init(Region& r) { 00127 rlio = new (r.ralloc(sizeof(RLIO))) RLIO(r); 00128 h = c = NULL; 00129 } 00130 00131 forceinline 00132 RangeListIter::RangeListIter(const RangeListIter& i) 00133 : rlio(i.rlio), h(i.h), c(i.c) { 00134 rlio->use_cnt++; 00135 } 00136 00137 forceinline RangeListIter& 00138 RangeListIter::operator =(const RangeListIter& i) { 00139 if (&i != this) { 00140 if (--rlio->use_cnt == 0) { 00141 Region& r = rlio->allocator(); 00142 rlio->~RLIO(); 00143 r.rfree(rlio,sizeof(RLIO)); 00144 } 00145 rlio = i.rlio; 00146 rlio->use_cnt++; 00147 c=i.c; h=i.h; 00148 } 00149 return *this; 00150 } 00151 00152 forceinline 00153 RangeListIter::~RangeListIter(void) { 00154 if (--rlio->use_cnt == 0) { 00155 Region& r = rlio->allocator(); 00156 rlio->~RLIO(); 00157 r.rfree(rlio,sizeof(RLIO)); 00158 } 00159 } 00160 00161 00162 forceinline void 00163 RangeListIter::set(RangeList* l) { 00164 h = c = l; 00165 } 00166 00167 forceinline bool 00168 RangeListIter::operator ()(void) const { 00169 return c != NULL; 00170 } 00171 00172 forceinline void 00173 RangeListIter::operator ++(void) { 00174 c = c->next; 00175 } 00176 00177 forceinline void 00178 RangeListIter::reset(void) { 00179 c = h; 00180 } 00181 00182 forceinline int 00183 RangeListIter::min(void) const { 00184 return c->min; 00185 } 00186 forceinline int 00187 RangeListIter::max(void) const { 00188 return c->max; 00189 } 00190 forceinline unsigned int 00191 RangeListIter::width(void) const { 00192 return static_cast<unsigned int>(c->max-c->min)+1; 00193 } 00194 00195 }}} 00196 00197 // STATISTICS: iter-any 00198