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 */
19
A typesafe wrapper around DYNAMIC_ARRAY
22
template <class Elem> class Dynamic_array
26
Dynamic_array(uint prealloc=16, uint increment=16)
28
init(prealloc, increment);
31
void init(uint prealloc=16, uint increment=16)
33
my_init_dynamic_array(&array, sizeof(Elem), prealloc, increment);
38
return *(((Elem*)array.buffer) + idx);
43
return (Elem*)array.buffer;
48
return ((Elem*)array.buffer) + array.elements;
53
return (insert_dynamic(&array, (uchar*)&el));
58
return array.elements;
68
delete_dynamic(&array);
71
typedef int (*CMP_FUNC)(const Elem *el1, const Elem *el2);
73
void sort(CMP_FUNC cmp_func)
75
my_qsort(array.buffer, array.elements, sizeof(Elem), (qsort_cmp)cmp_func);
81
Array of pointers to Elem that uses memory from MEM_ROOT
83
MEM_ROOT has no realloc() so this is supposed to be used for cases when
84
reallocations are rare.
87
template <class Elem> class Array
89
enum {alloc_increment = 16};
91
uint n_elements, max_element;
93
Array(MEM_ROOT *mem_root, uint prealloc=16)
95
buffer= (Elem**)alloc_root(mem_root, prealloc * sizeof(Elem**));
96
max_element = buffer? prealloc : 0;
102
return *(((Elem*)buffer) + idx);
112
return buffer + n_elements;
115
bool append(MEM_ROOT *mem_root, Elem *el)
117
if (n_elements == max_element)
120
if (!(newbuf= (Elem**)alloc_root(mem_root, (n_elements + alloc_increment)*
125
memcpy(newbuf, buffer, n_elements*sizeof(Elem*));
128
buffer[n_elements++]= el;
142
typedef int (*CMP_FUNC)(Elem * const *el1, Elem *const *el2);
144
void sort(CMP_FUNC cmp_func)
146
my_qsort(buffer, n_elements, sizeof(Elem*), (qsort_cmp)cmp_func);