23#ifndef MEM_ROOT_DEQUE_H
24#define MEM_ROOT_DEQUE_H
35template <
class Element_type>
38 size_t base_number_elems =
39 1024 /
sizeof(Element_type);
44 for (
size_t block_size = 16; block_size < 1024; ++block_size) {
45 if (block_size >= base_number_elems) {
108template <
class Element_type>
131 for (
size_t block_idx = 0; block_idx <
num_blocks(); ++block_idx) {
135 new (&
get(idx)) Element_type(other.
get(idx));
139 if (
this != &other) {
141 for (
const Element_type &elem : other) {
154 other.m_blocks =
nullptr;
155 other.m_begin_idx = other.m_end_idx = other.m_capacity = 0;
156 other.invalidate_iterators();
159 if (
this != &other) {
199 new (&
get(
m_end_idx++)) Element_type(std::move(element));
269 const Element_type &
back()
const {
285 template <
class Iterator_element_type>
311 typename = std::enable_if_t<
312 std::is_const<Iterator_element_type>::value &&
314 std::remove_const_t<Iterator_element_type>>::value>>
399 return *(*
this + idx);
446 return std::make_reverse_iterator(
end());
449 return std::make_reverse_iterator(
begin());
452 return std::make_reverse_iterator(
end());
455 return std::make_reverse_iterator(
begin());
493 return erase(position, std::next(position));
510 return begin() + idx;
527 return begin() + idx;
530 template <
class ForwardIt>
533 for (ForwardIt it = first; it !=
last; ++it) {
538 return begin() + idx;
556 sizeof(Element_type)));
614 Element_type &
get(
size_t physical_idx)
const {
627template <
class Element_type>
629 if (m_blocks ==
nullptr) {
630 return add_initial_block();
632 Block *new_blocks = m_root->ArrayAlloc<
Block>(num_blocks() + 1);
633 if (new_blocks ==
nullptr) {
636 memcpy(new_blocks, m_blocks,
sizeof(
Block) * num_blocks());
637 if (new_blocks[num_blocks()].
init(m_root)) {
641 m_blocks = new_blocks;
642 m_capacity += block_elements;
646template <
class Element_type>
648 if (m_blocks ==
nullptr) {
649 if (add_initial_block()) {
652 if (m_begin_idx == 0) {
654 m_begin_idx = m_end_idx = 1;
658 Block *new_blocks = m_root->ArrayAlloc<
Block>(num_blocks() + 1);
659 memcpy(new_blocks + 1, m_blocks,
sizeof(
Block) * num_blocks());
660 if (new_blocks[0].
init(m_root)) {
664 m_blocks = new_blocks;
665 m_begin_idx += block_elements;
666 m_end_idx += block_elements;
667 m_capacity += block_elements;
static mysql_service_status_t init()
Component initialization.
Definition: audit_api_message_emit.cc:570
Definition: mem_root_deque.h:286
Iterator_element_type * operator->() const
Definition: mem_root_deque.h:343
bool operator>(const Iterator &other) const
Definition: mem_root_deque.h:410
bool operator>=(const Iterator &other) const
Definition: mem_root_deque.h:416
bool operator==(const Iterator &other) const
Definition: mem_root_deque.h:334
Iterator operator+(difference_type offset)
Definition: mem_root_deque.h:382
Iterator operator--(int)
Definition: mem_root_deque.h:362
bool operator<(const Iterator &other) const
Definition: mem_root_deque.h:402
size_t m_generation
Definition: mem_root_deque.h:422
difference_type operator-(const Iterator &other) const
Definition: mem_root_deque.h:392
std::random_access_iterator_tag iterator_category
Definition: mem_root_deque.h:292
Iterator(const mem_root_deque *deque, size_t physical_idx)
Definition: mem_root_deque.h:297
bool operator<=(const Iterator &other) const
Definition: mem_root_deque.h:408
Iterator_element_type * pointer
Definition: mem_root_deque.h:290
Iterator_element_type & operator[](size_t idx) const
Definition: mem_root_deque.h:398
Iterator & operator--()
Definition: mem_root_deque.h:357
Iterator operator-(difference_type offset)
Definition: mem_root_deque.h:387
void assert_not_invalidated() const
Definition: mem_root_deque.h:425
Iterator & operator++()
Definition: mem_root_deque.h:327
Iterator_element_type & operator*() const
Definition: mem_root_deque.h:323
Iterator & operator-=(difference_type diff)
Definition: mem_root_deque.h:376
Iterator_element_type & reference
Definition: mem_root_deque.h:291
Iterator operator++(int)
Definition: mem_root_deque.h:349
Iterator(const T &other)
For const_iterator: Implicit conversion from iterator.
Definition: mem_root_deque.h:315
Iterator & operator+=(difference_type diff)
Definition: mem_root_deque.h:370
const mem_root_deque * m_deque
Definition: mem_root_deque.h:419
ptrdiff_t difference_type
Definition: mem_root_deque.h:288
Iterator_element_type value_type
Definition: mem_root_deque.h:289
size_t m_physical_idx
Definition: mem_root_deque.h:420
bool operator!=(const Iterator &other) const
Definition: mem_root_deque.h:341
A (partial) implementation of std::deque allocating its blocks on a MEM_ROOT.
Definition: mem_root_deque.h:109
Element_type & operator[](size_t idx) const
Definition: mem_root_deque.h:168
reverse_iterator rbegin()
Definition: mem_root_deque.h:439
iterator insert(const_iterator pos, Element_type &&value)
Insert an element at a given position.
Definition: mem_root_deque.h:522
std::reverse_iterator< const_iterator > reverse_const_iterator
Definition: mem_root_deque.h:435
const Element_type * const_pointer
Definition: mem_root_deque.h:117
void invalidate_iterators()
Definition: mem_root_deque.h:586
iterator erase(const_iterator first, const_iterator last)
Erase all the elements in the specified range.
Definition: mem_root_deque.h:469
size_t size() const
Definition: mem_root_deque.h:458
iterator insert(const_iterator pos, ForwardIt first, ForwardIt last)
Definition: mem_root_deque.h:531
bool push_back(Element_type &&element)
Adds the given element to the end of the deque.
Definition: mem_root_deque.h:193
static constexpr size_t block_elements
Number of elements in each block.
Definition: mem_root_deque.h:543
reverse_const_iterator rbegin() const
Definition: mem_root_deque.h:451
size_t size_type
Used to conform to STL algorithm demands.
Definition: mem_root_deque.h:112
reverse_const_iterator rend() const
Definition: mem_root_deque.h:454
reverse_const_iterator crbegin() const
Definition: mem_root_deque.h:445
Element_type & front()
Returns the first element in the deque.
Definition: mem_root_deque.h:253
bool add_initial_block()
Adds the first block of elements.
Definition: mem_root_deque.h:592
bool add_block_back()
Definition: mem_root_deque.h:628
const_iterator cend()
Definition: mem_root_deque.h:442
size_t m_begin_idx
Physical index to the first valid element.
Definition: mem_root_deque.h:567
const Element_type & back() const
Definition: mem_root_deque.h:269
mem_root_deque(const mem_root_deque &other)
Definition: mem_root_deque.h:125
size_t m_end_idx
Physical index one past the last valid element.
Definition: mem_root_deque.h:571
Element_type value_type
Definition: mem_root_deque.h:114
bool push_back(const Element_type &element)
Adds the given element to the end of the deque.
Definition: mem_root_deque.h:176
void clear()
Removes all elements from the deque.
Definition: mem_root_deque.h:277
reverse_iterator rend()
Definition: mem_root_deque.h:440
ptrdiff_t difference_type
Definition: mem_root_deque.h:113
std::reverse_iterator< iterator > reverse_iterator
Definition: mem_root_deque.h:434
iterator erase(const_iterator position)
Removes a single element from the array.
Definition: mem_root_deque.h:492
const Element_type & front() const
Definition: mem_root_deque.h:258
const Element_type & const_reference
Definition: mem_root_deque.h:118
Block * m_blocks
Pointer to the first block.
Definition: mem_root_deque.h:564
mem_root_deque & operator=(mem_root_deque &&other)
Definition: mem_root_deque.h:158
const_iterator end() const
Definition: mem_root_deque.h:444
iterator insert(const_iterator pos, const Element_type &value)
Insert an element at a given position.
Definition: mem_root_deque.h:505
bool push_front(const Element_type &element)
Adds the given element to the beginning of the deque.
Definition: mem_root_deque.h:210
mem_root_deque(MEM_ROOT *mem_root)
Constructor. Leaves the array in an empty, valid state.
Definition: mem_root_deque.h:121
mem_root_deque & operator=(const mem_root_deque &other)
Definition: mem_root_deque.h:138
size_t m_capacity
Number of blocks, multiplied by block_elements.
Definition: mem_root_deque.h:575
Element_type & reference
Definition: mem_root_deque.h:116
Element_type & back()
Returns the last element in the deque.
Definition: mem_root_deque.h:264
Element_type * pointer
Definition: mem_root_deque.h:115
reverse_const_iterator crend() const
Definition: mem_root_deque.h:448
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:614
const_iterator begin() const
Definition: mem_root_deque.h:443
~mem_root_deque()
Definition: mem_root_deque.h:166
const_iterator cbegin()
Definition: mem_root_deque.h:441
void pop_front()
Removes the first element from the deque.
Definition: mem_root_deque.h:246
bool empty() const
Definition: mem_root_deque.h:459
bool push_front(Element_type &&element)
Adds the given element to the end of the deque.
Definition: mem_root_deque.h:226
size_t num_blocks() const
Definition: mem_root_deque.h:609
iterator begin()
Definition: mem_root_deque.h:437
size_t generation() const
Definition: mem_root_deque.h:620
MEM_ROOT * m_root
Pointer to the MEM_ROOT that we store our blocks and elements on.
Definition: mem_root_deque.h:578
void pop_back()
Removes the last element from the deque.
Definition: mem_root_deque.h:239
bool add_block_front()
Definition: mem_root_deque.h:647
iterator end()
Definition: mem_root_deque.h:438
size_t m_generation
Incremented each time we make an operation that would invalidate iterators.
Definition: mem_root_deque.h:585
mem_root_deque(mem_root_deque &&other)
Definition: mem_root_deque.h:149
static MEM_ROOT mem_root
Definition: client_plugin.cc:109
static Bigint * diff(Bigint *a, Bigint *b, Stack_alloc *alloc)
Definition: dtoa.cc:1082
static constexpr size_t FindElementsPerBlock()
Definition: mem_root_deque.h:36
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:183
static mysql_service_status_t destroy(reference_caching_channel channel) noexcept
Definition: component.cc:49
The MEM_ROOT is a simple arena, where allocations are carved out of larger blocks.
Definition: my_alloc.h:82
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:179
void * Alloc(size_t length)
Allocate memory.
Definition: my_alloc.h:144
Definition: mem_root_deque.h:549
Element_type * elements
Definition: mem_root_deque.h:550
bool init(MEM_ROOT *mem_root)
Definition: mem_root_deque.h:552