1
0
mirror of https://github.com/MariaDB/server.git synced 2025-07-27 18:02:13 +03:00

Finished merging wl5986 started by Igor.

This commit is contained in:
unknown
2013-06-19 14:32:14 +03:00
parent 2534521f9a
commit dfcc502ab5
21 changed files with 1814 additions and 1849 deletions

View File

@ -99,29 +99,65 @@ template <class Elem> class Dynamic_array
DYNAMIC_ARRAY 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;
}
Elem *back()
/// @returns pointer to first element; undefined behaviour if array is empty
const Elem *front() const
{
return ((Elem*)array.buffer) + array.elements;
DBUG_ASSERT(array.elements >= 1);
return (const Elem*)array.buffer;
}
bool append(Elem &el)
/// @returns pointer to last element; undefined behaviour if array is empty.
Elem *back()
{
return (insert_dynamic(&array, (uchar*)&el));
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);
}
/**
@retval false ok
@retval true OOM, @c my_error() has been called.
*/
bool append(const Elem &el)
{
return insert_dynamic(&array, &el);
}
/// Pops the last element. Does nothing if array is empty.
@ -135,11 +171,27 @@ public:
delete_dynamic_element(&array, idx);
}
int elements()
int elements() const
{
return array.elements;
}
void elements(uint num_elements)
{
DBUG_ASSERT(num_elements <= array.max_element);
array.elements= num_elements;
}
void clear()
{
elements(0);
}
void set(uint idx, const Elem &el)
{
set_dynamic(&array, &el, idx);
}
~Dynamic_array()
{
delete_dynamic(&array);
@ -153,74 +205,4 @@ public:
}
};
/*
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()
{
return buffer + n_elements;
}
bool append(MEM_ROOT *mem_root, Elem *el)
{
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;
}
int elements()
{
return n_elements;
}
void clear()
{
n_elements= 0;
}
typedef int (*CMP_FUNC)(Elem * const *el1, Elem *const *el2);
void sort(CMP_FUNC cmp_func)
{
my_qsort(buffer, n_elements, sizeof(Elem*), (qsort_cmp)cmp_func);
}
};
#endif /* SQL_ARRAY_INCLUDED */