diff options
author | unknown <sanja@montyprogram.com> | 2013-06-19 14:32:14 +0300 |
---|---|---|
committer | unknown <sanja@montyprogram.com> | 2013-06-19 14:32:14 +0300 |
commit | dfcc502ab540b4d93fe3d40d0bac15fa3ae449dd (patch) | |
tree | ab179c039dab8cb251a20efcdd65f96a1eabf9a6 /sql/sql_array.h | |
parent | 2534521f9a7d66b48cb9ca9402e82a0c58b156d8 (diff) | |
download | mariadb-git-dfcc502ab540b4d93fe3d40d0bac15fa3ae449dd.tar.gz |
Finished merging wl5986 started by Igor.
Diffstat (limited to 'sql/sql_array.h')
-rw-r--r-- | sql/sql_array.h | 120 |
1 files changed, 51 insertions, 69 deletions
diff --git a/sql/sql_array.h b/sql/sql_array.h index 9ac27cca63b..baed3687215 100644 --- a/sql/sql_array.h +++ b/sql/sql_array.h @@ -100,28 +100,64 @@ template <class Elem> class Dynamic_array public: Dynamic_array(uint prealloc=16, uint increment=16) { + init(prealloc, increment); + } + + void init(uint prealloc=16, uint increment=16) + { my_init_dynamic_array(&array, sizeof(Elem), prealloc, increment, MYF(MY_THREAD_SPECIFIC)); } + /** + @note Though formally this could be declared "const" it would be + misleading at it returns a non-const pointer to array's data. + */ Elem& at(int idx) { return *(((Elem*)array.buffer) + idx); } + /// Const variant of at(), which cannot change data + const Elem& at(int idx) const + { + return *(((Elem*)array.buffer) + idx); + } + /// @returns pointer to first element; undefined behaviour if array is empty Elem *front() { + DBUG_ASSERT(array.elements >= 1); return (Elem*)array.buffer; } + /// @returns pointer to first element; undefined behaviour if array is empty + const Elem *front() const + { + DBUG_ASSERT(array.elements >= 1); + return (const Elem*)array.buffer; + } + + /// @returns pointer to last element; undefined behaviour if array is empty. Elem *back() { - return ((Elem*)array.buffer) + array.elements; + DBUG_ASSERT(array.elements >= 1); + return ((Elem*)array.buffer) + (array.elements - 1); + } + + /// @returns pointer to last element; undefined behaviour if array is empty. + const Elem *back() const + { + DBUG_ASSERT(array.elements >= 1); + return ((const Elem*)array.buffer) + (array.elements - 1); } - bool append(Elem &el) + /** + @retval false ok + @retval true OOM, @c my_error() has been called. + */ + bool append(const Elem &el) { - return (insert_dynamic(&array, (uchar*)&el)); + return insert_dynamic(&array, &el); } /// Pops the last element. Does nothing if array is empty. @@ -135,91 +171,37 @@ public: delete_dynamic_element(&array, idx); } - int elements() + int elements() const { return array.elements; } - ~Dynamic_array() - { - delete_dynamic(&array); - } - - typedef int (*CMP_FUNC)(const Elem *el1, const Elem *el2); - - void sort(CMP_FUNC cmp_func) - { - my_qsort(array.buffer, array.elements, sizeof(Elem), (qsort_cmp)cmp_func); - } -}; - -/* - Array of pointers to Elem that uses memory from MEM_ROOT - - MEM_ROOT has no realloc() so this is supposed to be used for cases when - reallocations are rare. -*/ - -template <class Elem> class Array -{ - enum {alloc_increment = 16}; - Elem **buffer; - uint n_elements, max_element; -public: - Array(MEM_ROOT *mem_root, uint prealloc=16) - { - buffer= (Elem**)alloc_root(mem_root, prealloc * sizeof(Elem**)); - max_element = buffer? prealloc : 0; - n_elements= 0; - } - - Elem& at(int idx) - { - return *(((Elem*)buffer) + idx); - } - - Elem **front() - { - return buffer; - } - - Elem **back() + void elements(uint num_elements) { - return buffer + n_elements; + DBUG_ASSERT(num_elements <= array.max_element); + array.elements= num_elements; } - bool append(MEM_ROOT *mem_root, Elem *el) + void clear() { - if (n_elements == max_element) - { - Elem **newbuf; - if (!(newbuf= (Elem**)alloc_root(mem_root, (n_elements + alloc_increment)* - sizeof(Elem**)))) - { - return FALSE; - } - memcpy(newbuf, buffer, n_elements*sizeof(Elem*)); - buffer= newbuf; - } - buffer[n_elements++]= el; - return FALSE; + elements(0); } - int elements() + void set(uint idx, const Elem &el) { - return n_elements; + set_dynamic(&array, &el, idx); } - void clear() + ~Dynamic_array() { - n_elements= 0; + delete_dynamic(&array); } - typedef int (*CMP_FUNC)(Elem * const *el1, Elem *const *el2); + typedef int (*CMP_FUNC)(const Elem *el1, const Elem *el2); void sort(CMP_FUNC cmp_func) { - my_qsort(buffer, n_elements, sizeof(Elem*), (qsort_cmp)cmp_func); + my_qsort(array.buffer, array.elements, sizeof(Elem), (qsort_cmp)cmp_func); } }; |