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) {
110template <
class Element_type>
133 for (
size_t block_idx = 0; block_idx <
num_blocks(); ++block_idx) {
137 new (&
get(idx)) Element_type(other.
get(idx));
141 if (
this != &other) {
143 for (
const Element_type &elem : other) {
156 other.m_blocks =
nullptr;
157 other.m_begin_idx = other.m_end_idx = other.m_capacity = 0;
158 other.invalidate_iterators();
161 if (
this != &other) {
201 new (&
get(
m_end_idx++)) Element_type(std::move(element));
271 const Element_type &
back()
const {
287 template <
class Iterator_element_type>
313 typename = std::enable_if_t<
314 std::is_const<Iterator_element_type>::value &&
316 std::remove_const_t<Iterator_element_type>>::value>>
401 return *(*
this + idx);
448 return std::make_reverse_iterator(
end());
451 return std::make_reverse_iterator(
begin());
454 return std::make_reverse_iterator(
end());
457 return std::make_reverse_iterator(
begin());
495 return erase(position, std::next(position));
512 return begin() + idx;
529 return begin() + idx;
532 template <
class ForwardIt>
535 for (ForwardIt it = first; it !=
last; ++it) {
540 return begin() + idx;
558 sizeof(Element_type)));
616 Element_type &
get(
size_t physical_idx)
const {
629template <
class Element_type>
631 if (m_blocks ==
nullptr) {
632 return add_initial_block();
634 Block *new_blocks = m_root->ArrayAlloc<
Block>(num_blocks() + 1);
635 if (new_blocks ==
nullptr) {
638 memcpy(new_blocks, m_blocks,
sizeof(
Block) * num_blocks());
639 if (new_blocks[num_blocks()].
init(m_root)) {
643 m_blocks = new_blocks;
644 m_capacity += block_elements;
648template <
class Element_type>
650 if (m_blocks ==
nullptr) {
651 if (add_initial_block()) {
654 if (m_begin_idx == 0) {
656 m_begin_idx = m_end_idx = 1;
660 Block *new_blocks = m_root->ArrayAlloc<
Block>(num_blocks() + 1);
661 memcpy(new_blocks + 1, m_blocks,
sizeof(
Block) * num_blocks());
662 if (new_blocks[0].
init(m_root)) {
666 m_blocks = new_blocks;
667 m_begin_idx += block_elements;
668 m_end_idx += block_elements;
669 m_capacity += block_elements;
static mysql_service_status_t init()
Component initialization.
Definition: audit_api_message_emit.cc:571
Definition: mem_root_deque.h:288
Iterator_element_type * operator->() const
Definition: mem_root_deque.h:345
bool operator>(const Iterator &other) const
Definition: mem_root_deque.h:412
Iterator operator+(difference_type offset) const
Definition: mem_root_deque.h:384
bool operator>=(const Iterator &other) const
Definition: mem_root_deque.h:418
bool operator==(const Iterator &other) const
Definition: mem_root_deque.h:336
Iterator operator--(int)
Definition: mem_root_deque.h:364
bool operator<(const Iterator &other) const
Definition: mem_root_deque.h:404
size_t m_generation
Definition: mem_root_deque.h:424
difference_type operator-(const Iterator &other) const
Definition: mem_root_deque.h:394
std::random_access_iterator_tag iterator_category
Definition: mem_root_deque.h:294
Iterator(const mem_root_deque *deque, size_t physical_idx)
Definition: mem_root_deque.h:299
bool operator<=(const Iterator &other) const
Definition: mem_root_deque.h:410
Iterator_element_type * pointer
Definition: mem_root_deque.h:292
Iterator_element_type & operator[](size_t idx) const
Definition: mem_root_deque.h:400
Iterator & operator--()
Definition: mem_root_deque.h:359
Iterator operator-(difference_type offset) const
Definition: mem_root_deque.h:389
void assert_not_invalidated() const
Definition: mem_root_deque.h:427
Iterator & operator++()
Definition: mem_root_deque.h:329
Iterator_element_type & operator*() const
Definition: mem_root_deque.h:325
Iterator & operator-=(difference_type diff)
Definition: mem_root_deque.h:378
Iterator_element_type & reference
Definition: mem_root_deque.h:293
Iterator operator++(int)
Definition: mem_root_deque.h:351
Iterator(const T &other)
For const_iterator: Implicit conversion from iterator.
Definition: mem_root_deque.h:317
Iterator & operator+=(difference_type diff)
Definition: mem_root_deque.h:372
const mem_root_deque * m_deque
Definition: mem_root_deque.h:421
ptrdiff_t difference_type
Definition: mem_root_deque.h:290
Iterator_element_type value_type
Definition: mem_root_deque.h:291
size_t m_physical_idx
Definition: mem_root_deque.h:422
bool operator!=(const Iterator &other) const
Definition: mem_root_deque.h:343
A (partial) implementation of std::deque allocating its blocks on a MEM_ROOT.
Definition: mem_root_deque.h:111
Element_type & operator[](size_t idx) const
Definition: mem_root_deque.h:170
reverse_iterator rbegin()
Definition: mem_root_deque.h:441
iterator insert(const_iterator pos, Element_type &&value)
Insert an element at a given position.
Definition: mem_root_deque.h:524
std::reverse_iterator< const_iterator > reverse_const_iterator
Definition: mem_root_deque.h:437
const Element_type * const_pointer
Definition: mem_root_deque.h:119
void invalidate_iterators()
Definition: mem_root_deque.h:588
iterator erase(const_iterator first, const_iterator last)
Erase all the elements in the specified range.
Definition: mem_root_deque.h:471
size_t size() const
Definition: mem_root_deque.h:460
iterator insert(const_iterator pos, ForwardIt first, ForwardIt last)
Definition: mem_root_deque.h:533
bool push_back(Element_type &&element)
Adds the given element to the end of the deque.
Definition: mem_root_deque.h:195
static constexpr size_t block_elements
Number of elements in each block.
Definition: mem_root_deque.h:545
reverse_const_iterator rbegin() const
Definition: mem_root_deque.h:453
size_t size_type
Used to conform to STL algorithm demands.
Definition: mem_root_deque.h:114
reverse_const_iterator rend() const
Definition: mem_root_deque.h:456
reverse_const_iterator crbegin() const
Definition: mem_root_deque.h:447
Element_type & front()
Returns the first element in the deque.
Definition: mem_root_deque.h:255
bool add_initial_block()
Adds the first block of elements.
Definition: mem_root_deque.h:594
bool add_block_back()
Definition: mem_root_deque.h:630
const_iterator cend()
Definition: mem_root_deque.h:444
size_t m_begin_idx
Physical index to the first valid element.
Definition: mem_root_deque.h:569
const Element_type & back() const
Definition: mem_root_deque.h:271
mem_root_deque(const mem_root_deque &other)
Definition: mem_root_deque.h:127
size_t m_end_idx
Physical index one past the last valid element.
Definition: mem_root_deque.h:573
Element_type value_type
Definition: mem_root_deque.h:116
bool push_back(const Element_type &element)
Adds the given element to the end of the deque.
Definition: mem_root_deque.h:178
void clear()
Removes all elements from the deque.
Definition: mem_root_deque.h:279
reverse_iterator rend()
Definition: mem_root_deque.h:442
ptrdiff_t difference_type
Definition: mem_root_deque.h:115
std::reverse_iterator< iterator > reverse_iterator
Definition: mem_root_deque.h:436
iterator erase(const_iterator position)
Removes a single element from the array.
Definition: mem_root_deque.h:494
const Element_type & front() const
Definition: mem_root_deque.h:260
const Element_type & const_reference
Definition: mem_root_deque.h:120
Block * m_blocks
Pointer to the first block.
Definition: mem_root_deque.h:566
mem_root_deque & operator=(mem_root_deque &&other)
Definition: mem_root_deque.h:160
const_iterator end() const
Definition: mem_root_deque.h:446
iterator insert(const_iterator pos, const Element_type &value)
Insert an element at a given position.
Definition: mem_root_deque.h:507
bool push_front(const Element_type &element)
Adds the given element to the beginning of the deque.
Definition: mem_root_deque.h:212
mem_root_deque(MEM_ROOT *mem_root)
Constructor. Leaves the array in an empty, valid state.
Definition: mem_root_deque.h:123
mem_root_deque & operator=(const mem_root_deque &other)
Definition: mem_root_deque.h:140
size_t m_capacity
Number of blocks, multiplied by block_elements.
Definition: mem_root_deque.h:577
Element_type & reference
Definition: mem_root_deque.h:118
Element_type & back()
Returns the last element in the deque.
Definition: mem_root_deque.h:266
Element_type * pointer
Definition: mem_root_deque.h:117
reverse_const_iterator crend() const
Definition: mem_root_deque.h:450
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:616
const_iterator begin() const
Definition: mem_root_deque.h:445
~mem_root_deque()
Definition: mem_root_deque.h:168
const_iterator cbegin()
Definition: mem_root_deque.h:443
void pop_front()
Removes the first element from the deque.
Definition: mem_root_deque.h:248
bool empty() const
Definition: mem_root_deque.h:461
bool push_front(Element_type &&element)
Adds the given element to the end of the deque.
Definition: mem_root_deque.h:228
size_t num_blocks() const
Definition: mem_root_deque.h:611
iterator begin()
Definition: mem_root_deque.h:439
size_t generation() const
Definition: mem_root_deque.h:622
MEM_ROOT * m_root
Pointer to the MEM_ROOT that we store our blocks and elements on.
Definition: mem_root_deque.h:580
void pop_back()
Removes the last element from the deque.
Definition: mem_root_deque.h:241
bool add_block_front()
Definition: mem_root_deque.h:649
iterator end()
Definition: mem_root_deque.h:440
size_t m_generation
Incremented each time we make an operation that would invalidate iterators.
Definition: mem_root_deque.h:587
mem_root_deque(mem_root_deque &&other)
Definition: mem_root_deque.h:151
static MEM_ROOT mem_root
Definition: client_plugin.cc:114
static Bigint * diff(Bigint *a, Bigint *b, Stack_alloc *alloc)
Definition: dtoa.cc:1076
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:459
uint16_t value_type
Definition: vt100.h:184
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:551
Element_type * elements
Definition: mem_root_deque.h:552
bool init(MEM_ROOT *mem_root)
Definition: mem_root_deque.h:554