MySQL 8.3.0
Source Code Documentation
Inplace_vector< objtype, array_size > Class Template Reference

Utility container class to store elements stably and scalably. More...

#include <inplace_vector.h>

Public Member Functions

 Inplace_vector (PSI_memory_key psi_key)
 
 ~Inplace_vector ()
 Release memory space and destroy all contained objects. More...
 
objtype * get_object (size_t index)
 Get an existing element's pointer, index must be in [0, m_obj_count). More...
 
objtype * append_object ()
 Allocate space for an object, and construct it using its default constructor, and return its address. More...
 
objtype * push_back (const objtype &obj)
 STL std::vector::push_back interface. More...
 
bool resize (size_t new_size, const objtype &val=objtype())
 STL std::vector::resize interface. More...
 
size_t size () const
 STL std::vector::size interface. More...
 
size_t capacity () const
 STL std::vector::capacity interface. More...
 
bool empty () const
 STL std::vector::empty interface. More...
 
void clear ()
 STL std::vector::clear interface. More...
 
const objtype & back () const
 STL std::vector::back interface. More...
 
objtype & back ()
 STL std::vector::back interface. More...
 
const objtype & operator[] (size_t i) const
 STL std::vector::operator[] interface. More...
 
objtype & operator[] (size_t i)
 STL std::vector::operator[] interface. More...
 
