MySQL 9.1.0
Source Code Documentation
Prealloced_array< Element_type, Prealloc > Class Template Reference

A typesafe replacement for DYNAMIC_ARRAY. More...

#include <prealloced_array.h>

Inheritance diagram for Prealloced_array< Element_type, Prealloc >:
[legend]

Classes

struct  External
 

Public Types

typedef Element_type value_type
 Standard typedefs. More...
 
typedef size_t size_type
 
typedef ptrdiff_t difference_type
 
typedef Element_type * iterator
 
typedef const Element_type * const_iterator
 

Public Member Functions

 Prealloced_array (PSI_memory_key psi_key)
 
 Prealloced_array (PSI_memory_key psi_key, size_t initial_size)
 Initializes (parts of) the array with default values. More...
 
 Prealloced_array (const Prealloced_array &that)
 An object instance "owns" its array, so we do deep copy here. More...
 
 Prealloced_array (Prealloced_array &&that)
 
 Prealloced_array (PSI_memory_key psi_key, const_iterator first, const_iterator last)
 Range constructor. More...
 
 Prealloced_array (std::initializer_list< Element_type > elems)
 
Prealloced_arrayoperator= (const Prealloced_array &that)
 Copies all the elements from 'that' into this container. More...
 
Prealloced_arrayoperator= (Prealloced_array &&that)
 
 ~Prealloced_array ()
 Runs DTOR on all elements if needed. More...
 
size_t capacity () const
 
size_t element_size () const
 
bool empty () const
 
size_t size () const
 
Element_type & at (size_t n)
 
const Element_type & at (size_t n) const
 
Element_type & operator[] (size_t n)
 
const Element_type & operator[] (size_t n) const
 
Element_type & back ()
 
const Element_type & back () const
 
Element_type & front ()
 
const Element_type & front () const
 
iterator begin ()
 begin : Returns a pointer to the first element in the array. More...
 
iterator end ()
 
const_iterator begin () const
 
const_iterator end () const
 
const_iterator cbegin () const
 Returns a constant pointer to the first element in the array. More...
 
const_iterator cend () const
 Returns a constant pointer to the past-the-end element in the array. More...
 
bool assign_at (size_t n, const value_type &val)
 Assigns a value to an arbitrary element, even where n >= size(). More...
 
bool reserve (size_t n)
 Reserves space for array elements. More...
 
void claim_memory_ownership (bool claim)
 Claim memory ownership. More...
 
bool push_back (const Element_type &element)
 Copies an element into the back of the array. More...
 
bool push_back (Element_type &&element)
 Copies (or moves, if possible) an element into the back of the array. More...
 
template<typename... Args>
bool emplace_back (Args &&...args)
 Constructs an element at the back of the array. More...
 
void pop_back ()
 Removes the last element in the array, effectively reducing the container size by one. More...
 
iterator insert (const_iterator position, const value_type &val)
 The array is extended by inserting a new element before the element at the specified position. More...
 
iterator insert (const_iterator position, value_type &&val)
 The array is extended by inserting a new element before the element at the specified position. More...
 
template<typename... Args>
iterator emplace (const_iterator position, Args &&...args)
 The array is extended by inserting a new element before the element at the specified position. More...
 
std::pair< iterator, bool > insert_unique (const value_type &val)
 Similar to std::set<>::insert() Extends the array by inserting a new element, but only if it cannot be found in the array already. More...
 
size_type erase_unique (const value_type &val)
 Similar to std::set<>::erase() Removes a single element from the array by value. More...
 
size_type count_unique (const value_type &val) const
 Similar to std::set<>::count() More...
 
iterator erase (const_iterator position)
 Removes a single element from the array. More...
 
iterator erase (size_t ix)
 Removes a single element from the array. More...
 
void erase_at_end (const_iterator first)
 Removes tail elements from the array. More...
 
iterator erase (const_iterator first, const_iterator last)
 Removes a range of elements from the array. More...
 
void swap (Prealloced_array &rhs)
 Exchanges the content of the container by the content of rhs, which is another vector object of the same type. More...
 
void shrink_to_fit ()
 Requests the container to reduce its capacity to fit its size. More...
 
void resize (size_t n, const Element_type &val=Element_type())
 Resizes the container so that it contains n elements. More...
 
void clear ()
 Removes (and destroys) all elements. More...
 

Static Public Attributes

static const size_t initial_capacity = Prealloc
 Initial capacity of the array. More...
 

Private Member Functions

bool using_inline_buffer () const
 
Element_type * buffer ()
 Gets the buffer in use. More...
 
