1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008 Sun Microsystems
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.
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.
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
20
#ifndef DRIZZLED_SQL_ARRAY_H
21
#define DRIZZLED_SQL_ARRAY_H
23
#include <mysys/my_sys.h>
26
Array of pointers to Elem that uses memory from MEM_ROOT
28
MEM_ROOT has no realloc() so this is supposed to be used for cases when
29
reallocations are rare.
32
template <class Elem> class Array
34
enum {alloc_increment = 16};
36
uint32_t n_elements, max_element;
38
Array(MEM_ROOT *mem_root, uint32_t prealloc=16)
40
buffer= (Elem**)alloc_root(mem_root, prealloc * sizeof(Elem**));
41
max_element = buffer? prealloc : 0;
47
return *(((Elem*)buffer) + idx);
57
return buffer + n_elements;
60
bool append(MEM_ROOT *mem_root, Elem *el)
62
if (n_elements == max_element)
65
if (!(newbuf= (Elem**)alloc_root(mem_root, (n_elements + alloc_increment)*
70
memcpy(newbuf, buffer, n_elements*sizeof(Elem*));
73
buffer[n_elements++]= el;
87
typedef int (*CMP_FUNC)(Elem * const *el1, Elem *const *el2);
89
void sort(CMP_FUNC cmp_func)
91
my_qsort(buffer, n_elements, sizeof(Elem*), (qsort_cmp)cmp_func);
95
#endif /* DRIZZLED_SQL_ARRAY_H */