summaryrefslogtreecommitdiff
path: root/sql/sql_array.h
diff options
context:
space:
mode:
authorSergei Golubchik <sergii@pisem.net>2013-07-21 16:39:19 +0200
committerSergei Golubchik <sergii@pisem.net>2013-07-21 16:39:19 +0200
commitb7b5f6f1ab49948b0e15b762266d4640b3d6b7fb (patch)
tree7c302c2025184dbd053aa6135f0ff28c8ce6f359 /sql/sql_array.h
parent5f6380adde2dac3f32b40339b9b702c0135eb7d6 (diff)
parentc1d6a2d7e194225ccc19a68ea5d0f368632620d0 (diff)
downloadmariadb-git-b7b5f6f1ab49948b0e15b762266d4640b3d6b7fb.tar.gz
10.0-monty merge
includes: * remove some remnants of "Bug#14521864: MYSQL 5.1 TO 5.5 BUGS PARTITIONING" * introduce LOCK_share, now LOCK_ha_data is strictly for engines * rea_create_table() always creates .par file (even in "frm-only" mode) * fix a 5.6 bug, temp file leak on dummy ALTER TABLE
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);
}
};