~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_list.h

edit

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
#ifndef DRIZZLED_SQL_LIST_H
21
21
#define DRIZZLED_SQL_LIST_H
22
22
 
 
23
 
23
24
#include <cstdlib>
24
25
#include <cassert>
25
26
#include <utility>
26
27
#include <algorithm>
27
 
#include <drizzled/memory/sql_alloc.h>
28
 
#include <drizzled/visibility.h>
29
 
 
30
 
namespace drizzled {
31
 
 
32
 
typedef struct st_sql_list 
 
28
#include "drizzled/memory/sql_alloc.h"
 
29
 
 
30
namespace drizzled
33
31
{
 
32
 
 
33
/** Struct to handle simple linked lists. */
 
34
typedef struct st_sql_list {
34
35
  uint32_t elements;
35
36
  unsigned char *first;
36
37
  unsigned char **next;
37
38
 
38
 
  inline void clear()
 
39
  st_sql_list() {}                              /* Remove gcc warning */
 
40
  inline void empty()
39
41
  {
40
42
    elements=0;
41
43
    first=0;
51
53
  inline void save_and_clear(struct st_sql_list *save)
52
54
  {
53
55
    *save= *this;
54
 
    clear();
 
56
    empty();
55
57
  }
56
58
  inline void push_front(struct st_sql_list *save)
57
59
  {
101
103
};
102
104
 
103
105
 
104
 
extern DRIZZLED_API list_node end_of_list;
 
106
extern list_node end_of_list;
105
107
 
106
108
class base_list :public memory::SqlAlloc
107
109
{
111
113
public:
112
114
  uint32_t elements;
113
115
 
114
 
  inline void clear() { elements=0; first= &end_of_list; last=&first;}
115
 
  inline base_list() { clear(); }
 
116
  inline void empty() { elements=0; first= &end_of_list; last=&first;}
 
117
  inline base_list() { empty(); }
116
118
  /**
117
119
    This is a shallow copy constructor that implicitly passes the ownership
118
120
    from the source list to the new instance. The old instance is not
230
232
  inline bool is_empty() { return first == &end_of_list ; }
231
233
  inline list_node *last_ref() { return &end_of_list; }
232
234
  friend class base_list_iterator;
 
235
  friend class error_list;
 
236
  friend class error_list_iterator;
233
237
 
234
238
#ifdef LIST_EXTRA_DEBUG
235
239
  /*
291
295
protected:
292
296
  base_list *list;
293
297
  list_node **el,**prev,*current;
294
 
public:
295
298
  void sublist(base_list &ls, uint32_t elm)
296
299
  {
297
300
    ls.first= *el;
298
301
    ls.last= list->last;
299
302
    ls.elements= elm;
300
303
  }
 
304
public:
301
305
  base_list_iterator()
302
306
    :list(0), el(0), prev(0), current(0)
303
307
  {}
304
308
 
305
 
  base_list_iterator(base_list &list_par, list_node** el0)
306
 
    :list(&list_par), el(el0), prev(0), current(0)
 
309
  base_list_iterator(base_list &list_par)
 
310
  { init(list_par); }
 
311
 
 
312
  inline void init(base_list &list_par)
307
313
  {
 
314
    list= &list_par;
 
315
    el= &list_par.first;
 
316
    prev= 0;
 
317
    current= 0;
308
318
  }
309
319
 
310
320
  inline void *next(void)
314
324
    el= &current->next;
315
325
    return current->info;
316
326
  }
 
327
  inline void *next_fast(void)
 
328
  {
 
329
    list_node *tmp;
 
330
    tmp= *el;
 
331
    el= &tmp->next;
 
332
    return tmp->info;
 
333
  }
 
334
  inline void rewind(void)
 
335
  {
 
336
    el= &list->first;
 
337
  }
317
338
  inline void *replace(void *element)
318
339
  {                                             // Return old element
319
340
    void *tmp=current->info;
355
376
  {
356
377
    return el == &list->last_ref()->next;
357
378
  }
 
379
  friend class error_list_iterator;
358
380
};
359
381
 
360
 
template <class T> class List_iterator;
361
 
 
362
382
template <class T> class List :public base_list
363
383
{
364
384
public:
365
 
  typedef List_iterator<T> iterator;
366
 
 
367
 
  friend class List_iterator<T>;
368
 
 
369
385
  inline List() :base_list() {}
370
386
  inline List(const List<T> &tmp) :base_list(tmp) {}
371
387
  inline List(const List<T> &tmp, memory::Root *mem_root) :
374
390
  inline bool push_back(T *a, memory::Root *mem_root)
375
391
  { return base_list::push_back(a, mem_root); }
376
392
  inline bool push_front(T *a) { return base_list::push_front(a); }
377
 
  inline T* head() {return static_cast<T*>(base_list::head()); }
378
 
  inline T* pop()  {return static_cast<T*>(base_list::pop()); }
 
393
  inline T* head() {return (T*) base_list::head(); }
 
394
  inline T** head_ref() {return (T**) base_list::head_ref(); }
 
395
  inline T* pop()  {return (T*) base_list::pop(); }
379
396
  inline void concat(List<T> *list) { base_list::concat(list); }
380
397
  inline void disjoin(List<T> *list) { base_list::disjoin(list); }
381
398
  inline void prepand(List<T> *list) { base_list::prepand(list); }
387
404
      next=element->next;
388
405
      delete (T*) element->info;
389
406
    }
390
 
    clear();
391
 
  }
392
 
 
393
 
  iterator begin()
394
 
  {
395
 
    return iterator(*this, &first);
 
407
    empty();
396
408
  }
397
409
};
398
410
 
400
412
template <class T> class List_iterator :public base_list_iterator
401
413
{
402
414
public:
403
 
  List_iterator(List<T>& a, list_node** b) : base_list_iterator(a, b) {};
404
 
  List_iterator() {};
 
415
  List_iterator(List<T> &a) : base_list_iterator(a) {}
 
416
  List_iterator() : base_list_iterator() {}
 
417
  inline void init(List<T> &a) { base_list_iterator::init(a); }
405
418
  inline T* operator++(int) { return (T*) base_list_iterator::next(); }
406
419
  inline T *replace(T *a)   { return (T*) base_list_iterator::replace(a); }
407
420
  inline T *replace(List<T> &a) { return (T*) base_list_iterator::replace(a); }
 
421
  inline void rewind(void)  { base_list_iterator::rewind(); }
 
422
  inline void remove()      { base_list_iterator::remove(); }
 
423
  inline void after(T *a)   { base_list_iterator::after(a); }
408
424
  inline T** ref(void)      { return (T**) base_list_iterator::ref(); }
409
425
};
410
426
 
 
427
 
 
428
template <class T> class List_iterator_fast :public base_list_iterator
 
429
{
 
430
protected:
 
431
  inline T *replace(T *)   { return (T*) 0; }
 
432
  inline T *replace(List<T> &) { return (T*) 0; }
 
433
  inline void remove(void)  { }
 
434
  inline void after(T *)   { }
 
435
  inline T** ref(void)      { return (T**) 0; }
 
436
 
 
437
public:
 
438
  inline List_iterator_fast(List<T> &a) : base_list_iterator(a) {}
 
439
  inline List_iterator_fast() : base_list_iterator() {}
 
440
  inline void init(List<T> &a) { base_list_iterator::init(a); }
 
441
  inline T* operator++(int) { return (T*) base_list_iterator::next_fast(); }
 
442
  inline void rewind(void)  { base_list_iterator::rewind(); }
 
443
  void sublist(List<T> &list_arg, uint32_t el_arg)
 
444
  {
 
445
    base_list_iterator::sublist(list_arg, el_arg);
 
446
  }
 
447
};
 
448
 
 
449
 
411
450
/**
412
451
  Make a deep copy of each list element.
413
452
 
424
463
*/
425
464
 
426
465
template <typename T>
427
 
void list_copy_and_replace_each_value(List<T> &list, memory::Root *mem_root)
 
466
inline
 
467
void
 
468
list_copy_and_replace_each_value(List<T> &list, memory::Root *mem_root)
428
469
{
429
470
  /* Make a deep copy of each element */
430
 
  typename List<T>::iterator it(list.begin());
 
471
  List_iterator<T> it(list);
431
472
  T *el;
432
473
  while ((el= it++))
433
474
    it.replace(el->clone(mem_root));