~drizzle-trunk/drizzle/development

390.1.2 by Monty Taylor
Fixed copyright headers in drizzled/
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
1999.6.1 by kalebral at gmail
update Copyright strings to a more common format to help with creating the master debian copyright file
4
 *  Copyright (C) 2008 Sun Microsystems, Inc.
390.1.2 by Monty Taylor
Fixed copyright headers in drizzled/
5
 *
6
 *  This program is free software; you can redistribute it and/or modify
7
 *  it under the terms of the GNU General Public License as published by
8
 *  the Free Software Foundation; version 2 of the License.
9
 *
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU General Public License for more details.
14
 *
15
 *  You should have received a copy of the GNU General Public License
16
 *  along with this program; if not, write to the Free Software
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
 */
19
2234 by Brian Aker
Mass removal of ifdef/endif in favor of pragma once.
20
#pragma once
1 by brian
clean slate
21
1241.9.1 by Monty Taylor
Removed global.h. Fixed all the headers.
22
#include <cstdlib>
23
#include <cassert>
322.2.2 by Mats Kindahl
Hiding THD::proc_info field and providing a setter and getter.
24
#include <utility>
398.1.5 by Monty Taylor
Removed C++ includes and std namespace from global.h.
25
#include <algorithm>
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
26
#include <drizzled/memory/sql_alloc.h>
27
#include <drizzled/visibility.h>
2119.4.2 by Monty Taylor
Fixed a sun studio compile issue.
28
2179.1.12 by Olaf van der Spek
x
29
namespace drizzled {
322.2.2 by Mats Kindahl
Hiding THD::proc_info field and providing a setter and getter.
30
2179.1.12 by Olaf van der Spek
x
31
typedef struct st_sql_list 
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
32
{
482 by Brian Aker
Remove uint.
33
  uint32_t elements;
481 by Brian Aker
Remove all of uchar.
34
  unsigned char *first;
35
  unsigned char **next;
243.1.1 by Jay Pipes
* Pulled Object_creation_ctx and Default_creation_ctx out of mysql_priv.h
36
2183.2.24 by Olaf van der Spek
Remove inline keyword
37
  void clear()
243.1.1 by Jay Pipes
* Pulled Object_creation_ctx and Default_creation_ctx out of mysql_priv.h
38
  {
39
    elements=0;
40
    first=0;
41
    next= &first;
42
  }
2183.2.19 by Olaf van der Spek
Use List::size()
43
44
  size_t size() const
45
  {
46
    return elements;
47
  }
48
2183.2.24 by Olaf van der Spek
Remove inline keyword
49
  void link_in_list(unsigned char *element,unsigned char **next_ptr)
243.1.1 by Jay Pipes
* Pulled Object_creation_ctx and Default_creation_ctx out of mysql_priv.h
50
  {
51
    elements++;
52
    (*next)=element;
53
    next= next_ptr;
54
    *next=0;
55
  }
2183.2.24 by Olaf van der Spek
Remove inline keyword
56
  void save_and_clear(struct st_sql_list *save)
243.1.1 by Jay Pipes
* Pulled Object_creation_ctx and Default_creation_ctx out of mysql_priv.h
57
  {
58
    *save= *this;
2179.2.1 by Olaf van der Spek
Rename List::empty to clear (std::list uses clear)
59
    clear();
243.1.1 by Jay Pipes
* Pulled Object_creation_ctx and Default_creation_ctx out of mysql_priv.h
60
  }
2183.2.24 by Olaf van der Spek
Remove inline keyword
61
  void push_front(struct st_sql_list *save)
243.1.1 by Jay Pipes
* Pulled Object_creation_ctx and Default_creation_ctx out of mysql_priv.h
62
  {
63
    *save->next= first;				/* link current list last */
64
    first= save->first;
65
    elements+= save->elements;
66
  }
2183.2.24 by Olaf van der Spek
Remove inline keyword
67
  void push_back(struct st_sql_list *save)
243.1.1 by Jay Pipes
* Pulled Object_creation_ctx and Default_creation_ctx out of mysql_priv.h
68
  {
69
    if (save->first)
70
    {
71
      *next= save->first;
72
      next= save->next;
73
      elements+= save->elements;
74
    }
75
  }
76
} SQL_LIST;
1 by brian
clean slate
77
78
/*
79
  Basic single linked list
80
  Used for item and item_buffs.
81
  All list ends with a pointer to the 'end_of_list' element, which
82
  data pointer is a null pointer and the next pointer points to itself.
83
  This makes it very fast to traverse lists as we don't have to
84
  test for a specialend condition for list that can't contain a null
85
  pointer.
86
*/
87
88
89
/**
90
  list_node - a node of a single-linked list.
91
  @note We never call a destructor for instances of this class.
92
*/
93
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
94
struct list_node : public memory::SqlAlloc
1 by brian
clean slate
95
{
96
  list_node *next;
97
  void *info;
98
  list_node(void *info_par,list_node *next_par)
99
    :next(next_par),info(info_par)
100
  {}
101
  list_node()					/* For end_of_list */
102
  {
103
    info= 0;
104
    next= this;
105
  }
106
};
107
2119.4.2 by Monty Taylor
Fixed a sun studio compile issue.
108
extern DRIZZLED_API list_node end_of_list;
1 by brian
clean slate
109
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
110
class base_list :public memory::SqlAlloc
1 by brian
clean slate
111
{
112
protected:
113
  list_node *first,**last;
2183.2.20 by Olaf van der Spek
Use List::size()
114
  uint32_t elements;
1 by brian
clean slate
115
public:
116
2183.2.24 by Olaf van der Spek
Remove inline keyword
117
  void clear() { elements=0; first= &end_of_list; last=&first;}
118
  base_list() { clear(); }
1 by brian
clean slate
119
  /**
120
    This is a shallow copy constructor that implicitly passes the ownership
121
    from the source list to the new instance. The old instance is not
122
    updated, so both objects end up sharing the same nodes. If one of
123
    the instances then adds or removes a node, the other becomes out of
124
    sync ('last' pointer), while still operational. Some old code uses and
125
    relies on this behaviour. This logic is quite tricky: please do not use
126
    it in any new code.
127
  */
2183.2.24 by Olaf van der Spek
Remove inline keyword
128
  base_list(const base_list &tmp) :memory::SqlAlloc()
1 by brian
clean slate
129
  {
130
    elements= tmp.elements;
131
    first= tmp.first;
132
    last= elements ? tmp.last : &first;
133
  }
2183.2.24 by Olaf van der Spek
Remove inline keyword
134
  base_list(bool) { }
2241.2.11 by Olaf van der Spek
Refactor
135
  void push_back(void *info)
136
  {
137
    *last = new list_node(info, &end_of_list);
138
    last= &(*last)->next;
139
    elements++;
140
  }
141
  void push_back(void *info, memory::Root *mem_root)
142
  {
143
    *last = new (mem_root) list_node(info, &end_of_list);
144
    last= &(*last)->next;
145
    elements++;
146
  }
147
  void push_front(void *info)
1 by brian
clean slate
148
  {
149
    list_node *node=new list_node(info,first);
2241.2.11 by Olaf van der Spek
Refactor
150
    if (last == &first)
151
			last= &node->next;
1 by brian
clean slate
152
      first=node;
153
      elements++;
154
  }
155
  void remove(list_node **prev)
156
  {
157
    list_node *node=(*prev)->next;
158
    if (!--elements)
159
      last= &first;
160
    else if (last == &(*prev)->next)
161
      last= prev;
162
    delete *prev;
163
    *prev=node;
164
  }
2183.2.24 by Olaf van der Spek
Remove inline keyword
165
  void concat(base_list *list)
1 by brian
clean slate
166
  {
167
    if (!list->is_empty())
168
    {
169
      *last= list->first;
170
      last= list->last;
171
      elements+= list->elements;
172
    }
173
  }
2183.2.24 by Olaf van der Spek
Remove inline keyword
174
  void *pop()
1 by brian
clean slate
175
  {
176
    if (first == &end_of_list) return 0;
177
    list_node *tmp=first;
178
    first=first->next;
179
    if (!--elements)
180
      last= &first;
181
    return tmp->info;
182
  }
2183.2.24 by Olaf van der Spek
Remove inline keyword
183
  void disjoin(base_list *list)
1 by brian
clean slate
184
  {
185
    list_node **prev= &first;
186
    list_node *node= first;
187
    list_node *list_first= list->first;
188
    elements=0;
189
    while (node && node != list_first)
190
    {
191
      prev= &node->next;
192
      node= node->next;
193
      elements++;
194
    }
195
    *prev= *last;
196
    last= prev;
197
  }
2183.2.24 by Olaf van der Spek
Remove inline keyword
198
  void prepand(base_list *list)
1 by brian
clean slate
199
  {
200
    if (!list->is_empty())
201
    {
202
      *list->last= first;
203
      first= list->first;
204
      elements+= list->elements;
205
    }
206
  }
207
  /**
208
    Swap two lists.
209
  */
2183.2.24 by Olaf van der Spek
Remove inline keyword
210
  void swap(base_list &rhs)
1 by brian
clean slate
211
  {
322.2.2 by Mats Kindahl
Hiding THD::proc_info field and providing a setter and getter.
212
    std::swap(first, rhs.first);
213
    std::swap(last, rhs.last);
214
    std::swap(elements, rhs.elements);
1 by brian
clean slate
215
  }
2183.2.24 by Olaf van der Spek
Remove inline keyword
216
  bool is_empty() { return first == &end_of_list ; }
1 by brian
clean slate
217
  friend class base_list_iterator;
218
219
#ifdef LIST_EXTRA_DEBUG
220
  /*
221
    Check list invariants and print results into trace. Invariants are:
222
      - (*last) points to end_of_list
223
      - There are no NULLs in the list.
224
      - base_list::elements is the number of elements in the list.
225
226
    SYNOPSIS
227
      check_list()
228
        name  Name to print to trace file
229
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
230
    RETURN
1 by brian
clean slate
231
      1  The list is Ok.
232
      0  List invariants are not met.
233
  */
234
235
  bool check_list(const char *name)
236
  {
237
    base_list *list= this;
238
    list_node *node= first;
482 by Brian Aker
Remove uint.
239
    uint32_t cnt= 0;
1 by brian
clean slate
240
241
    while (node->next != &end_of_list)
242
    {
243
      if (!node->info)
244
      {
51.1.53 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
245
        return false;
1 by brian
clean slate
246
      }
247
      node= node->next;
248
      cnt++;
249
    }
250
    if (last != &(node->next))
251
    {
51.1.53 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
252
      return false;
1 by brian
clean slate
253
    }
254
    if (cnt+1 != elements)
255
    {
51.1.53 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
256
      return false;
1 by brian
clean slate
257
    }
51.1.53 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
258
    return true;
1 by brian
clean slate
259
  }
260
#endif // LIST_EXTRA_DEBUG
261
262
protected:
263
  void after(void *info,list_node *node)
264
  {
265
    list_node *new_node=new list_node(info,node->next);
266
    node->next=new_node;
267
    elements++;
268
    if (last == &(node->next))
269
      last= &new_node->next;
270
  }
271
};
272
273
274
class base_list_iterator
275
{
276
protected:
277
  base_list *list;
278
  list_node **el,**prev,*current;
2183.2.4 by Olaf van der Spek
merge trunk
279
public:
482 by Brian Aker
Remove uint.
280
  void sublist(base_list &ls, uint32_t elm)
1 by brian
clean slate
281
  {
282
    ls.first= *el;
283
    ls.last= list->last;
284
    ls.elements= elm;
285
  }
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
286
  base_list_iterator()
1 by brian
clean slate
287
    :list(0), el(0), prev(0), current(0)
288
  {}
289
2183.2.9 by Olaf van der Spek
x
290
  base_list_iterator(base_list &list_par, list_node** el0)
291
    :list(&list_par), el(el0), prev(0), current(0)
1 by brian
clean slate
292
  {
293
  }
294
295
  void *replace(base_list &new_list)
296
  {
297
    void *ret_value=current->info;
298
    if (!new_list.is_empty())
299
    {
300
      *new_list.last=current->next;
301
      current->info=new_list.first->info;
302
      current->next=new_list.first->next;
2183.2.24 by Olaf van der Spek
Remove inline keyword
303
      if (list->last == &current->next && new_list.elements > 1)
304
        list->last= new_list.last;
1 by brian
clean slate
305
      list->elements+=new_list.elements-1;
306
    }
307
    return ret_value;				// return old element
308
  }
2183.2.24 by Olaf van der Spek
Remove inline keyword
309
  void remove()			// Remove current
1 by brian
clean slate
310
  {
311
    list->remove(prev);
312
    el=prev;
313
    current=0;					// Safeguard
314
  }
315
  void after(void *element)			// Insert element after current
316
  {
317
    list->after(element,current);
318
    current=current->next;
319
    el= &current->next;
320
  }
321
};
322
2179.1.1 by Olaf van der Spek
x
323
template <class T> class List_iterator;
324
2183.2.24 by Olaf van der Spek
Remove inline keyword
325
template <class T> class List : public base_list
1 by brian
clean slate
326
{
327
public:
2179.1.1 by Olaf van der Spek
x
328
  typedef List_iterator<T> iterator;
329
2183.2.9 by Olaf van der Spek
x
330
  friend class List_iterator<T>;
331
2183.2.24 by Olaf van der Spek
Remove inline keyword
332
  List() {}
333
  List(const List<T> &tmp) : base_list(tmp) {}
334
  List(const List<T> &tmp, memory::Root *mem_root) : base_list(tmp, mem_root) {}
2241.2.11 by Olaf van der Spek
Refactor
335
  void push_back(T *a) { base_list::push_back(a); }
336
  void push_back(T *a, memory::Root *mem_root) { base_list::push_back(a, mem_root); }
337
  void push_front(T *a) { base_list::push_front(a); }
2183.2.24 by Olaf van der Spek
Remove inline keyword
338
  T& front() {return *static_cast<T*>(first->info); }
339
  T* pop()  {return static_cast<T*>(base_list::pop()); }
340
  void concat(List<T> *list) { base_list::concat(list); }
341
  void disjoin(List<T> *list) { base_list::disjoin(list); }
342
  void prepand(List<T> *list) { base_list::prepand(list); }
343
  void delete_elements()
1 by brian
clean slate
344
  {
345
    list_node *element,*next;
346
    for (element=first; element != &end_of_list; element=next)
347
    {
348
      next=element->next;
349
      delete (T*) element->info;
350
    }
2179.2.1 by Olaf van der Spek
Rename List::empty to clear (std::list uses clear)
351
    clear();
1 by brian
clean slate
352
  }
2179.1.6 by Olaf van der Spek
x
353
354
  iterator begin()
355
  {
2183.2.9 by Olaf van der Spek
x
356
    return iterator(*this, &first);
2179.1.6 by Olaf van der Spek
x
357
  }
2183.2.17 by Olaf van der Spek
Use List::size()
358
359
  size_t size() const
360
  {
361
    return elements;
362
  }
2183.2.20 by Olaf van der Spek
Use List::size()
363
364
  void set_size(size_t v)
365
  {
366
    elements = v;
367
  }
1 by brian
clean slate
368
};
369
2183.2.24 by Olaf van der Spek
Remove inline keyword
370
template <class T> class List_iterator : public base_list_iterator
1 by brian
clean slate
371
{
372
public:
2183.2.9 by Olaf van der Spek
x
373
  List_iterator(List<T>& a, list_node** b) : base_list_iterator(a, b) {};
374
  List_iterator() {};
2207.8.1 by Olaf van der Spek
x
375
  T *operator++(int) { prev=el; current= *el; el= &current->next; return (T*)current->info; }
2183.2.24 by Olaf van der Spek
Remove inline keyword
376
  T *replace(T *a)   { T* old = (T*) current->info; current->info= a; return old; }
377
  void replace(List<T> &a) { base_list_iterator::replace(a); }
378
  T** ref() { return (T**) &current->info; }
379
380
  T& operator*()
381
  {
382
    return *(T*)current->info;
383
  }
2207.8.2 by Olaf van der Spek
x
384
385
  T* operator->()
386
  {
387
    return (T*)current->info;
388
  }
1 by brian
clean slate
389
};
390
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
391
} /* namespace drizzled */
584.1.15 by Monty Taylor
The mega-patch from hell. Renamed sql_class to session (since that's what it is) and removed it and field and table from common_includes.
392