summaryrefslogtreecommitdiff
path: root/sql/sql_array.h
diff options
context:
space:
mode:
Diffstat (limited to 'sql/sql_array.h')
-rw-r--r--sql/sql_array.h124
1 files changed, 57 insertions, 67 deletions
diff --git a/sql/sql_array.h b/sql/sql_array.h
index 43ca4ef4219..697819787f2 100644
--- a/sql/sql_array.h
+++ b/sql/sql_array.h
@@ -92,6 +92,8 @@ private:
/*
A typesafe wrapper around DYNAMIC_ARRAY
+
+ TODO: Change creator to take a THREAD_SPECIFIC option.
*/
template <class Elem> class Dynamic_array
@@ -100,125 +102,113 @@ 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));
+ MYF(0));
}
+ /**
+ @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(size_t idx)
{
return *(((Elem*)array.buffer) + idx);
}
-
- Elem *front()
+ /// Const variant of at(), which cannot change data
+ const Elem& at(size_t idx) const
{
- return (Elem*)array.buffer;
- }
-
- Elem *back()
- {
- return ((Elem*)array.buffer) + array.elements;
+ return *(((Elem*)array.buffer) + idx);
}
- bool append(Elem &el)
+ /// @returns pointer to first element; undefined behaviour if array is empty
+ Elem *front()
{
- return (insert_dynamic(&array, (uchar*)&el));
+ DBUG_ASSERT(array.elements >= 1);
+ return (Elem*)array.buffer;
}
- bool append_val(Elem el)
+ /// @returns pointer to first element; undefined behaviour if array is empty
+ const Elem *front() const
{
- return (insert_dynamic(&array, (uchar*)&el));
+ DBUG_ASSERT(array.elements >= 1);
+ return (const Elem*)array.buffer;
}
- size_t elements()
+ /// @returns pointer to last element; undefined behaviour if array is empty.
+ Elem *back()
{
- return array.elements;
+ DBUG_ASSERT(array.elements >= 1);
+ return ((Elem*)array.buffer) + (array.elements - 1);
}
- void set_elements(size_t n)
+ /// @returns pointer to last element; undefined behaviour if array is empty.
+ const Elem *back() const
{
- array.elements= n;
+ DBUG_ASSERT(array.elements >= 1);
+ return ((const Elem*)array.buffer) + (array.elements - 1);
}
- ~Dynamic_array()
+ /**
+ @retval false ok
+ @retval true OOM, @c my_error() has been called.
+ */
+ bool append(const Elem &el)
{
- delete_dynamic(&array);
+ return insert_dynamic(&array, &el);
}
- typedef int (*CMP_FUNC)(const Elem *el1, const Elem *el2);
-
- void sort(CMP_FUNC cmp_func)
+ bool append_val(Elem el)
{
- my_qsort(array.buffer, array.elements, sizeof(Elem), (qsort_cmp)cmp_func);
+ return (insert_dynamic(&array, (uchar*)&el));
}
-};
-/*
- 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)
+ /// Pops the last element. Does nothing if array is empty.
+ Elem& pop()
{
- buffer= (Elem**)alloc_root(mem_root, prealloc * sizeof(Elem**));
- max_element = buffer? prealloc : 0;
- n_elements= 0;
+ return *((Elem*)pop_dynamic(&array));
}
- Elem& at(int idx)
+ void del(uint idx)
{
- return *(((Elem*)buffer) + idx);
+ delete_dynamic_element(&array, idx);
}
- Elem **front()
+ size_t elements() const
{
- return buffer;
+ return array.elements;
}
- Elem **back()
+ void elements(size_t 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);
}
};