~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_array.h

  • Committer: Brian Aker
  • Date: 2009-08-18 07:20:29 UTC
  • mfrom: (1117.1.9 merge)
  • Revision ID: brian@gaz-20090818072029-s9ch5lcmltxwidn7
Merge of Brian

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