My Project
sql_array.h
00001 #ifndef SQL_ARRAY_INCLUDED
00002 #define SQL_ARRAY_INCLUDED
00003 
00004 /* Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; version 2 of the License.
00009 
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013    GNU General Public License for more details.
00014 
00015    You should have received a copy of the GNU General Public License
00016    along with this program; if not, write to the Free Software Foundation,
00017    51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */
00018 
00019 #include <my_sys.h>
00020 
00033 template <typename Element_type> class Bounds_checked_array
00034 {
00035 public:
00036   Bounds_checked_array() : m_array(NULL), m_size(0) {}
00037 
00038   Bounds_checked_array(Element_type *el, size_t size)
00039     : m_array(el), m_size(size)
00040   {}
00041 
00042   void reset() { m_array= NULL; m_size= 0; }
00043  
00044   void reset(Element_type *array, size_t size)
00045   {
00046     m_array= array;
00047     m_size= size;
00048   }
00049 
00055   void resize(size_t new_size)
00056   {
00057     DBUG_ASSERT(new_size <= m_size);
00058     m_size= new_size;
00059   }
00060 
00061   Element_type &operator[](size_t n)
00062   {
00063     DBUG_ASSERT(n < m_size);
00064     return m_array[n];
00065   }
00066 
00067   const Element_type &operator[](size_t n) const
00068   {
00069     DBUG_ASSERT(n < m_size);
00070     return m_array[n];
00071   }
00072 
00073   size_t element_size() const { return sizeof(Element_type); }
00074   size_t size() const         { return m_size; }
00075 
00076   bool is_null() const { return m_array == NULL; }
00077 
00078   void pop_front()
00079   {
00080     DBUG_ASSERT(m_size > 0);
00081     m_array+= 1;
00082     m_size-= 1;
00083   }
00084 
00085   Element_type *array() const { return m_array; }
00086 
00087   bool operator==(const Bounds_checked_array<Element_type>&rhs) const
00088   {
00089     return m_array == rhs.m_array && m_size == rhs.m_size;
00090   }
00091   bool operator!=(const Bounds_checked_array<Element_type>&rhs) const
00092   {
00093     return m_array != rhs.m_array || m_size != rhs.m_size;
00094   }
00095 
00096 private:
00097   Element_type *m_array;
00098   size_t        m_size;
00099 };
00100 
00101 /*
00102   A typesafe wrapper around DYNAMIC_ARRAY
00103 */
00104 
00105 template <class Elem> class Dynamic_array
00106 {
00107   DYNAMIC_ARRAY  array;
00108 public:
00109   Dynamic_array(uint prealloc=16, uint increment=16)
00110   {
00111     init(prealloc, increment);
00112   }
00113 
00114   void init(uint prealloc=16, uint increment=16)
00115   {
00116     my_init_dynamic_array(&array, sizeof(Elem), prealloc, increment);
00117   }
00118 
00123   Elem& at(int idx)
00124   {
00125     return *(((Elem*)array.buffer) + idx);
00126   }
00128   const Elem& at(int idx) const
00129   {
00130     return *(((Elem*)array.buffer) + idx);
00131   }
00132 
00134   Elem *front()
00135   {
00136     DBUG_ASSERT(array.elements >= 1);
00137     return (Elem*)array.buffer;
00138   }
00139 
00141   const Elem *front() const
00142   {
00143     DBUG_ASSERT(array.elements >= 1);
00144     return (const Elem*)array.buffer;
00145   }
00146 
00148   Elem *back()
00149   {
00150     DBUG_ASSERT(array.elements >= 1);
00151     return ((Elem*)array.buffer) + (array.elements - 1);
00152   }
00153 
00155   const Elem *back() const
00156   {
00157     DBUG_ASSERT(array.elements >= 1);
00158     return ((const Elem*)array.buffer) + (array.elements - 1);
00159   }
00160 
00165   bool append(const Elem &el)
00166   {
00167     return insert_dynamic(&array, &el);
00168   }
00169 
00171   Elem& pop()
00172   {
00173     return *((Elem*)pop_dynamic(&array));
00174   }
00175 
00176   void del(uint idx)
00177   {
00178     delete_dynamic_element(&array, idx);
00179   }
00180 
00181   int elements() const
00182   {
00183     return array.elements;
00184   }
00185 
00186   void elements(uint num_elements)
00187   {
00188     DBUG_ASSERT(num_elements <= array.max_element);
00189     array.elements= num_elements;
00190   }
00191 
00192   void clear()
00193   {
00194     elements(0);
00195   }
00196 
00197   void set(uint idx, const Elem &el)
00198   {
00199     set_dynamic(&array, &el, idx);
00200   }
00201 
00202   ~Dynamic_array()
00203   {
00204     delete_dynamic(&array);
00205   }
00206 
00207   typedef int (*CMP_FUNC)(const Elem *el1, const Elem *el2);
00208 
00209   void sort(CMP_FUNC cmp_func)
00210   {
00211     my_qsort(array.buffer, array.elements, sizeof(Elem), (qsort_cmp)cmp_func);
00212   }
00213 };
00214 
00215 #endif /* SQL_ARRAY_INCLUDED */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines