MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Vector.hpp
1 /*
2  Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; version 2 of the License.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17 
18 #ifndef NDB_VECTOR_HPP
19 #define NDB_VECTOR_HPP
20 
21 #include <ndb_global.h>
22 #include <portlib/NdbMutex.h>
23 
24 template<class T>
25 class Vector {
26 public:
27  Vector(int sz = 10);
28  ~Vector();
29 
30  T& operator[](unsigned i);
31  const T& operator[](unsigned i) const;
32  unsigned size() const { return m_size; };
33 
34  int push_back(const T &);
35  void push(const T&, unsigned pos);
36  T& set(T&, unsigned pos, T& fill_obj);
37  T& back();
38 
39  void erase(unsigned index);
40 
41  void clear();
42 
43  int fill(unsigned new_size, T & obj);
44 
45  Vector<T>& operator=(const Vector<T>&);
46 
48  Vector(const Vector&);
52  bool equal(const Vector<T>& obj) const;
53 
54  int assign(const T*, unsigned cnt);
55  int assign(const Vector<T>& obj) { return assign(obj.getBase(), obj.size());}
56 
57  T* getBase() { return m_items;}
58  const T* getBase() const { return m_items;}
59 private:
60  T * m_items;
61  unsigned m_size;
62  unsigned m_incSize;
63  unsigned m_arraySize;
64 };
65 
66 template<class T>
67 Vector<T>::Vector(int i){
68  m_items = new T[i];
69  if (m_items == NULL)
70  {
71  errno = ENOMEM;
72  m_size = 0;
73  m_arraySize = 0;
74  m_incSize = 0;
75  return;
76  }
77  m_size = 0;
78  m_arraySize = i;
79  m_incSize = 50;
80 }
81 
82 template<class T>
84  m_items(new T[src.m_size]),
85  m_size(src.m_size),
86  m_incSize(src.m_incSize),
87  m_arraySize(src.m_size)
88 
89 {
90  if (unlikely(m_items == NULL)){
91  errno = ENOMEM;
92  m_size = 0;
93  m_arraySize = 0;
94  m_incSize = 0;
95  return;
96  }
97  for(unsigned i = 0; i < m_size; i++){
98  m_items[i] = src.m_items[i];
99  }
100 }
101 
102 template<class T>
104  delete[] m_items;
105  // safety for placement new usage
106  m_items = 0;
107  m_size = 0;
108  m_arraySize = 0;
109 }
110 
111 template<class T>
112 T &
113 Vector<T>::operator[](unsigned i){
114  if(i >= m_size)
115  abort();
116  return m_items[i];
117 }
118 
119 template<class T>
120 const T &
121 Vector<T>::operator[](unsigned i) const {
122  if(i >= m_size)
123  abort();
124  return m_items[i];
125 }
126 
127 template<class T>
128 T &
130  return (* this)[m_size - 1];
131 }
132 
133 template<class T>
134 int
135 Vector<T>::push_back(const T & t){
136  if(m_size == m_arraySize){
137  T * tmp = new T [m_arraySize + m_incSize];
138  if(tmp == NULL)
139  {
140  errno = ENOMEM;
141  return -1;
142  }
143  for (unsigned k = 0; k < m_size; k++)
144  tmp[k] = m_items[k];
145  delete[] m_items;
146  m_items = tmp;
147  m_arraySize = m_arraySize + m_incSize;
148  }
149  m_items[m_size] = t;
150  m_size++;
151  return 0;
152 }
153 
154 template<class T>
155 void
156 Vector<T>::push(const T & t, unsigned pos)
157 {
158  push_back(t);
159  if (pos < m_size - 1)
160  {
161  for(unsigned i = m_size - 1; i > pos; i--)
162  {
163  m_items[i] = m_items[i-1];
164  }
165  m_items[pos] = t;
166  }
167 }
168 
169 template<class T>
170 T&
171 Vector<T>::set(T & t, unsigned pos, T& fill_obj)
172 {
173  fill(pos, fill_obj);
174  T& ret = m_items[pos];
175  m_items[pos] = t;
176  return ret;
177 }
178 
179 template<class T>
180 void
181 Vector<T>::erase(unsigned i){
182  if(i >= m_size)
183  abort();
184 
185  for (unsigned k = i; k + 1 < m_size; k++)
186  m_items[k] = m_items[k + 1];
187  m_size--;
188 }
189 
190 template<class T>
191 void
193  m_size = 0;
194 }
195 
196 template<class T>
197 int
198 Vector<T>::fill(unsigned new_size, T & obj){
199  while(m_size <= new_size)
200  if (push_back(obj))
201  return -1;
202  return 0;
203 }
204 
205 template<class T>
206 Vector<T>&
207 Vector<T>::operator=(const Vector<T>& obj){
208  if(this != &obj){
209  clear();
210  for(unsigned i = 0; i<obj.size(); i++){
211  push_back(obj[i]);
212  }
213  }
214  return * this;
215 }
216 
217 template<class T>
218 int
219 Vector<T>::assign(const T* src, unsigned cnt)
220 {
221  clear();
222  for (unsigned i = 0; i<cnt; i++)
223  {
224  int ret;
225  if ((ret = push_back(src[i])))
226  return ret;
227  }
228  return 0;
229 }
230 
231 template<class T>
232 bool
233 Vector<T>::equal(const Vector<T>& obj) const
234 {
235  if (size() != obj.size())
236  return false;
237 
238  return memcmp(getBase(), obj.getBase(), size() * sizeof(T)) == 0;
239 }
240 
241 template<class T>
242 class MutexVector : public NdbLockable {
243 public:
244  MutexVector(int sz = 10);
245  ~MutexVector();
246 
247  T& operator[](unsigned i);
248  const T& operator[](unsigned i) const;
249  unsigned size() const { return m_size; };
250 
251  int push_back(const T &);
252  int push_back(const T &, bool lockMutex);
253  T& back();
254 
255  void erase(unsigned index);
256  void erase(unsigned index, bool lockMutex);
257 
258  void clear();
259  void clear(bool lockMutex);
260 
261  int fill(unsigned new_size, T & obj);
262 private:
263  T * m_items;
264  unsigned m_size;
265  unsigned m_incSize;
266  unsigned m_arraySize;
267 };
268 
269 template<class T>
271  m_items = new T[i];
272  if (m_items == NULL)
273  {
274  errno = ENOMEM;
275  m_size = 0;
276  m_arraySize = 0;
277  m_incSize = 0;
278  return;
279  }
280  m_size = 0;
281  m_arraySize = i;
282  m_incSize = 50;
283 }
284 
285 template<class T>
287  delete[] m_items;
288  // safety for placement new usage
289  m_items = 0;
290  m_size = 0;
291  m_arraySize = 0;
292 }
293 
294 template<class T>
295 T &
296 MutexVector<T>::operator[](unsigned i){
297  if(i >= m_size)
298  abort();
299  return m_items[i];
300 }
301 
302 template<class T>
303 const T &
304 MutexVector<T>::operator[](unsigned i) const {
305  if(i >= m_size)
306  abort();
307  return m_items[i];
308 }
309 
310 template<class T>
311 T &
313  return (* this)[m_size - 1];
314 }
315 
316 template<class T>
317 int
318 MutexVector<T>::push_back(const T & t){
319  lock();
320  if(m_size == m_arraySize){
321  T * tmp = new T [m_arraySize + m_incSize];
322  if (tmp == NULL)
323  {
324  errno = ENOMEM;
325  unlock();
326  return -1;
327  }
328  for (unsigned k = 0; k < m_size; k++)
329  tmp[k] = m_items[k];
330  delete[] m_items;
331  m_items = tmp;
332  m_arraySize = m_arraySize + m_incSize;
333  }
334  m_items[m_size] = t;
335  m_size++;
336  unlock();
337  return 0;
338 }
339 
340 template<class T>
341 int
342 MutexVector<T>::push_back(const T & t, bool lockMutex){
343  if(lockMutex)
344  lock();
345  if(m_size == m_arraySize){
346  T * tmp = new T [m_arraySize + m_incSize];
347  if (tmp == NULL)
348  {
349  errno = ENOMEM;
350  if(lockMutex)
351  unlock();
352  return -1;
353  }
354  for (unsigned k = 0; k < m_size; k++)
355  tmp[k] = m_items[k];
356  delete[] m_items;
357  m_items = tmp;
358  m_arraySize = m_arraySize + m_incSize;
359  }
360  m_items[m_size] = t;
361  m_size++;
362  if(lockMutex)
363  unlock();
364  return 0;
365 }
366 
367 template<class T>
368 void
369 MutexVector<T>::erase(unsigned i){
370  if(i >= m_size)
371  abort();
372 
373  lock();
374  for (unsigned k = i; k + 1 < m_size; k++)
375  m_items[k] = m_items[k + 1];
376  m_size--;
377  unlock();
378 }
379 
380 template<class T>
381 void
382 MutexVector<T>::erase(unsigned i, bool _lock){
383  if(i >= m_size)
384  abort();
385 
386  if(_lock)
387  lock();
388  for (unsigned k = i; k + 1 < m_size; k++)
389  m_items[k] = m_items[k + 1];
390  m_size--;
391  if(_lock)
392  unlock();
393 }
394 
395 template<class T>
396 void
398  lock();
399  m_size = 0;
400  unlock();
401 }
402 
403 template<class T>
404 void
405 MutexVector<T>::clear(bool l){
406  if(l) lock();
407  m_size = 0;
408  if(l) unlock();
409 }
410 
411 template<class T>
412 int
413 MutexVector<T>::fill(unsigned new_size, T & obj){
414  while(m_size <= new_size)
415  if (push_back(obj))
416  return -1;
417  return 0;
418 }
419 
420 #endif