~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_array.h

  • Committer: Monty Taylor
  • Date: 2008-09-14 21:16:59 UTC
  • mto: This revision was merged to the branch mainline in revision 388.
  • Revision ID: monty@inaugust.com-20080914211659-nhjt4mobp3uazgt0
libdrizzle.h cleanup. Removed some unused things. Started splitting header into
file-per-struct like libmemcached is.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2003 MySQL AB
 
2
 
 
3
   This program is free software; you can redistribute it and/or modify
 
4
   it under the terms of the GNU General Public License as published by
 
5
   the Free Software Foundation; version 2 of the License.
 
6
 
 
7
   This program is distributed in the hope that it will be useful,
 
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
   GNU General Public License for more details.
 
11
 
 
12
   You should have received a copy of the GNU General Public License
 
13
   along with this program; if not, write to the Free Software
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
15
 
 
16
#include <mysys/my_sys.h>
 
17
 
 
18
/* 
 
19
  Array of pointers to Elem that uses memory from MEM_ROOT
 
20
 
 
21
  MEM_ROOT has no realloc() so this is supposed to be used for cases when
 
22
  reallocations are rare.
 
23
*/
 
24
 
 
25
template <class Elem> class Array
 
26
{
 
27
  enum {alloc_increment = 16};
 
28
  Elem **buffer;
 
29
  uint n_elements, max_element;
 
30
public:
 
31
  Array(MEM_ROOT *mem_root, uint prealloc=16)
 
32
  {
 
33
    buffer= (Elem**)alloc_root(mem_root, prealloc * sizeof(Elem**));
 
34
    max_element = buffer? prealloc : 0;
 
35
    n_elements= 0;
 
36
  }
 
37
 
 
38
  Elem& at(int idx)
 
39
  {
 
40
    return *(((Elem*)buffer) + idx);
 
41
  }
 
42
 
 
43
  Elem **front()
 
44
  {
 
45
    return buffer;
 
46
  }
 
47
 
 
48
  Elem **back()
 
49
  {
 
50
    return buffer + n_elements;
 
51
  }
 
52
 
 
53
  bool append(MEM_ROOT *mem_root, Elem *el)
 
54
  {
 
55
    if (n_elements == max_element)
 
56
    {
 
57
      Elem **newbuf;
 
58
      if (!(newbuf= (Elem**)alloc_root(mem_root, (n_elements + alloc_increment)*
 
59
                                                  sizeof(Elem**))))
 
60
      {
 
61
        return false;
 
62
      }
 
63
      memcpy(newbuf, buffer, n_elements*sizeof(Elem*));
 
64
      buffer= newbuf;
 
65
    }
 
66
    buffer[n_elements++]= el;
 
67
    return false;
 
68
  }
 
69
 
 
70
  int elements()
 
71
  {
 
72
    return n_elements;
 
73
  }
 
74
 
 
75
  void clear()
 
76
  {
 
77
    n_elements= 0;
 
78
  }
 
79
 
 
80
  typedef int (*CMP_FUNC)(Elem * const *el1, Elem *const *el2);
 
81
 
 
82
  void sort(CMP_FUNC cmp_func)
 
83
  {
 
84
    my_qsort(buffer, n_elements, sizeof(Elem*), (qsort_cmp)cmp_func);
 
85
  }
 
86
};
 
87