MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SLList.hpp
1 /*
2  Copyright (C) 2003-2006 MySQL AB
3  All rights reserved. Use is subject to license terms.
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; version 2 of the License.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 
19 #ifndef SLLIST_HPP
20 #define SLLIST_HPP
21 
22 #include "ArrayPool.hpp"
23 #include <NdbOut.hpp>
24 
29 template <typename P, typename T, typename U = T>
30 class SLListImpl
31 {
32 public:
36  struct HeadPOD {
37  Uint32 firstItem;
38  void init() { firstItem = RNIL;}
39  };
40 
41  struct Head : public HeadPOD {
42  Head();
43  Head& operator= (const HeadPOD& src) {
44  this->firstItem = src.firstItem;
45  return *this;
46  }
47  };
48 
49  SLListImpl(P & thePool);
50 
56  bool seize(Ptr<T> &);
57 
63  bool seizeId(Ptr<T> &, Uint32 i);
64 
70  bool seizeN(Ptr<T> &, Uint32 n);
71 
75  void release();
76 
80  void remove();
81 
85  void getPtr(Ptr<T> &, Uint32 i) const;
86 
90  void getPtr(Ptr<T> &) const ;
91 
95  T * getPtr(Uint32 i) const ;
96 
102  bool first(Ptr<T> &) const ;
103 
109  bool next(Ptr<T> &) const ;
110 
116  bool hasNext(const Ptr<T> &) const;
117 
121  void add(Ptr<T> & p){
122  p.p->U::nextList = head.firstItem;
123  head.firstItem = p.i;
124  }
125 
130  void add(Uint32 first, Ptr<T> & last);
131 
137  bool remove_front(Ptr<T> &);
138 
139  Uint32 noOfElements() const {
140  Uint32 c = 0;
141  Uint32 i = head.firstItem;
142  while(i != RNIL){
143  c++;
144  const T * t = thePool.getPtr(i);
145  i = t->U::nextList;
146  }
147  return c;
148  }
149 
154  void print(NdbOut & out) {
155  out << "firstItem = " << head.firstItem << endl;
156  Uint32 i = head.firstItem;
157  while(i != RNIL){
158  T * t = thePool.getPtr(i);
159  t->print(out); out << " ";
160  i = t->next;
161  }
162  }
163 
164  inline bool empty() const { return head.firstItem == RNIL;}
165 
166 protected:
167  Head head;
168  P & thePool;
169 };
170 
171 template <typename P, typename T, typename U = T>
172 class LocalSLListImpl : public SLListImpl<P, T, U>
173 {
174 public:
175  LocalSLListImpl(P & thePool, typename SLListImpl<P, T, U>::HeadPOD & _src)
176  : SLListImpl<P, T, U>(thePool), src(_src)
177  {
178  this->head = src;
179  }
180 
181  ~LocalSLListImpl(){
182  src = this->head;
183  }
184 private:
185  typename SLListImpl<P, T, U>::HeadPOD & src;
186 };
187 
188 template <typename P, typename T, typename U>
189 inline
191  thePool(_pool)
192 {
193 }
194 
195 template <typename P, typename T, typename U>
196 inline
198 {
199  this->init();
200 }
201 
202 template <typename P, typename T, typename U>
203 inline
204 bool
206 {
207  thePool.seize(p);
208  T * t = p.p;
209  Uint32 ff = head.firstItem;
210  if(p.i != RNIL)
211  {
212  t->U::nextList = ff;
213  head.firstItem = p.i;
214  return true;
215  }
216  return false;
217 }
218 
219 template <typename P, typename T, typename U>
220 inline
221 bool
223 {
224  thePool.seizeId(p, ir);
225  T * t = p.p;
226  Uint32 ff = head.firstItem;
227  if(p.i != RNIL)
228  {
229  t->U::nextList = ff;
230  head.firstItem = p.i;
231  return true;
232  }
233  return false;
234 }
235 
236 template <typename P, typename T, typename U>
237 inline
238 bool
240 {
241  for(Uint32 i = 0; i < n; i++)
242  {
243  if(seize(p) == RNIL)
244  {
248  for(; i > 0; i--)
249  {
250  p.i = head.firstItem;
251  thePool.getPtr(p);
252  head.firstItem = p.p->U::nextList;
253  thePool.release(p);
254  }
255  return false;
256  }
257  }
258 
262  p.i = head.firstItem;
263  p.p = thePool.getPtr(head.firstItem);
264 
265  return true;
266 }
267 
268 
269 template <typename P, typename T, typename U>
270 inline
271 void
273 {
274  head.firstItem = RNIL;
275 }
276 
277 template <typename P, typename T, typename U>
278 inline
279 bool
281 {
282  p.i = head.firstItem;
283  if (p.i != RNIL)
284  {
285  p.p = thePool.getPtr(p.i);
286  head.firstItem = p.p->U::nextList;
287  return true;
288  }
289  return false;
290 }
291 
292 template <typename P, typename T, typename U>
293 inline
294 void
296 {
297  last.p->U::nextList = head.firstItem;
298  head.firstItem = first;
299 }
300 
301 template <typename P, typename T, typename U>
302 inline
303 void
305 {
306  Ptr<T> ptr;
307  Uint32 curr = head.firstItem;
308  while(curr != RNIL)
309  {
310  thePool.getPtr(ptr, curr);
311  curr = ptr.p->U::nextList;
312  thePool.release(ptr);
313  }
314  head.firstItem = RNIL;
315 }
316 
317 template <typename P, typename T, typename U>
318 inline
319 void
320 SLListImpl<P, T, U>::getPtr(Ptr<T> & p, Uint32 i) const
321 {
322  p.i = i;
323  p.p = thePool.getPtr(i);
324 }
325 
326 template <typename P, typename T, typename U>
327 inline
328 void
330 {
331  thePool.getPtr(p);
332 }
333 
334 template <typename P, typename T, typename U>
335 inline
336 T *
338 {
339  return thePool.getPtr(i);
340 }
341 
347 template <typename P, typename T, typename U>
348 inline
349 bool
351 {
352  Uint32 i = head.firstItem;
353  p.i = i;
354  if(i != RNIL)
355  {
356  p.p = thePool.getPtr(i);
357  return true;
358  }
359  p.p = NULL;
360  return false;
361 }
362 
363 template <typename P, typename T, typename U>
364 inline
365 bool
367 {
368  Uint32 i = p.p->U::nextList;
369  p.i = i;
370  if(i != RNIL)
371  {
372  p.p = thePool.getPtr(i);
373  return true;
374  }
375  p.p = NULL;
376  return false;
377 }
378 
379 template <typename P, typename T, typename U>
380 inline
381 bool
383 {
384  return p.p->U::nextList != RNIL;
385 }
386 
387 // Specializations
388 
389 template <typename T, typename U = T>
390 class SLList : public SLListImpl<ArrayPool<T>, T, U>
391 {
392 public:
393  SLList(ArrayPool<T> & p) : SLListImpl<ArrayPool<T>, T, U>(p) {}
394 };
395 
396 template <typename T, typename U = T>
397 class LocalSLList : public LocalSLListImpl<ArrayPool<T>,T,U> {
398 public:
399  LocalSLList(ArrayPool<T> & p, typename SLList<T,U>::Head & _src)
400  : LocalSLListImpl<ArrayPool<T>,T,U>(p, _src) {}
401 };
402 
403 
404 #endif