17 #ifndef MEM_ROOT_ARRAY_INCLUDED
18 #define MEM_ROOT_ARRAY_INCLUDED
46 template<
typename Element_type,
bool has_trivial_destructor>
51 : m_root(root), m_array(NULL), m_size(0), m_capacity(0)
53 DBUG_ASSERT(m_root != NULL);
61 Element_type &at(
size_t n)
63 DBUG_ASSERT(n < size());
67 const Element_type &at(
size_t n)
const
69 DBUG_ASSERT(n < size());
74 Element_type *begin() {
return &m_array[0]; }
77 Element_type *end() {
return &m_array[size()]; }
90 void chop(
const size_t pos)
92 DBUG_ASSERT(pos < m_size);
93 if (!has_trivial_destructor)
95 for (
size_t ix= pos; ix < m_size; ++ix)
97 Element_type *p= &m_array[ix];
111 bool reserve(
size_t n)
116 void *mem= alloc_root(m_root, n * element_size());
119 Element_type *array=
static_cast<Element_type*
>(mem);
122 for (
size_t ix= 0; ix < m_size; ++ix)
124 Element_type *new_p= &array[ix];
125 Element_type *old_p= &m_array[ix];
126 new (new_p) Element_type(*old_p);
127 if (!has_trivial_destructor)
128 old_p->~Element_type();
145 bool push_back(
const Element_type &element)
147 const size_t min_capacity= 20;
148 const size_t expansion_factor= 2;
149 if (0 == m_capacity && reserve(min_capacity))
151 if (m_size == m_capacity && reserve(m_capacity * expansion_factor))
153 Element_type *p= &m_array[m_size++];
154 new (p) Element_type(element);
158 size_t capacity()
const {
return m_capacity; }
159 size_t element_size()
const {
return sizeof(Element_type); }
160 bool empty()
const {
return size() == 0; }
161 size_t size()
const {
return m_size; }
165 Element_type *m_array;
175 #endif // MEM_ROOT_ARRAY_INCLUDED