24#ifndef MEM_ROOT_DEQUE_H
25#define MEM_ROOT_DEQUE_H
37template <
class Element_type>
40 size_t base_number_elems =
41 1024 /
sizeof(Element_type);
46 for (
size_t block_size = 16; block_size < 1024; ++block_size) {
47 if (block_size >= base_number_elems) {
171template <
class Element_type>
198 for (
size_t block_idx = 0; block_idx <
num_blocks(); ++block_idx) {
202 new (&
get(idx)) Element_type(other.
get(idx));
206 if (
this != &other) {
208 for (
const Element_type &elem : other) {
226 other.m_blocks =
nullptr;
227 other.m_begin_idx = other.m_end_idx = other.m_capacity = 0;
228 other.m_block_slots = other.m_first_block_idx = 0;
229 other.m_back_growth_exp = other.m_front_growth_exp = 0;
230 other.invalidate_iterators();
233 if (
this != &other) {
273 new (&
get(
m_end_idx++)) Element_type(std::move(element));
343 const Element_type &
back()
const {
365 template <
class Iterator_element_type>
391 typename = std::enable_if_t<
394 std::remove_const_t<Iterator_element_type>>
::value>>
479 return *(*
this + idx);
526 return std::make_reverse_iterator(
end());
529 return std::make_reverse_iterator(
begin());
532 return std::make_reverse_iterator(
end());
535 return std::make_reverse_iterator(
begin());
573 return erase(position, std::next(position));
590 return begin() + idx;
607 return begin() + idx;
610 template <
class ForwardIt>
613 for (ForwardIt it = first; it !=
last; ++it) {
618 return begin() + idx;
636 sizeof(Element_type)));
715 Element_type &
get(
size_t physical_idx)
const {
725template <
class Element_type>
727 if (m_blocks ==
nullptr) {
728 return add_initial_block();
734 size_t next_block_idx = m_first_block_idx + num_blocks();
735 if (next_block_idx < m_block_slots) {
737 if (m_blocks[next_block_idx].
init(m_root)) {
740 m_capacity += block_elements;
746 size_t growth =
size_t{1} << m_back_growth_exp;
747 size_t new_blocks_allocated = m_block_slots + growth;
748 Block *new_blocks = m_root->ArrayAlloc<
Block>(new_blocks_allocated);
749 if (new_blocks ==
nullptr) {
753 std::copy_n(m_blocks + m_first_block_idx, num_blocks(),
754 new_blocks + m_first_block_idx);
756 if (new_blocks[next_block_idx].
init(m_root)) {
760 m_blocks = new_blocks;
761 m_block_slots = new_blocks_allocated;
762 m_capacity += block_elements;
767template <
class Element_type>
769 if (m_blocks ==
nullptr) {
770 if (add_initial_block()) {
773 if (m_begin_idx == 0) {
775 m_begin_idx = m_end_idx = 1;
781 if (m_first_block_idx > 0) {
784 if (m_blocks[m_first_block_idx].
init(m_root)) {
788 m_begin_idx += block_elements;
789 m_end_idx += block_elements;
790 m_capacity += block_elements;
796 size_t growth =
size_t{1} << m_front_growth_exp;
797 size_t new_blocks_allocated = m_block_slots + growth;
798 Block *new_blocks = m_root->ArrayAlloc<
Block>(new_blocks_allocated);
799 if (new_blocks ==
nullptr) {
805 size_t new_first_block_idx = growth - 1;
806 std::copy_n(m_blocks + m_first_block_idx, num_blocks(),
807 new_blocks + new_first_block_idx + 1);
809 if (new_blocks[new_first_block_idx].
init(m_root)) {
813 m_blocks = new_blocks;
814 m_block_slots = new_blocks_allocated;
815 m_first_block_idx = new_first_block_idx;
816 m_begin_idx += block_elements;
817 m_end_idx += block_elements;
818 m_capacity += block_elements;
819 ++m_front_growth_exp;
static mysql_service_status_t init()
Component initialization.
Definition: audit_api_message_emit.cc:566
Definition: mem_root_deque.h:366
Iterator_element_type * operator->() const
Definition: mem_root_deque.h:423
bool operator>(const Iterator &other) const
Definition: mem_root_deque.h:490
Iterator operator+(difference_type offset) const
Definition: mem_root_deque.h:462
bool operator>=(const Iterator &other) const
Definition: mem_root_deque.h:496
bool operator==(const Iterator &other) const
Definition: mem_root_deque.h:414
Iterator operator--(int)
Definition: mem_root_deque.h:442
bool operator<(const Iterator &other) const
Definition: mem_root_deque.h:482
size_t m_generation
Definition: mem_root_deque.h:502
difference_type operator-(const Iterator &other) const
Definition: mem_root_deque.h:472
std::random_access_iterator_tag iterator_category
Definition: mem_root_deque.h:372
Iterator(const mem_root_deque *deque, size_t physical_idx)
Definition: mem_root_deque.h:377
bool operator<=(const Iterator &other) const
Definition: mem_root_deque.h:488
Iterator_element_type * pointer
Definition: mem_root_deque.h:370
Iterator_element_type & operator[](size_t idx) const
Definition: mem_root_deque.h:478
Iterator & operator--()
Definition: mem_root_deque.h:437
Iterator operator-(difference_type offset) const
Definition: mem_root_deque.h:467
void assert_not_invalidated() const
Definition: mem_root_deque.h:505
Iterator & operator++()
Definition: mem_root_deque.h:407
Iterator_element_type & operator*() const
Definition: mem_root_deque.h:403
Iterator & operator-=(difference_type diff)
Definition: mem_root_deque.h:456
Iterator_element_type & reference
Definition: mem_root_deque.h:371
Iterator operator++(int)
Definition: mem_root_deque.h:429
Iterator(const T &other)
For const_iterator: Implicit conversion from iterator.
Definition: mem_root_deque.h:395
Iterator & operator+=(difference_type diff)
Definition: mem_root_deque.h:450
const mem_root_deque * m_deque
Definition: mem_root_deque.h:499
ptrdiff_t difference_type
Definition: mem_root_deque.h:368
Iterator_element_type value_type
Definition: mem_root_deque.h:369
size_t m_physical_idx
Definition: mem_root_deque.h:500
bool operator!=(const Iterator &other) const
Definition: mem_root_deque.h:421
A (partial) implementation of std::deque allocating its blocks on a MEM_ROOT.
Definition: mem_root_deque.h:172
uint8_t m_back_growth_exp
Growth exponent for back expansion.
Definition: mem_root_deque.h:671
Element_type & operator[](size_t idx) const
Definition: mem_root_deque.h:242
reverse_iterator rbegin()
Definition: mem_root_deque.h:519
iterator insert(const_iterator pos, Element_type &&value)
Insert an element at a given position.
Definition: mem_root_deque.h:602
std::reverse_iterator< const_iterator > reverse_const_iterator
Definition: mem_root_deque.h:515
const Element_type * const_pointer
Definition: mem_root_deque.h:180
void invalidate_iterators()
Definition: mem_root_deque.h:686
size_t m_first_block_idx
Index in m_blocks where the first initialized block is stored.
Definition: mem_root_deque.h:666
iterator erase(const_iterator first, const_iterator last)
Erase all the elements in the specified range.
Definition: mem_root_deque.h:549
size_t size() const
Definition: mem_root_deque.h:538
iterator insert(const_iterator pos, ForwardIt first, ForwardIt last)
Definition: mem_root_deque.h:611
uint8_t back_growth_exp() const
Definition: mem_root_deque.h:361
size_t m_block_slots
Number of Block slots allocated in m_blocks array.
Definition: mem_root_deque.h:660
bool push_back(Element_type &&element)
Adds the given element to the end of the deque.
Definition: mem_root_deque.h:267
static constexpr size_t block_elements
Number of elements in each block.
Definition: mem_root_deque.h:623
reverse_const_iterator rbegin() const
Definition: mem_root_deque.h:531
size_t size_type
Used to conform to STL algorithm demands.
Definition: mem_root_deque.h:175
reverse_const_iterator rend() const
Definition: mem_root_deque.h:534
reverse_const_iterator crbegin() const
Definition: mem_root_deque.h:525
Element_type & front()
Returns the first element in the deque.
Definition: mem_root_deque.h:327
bool add_initial_block()
Adds the first block of elements.
Definition: mem_root_deque.h:692
bool add_block_back()
Definition: mem_root_deque.h:726
const_iterator cend()
Definition: mem_root_deque.h:522
size_t capacity() const
Definition: mem_root_deque.h:363
size_t m_begin_idx
Physical index to the first valid element.
Definition: mem_root_deque.h:647
uint8_t front_growth_exp() const
Definition: mem_root_deque.h:362
const Element_type & back() const
Definition: mem_root_deque.h:343
uint8_t m_front_growth_exp
Growth exponent for front expansion.
Definition: mem_root_deque.h:675
mem_root_deque(const mem_root_deque &other)
Definition: mem_root_deque.h:188
size_t m_end_idx
Physical index one past the last valid element.
Definition: mem_root_deque.h:651
Element_type value_type
Definition: mem_root_deque.h:177
bool push_back(const Element_type &element)
Adds the given element to the end of the deque.
Definition: mem_root_deque.h:250
void clear()
Removes all elements from the deque.
Definition: mem_root_deque.h:351
reverse_iterator rend()
Definition: mem_root_deque.h:520
ptrdiff_t difference_type
Definition: mem_root_deque.h:176
std::reverse_iterator< iterator > reverse_iterator
Definition: mem_root_deque.h:514
iterator erase(const_iterator position)
Removes a single element from the array.
Definition: mem_root_deque.h:572
const Element_type & front() const
Definition: mem_root_deque.h:332
const Element_type & const_reference
Definition: mem_root_deque.h:181
Block * m_blocks
Pointer to the first block.
Definition: mem_root_deque.h:644
mem_root_deque & operator=(mem_root_deque &&other)
Definition: mem_root_deque.h:232
size_t first_block_idx() const
Definition: mem_root_deque.h:360
const_iterator end() const
Definition: mem_root_deque.h:524
size_t block_slots() const
Definition: mem_root_deque.h:359
iterator insert(const_iterator pos, const Element_type &value)
Insert an element at a given position.
Definition: mem_root_deque.h:585
bool push_front(const Element_type &element)
Adds the given element to the beginning of the deque.
Definition: mem_root_deque.h:284
mem_root_deque(MEM_ROOT *mem_root)
Constructor. Leaves the array in an empty, valid state.
Definition: mem_root_deque.h:184
mem_root_deque & operator=(const mem_root_deque &other)
Definition: mem_root_deque.h:205
size_t m_capacity
Number of blocks, multiplied by block_elements.
Definition: mem_root_deque.h:655
Element_type & reference
Definition: mem_root_deque.h:179
Element_type & back()
Returns the last element in the deque.
Definition: mem_root_deque.h:338
Element_type * pointer
Definition: mem_root_deque.h:178
reverse_const_iterator crend() const
Definition: mem_root_deque.h:528
Element_type & get(size_t physical_idx) const
Gets a reference to the memory used to store an element with the given physical index,...
Definition: mem_root_deque.h:715
const_iterator begin() const
Definition: mem_root_deque.h:523
~mem_root_deque()
Definition: mem_root_deque.h:240
const_iterator cbegin()
Definition: mem_root_deque.h:521
void pop_front()
Removes the first element from the deque.
Definition: mem_root_deque.h:320
bool empty() const
Definition: mem_root_deque.h:539
bool push_front(Element_type &&element)
Adds the given element to the end of the deque.
Definition: mem_root_deque.h:300
size_t num_blocks() const
Definition: mem_root_deque.h:710
iterator begin()
Definition: mem_root_deque.h:517
size_t generation() const
Definition: mem_root_deque.h:721
MEM_ROOT * m_root
Pointer to the MEM_ROOT that we store our blocks and elements on.
Definition: mem_root_deque.h:678
void pop_back()
Removes the last element from the deque.
Definition: mem_root_deque.h:313
bool add_block_front()
Definition: mem_root_deque.h:768
iterator end()
Definition: mem_root_deque.h:518
size_t m_generation
Incremented each time we make an operation that would invalidate iterators.
Definition: mem_root_deque.h:685
mem_root_deque(mem_root_deque &&other)
Definition: mem_root_deque.h:216
static MEM_ROOT mem_root
Definition: client_plugin.cc:114
static Bigint * diff(Bigint *a, Bigint *b, Stack_alloc *alloc)
Definition: dtoa.cc:1081
#define T
Definition: jit_executor_value.cc:373
static constexpr size_t FindElementsPerBlock()
Definition: mem_root_deque.h:38
This file follows Google coding style, except for the name MEM_ROOT (which is kept for historical rea...
void destroy_at(T *ptr)
Definition: my_alloc.h:462
uint16_t value_type
Definition: vt100.h:184
ValueType value(const std::optional< ValueType > &v)
Definition: gtid.h:83
The MEM_ROOT is a simple arena, where allocations are carved out of larger blocks.
Definition: my_alloc.h:83
T * ArrayAlloc(size_t num, Args... args)
Allocate 'num' objects of type T, and initialize them to a default value that is created by passing t...
Definition: my_alloc.h:180
void * Alloc(size_t length)
Allocate memory.
Definition: my_alloc.h:145
Definition: mem_root_deque.h:629
Element_type * elements
Definition: mem_root_deque.h:630
bool init(MEM_ROOT *mem_root)
Definition: mem_root_deque.h:632