~ubuntu-branches/ubuntu/trusty/mysql-5.6/trusty

« back to all changes in this revision

Viewing changes to sql/sql_array.h

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2014-02-12 11:54:27 UTC
  • Revision ID: package-import@ubuntu.com-20140212115427-oq6tfsqxl1wuwehi
Tags: upstream-5.6.15
ImportĀ upstreamĀ versionĀ 5.6.15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#ifndef SQL_ARRAY_INCLUDED
 
2
#define SQL_ARRAY_INCLUDED
 
3
 
 
4
/* Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.
 
5
 
 
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.
 
9
 
 
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.
 
14
 
 
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 */
 
18
 
 
19
#include <my_sys.h>
 
20
 
 
21
/**
 
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,
 
24
   and a length.
 
25
 
 
26
   @remark
 
27
   We want the compiler-generated versions of:
 
28
   - the copy CTOR (memberwise initialization)
 
29
   - the assignment operator (memberwise assignment)
 
30
 
 
31
   @param Element_type The type of the elements of the container.
 
32
 */
 
33
template <typename Element_type> class Bounds_checked_array
 
34
{
 
35
public:
 
36
  Bounds_checked_array() : m_array(NULL), m_size(0) {}
 
37
 
 
38
  Bounds_checked_array(Element_type *el, size_t size)
 
39
    : m_array(el), m_size(size)
 
40
  {}
 
41
 
 
42
  void reset() { m_array= NULL; m_size= 0; }
 
43
 
 
44
  void reset(Element_type *array, size_t size)
 
45
  {
 
46
    m_array= array;
 
47
    m_size= size;
 
48
  }
 
49
 
 
50
  /**
 
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
 
53
    current size.
 
54
   */
 
55
  void resize(size_t new_size)
 
56
  {
 
57
    DBUG_ASSERT(new_size <= m_size);
 
58
    m_size= new_size;
 
59
  }
 
60
 
 
61
  Element_type &operator[](size_t n)
 
62
  {
 
63
    DBUG_ASSERT(n < m_size);
 
64
    return m_array[n];
 
65
  }
 
66
 
 
67
  const Element_type &operator[](size_t n) const
 
68
  {
 
69
    DBUG_ASSERT(n < m_size);
 
70
    return m_array[n];
 
71
  }
 
72
 
 
73
  size_t element_size() const { return sizeof(Element_type); }
 
74
  size_t size() const         { return m_size; }
 
75
 
 
76
  bool is_null() const { return m_array == NULL; }
 
77
 
 
78
  void pop_front()
 
79
  {
 
80
    DBUG_ASSERT(m_size > 0);
 
81
    m_array+= 1;
 
82
    m_size-= 1;
 
83
  }
 
84
 
 
85
  Element_type *array() const { return m_array; }
 
86
 
 
87
  bool operator==(const Bounds_checked_array<Element_type>&rhs) const
 
88
  {
 
89
    return m_array == rhs.m_array && m_size == rhs.m_size;
 
90
  }
 
91
  bool operator!=(const Bounds_checked_array<Element_type>&rhs) const
 
92
  {
 
93
    return m_array != rhs.m_array || m_size != rhs.m_size;
 
94
  }
 
95
 
 
96
private:
 
97
  Element_type *m_array;
 
98
  size_t        m_size;
 
99
};
 
100
 
 
101
/*
 
102
  A typesafe wrapper around DYNAMIC_ARRAY
 
103
*/
 
104
 
 
105
template <class Elem> class Dynamic_array
 
106
{
 
107
  DYNAMIC_ARRAY  array;
 
108
public:
 
109
  Dynamic_array(uint prealloc=16, uint increment=16)
 
110
  {
 
111
    init(prealloc, increment);
 
112
  }
 
113
 
 
114
  void init(uint prealloc=16, uint increment=16)
 
115
  {
 
116
    my_init_dynamic_array(&array, sizeof(Elem), prealloc, increment);
 
117
  }
 
118
 
 
119
  /**
 
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.
 
122
  */
 
123
  Elem& at(int idx)
 
124
  {
 
125
    return *(((Elem*)array.buffer) + idx);
 
126
  }
 
127
  /// Const variant of at(), which cannot change data
 
128
  const Elem& at(int idx) const
 
129
  {
 
130
    return *(((Elem*)array.buffer) + idx);
 
131
  }
 
132
 
 
133
  /// @returns pointer to first element; undefined behaviour if array is empty
 
134
  Elem *front()
 
135
  {
 
136
    DBUG_ASSERT(array.elements >= 1);
 
137
    return (Elem*)array.buffer;
 
138
  }
 
139
 
 
140
  /// @returns pointer to first element; undefined behaviour if array is empty
 
141
  const Elem *front() const
 
142
  {
 
143
    DBUG_ASSERT(array.elements >= 1);
 
144
    return (const Elem*)array.buffer;
 
145
  }
 
146
 
 
147
  /// @returns pointer to last element; undefined behaviour if array is empty.
 
148
  Elem *back()
 
149
  {
 
150
    DBUG_ASSERT(array.elements >= 1);
 
151
    return ((Elem*)array.buffer) + (array.elements - 1);
 
152
  }
 
153
 
 
154
  /// @returns pointer to last element; undefined behaviour if array is empty.
 
155
  const Elem *back() const
 
156
  {
 
157
    DBUG_ASSERT(array.elements >= 1);
 
158
    return ((const Elem*)array.buffer) + (array.elements - 1);
 
159
  }
 
160
 
 
161
  /**
 
162
     @retval false ok
 
163
     @retval true  OOM, @c my_error() has been called.
 
164
  */
 
165
  bool append(const Elem &el)
 
166
  {
 
167
    return insert_dynamic(&array, &el);
 
168
  }
 
169
 
 
170
  /// Pops the last element. Does nothing if array is empty.
 
171
  Elem& pop()
 
172
  {
 
173
    return *((Elem*)pop_dynamic(&array));
 
174
  }
 
175
 
 
176
  void del(uint idx)
 
177
  {
 
178
    delete_dynamic_element(&array, idx);
 
179
  }
 
180
 
 
181
  int elements() const
 
182
  {
 
183
    return array.elements;
 
184
  }
 
185
 
 
186
  void elements(uint num_elements)
 
187
  {
 
188
    DBUG_ASSERT(num_elements <= array.max_element);
 
189
    array.elements= num_elements;
 
190
  }
 
191
 
 
192
  void clear()
 
193
  {
 
194
    elements(0);
 
195
  }
 
196
 
 
197
  void set(uint idx, const Elem &el)
 
198
  {
 
199
    set_dynamic(&array, &el, idx);
 
200
  }
 
201
 
 
202
  ~Dynamic_array()
 
203
  {
 
204
    delete_dynamic(&array);
 
205
  }
 
206
 
 
207
  typedef int (*CMP_FUNC)(const Elem *el1, const Elem *el2);
 
208
 
 
209
  void sort(CMP_FUNC cmp_func)
 
210
  {
 
211
    my_qsort(array.buffer, array.elements, sizeof(Elem), (qsort_cmp)cmp_func);
 
212
  }
 
213
};
 
214
 
 
215
#endif /* SQL_ARRAY_INCLUDED */