1
#ifndef SQL_ARRAY_INCLUDED
2
#define SQL_ARRAY_INCLUDED
4
/* Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.
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 Foundation,
17
51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */
22
A wrapper class which provides array bounds checking.
23
We do *not* own the array, we simply have a pointer to the first element,
27
We want the compiler-generated versions of:
28
- the copy CTOR (memberwise initialization)
29
- the assignment operator (memberwise assignment)
31
@param Element_type The type of the elements of the container.
33
template <typename Element_type> class Bounds_checked_array
36
Bounds_checked_array() : m_array(NULL), m_size(0) {}
38
Bounds_checked_array(Element_type *el, size_t size)
39
: m_array(el), m_size(size)
42
void reset() { m_array= NULL; m_size= 0; }
44
void reset(Element_type *array, size_t size)
51
Set a new bound on the array. Does not resize the underlying
52
array, so the new size must be smaller than or equal to the
55
void resize(size_t new_size)
57
DBUG_ASSERT(new_size <= m_size);
61
Element_type &operator[](size_t n)
63
DBUG_ASSERT(n < m_size);
67
const Element_type &operator[](size_t n) const
69
DBUG_ASSERT(n < m_size);
73
size_t element_size() const { return sizeof(Element_type); }
74
size_t size() const { return m_size; }
76
bool is_null() const { return m_array == NULL; }
80
DBUG_ASSERT(m_size > 0);
85
Element_type *array() const { return m_array; }
87
bool operator==(const Bounds_checked_array<Element_type>&rhs) const
89
return m_array == rhs.m_array && m_size == rhs.m_size;
91
bool operator!=(const Bounds_checked_array<Element_type>&rhs) const
93
return m_array != rhs.m_array || m_size != rhs.m_size;
97
Element_type *m_array;
102
A typesafe wrapper around DYNAMIC_ARRAY
105
template <class Elem> class Dynamic_array
109
Dynamic_array(uint prealloc=16, uint increment=16)
111
init(prealloc, increment);
114
void init(uint prealloc=16, uint increment=16)
116
my_init_dynamic_array(&array, sizeof(Elem), prealloc, increment);
120
@note Though formally this could be declared "const" it would be
121
misleading at it returns a non-const pointer to array's data.
125
return *(((Elem*)array.buffer) + idx);
127
/// Const variant of at(), which cannot change data
128
const Elem& at(int idx) const
130
return *(((Elem*)array.buffer) + idx);
133
/// @returns pointer to first element; undefined behaviour if array is empty
136
DBUG_ASSERT(array.elements >= 1);
137
return (Elem*)array.buffer;
140
/// @returns pointer to first element; undefined behaviour if array is empty
141
const Elem *front() const
143
DBUG_ASSERT(array.elements >= 1);
144
return (const Elem*)array.buffer;
147
/// @returns pointer to last element; undefined behaviour if array is empty.
150
DBUG_ASSERT(array.elements >= 1);
151
return ((Elem*)array.buffer) + (array.elements - 1);
154
/// @returns pointer to last element; undefined behaviour if array is empty.
155
const Elem *back() const
157
DBUG_ASSERT(array.elements >= 1);
158
return ((const Elem*)array.buffer) + (array.elements - 1);
163
@retval true OOM, @c my_error() has been called.
165
bool append(const Elem &el)
167
return insert_dynamic(&array, &el);
170
/// Pops the last element. Does nothing if array is empty.
173
return *((Elem*)pop_dynamic(&array));
178
delete_dynamic_element(&array, idx);
183
return array.elements;
186
void elements(uint num_elements)
188
DBUG_ASSERT(num_elements <= array.max_element);
189
array.elements= num_elements;
197
void set(uint idx, const Elem &el)
199
set_dynamic(&array, &el, idx);
204
delete_dynamic(&array);
207
typedef int (*CMP_FUNC)(const Elem *el1, const Elem *el2);
209
void sort(CMP_FUNC cmp_func)
211
my_qsort(array.buffer, array.elements, sizeof(Elem), (qsort_cmp)cmp_func);
215
#endif /* SQL_ARRAY_INCLUDED */