~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_array.h

Merged vcol stuff.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 
3
 *
 
4
 *  Copyright (C) 2008 Sun Microsystems
 
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
 
 
20
#include <mysys/my_sys.h>
 
21
 
 
22
/* 
 
23
  Array of pointers to Elem that uses memory from MEM_ROOT
 
24
 
 
25
  MEM_ROOT has no realloc() so this is supposed to be used for cases when
 
26
  reallocations are rare.
 
27
*/
 
28
 
 
29
template <class Elem> class Array
 
30
{
 
31
  enum {alloc_increment = 16};
 
32
  Elem **buffer;
 
33
  uint32_t n_elements, max_element;
 
34
public:
 
35
  Array(MEM_ROOT *mem_root, uint32_t prealloc=16)
 
36
  {
 
37
    buffer= (Elem**)alloc_root(mem_root, prealloc * sizeof(Elem**));
 
38
    max_element = buffer? prealloc : 0;
 
39
    n_elements= 0;
 
40
  }
 
41
 
 
42
  Elem& at(int idx)
 
43
  {
 
44
    return *(((Elem*)buffer) + idx);
 
45
  }
 
46
 
 
47
  Elem **front()
 
48
  {
 
49
    return buffer;
 
50
  }
 
51
 
 
52
  Elem **back()
 
53
  {
 
54
    return buffer + n_elements;
 
55
  }
 
56
 
 
57
  bool append(MEM_ROOT *mem_root, Elem *el)
 
58
  {
 
59
    if (n_elements == max_element)
 
60
    {
 
61
      Elem **newbuf;
 
62
      if (!(newbuf= (Elem**)alloc_root(mem_root, (n_elements + alloc_increment)*
 
63
                                                  sizeof(Elem**))))
 
64
      {
 
65
        return false;
 
66
      }
 
67
      memcpy(newbuf, buffer, n_elements*sizeof(Elem*));
 
68
      buffer= newbuf;
 
69
    }
 
70
    buffer[n_elements++]= el;
 
71
    return false;
 
72
  }
 
73
 
 
74
  int elements()
 
75
  {
 
76
    return n_elements;
 
77
  }
 
78
 
 
79
  void clear()
 
80
  {
 
81
    n_elements= 0;
 
82
  }
 
83
 
 
84
  typedef int (*CMP_FUNC)(Elem * const *el1, Elem *const *el2);
 
85
 
 
86
  void sort(CMP_FUNC cmp_func)
 
87
  {
 
88
    my_qsort(buffer, n_elements, sizeof(Elem*), (qsort_cmp)cmp_func);
 
89
  }
 
90
};
 
91