const Element_type * buffer () const
 
void set_size (size_t n)
 
void adjust_size (int delta)
 

Private Attributes

PSI_memory_key m_psi_key
 
int m_inline_size = 0
 
union {
   External   m_ext {}
 
   Element_type   m_buff [Prealloc]
 
}; 
 

Static Private Attributes

static constexpr bool Has_trivial_destructor
 Is Element_type trivially destructible? If it is, we don't destroy elements when they are removed from the array or when the array is destroyed. More...
 

Detailed Description

template<typename Element_type, size_t Prealloc>
class Prealloced_array< Element_type, Prealloc >

A typesafe replacement for DYNAMIC_ARRAY.

We do our own memory management, and pre-allocate space for a number of elements. The purpose is to pre-allocate enough elements to cover normal use cases, thus saving malloc()/free() overhead. If we run out of space, we use malloc to allocate more space.

The interface is chosen to be similar to std::vector. We keep the std::vector property that storage is contiguous.

We have fairly low overhead over the inline storage; typically 8 bytes (e.g. Prealloced_array<TABLE *, 4> needs 40 bytes on 64-bit platforms).

Remarks
Unlike DYNAMIC_ARRAY, elements are properly copied (rather than memcpy()d) if the underlying array needs to be expanded.
Depending on Has_trivial_destructor, we destroy objects which are removed from the array (including when the array object itself is destroyed).
Template Parameters
Element_typeThe type of the elements of the container. Elements must be copyable or movable.
PreallocNumber of elements to pre-allocate.

Member Typedef Documentation

◆ const_iterator

template<typename Element_type , size_t Prealloc>
typedef const Element_type* Prealloced_array< Element_type, Prealloc >::const_iterator

◆ difference_type

template<typename Element_type , size_t Prealloc>
typedef ptrdiff_t Prealloced_array< Element_type, Prealloc >::difference_type

◆ iterator

template<typename Element_type , size_t Prealloc>
typedef Element_type* Prealloced_array< Element_type, Prealloc >::iterator

◆ size_type

template<typename Element_type , size_t Prealloc>
typedef size_t Prealloced_array< Element_type, Prealloc >::size_type

◆ value_type

template<typename Element_type , size_t Prealloc>
typedef Element_type Prealloced_array< Element_type, Prealloc >::value_type

Standard typedefs.

Constructor & Destructor Documentation

◆ Prealloced_array() [1/6]

template<typename Element_type , size_t Prealloc>
Prealloced_array< Element_type, Prealloc >::Prealloced_array ( PSI_memory_key  psi_key)
inlineexplicit

◆ Prealloced_array() [2/6]

template<typename Element_type , size_t Prealloc>
Prealloced_array< Element_type, Prealloc >::Prealloced_array ( PSI_memory_key  psi_key,
size_t  initial_size 
)
inline

Initializes (parts of) the array with default values.

Using 'Prealloc' for initial_size makes this similar to a raw C array.

◆ Prealloced_array() [3/6]

template<typename Element_type , size_t Prealloc>
Prealloced_array< Element_type, Prealloc >::Prealloced_array ( const Prealloced_array< Element_type, Prealloc > &  that)
inline

An object instance "owns" its array, so we do deep copy here.

◆ Prealloced_array() [4/6]

template<typename Element_type , size_t Prealloc>
Prealloced_array< Element_type, Prealloc >::Prealloced_array ( Prealloced_array< Element_type, Prealloc > &&  that)
inline

◆ Prealloced_array() [5/6]

template<typename Element_type , size_t Prealloc>
Prealloced_array< Element_type, Prealloc >::Prealloced_array ( PSI_memory_key  psi_key,
const_iterator  first,
const_iterator  last 
)
inline

Range constructor.

Constructs a container with as many elements as the range [first,last), with each element constructed from its corresponding element in that range, in the same order.

◆ Prealloced_array() [6/6]

template<typename Element_type , size_t Prealloc>
Prealloced_array< Element_type, Prealloc >::Prealloced_array ( std::initializer_list< Element_type >  elems)
inline

◆ ~Prealloced_array()

template<typename Element_type , size_t Prealloc>
Prealloced_array< Element_type, Prealloc >::~Prealloced_array ( )
inline

Runs DTOR on all elements if needed.

Deallocates array if we exceeded the Preallocated amount.

Member Function Documentation

◆ adjust_size()

template<typename Element_type , size_t Prealloc>
void Prealloced_array< Element_type, Prealloc >::adjust_size ( int  delta)
inlineprivate

◆ assign_at()

template<typename Element_type , size_t Prealloc>
bool Prealloced_array< Element_type, Prealloc >::assign_at ( size_t  n,
const value_type val 
)
inline

Assigns a value to an arbitrary element, even where n >= size().

The array is extended with default values if necessary.

Return values
trueif out-of-memory, false otherwise.

◆ at() [1/2]

template<typename Element_type , size_t Prealloc>
Element_type & Prealloced_array< Element_type, Prealloc >::at ( size_t  n)
inline

◆ at() [2/2]

template<typename Element_type , size_t Prealloc>
const Element_type & Prealloced_array< Element_type, Prealloc >::at ( size_t  n) const
inline

◆ back() [1/2]

template<typename Element_type , size_t Prealloc>
Element_type & Prealloced_array< Element_type, Prealloc >::back ( )
inline

◆ back() [2/2]

template<typename Element_type , size_t Prealloc>
const Element_type & Prealloced_array< Element_type, Prealloc >::back ( ) const
inline

◆ begin() [1/2]

template<typename Element_type , size_t Prealloc>
iterator Prealloced_array< Element_type, Prealloc >::begin ( )
inline

begin : Returns a pointer to the first element in the array.

end : Returns a pointer to the past-the-end element in the array.

◆ begin() [2/2]

template<typename Element_type , size_t Prealloc>
const_iterator Prealloced_array< Element_type, Prealloc >::begin ( ) const
inline

◆ buffer() [1/2]

template<typename Element_type , size_t Prealloc>
Element_type * Prealloced_array< Element_type, Prealloc >::buffer ( )
inlineprivate

Gets the buffer in use.

◆ buffer() [2/2]

template<typename Element_type , size_t Prealloc>
const Element_type * Prealloced_array< Element_type, Prealloc >::buffer ( ) const
inlineprivate

◆ capacity()

template<typename Element_type , size_t Prealloc>
size_t Prealloced_array< Element_type, Prealloc >::capacity ( ) const
inline

◆ cbegin()

template<typename Element_type , size_t Prealloc>
const_iterator Prealloced_array< Element_type, Prealloc >::cbegin ( ) const
inline

Returns a constant pointer to the first element in the array.

◆ cend()

template<typename Element_type , size_t Prealloc>
const_iterator Prealloced_array< Element_type, Prealloc >::cend ( ) const
inline

Returns a constant pointer to the past-the-end element in the array.

◆ claim_memory_ownership()

template<typename Element_type , size_t Prealloc>
void Prealloced_array< Element_type, Prealloc >::claim_memory_ownership ( bool  claim)
inline

Claim memory ownership.

Parameters
claimtake ownership of memory

◆ clear()

template<typename Element_type , size_t Prealloc>
void Prealloced_array< Element_type, Prealloc >::clear ( )
inline

Removes (and destroys) all elements.

Does not change capacity.

◆ count_unique()

template<typename Element_type , size_t Prealloc>
size_type Prealloced_array< Element_type, Prealloc >::count_unique ( const value_type val) const
inline

Similar to std::set<>::count()

Note
Assumes that array is maintained with insert_unique/erase_unique.
Return values
1if element is found, 0 otherwise.

◆ element_size()

template<typename Element_type , size_t Prealloc>
size_t Prealloced_array< Element_type, Prealloc >::element_size ( ) const
inline

◆ emplace()

template<typename Element_type , size_t Prealloc>
template<typename... Args>
iterator Prealloced_array< Element_type, Prealloc >::emplace ( const_iterator  position,
Args &&...  args 
)
inline

The array is extended by inserting a new element before the element at the specified position.

The element is constructed in-place.

This is generally an inefficient operation, since we need to copy elements to make a new "hole" in the array.

We use std::rotate to move objects, hence Element_type must be move-assignable and move-constructible.

Returns
an iterator pointing to the inserted value

◆ emplace_back()

template<typename Element_type , size_t Prealloc>
template<typename... Args>
bool Prealloced_array< Element_type, Prealloc >::emplace_back ( Args &&...  args)
inline

Constructs an element at the back of the array.

Complexity: Constant (amortized time, reallocation may happen).

Returns
true if out-of-memory, false otherwise

◆ empty()

template<typename Element_type , size_t Prealloc>
bool Prealloced_array< Element_type, Prealloc >::empty ( ) const
inline

◆ end() [1/2]

template<typename Element_type , size_t Prealloc>
iterator Prealloced_array< Element_type, Prealloc >::end ( )
inline

◆ end() [2/2]

