MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DLList.hpp
1 /*
2  Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; version 2 of the License.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17 
18 #ifndef DLLIST_HPP
19 #define DLLIST_HPP
20 
21 #include "ArrayPool.hpp"
22 
27 template <typename P, typename T, typename U = T>
28 class DLListImpl
29 {
30 public:
34  struct HeadPOD {
35  Uint32 firstItem;
36  inline bool isEmpty() const { return firstItem == RNIL; }
37  inline void init () {
38  firstItem = RNIL;
39 #ifdef VM_TRACE
40  in_use = false;
41 #endif
42  }
43 
44 #ifdef VM_TRACE
45  bool in_use;
46 #endif
47  };
48 
49  struct Head : public HeadPOD
50  {
51  Head();
52  Head& operator=(const HeadPOD& src) {
53  this->firstItem = src.firstItem;
54  return *this;
55  }
56  };
57 
58  DLListImpl(P& thePool);
59 
65  bool seize(Ptr<T> &);
66 
72  bool seizeId(Ptr<T> &, Uint32 i);
73 
77  bool findId(Uint32 i) const;
78 
82  void release(Uint32 i);
83 
87  void release(Ptr<T> &);
88 
92  void release();
93 
97  void remove();
98 
104  void add(Ptr<T> &);
105 
110  void add(Uint32 first, Ptr<T> & last);
111 
117  void remove(Ptr<T> &);
118 
124  void remove(T*);
125 
129  void getPtr(Ptr<T> &, Uint32 i) const;
130 
134  void getPtr(Ptr<T> &) const ;
135 
139  T * getPtr(Uint32 i) const ;
140 
146  bool first(Ptr<T> &) const ;
147 
155  bool next(Ptr<T> &) const ;
156 
163  bool hasNext(const Ptr<T> &) const;
164 
165  inline bool isEmpty() const { return head.firstItem == RNIL;}
166 
167 protected:
168  Head head;
169  P & thePool;
170 };
171 
172 template <typename P, typename T, typename U = T>
173 class LocalDLListImpl : public DLListImpl<P,T,U>
174 {
175 public:
176  LocalDLListImpl(P & thePool, typename DLListImpl<P,T,U>::HeadPOD & _src)
177  : DLListImpl<P,T,U>(thePool), src(_src)
178  {
179  this->head = src;
180 #ifdef VM_TRACE
181  assert(src.in_use == false);
182  src.in_use = true;
183 #endif
184  }
185 
186  ~LocalDLListImpl(){
187 #ifdef VM_TRACE
188  assert(src.in_use == true);
189 #endif
190  src = this->head;
191  }
192 private:
193  typename DLListImpl<P,T,U>::HeadPOD & src;
194 };
195 
196 template <typename P, typename T, typename U>
197 inline
199  : thePool(_pool)
200 {
201  // Require user defined constructor on T since we fiddle
202  // with T's members
203  ASSERT_TYPE_HAS_CONSTRUCTOR(T);
204 }
205 
206 template <typename P, typename T, typename U>
207 inline
209 {
210  this->init();
211 }
212 
218 template <typename P, typename T, typename U>
219 inline
220 bool
222 {
223  if (likely(thePool.seize(p)))
224  {
225  add(p);
226  return true;
227  }
228  return false;
229 }
230 
236 template <typename P, typename T, typename U>
237 inline
238 bool
240 {
241  if (likely(thePool.seizeId(p, ir)))
242  {
243  add(p);
244  return true;
245  }
246  return false;
247 }
248 
249 template <typename P, typename T, typename U>
250 inline
251 bool
253 {
254  return thePool.findId(i);
255 }
256 
257 template <typename P, typename T, typename U>
258 inline
259 void
261 {
262  T * t = p.p;
263  Uint32 ff = head.firstItem;
264 
265  t->U::nextList = ff;
266  t->U::prevList = RNIL;
267  head.firstItem = p.i;
268 
269  if(ff != RNIL)
270  {
271  T * t2 = thePool.getPtr(ff);
272  t2->U::prevList = p.i;
273  }
274 }
275 
276 template <typename P, typename T, typename U>
277 inline
278 void
280 {
281  Uint32 ff = head.firstItem;
282 
283  head.firstItem = first;
284  lastPtr.p->U::nextList = ff;
285 
286  if(ff != RNIL)
287  {
288  T * t2 = thePool.getPtr(ff);
289  t2->U::prevList = lastPtr.i;
290  }
291 }
292 
293 template <typename P, typename T, typename U>
294 inline
295 void
297 {
298  remove(p.p);
299 }
300 
301 template <typename P, typename T, typename U>
302 inline
303 void
305 {
306  T * t = p;
307  Uint32 ni = t->U::nextList;
308  Uint32 pi = t->U::prevList;
309 
310  if(ni != RNIL){
311  T * tn = thePool.getPtr(ni);
312  tn->U::prevList = pi;
313  }
314 
315  if(pi != RNIL){
316  T * tp = thePool.getPtr(pi);
317  tp->U::nextList = ni;
318  } else {
319  head.firstItem = ni;
320  }
321 }
322 
326 template <typename P, typename T, typename U>
327 inline
328 void
330 {
331  Ptr<T> p;
332  p.i = i;
333  p.p = thePool.getPtr(i);
334  release(p);
335 }
336 
340 template <typename P, typename T, typename U>
341 inline
342 void
344 {
345  remove(p);
346  thePool.release(p);
347 }
348 
349 template <typename P, typename T, typename U>
350 inline
351 void
353 {
354  Ptr<T> ptr;
355  Uint32 curr = head.firstItem;
356  while(curr != RNIL)
357  {
358  thePool.getPtr(ptr, curr);
359  curr = ptr.p->U::nextList;
360  thePool.release(ptr);
361  }
362  head.firstItem = RNIL;
363 }
364 
365 template <typename P, typename T, typename U>
366 inline
367 void
369 {
370  head.firstItem = RNIL;
371 }
372 
373 template <typename P, typename T, typename U>
374 inline
375 void
376 DLListImpl<P,T,U>::getPtr(Ptr<T> & p, Uint32 i) const
377 {
378  p.i = i;
379  p.p = thePool.getPtr(i);
380 }
381 
382 template <typename P, typename T, typename U>
383 inline
384 void
386 {
387  thePool.getPtr(p);
388 }
389 
390 template <typename P, typename T, typename U>
391 inline
392 T *
394 {
395  return thePool.getPtr(i);
396 }
397 
403 template <typename P, typename T, typename U>
404 inline
405 bool
407 {
408  Uint32 i = head.firstItem;
409  p.i = i;
410  if(i != RNIL)
411  {
412  p.p = thePool.getPtr(i);
413  return true;
414  }
415  p.p = NULL;
416  return false;
417 }
418 
419 template <typename P, typename T, typename U>
420 inline
421 bool
423 {
424  Uint32 i = p.p->U::nextList;
425  p.i = i;
426  if(i != RNIL){
427  p.p = thePool.getPtr(i);
428  return true;
429  }
430  p.p = NULL;
431  return false;
432 }
433 
434 template <typename P, typename T, typename U>
435 inline
436 bool
438 {
439  return p.p->U::nextList != RNIL;
440 }
441 
442 // Specializations
443 
444 template <typename T, typename U = T>
445 class DLList : public DLListImpl<ArrayPool<T>, T, U>
446 {
447 public:
448  DLList(ArrayPool<T> & p) : DLListImpl<ArrayPool<T>, T, U>(p) {}
449 };
450 
451 template <typename T, typename U = T>
452 class LocalDLList : public LocalDLListImpl<ArrayPool<T>, T, U> {
453 public:
454  LocalDLList(ArrayPool<T> & p, typename DLList<T,U>::HeadPOD & _src)
455  : LocalDLListImpl<ArrayPool<T>, T, U>(p, _src) {}
456 };
457 
458 #endif