1
/* Copyright (C) 2003 MySQL AB
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.
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.
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 */
16
#include <mysys/my_sys.h>
19
Array of pointers to Elem that uses memory from MEM_ROOT
21
MEM_ROOT has no realloc() so this is supposed to be used for cases when
22
reallocations are rare.
25
template <class Elem> class Array
27
enum {alloc_increment = 16};
29
uint n_elements, max_element;
31
Array(MEM_ROOT *mem_root, uint prealloc=16)
33
buffer= (Elem**)alloc_root(mem_root, prealloc * sizeof(Elem**));
34
max_element = buffer? prealloc : 0;
40
return *(((Elem*)buffer) + idx);
50
return buffer + n_elements;
53
bool append(MEM_ROOT *mem_root, Elem *el)
55
if (n_elements == max_element)
58
if (!(newbuf= (Elem**)alloc_root(mem_root, (n_elements + alloc_increment)*
63
memcpy(newbuf, buffer, n_elements*sizeof(Elem*));
66
buffer[n_elements++]= el;
80
typedef int (*CMP_FUNC)(Elem * const *el1, Elem *const *el2);
82
void sort(CMP_FUNC cmp_func)
84
my_qsort(buffer, n_elements, sizeof(Elem*), (qsort_cmp)cmp_func);