24#ifndef MEM_ROOT_DEQUE_H
25#define MEM_ROOT_DEQUE_H
36template <
class Element_type>
39 size_t base_number_elems =
40 1024 /
sizeof(Element_type);
45 for (
size_t block_size = 16; block_size < 1024; ++block_size) {
46 if (block_size >= base_number_elems) {
109template <
class Element_type>
132 for (
size_t block_idx = 0; block_idx <
num_blocks(); ++block_idx) {
136 new (&
get(idx)) Element_type(other.
get(idx));
140 if (
this != &other) {
142 for (
const Element_type &elem : other) {
155 other.m_blocks =
nullptr;
156 other.m_begin_idx = other.m_end_idx = other.m_capacity = 0;
157 other.invalidate_iterators();
160 if (
this != &other) {
200 new (&
get(
m_end_idx++)) Element_type(std::move(element));
270 const Element_type &
back()
const {
286 template <
class Iterator_element_type>
312 typename = std::enable_if_t<
313 std::is_const<Iterator_element_type>::value &&
315 std::remove_const_t<Iterator_element_type>>::value>>
400 return *(*
this + idx);
447 return std::make_reverse_iterator(
end());
450 return std::make_reverse_iterator(
begin());
453 return std::make_reverse_iterator(
end());
456 return std::make_reverse_iterator(
begin());
494 return erase(position, std::next(position));
511 return begin() + idx;
528 return begin() + idx;
531 template <
class ForwardIt>
534 for (ForwardIt it = first; it !=
last; ++it) {
539 return begin() + idx;
557 sizeof(Element_type)));
615 Element_type &
get(
size_t physical_idx)
const {
628template <
class Element_type>
630 if (m_blocks ==
nullptr) {
631 return add_initial_block();
633 Block *new_blocks = m_root->ArrayAlloc<
Block>(num_blocks() + 1);
634 if (new_blocks ==
nullptr) {
637 memcpy(new_blocks, m_blocks,
sizeof(
Block) * num_blocks());
638 if (new_blocks[num_blocks()].
init(m_root)) {
642 m_blocks = new_blocks;
643 m_capacity += block_elements;
647template <
class Element_type>
649 if (m_blocks ==
nullptr) {
650 if (add_initial_block()) {
653 if (m_begin_idx == 0) {
655 m_begin_idx = m_end_idx = 1;
659 Block *new_blocks = m_root->ArrayAlloc<
Block>(num_blocks() + 1);
660 memcpy(new_blocks + 1, m_blocks,
sizeof(
Block) * num_blocks());
661 if (new_blocks[0].
init(m_root)) {
665 m_blocks = new_blocks;
666 m_begin_idx += block_elements;
667 m_end_idx += block_elements;
668 m_capacity += block_elements;
static mysql_service_status_t init()
Component initialization.
Definition: audit_api_message_emit.cc:571
Definition: mem_root_deque.h:287
Iterator_element_type * operator->() const
Definition: mem_root_deque.h:344
bool operator>(const Iterator &other) const
Definition: mem_root_deque.h:411
Iterator operator+(difference_type offset) const
Definition: mem_root_deque.h:383
bool operator>=(const Iterator &other) const
Definition: mem_root_deque.h:417
bool operator==(const Iterator &other) const
Definition: mem_root_deque.h:335
Iterator operator--(int)
Definition: mem_root_deque.h:363
bool operator<(const Iterator &other) const
Definition: mem_root_deque.h:403
size_t m_generation
Definition: mem_root_deque.h:423
difference_type operator-(const Iterator &other) const
Definition: mem_root_deque.h:393
std::random_access_iterator_tag iterator_category
Definition: mem_root_deque.h:293
Iterator(const mem_root_deque *deque, size_t physical_idx)
Definition: mem_root_deque.h:298
bool operator<=(const Iterator &other) const
Definition: mem_root_deque.h:409
Iterator_element_type * pointer
Definition: mem_root_deque.h:291
Iterator_element_type & operator[](size_t idx) const
Definition: mem_root_deque.h:399
Iterator & operator--()
Definition: mem_root_deque.h:358
Iterator operator-(difference_type offset) const
Definition: mem_root_deque.h:388
void assert_not_invalidated() const
Definition: mem_root_deque.h:426
Iterator & operator++()
Definition: mem_root_deque.h:328
Iterator_element_type & operator*() const
Definition: mem_root_deque.h:324
Iterator & operator-=(difference_type diff)
Definition: mem_root_deque.h:377
Iterator_element_type & reference
Definition: mem_root_deque.h:292
Iterator operator++(int)
Definition: mem_root_deque.h:350
Iterator(const T &other)
For const_iterator: Implicit conversion from iterator.
Definition: mem_root_deque.h:316
Iterator & operator+=(difference_type diff)
Definition: mem_root_deque.h:371
const mem_root_deque * m_deque
Definition: mem_root_deque.h:420
ptrdiff_t difference_type
Definition: mem_root_deque.h:289
Iterator_element_type value_type
Definition: mem_root_deque.h:290
size_t m_physical_idx
Definition: mem_root_deque.h:421
bool operator!=(const Iterator &other) const
Definition: mem_root_deque.h:342
A (partial) implementation of std::deque allocating its blocks on a MEM_ROOT.
Definition: mem_root_deque.h:110
Element_type & operator[](size_t idx) const
Definition: mem_root_deque.h:169
reverse_iterator rbegin()
Definition: mem_root_deque.h:440
iterator insert(const_iterator pos, Element_type &&value)
Insert an element at a given position.
Definition: mem_root_deque.h:523
std::reverse_iterator< const_iterator > reverse_const_iterator
Definition: mem_root_deque.h:436
const Element_type * const_pointer
Definition: mem_root_deque.h:118
void invalidate_iterators()
Definition: mem_root_deque.h:587
iterator erase(const_iterator first, const_iterator last)
Erase all the elements in the specified range.
Definition: mem_root_deque.h:470
size_t size() const
Definition: mem_root_deque.h:459
iterator insert(const_iterator pos, ForwardIt first, ForwardIt last)
Definition: mem_root_deque.h:532
bool push_back(Element_type &&element)
Adds the given element to the end of the deque.
Definition: mem_root_deque.h:194
static constexpr size_t block_elements
Number of elements in each block.
Definition: mem_root_deque.h:544
reverse_const_iterator rbegin() const
Definition: mem_root_deque.h:452
size_t size_type
Used to conform to STL algorithm demands.
Definition: mem_root_deque.h:113
reverse_const_iterator rend() const
Definition: mem_root_deque.h:455
reverse_const_iterator crbegin() const
Definition: mem_root_deque.h:446
Element_type & front()
Returns the first element in the deque.
Definition: mem_root_deque.h:254
bool add_initial_block()
Adds the first block of elements.
Definition: mem_root_deque.h:593
bool add_block_back()
Definition: mem_root_deque.h:629
const_iterator cend()
Definition: mem_root_deque.h:443
size_t m_begin_idx
Physical index to the first valid element.
Definition: mem_root_deque.h:568
const Element_type & back() const
Definition: mem_root_deque.h:270
mem_root_deque(const mem_root_deque &other)
Definition: mem_root_deque.h:126
size_t m_end_idx
Physical index one past the last valid element.
Definition: mem_root_deque.h:572
Element_type value_type
Definition: mem_root_deque.h:115
bool push_back(const Element_type &element)
Adds the given element to the end of the deque.
Definition: mem_root_deque.h:177
void clear()
Removes all elements from the deque.
Definition: mem_root_deque.h:278
reverse_iterator rend()
Definition: mem_root_deque.h:441
ptrdiff_t difference_type
Definition: mem_root_deque.h:114
std::reverse_iterator< iterator > reverse_iterator
Definition: mem_root_deque.h:435
iterator erase(const_iterator position)
Removes a single element from the array.
Definition: mem_root_deque.h:493
const Element_type & front() const
Definition: mem_root_deque.h:259
const Element_type & const_reference
Definition: mem_root_deque.h:119
Block * m_blocks
Pointer to the first block.
Definition: mem_root_deque.h:565
mem_root_deque & operator=(mem_root_deque &&other)
Definition: mem_root_deque.h:159
const_iterator end() const
Definition: mem_root_deque.h:445
iterator insert(const_iterator pos, const Element_type &value)
Insert an element at a given position.
Definition: mem_root_deque.h:506
bool push_front(const Element_type &element)
Adds the given element to the beginning of the deque.
Definition: mem_root_deque.h:211
mem_root_deque(MEM_ROOT *mem_root)
Constructor. Leaves the array in an empty, valid state.
Definition: mem_root_deque.h:122
mem_root_deque & operator=(const mem_root_deque &other)
Definition: mem_root_deque.h:139
size_t m_capacity
Number of blocks, multiplied by block_elements.
Definition: mem_root_deque.h:576
Element_type & reference
Definition: mem_root_deque.h:117
Element_type & back()
Returns the last element in the deque.
Definition: mem_root_deque.h:265
Element_type * pointer
Definition: mem_root_deque.h:116
reverse_const_iterator crend() const
Definition: mem_root_deque.h:449
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:615
const_iterator begin() const
Definition: mem_root_deque.h:444
~mem_root_deque()
Definition: mem_root_deque.h:167
const_iterator cbegin()
Definition: mem_root_deque.h:442
void pop_front()
Removes the first element from the deque.
Definition: mem_root_deque.h:247
bool empty() const
Definition: mem_root_deque.h:460
bool push_front(Element_type &&element)
Adds the given element to the end of the deque.
Definition: mem_root_deque.h:227
size_t num_blocks() const
Definition: mem_root_deque.h:610
iterator begin()
Definition: mem_root_deque.h:438
size_t generation() const
Definition: mem_root_deque.h:621
MEM_ROOT * m_root
Pointer to the MEM_ROOT that we store our blocks and elements on.
Definition: mem_root_deque.h:579
void pop_back()
Removes the last element from the deque.
Definition: mem_root_deque.h:240
bool add_block_front()
Definition: mem_root_deque.h:648
iterator end()
Definition: mem_root_deque.h:439
size_t m_generation
Incremented each time we make an operation that would invalidate iterators.
Definition: mem_root_deque.h:586
mem_root_deque(mem_root_deque &&other)
Definition: mem_root_deque.h:150
static MEM_ROOT mem_root
Definition: client_plugin.cc:110
static Bigint * diff(Bigint *a, Bigint *b, Stack_alloc *alloc)
Definition: dtoa.cc:1080
static constexpr size_t FindElementsPerBlock()
Definition: mem_root_deque.h:37
This file follows Google coding style, except for the name MEM_ROOT (which is kept for historical rea...
uint16_t value_type
Definition: vt100.h:184
static int destroy(mysql_cond_t *that, const char *, unsigned int)
Definition: mysql_cond_v1_native.cc:54
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:550
Element_type * elements
Definition: mem_root_deque.h:551
bool init(MEM_ROOT *mem_root)
Definition: mem_root_deque.h:553