template<typename Element_type , size_t Prealloc>
const_iterator Prealloced_array< Element_type, Prealloc >::end ( ) const
inline

◆ erase() [1/3]

template<typename Element_type , size_t Prealloc>
iterator Prealloced_array< Element_type, Prealloc >::erase ( const_iterator  first,
const_iterator  last 
)
inline

Removes a range of elements from the array.

The removed elements are destroyed. This effectively reduces the containers size by 'last - first'.

This is generally an inefficient operation, since we need to move or copy elements to fill the "hole" in the array.

We use std::move to move objects, hence Element_type must be move-assignable.

◆ erase() [2/3]

template<typename Element_type , size_t Prealloc>
iterator Prealloced_array< Element_type, Prealloc >::erase ( const_iterator  position)
inline

Removes a single element from the array.

The removed element is destroyed. This effectively reduces the container size by one.

This is generally an inefficient operation, since we need to move or copy elements to fill the "hole" in the array.

We use std::move to move objects, hence Element_type must be move-assignable.

◆ erase() [3/3]

template<typename Element_type , size_t Prealloc>
iterator Prealloced_array< Element_type, Prealloc >::erase ( size_t  ix)
inline

Removes a single element from the array.

◆ erase_at_end()

template<typename Element_type , size_t Prealloc>
void Prealloced_array< Element_type, Prealloc >::erase_at_end ( const_iterator  first)
inline

Removes tail elements from the array.

The removed elements are destroyed. This effectively reduces the containers size by 'end() - first'.

◆ erase_unique()

template<typename Element_type , size_t Prealloc>
size_type Prealloced_array< Element_type, Prealloc >::erase_unique ( const value_type val)
inline

Similar to std::set<>::erase() Removes a single element from the array by value.

The removed element is destroyed. This effectively reduces the container size by one.

This is generally an inefficient operation, since we need to copy elements to fill the "hole" in the array.

Assumes that the array is sorted with std::less<Element_type>.

Return values
numberof elements removed, 0 or 1.

◆ front() [1/2]

template<typename Element_type , size_t Prealloc>
Element_type & Prealloced_array< Element_type, Prealloc >::front ( )
inline

◆ front() [2/2]

template<typename Element_type , size_t Prealloc>
const Element_type & Prealloced_array< Element_type, Prealloc >::front ( ) const
inline

◆ insert() [1/2]

template<typename Element_type , size_t Prealloc>
iterator Prealloced_array< Element_type, Prealloc >::insert ( const_iterator  position,
const value_type val 
)
inline

The array is extended by inserting a new element before the element at the specified position.

This is generally an inefficient operation, since we need to copy elements to make a new "hole" in the array.

We use std::rotate to move objects, hence Element_type must be move-assignable and move-constructible.

Returns
an iterator pointing to the inserted value

◆ insert() [2/2]

template<typename Element_type , size_t Prealloc>
iterator Prealloced_array< Element_type, Prealloc >::insert ( const_iterator  position,
value_type &&  val 
)
inline

The array is extended by inserting a new element before the element at the specified position.

The element is moved into the array, if possible.

This is generally an inefficient operation, since we need to copy elements to make a new "hole" in the array.

We use std::rotate to move objects, hence Element_type must be move-assignable and move-constructible.

Returns
an iterator pointing to the inserted value

◆ insert_unique()

template<typename Element_type , size_t Prealloc>
std::pair< iterator, bool > Prealloced_array< Element_type, Prealloc >::insert_unique ( const value_type val)
inline

Similar to std::set<>::insert() Extends the array by inserting a new element, but only if it cannot be found in the array already.

Assumes that the array is sorted with std::less<Element_type> Insertion using this function will maintain order.

Return values
Apair, with its member pair::first set an iterator pointing to either the newly inserted element, or to the equivalent element already in the array. The pair::second element is set to true if the new element was inserted, or false if an equivalent element already existed.

◆ operator=() [1/2]

template<typename Element_type , size_t Prealloc>
Prealloced_array & Prealloced_array< Element_type, Prealloc >::operator= ( const Prealloced_array< Element_type, Prealloc > &  that)
inline

Copies all the elements from 'that' into this container.

Any objects in this container are destroyed first.

◆ operator=() [2/2]

template<typename Element_type , size_t Prealloc>
Prealloced_array & Prealloced_array< Element_type, Prealloc >::operator= ( Prealloced_array< Element_type, Prealloc > &&  that)
inline

◆ operator[]() [1/2]

template<typename Element_type , size_t Prealloc>
Element_type & Prealloced_array< Element_type, Prealloc >::operator[] ( size_t  n)
inline

