~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
2385.3.7 by Olaf van der Spek
Refactor DYNAMIC_ARRAY
31
struct 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
  }
2385.3.7 by Olaf van der Spek
Refactor DYNAMIC_ARRAY
56
  void save_and_clear(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
  }
2385.3.7 by Olaf van der Spek
Refactor DYNAMIC_ARRAY
61
  void push_front(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
  }
2385.3.7 by Olaf van der Spek
Refactor DYNAMIC_ARRAY
67
  void push_back(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
  }
2385.3.7 by Olaf van der Spek
Refactor DYNAMIC_ARRAY
76
};
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
  }
2318.6.74 by Olaf van der Spek
Refactor
141
  void push_back(void *info, memory::Root& mem)
2241.2.11 by Olaf van der Spek
Refactor
142
  {
2318.6.74 by Olaf van der Spek
Refactor
143
    *last = new (mem) list_node(info, &end_of_list);
2241.2.11 by Olaf van der Spek
Refactor
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
    list_node *node= first;
482 by Brian Aker
Remove uint.
238
    uint32_t cnt= 0;
1 by brian
clean slate
239
240
    while (node->next != &end_of_list)
241
    {
242
      if (!node->info)
243
      {
51.1.53 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
244
        return false;
1 by brian
clean slate
245
      }
246
      node= node->next;
247
      cnt++;
248
    }
249
    if (last != &(node->next))
250
    {
51.1.53 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
251
      return false;
1 by brian
clean slate
252
    }
253
    if (cnt+1 != elements)
254
    {
51.1.53 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
255
      return false;
1 by brian
clean slate
256
    }
51.1.53 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
257
    return true;
1 by brian
clean slate
258
  }
259
#endif // LIST_EXTRA_DEBUG
260
261
protected:
262
  void after(void *info,list_node *node)
263
  {
264
    list_node *new_node=new list_node(info,node->next);
265
    node->next=new_node;
266
    elements++;
267
    if (last == &(node->next))
268
      last= &new_node->next;
269
  }
270
};
271
272
273
class base_list_iterator
274
{
275
protected:
276
  base_list *list;
277
  list_node **el,**prev,*current;
2183.2.4 by Olaf van der Spek
merge trunk
278
public:
2385.2.3 by Olaf van der Spek
cppcheck
279
  void sublist(base_list &ls, uint32_t elm) const
1 by brian
clean slate
280
  {
281
    ls.first= *el;
282
    ls.last= list->last;
283
    ls.elements= elm;
284
  }
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
285
  base_list_iterator()
1 by brian
clean slate
286
    :list(0), el(0), prev(0), current(0)
287
  {}
288
2183.2.9 by Olaf van der Spek
x
289
  base_list_iterator(base_list &list_par, list_node** el0)
290
    :list(&list_par), el(el0), prev(0), current(0)
1 by brian
clean slate
291
  {
292
  }
293
294
  void *replace(base_list &new_list)
295
  {
296
    void *ret_value=current->info;
297
    if (!new_list.is_empty())
298
    {
299
      *new_list.last=current->next;
300
      current->info=new_list.first->info;
301
      current->next=new_list.first->next;
2183.2.24 by Olaf van der Spek
Remove inline keyword
302
      if (list->last == &current->next && new_list.elements > 1)
303
        list->last= new_list.last;
1 by brian
clean slate
304
      list->elements+=new_list.elements-1;
305
    }
306
    return ret_value;				// return old element
307
  }
2183.2.24 by Olaf van der Spek
Remove inline keyword
308
  void remove()			// Remove current
1 by brian
clean slate
309
  {
310
    list->remove(prev);
311
    el=prev;
312
    current=0;					// Safeguard
313
  }
314
  void after(void *element)			// Insert element after current
315
  {
316
    list->after(element,current);
317
    current=current->next;
318
    el= &current->next;
319
  }
320
};
321
2179.1.1 by Olaf van der Spek
x
322
template <class T> class List_iterator;
323
2183.2.24 by Olaf van der Spek
Remove inline keyword
324
template <class T> class List : public base_list
1 by brian
clean slate
325
{
326
public:
2179.1.1 by Olaf van der Spek
x
327
  typedef List_iterator<T> iterator;
328
2183.2.9 by Olaf van der Spek
x
329
  friend class List_iterator<T>;
330
2183.2.24 by Olaf van der Spek
Remove inline keyword
331
  List() {}
332
  List(const List<T> &tmp) : base_list(tmp) {}
333
  List(const List<T> &tmp, memory::Root *mem_root) : base_list(tmp, mem_root) {}
334
  T& front() {return *static_cast<T*>(first->info); }
335
  T* pop()  {return static_cast<T*>(base_list::pop()); }
336
  void concat(List<T> *list) { base_list::concat(list); }
337
  void disjoin(List<T> *list) { base_list::disjoin(list); }
338
  void prepand(List<T> *list) { base_list::prepand(list); }
339
  void delete_elements()
1 by brian
clean slate
340
  {
341
    list_node *element,*next;
342
    for (element=first; element != &end_of_list; element=next)
343
    {
344
      next=element->next;
345
      delete (T*) element->info;
346
    }
2179.2.1 by Olaf van der Spek
Rename List::empty to clear (std::list uses clear)
347
    clear();
1 by brian
clean slate
348
  }
2179.1.6 by Olaf van der Spek
x
349
350
  iterator begin()
351
  {
2183.2.9 by Olaf van der Spek
x
352
    return iterator(*this, &first);
2179.1.6 by Olaf van der Spek
x
353
  }
2183.2.17 by Olaf van der Spek
Use List::size()
354
355
  size_t size() const
356
  {
357
    return elements;
358
  }
2183.2.20 by Olaf van der Spek
Use List::size()
359
360
  void set_size(size_t v)
361
  {
362
    elements = v;
363
  }
1 by brian
clean slate
364
};
365
2183.2.24 by Olaf van der Spek
Remove inline keyword
366
template <class T> class List_iterator : public base_list_iterator
1 by brian
clean slate
367
{
368
public:
2183.2.9 by Olaf van der Spek
x
369
  List_iterator(List<T>& a, list_node** b) : base_list_iterator(a, b) {};
370
  List_iterator() {};
2207.8.1 by Olaf van der Spek
x
371
  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
372
  T *replace(T *a)   { T* old = (T*) current->info; current->info= a; return old; }
373
  void replace(List<T> &a) { base_list_iterator::replace(a); }
374
  T** ref() { return (T**) &current->info; }
375
376
  T& operator*()
377
  {
378
    return *(T*)current->info;
379
  }
2207.8.2 by Olaf van der Spek
x
380
381
  T* operator->()
382
  {
383
    return (T*)current->info;
384
  }
1 by brian
clean slate
385
};
386
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
387
} /* 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.
388