ranges-list.hpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
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);
00072 RangeList* get(void) const;
00074 RangeList* range(int min, int max, RangeList*& f);
00076 RangeList* range(int min, int max);
00078 template<class I>
00079 RangeList* range(I& i, RangeList*& f);
00081 template<class I>
00082 RangeList* range(I& i);
00084 template<class I>
00085 RangeList* copy(I& i);
00086 public:
00088
00089
00090 RangeListIter(void);
00092 RangeListIter(const RangeListIter& i);
00094 RangeListIter(Region& r);
00096 void init(Region& r);
00098 RangeListIter& operator =(const RangeListIter& i);
00100
00102
00103
00104 bool operator ()(void) const;
00106 void operator ++(void);
00108 void reset(void);
00110
00112
00113
00114 int min(void) const;
00116 int max(void) const;
00118 unsigned int width(void) const;
00120
00122 ~RangeListIter(void);
00123 };
00124
00125
00126 forceinline
00127 RangeListIter::RLIO::RLIO(Region& r)
00128 : Support::BlockAllocator<RangeList,Region>(r), use_cnt(1) {}
00129
00130
00131 forceinline
00132 RangeListIter::RangeListIter(void)
00133 : rlio(NULL), h(NULL), c(NULL) {}
00134
00135 forceinline
00136 RangeListIter::RangeListIter(Region& r)
00137 : rlio(new (r.ralloc(sizeof(RLIO))) RLIO(r)), h(NULL), c(NULL) {}
00138
00139 forceinline void
00140 RangeListIter::init(Region& r) {
00141 rlio = new (r.ralloc(sizeof(RLIO))) RLIO(r);
00142 h = c = NULL;
00143 }
00144
00145 forceinline
00146 RangeListIter::RangeListIter(const RangeListIter& i)
00147 : rlio(i.rlio), h(i.h), c(i.c) {
00148 if (rlio != NULL)
00149 rlio->use_cnt++;
00150 }
00151
00152 forceinline RangeListIter&
00153 RangeListIter::operator =(const RangeListIter& i) {
00154 if (&i != this) {
00155 if ((rlio != NULL) && (--rlio->use_cnt == 0)) {
00156 Region& r = rlio->allocator();
00157 rlio->~RLIO();
00158 r.rfree(rlio,sizeof(RLIO));
00159 }
00160 rlio = i.rlio;
00161 if (rlio != NULL)
00162 rlio->use_cnt++;
00163 c=i.c; h=i.h;
00164 }
00165 return *this;
00166 }
00167
00168 forceinline
00169 RangeListIter::~RangeListIter(void) {
00170 if ((rlio != NULL) && (--rlio->use_cnt == 0)) {
00171 Region& r = rlio->allocator();
00172 rlio->~RLIO();
00173 r.rfree(rlio,sizeof(RLIO));
00174 }
00175 }
00176
00177
00178 forceinline void
00179 RangeListIter::set(RangeList* l) {
00180 h = c = l;
00181 }
00182
00183 forceinline RangeListIter::RangeList*
00184 RangeListIter::get(void) const {
00185 return h;
00186 }
00187
00188 forceinline RangeListIter::RangeList*
00189 RangeListIter::range(int min, int max, RangeList*& f) {
00190 RangeList* t;
00191
00192 if (f != NULL) {
00193 t = f; f = f->next;
00194 } else {
00195 t = new (*rlio) RangeList;
00196 }
00197 t->min = min; t->max = max;
00198 return t;
00199 }
00200
00201 forceinline RangeListIter::RangeList*
00202 RangeListIter::range(int min, int max) {
00203 RangeList* t = new (*rlio) RangeList;
00204 t->min = min; t->max = max;
00205 return t;
00206 }
00207
00208 template<class I>
00209 forceinline RangeListIter::RangeList*
00210 RangeListIter::range(I& i, RangeList*& f) {
00211 return range(i.min(),i.max(),f);
00212 }
00213
00214 template<class I>
00215 forceinline RangeListIter::RangeList*
00216 RangeListIter::range(I& i) {
00217 return range(i.min(),i.max());
00218 }
00219
00220 template<class I>
00221 inline RangeListIter::RangeList*
00222 RangeListIter::copy(I& i) {
00223 RangeList* h;
00224 RangeList** c = &h;
00225 for ( ; i(); ++i) {
00226 RangeList* t = range(i);
00227 *c = t; c = &t->next;
00228 }
00229 *c = NULL;
00230 return h;
00231 }
00232
00233 forceinline bool
00234 RangeListIter::operator ()(void) const {
00235 return c != NULL;
00236 }
00237
00238 forceinline void
00239 RangeListIter::operator ++(void) {
00240 c = c->next;
00241 }
00242
00243 forceinline void
00244 RangeListIter::reset(void) {
00245 c = h;
00246 }
00247
00248 forceinline int
00249 RangeListIter::min(void) const {
00250 return c->min;
00251 }
00252 forceinline int
00253 RangeListIter::max(void) const {
00254 return c->max;
00255 }
00256 forceinline unsigned int
00257 RangeListIter::width(void) const {
00258 return static_cast<unsigned int>(c->max-c->min)+1;
00259 }
00260
00261 }}}
00262
00263
00264