◆ operator[]() [2/2]

template<typename Element_type , size_t Prealloc>
const Element_type & Prealloced_array< Element_type, Prealloc >::operator[] ( size_t  n) const
inline

◆ pop_back()

template<typename Element_type , size_t Prealloc>
void Prealloced_array< Element_type, Prealloc >::pop_back ( )
inline

Removes the last element in the array, effectively reducing the container size by one.

This destroys the removed element.

◆ push_back() [1/2]

template<typename Element_type , size_t Prealloc>
bool Prealloced_array< Element_type, Prealloc >::push_back ( const Element_type &  element)
inline

Copies an element into the back of the array.

Complexity: Constant (amortized time, reallocation may happen).

Returns
true if out-of-memory, false otherwise

◆ push_back() [2/2]

template<typename Element_type , size_t Prealloc>
bool Prealloced_array< Element_type, Prealloc >::push_back ( Element_type &&  element)
inline

Copies (or moves, if possible) an element into the back of the array.

Complexity: Constant (amortized time, reallocation may happen).

Returns
true if out-of-memory, false otherwise

◆ reserve()

template<typename Element_type , size_t Prealloc>
bool Prealloced_array< Element_type, Prealloc >::reserve ( size_t  n)
inline

Reserves space for array elements.

Copies (or moves, if possible) over existing elements, in case we are re-expanding the array.

Parameters
nnumber of elements.
Return values
trueif out-of-memory, false otherwise.

◆ resize()

template<typename Element_type , size_t Prealloc>
void Prealloced_array< Element_type, Prealloc >::resize ( size_t  n,
const Element_type &  val = Element_type() 
)
inline

Resizes the container so that it contains n elements.

If n is smaller than the current container size, the content is reduced to its first n elements, removing those beyond (and destroying them).

If n is greater than the current container size, the content is expanded by inserting at the end as many elements as needed to reach a size of n. If val is specified, the new elements are initialized as copies of val, otherwise, they are value-initialized.

If n is also greater than the current container capacity, an automatic reallocation of the allocated storage space takes place.

Notice that this function changes the actual content of the container by inserting or erasing elements from it.

◆ set_size()

template<typename Element_type , size_t Prealloc>
void Prealloced_array< Element_type, Prealloc >::set_size ( size_t  n)
inlineprivate

◆ shrink_to_fit()

template<typename Element_type , size_t Prealloc>
void Prealloced_array< Element_type, Prealloc >::shrink_to_fit ( )
inline

Requests the container to reduce its capacity to fit its size.

◆ size()

template<typename Element_type , size_t Prealloc>
size_t Prealloced_array< Element_type, Prealloc >::size ( ) const
inline

◆ swap()

template<typename Element_type , size_t Prealloc>
void Prealloced_array< Element_type, Prealloc >::swap ( Prealloced_array< Element_type, Prealloc > &  rhs)
inline

Exchanges the content of the container by the content of rhs, which is another vector object of the same type.

Sizes may differ.

We use std::swap to do the operation.

◆ using_inline_buffer()

template<typename Element_type , size_t Prealloc>
bool Prealloced_array< Element_type, Prealloc >::using_inline_buffer ( ) const
inlineprivate

Member Data Documentation

◆ 

union { ... } Prealloced_array< Element_type, Prealloc >::@8

◆ Has_trivial_destructor

template<typename Element_type , size_t Prealloc>
constexpr bool Prealloced_array< Element_type, Prealloc >::Has_trivial_destructor
staticconstexprprivate
Initial value:
=
std::is_trivially_destructible<Element_type>::value

Is Element_type trivially destructible? If it is, we don't destroy elements when they are removed from the array or when the array is destroyed.

◆ initial_capacity

template<typename Element_type , size_t Prealloc>
const size_t Prealloced_array< Element_type, Prealloc >::initial_capacity = Prealloc
static

Initial capacity of the array.

◆ m_buff

template<typename Element_type , size_t Prealloc>
Element_type Prealloced_array< Element_type, Prealloc >::m_buff[Prealloc]

◆ m_ext

template<typename Element_type , size_t Prealloc>
External Prealloced_array< Element_type, Prealloc >::m_ext {}

◆ m_inline_size

template<typename Element_type , size_t Prealloc>
int Prealloced_array< Element_type, Prealloc >::m_inline_size = 0
private

◆ m_psi_key

template<typename Element_type , size_t Prealloc>
PSI_memory_key Prealloced_array< Element_type, Prealloc >::m_psi_key
private

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