void delete_all_objects ()
 Destroy all elements (by calling each element's destructor) stored in the vector, and then release all memory held by it. More...
 

Private Member Functions

objtype * get_space (size_t index)
 Return an existing used slot, or append exactly one slot at end of the last array and return it. More...
 
void append_new_array ()
 
 Inplace_vector (const Inplace_vector &)
 
Inplace_vectoroperator= (const Inplace_vector &rhs)
 

Private Attributes

std::vector< objtype * > m_obj_arrays
 
PSI_memory_key m_psi_key
 
size_t m_obj_count
 
bool m_outof_mem
 

Detailed Description

template<typename objtype, size_t array_size = 16>
class Inplace_vector< objtype, array_size >

Utility container class to store elements stably and scalably.

The address of an element stored in the container is stable as long as the object is alive, no object is copy-constructed/reassigned by push_back operations once it's stored into this container. And users of such containers can* assign to elements stored in the container just like using std::vector.

It is similar to STL vector but it is uniquely suitable in below situation: whenever stable element address, or element copy construction/assignment behaviors are forbidden. It only has a limited subset of the std::vector interface, and especially it doesn't have an iterator interface or element elimination interface, we don't need them for now. And this container is not multi-threading safe. It uses my_malloc/my_free to allocate/free memory arrays and caller can pass PSI key.

The container keeps a collection of arrays, each of which has a fixed NO. of slots to store elements. When one array is full, another is appended. When the vector shrinks at tail, useless arrays are removed and its memory space released.

Template Parameters
objtypeThe type of the elements to store.
array_sizeThe NO. of element slots in each array.

Constructor & Destructor Documentation

◆ Inplace_vector() [1/2]

template<typename objtype , size_t array_size = 16>
Inplace_vector< objtype, array_size >::Inplace_vector ( const Inplace_vector< objtype, array_size > &  )
private

◆ Inplace_vector() [2/2]

template<typename objtype , size_t array_size = 16>
Inplace_vector< objtype, array_size >::Inplace_vector ( PSI_memory_key  psi_key)
inlineexplicit

◆ ~Inplace_vector()

template<typename objtype , size_t array_size = 16>
Inplace_vector< objtype, array_size >::~Inplace_vector ( )
inline

Release memory space and destroy all contained objects.

Member Function Documentation

◆ append_new_array()

template<typename objtype , size_t array_size = 16>
void Inplace_vector< objtype, array_size >::append_new_array ( )
inlineprivate

◆ append_object()

template<typename objtype , size_t array_size = 16>
objtype * Inplace_vector< objtype, array_size >::append_object ( )
inline

Allocate space for an object, and construct it using its default constructor, and return its address.

Returns
the appended object's address; NULL if out of memory.

◆ back() [1/2]

template<typename objtype , size_t array_size = 16>
objtype & Inplace_vector< objtype, array_size >::back ( )
inline

STL std::vector::back interface.

Returns
the reference of the last object stored in the vector.

◆ back() [2/2]

template<typename objtype , size_t array_size = 16>
const objtype & Inplace_vector< objtype, array_size >::back ( ) const
inline

STL std::vector::back interface.

Returns
the reference of the last object stored in the vector.

◆ capacity()

template<typename objtype , size_t array_size = 16>
size_t Inplace_vector< objtype, array_size >::capacity ( ) const
inline

STL std::vector::capacity interface.

Returns
the max number of element that can be stored into this vector without growing its size.

◆ clear()

template<typename objtype , size_t array_size = 16>
void Inplace_vector< objtype, array_size >::clear ( )
inline

STL std::vector::clear interface.

Destroy all elements (by calling each element's destructor) stored in the vector, and then release all memory held by it.

◆ delete_all_objects()

template<typename objtype , size_t array_size = 16>
void Inplace_vector< objtype, array_size >::delete_all_objects ( )
inline

Destroy all elements (by calling each element's destructor) stored in the vector, and then release all memory held by it.

◆ empty()

template<typename objtype , size_t array_size = 16>
bool Inplace_vector< objtype, array_size >::empty ( ) const
inline

STL std::vector::empty interface.

Returns
whether size() == 0.

◆ get_object()

template<typename objtype , size_t array_size = 16>
objtype * Inplace_vector< objtype, array_size >::get_object ( size_t  index)
inline

Get an existing element's pointer, index must be in [0, m_obj_count).

Parameters
indexThe index of the element to return. It must be within valid in-use range of the vector.
Returns
The element address specified by index; NULL if out of memory.

◆ get_space()

template<typename objtype , size_t array_size = 16>
objtype * Inplace_vector< objtype, array_size >::get_space ( size_t  index)
inlineprivate

Return an existing used slot, or append exactly one slot at end of the last array and return it.

If the last array is already full before the append, allocate a new array then allocate one slot and return it.

Parameters
indexThe index of the element slot to return. It must be one within valid in-use range of the vector, or be equal to the size of the vector.
Returns
the object pointer stored in the specified slot; NULL if need to allocate more space but out of memory.

◆ operator=()

template<typename objtype , size_t array_size = 16>
Inplace_vector & Inplace_vector< objtype, array_size >::operator= ( const Inplace_vector< objtype, array_size > &  rhs)
private

◆ operator[]() [1/2]

template<typename objtype , size_t array_size = 16>
objtype & Inplace_vector< objtype, array_size >::operator[] ( size_t  i)
inline

STL std::vector::operator[] interface.

Parameters
iThe index of the element to return. It must be within valid in-use range of the vector.
Returns
The element reference specified by index.

◆ operator[]() [2/2]

template<typename objtype , size_t array_size = 16>
const objtype & Inplace_vector< objtype, array_size >::operator[] ( size_t  i) const
inline

STL std::vector::operator[] interface.

Parameters
iThe index of the element to return. It must be within valid in-use range of the vector.
Returns
The element reference specified by index.

◆ push_back()

template<typename objtype , size_t array_size = 16>
objtype * Inplace_vector< objtype, array_size >::push_back ( const objtype &  obj)
inline

STL std::vector::push_back interface.

It's guaranteed that existing elements stored in the vector is never copy constructed/reassigned by this operation. When the last element array is full, a new one is allocated and tracked.

Parameters
objThe element to store into the vector.
Returns
The appended object stored in the container; NULL if out of memory.

◆ resize()

template<typename objtype , size_t array_size = 16>
bool Inplace_vector< objtype, array_size >::resize ( size_t  new_size,
const objtype &  val = objtype() 
)
inline

STL std::vector::resize interface.

Has identical behavior as STL std::vector::resize except that no element copy construction or reassignment is ever caused by this operation.

Parameters
new_sizeNew size of vector. If smaller than current size, objects at the tail are removed and destroyed. If greater, new objects are added with default value.
valdefault value assigned to extended slots in the vector. Unused if the vector is shrunk.
Returns
true if out of memory; false if successful.

◆ size()

template<typename objtype , size_t array_size = 16>
size_t Inplace_vector< objtype, array_size >::size ( ) const
inline

STL std::vector::size interface.

Returns
the number of elements effectively stored in the vector.

Member Data Documentation

◆ m_obj_arrays

template<typename objtype , size_t array_size = 16>
std::vector<objtype *> Inplace_vector< objtype, array_size >::m_obj_arrays
private

◆ m_obj_count

template<typename objtype , size_t array_size = 16>
size_t Inplace_vector< objtype, array_size >::m_obj_count
private

◆ m_outof_mem

template<typename objtype , size_t array_size = 16>
bool Inplace_vector< objtype, array_size >::m_outof_mem
private

◆ m_psi_key

template<typename objtype , size_t array_size = 16>
PSI_memory_key Inplace_vector< objtype, array_size >::m_psi_key
private

The documentation for this class was generated from the following file: