~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
 *
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
 */
1 by brian
clean slate
19
575.4.7 by Monty Taylor
More header cleanup.
20
#ifndef DRIZZLED_SQL_ARRAY_H
21
#define DRIZZLED_SQL_ARRAY_H
22
212.5.13 by Monty Taylor
Moved my_sys/my_pthread/my_nosys and mysys_err to mysys.
23
#include <mysys/my_sys.h>
1 by brian
clean slate
24
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
25
/*
1 by brian
clean slate
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;
482 by Brian Aker
Remove uint.
36
  uint32_t n_elements, max_element;
1 by brian
clean slate
37
public:
482 by Brian Aker
Remove uint.
38
  Array(MEM_ROOT *mem_root, uint32_t prealloc=16)
1 by brian
clean slate
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
      {
51.1.48 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
68
        return false;
1 by brian
clean slate
69
      }
70
      memcpy(newbuf, buffer, n_elements*sizeof(Elem*));
71
      buffer= newbuf;
72
    }
73
    buffer[n_elements++]= el;
51.1.48 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
74
    return false;
1 by brian
clean slate
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
575.4.7 by Monty Taylor
More header cleanup.
95
#endif /* DRIZZLED_SQL_ARRAY_H */