Generated on Fri Aug 24 2012 04:52:12 for Gecode by doxygen 1.8.1.2
ranges-list.hpp
Go to the documentation of this file.
1 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /*
3  * Main authors:
4  * Christian Schulte <schulte@gecode.org>
5  *
6  * Copyright:
7  * Christian Schulte, 2010
8  *
9  * Last modified:
10  * $Date: 2012-03-23 02:42:16 +1100 (Fri, 23 Mar 2012) $ by $Author: schulte $
11  * $Revision: 12623 $
12  *
13  * This file is part of Gecode, the generic constraint
14  * development environment:
15  * http://www.gecode.org
16  *
17  * Permission is hereby granted, free of charge, to any person obtaining
18  * a copy of this software and associated documentation files (the
19  * "Software"), to deal in the Software without restriction, including
20  * without limitation the rights to use, copy, modify, merge, publish,
21  * distribute, sublicense, and/or sell copies of the Software, and to
22  * permit persons to whom the Software is furnished to do so, subject to
23  * the following conditions:
24  *
25  * The above copyright notice and this permission notice shall be
26  * included in all copies or substantial portions of the Software.
27  *
28  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35  *
36  */
37 
38 namespace Gecode { namespace Iter { namespace Ranges {
39 
45  class RangeListIter {
46  protected:
48  class RangeList : public Support::BlockClient<RangeList,Region> {
49  public:
51  int min, max;
54  };
56  class RLIO : public Support::BlockAllocator<RangeList,Region> {
57  public:
59  unsigned int use_cnt;
61  RLIO(Region& r);
62  };
70  void set(RangeList* l);
72  RangeList* get(void) const;
74  RangeList* range(int min, int max, RangeList*& f);
76  RangeList* range(int min, int max);
78  template<class I>
79  RangeList* range(I& i, RangeList*& f);
81  template<class I>
82  RangeList* range(I& i);
84  template<class I>
85  RangeList* copy(I& i);
86  public:
88 
89 
90  RangeListIter(void);
96  void init(Region& r);
100 
102 
103 
104  bool operator ()(void) const;
106  void operator ++(void);
108  void reset(void);
110 
112 
113 
114  int min(void) const;
116  int max(void) const;
118  unsigned int width(void) const;
120 
122  ~RangeListIter(void);
123  };
124 
125 
128  : Support::BlockAllocator<RangeList,Region>(r), use_cnt(1) {}
129 
130 
133  : rlio(NULL) {}
134 
137  : rlio(new (r.ralloc(sizeof(RLIO))) RLIO(r)),
138  h(NULL), c(NULL) {}
139 
140  forceinline void
142  rlio = new (r.ralloc(sizeof(RLIO))) RLIO(r);
143  h = c = NULL;
144  }
145 
148  : rlio(i.rlio), h(i.h), c(i.c) {
149  rlio->use_cnt++;
150  }
151 
154  if (&i != this) {
155  if ((rlio != NULL) && (--rlio->use_cnt == 0)) {
156  Region& r = rlio->allocator();
157  rlio->~RLIO();
158  r.rfree(rlio,sizeof(RLIO));
159  }
160  rlio = i.rlio;
161  rlio->use_cnt++;
162  c=i.c; h=i.h;
163  }
164  return *this;
165  }
166 
169  if ((rlio != NULL) && (--rlio->use_cnt == 0)) {
170  Region& r = rlio->allocator();
171  rlio->~RLIO();
172  r.rfree(rlio,sizeof(RLIO));
173  }
174  }
175 
176 
177  forceinline void
179  h = c = l;
180  }
181 
183  RangeListIter::get(void) const {
184  return h;
185  }
186 
189  RangeList* t;
190  // Take element from freelist if possible
191  if (f != NULL) {
192  t = f; f = f->next;
193  } else {
194  t = new (*rlio) RangeList;
195  }
196  t->min = min; t->max = max;
197  return t;
198  }
199 
202  RangeList* t = new (*rlio) RangeList;
203  t->min = min; t->max = max;
204  return t;
205  }
206 
207  template<class I>
210  return range(i.min(),i.max(),f);
211  }
212 
213  template<class I>
216  return range(i.min(),i.max());
217  }
218 
219  template<class I>
222  RangeList* h;
223  RangeList** c = &h;
224  for ( ; i(); ++i) {
225  RangeList* t = range(i);
226  *c = t; c = &t->next;
227  }
228  *c = NULL;
229  return h;
230  }
231 
232  forceinline bool
234  return c != NULL;
235  }
236 
237  forceinline void
239  c = c->next;
240  }
241 
242  forceinline void
244  c = h;
245  }
246 
247  forceinline int
248  RangeListIter::min(void) const {
249  return c->min;
250  }
251  forceinline int
252  RangeListIter::max(void) const {
253  return c->max;
254  }
255  forceinline unsigned int
256  RangeListIter::width(void) const {
257  return static_cast<unsigned int>(c->max-c->min)+1;
258  }
259 
260 }}}
261 
262 // STATISTICS: iter-any
263