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
#include <mysys/my_sys.h>
23
Array of pointers to Elem that uses memory from MEM_ROOT
25
MEM_ROOT has no realloc() so this is supposed to be used for cases when
26
reallocations are rare.
29
template <class Elem> class Array
31
enum {alloc_increment = 16};
33
uint32_t n_elements, max_element;
35
Array(MEM_ROOT *mem_root, uint32_t prealloc=16)
37
buffer= (Elem**)alloc_root(mem_root, prealloc * sizeof(Elem**));
38
max_element = buffer? prealloc : 0;
44
return *(((Elem*)buffer) + idx);
54
return buffer + n_elements;
57
bool append(MEM_ROOT *mem_root, Elem *el)
59
if (n_elements == max_element)
62
if (!(newbuf= (Elem**)alloc_root(mem_root, (n_elements + alloc_increment)*
67
memcpy(newbuf, buffer, n_elements*sizeof(Elem*));
70
buffer[n_elements++]= el;
84
typedef int (*CMP_FUNC)(Elem * const *el1, Elem *const *el2);
86
void sort(CMP_FUNC cmp_func)
88
my_qsort(buffer, n_elements, sizeof(Elem*), (qsort_cmp)cmp_func);