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) {
170template <
class Element_type>
197 for (
size_t block_idx = 0; block_idx <
num_blocks(); ++block_idx) {
201 new (&
get(idx)) Element_type(other.
get(idx));
205 if (
this != &other) {
207 for (
const Element_type &elem : other) {
225 other.m_blocks =
nullptr;
226 other.m_begin_idx = other.m_end_idx = other.m_capacity = 0;
227 other.m_block_slots = other.m_first_block_idx = 0;
228 other.m_back_growth_exp = other.m_front_growth_exp = 0;
229 other.invalidate_iterators();
232 if (
this != &other) {
272 new (&
get(
m_end_idx++)) Element_type(std::move(element));
342 const Element_type &
back()
const {
364 template <
class Iterator_element_type>
390 typename = std::enable_if_t<
391 std::is_const<Iterator_element_type>::value &&
393 std::remove_const_t<Iterator_element_type>>::value>>
478 return *(*
this + idx);
525 return std::make_reverse_iterator(
end());
528 return std::make_reverse_iterator(
begin());
531 return std::make_reverse_iterator(
end());
534 return std::make_reverse_iterator(
begin());
572 return erase(position, std::next(position));
589 return begin() + idx;
606 return begin() + idx;
609 template <
class ForwardIt>
612 for (ForwardIt it = first; it !=
last; ++it) {
617 return begin() + idx;
635 sizeof(Element_type)));
714 Element_type &
get(
size_t physical_idx)
const {
724template <
class Element_type>
726 if (m_blocks ==
nullptr) {
727 return add_initial_block();
733 size_t next_block_idx = m_first_block_idx + num_blocks();
734 if (next_block_idx < m_block_slots) {
736 if (m_blocks[next_block_idx].
init(m_root)) {
739 m_capacity += block_elements;
745 size_t growth =
size_t{1} << m_back_growth_exp;
746 size_t new_blocks_allocated = m_block_slots + growth;
747 Block *new_blocks = m_root->ArrayAlloc<
Block>(new_blocks_allocated);
748 if (new_blocks ==
nullptr) {
752 std::copy_n(m_blocks + m_first_block_idx, num_blocks(),
753 new_blocks + m_first_block_idx);
755 if (new_blocks[next_block_idx].
init(m_root)) {
759 m_blocks = new_blocks;
760 m_block_slots = new_blocks_allocated;
761 m_capacity += block_elements;
766template <
class Element_type>
768 if (m_blocks ==
nullptr) {
769 if (add_initial_block()) {
772 if (m_begin_idx == 0) {
774 m_begin_idx = m_end_idx = 1;
780 if (m_first_block_idx > 0) {
783 if (m_blocks[m_first_block_idx].
init(m_root)) {
787 m_begin_idx += block_elements;
788 m_end_idx += block_elements;
789 m_capacity += block_elements;
795 size_t growth =
size_t{1} << m_front_growth_exp;
796 size_t new_blocks_allocated = m_block_slots + growth;
797 Block *new_blocks = m_root->ArrayAlloc<
Block>(new_blocks_allocated);
798 if (new_blocks ==
nullptr) {
804 size_t new_first_block_idx = growth - 1;
805 std::copy_n(m_blocks + m_first_block_idx, num_blocks(),
806 new_blocks + new_first_block_idx + 1);
808 if (new_blocks[new_first_block_idx].
init(m_root)) {
812 m_blocks = new_blocks;
813 m_block_slots = new_blocks_allocated;
814 m_first_block_idx = new_first_block_idx;
815 m_begin_idx += block_elements;
816 m_end_idx += block_elements;
817 m_capacity += block_elements;
818 ++m_front_growth_exp;
static mysql_service_status_t init()
Component initialization.
Definition: audit_api_message_emit.cc:571
Definition: mem_root_deque.h:365
Iterator_element_type * operator->() const
Definition: mem_root_deque.h:422
bool operator>(const Iterator &other) const
Definition: mem_root_deque.h:489
Iterator operator+(difference_type offset) const
Definition: mem_root_deque.h:461
bool operator>=(const Iterator &other) const
Definition: mem_root_deque.h:495
bool operator==(const Iterator &other) const
Definition: mem_root_deque.h:413
Iterator operator--(int)
Definition: mem_root_deque.h:441
bool operator<(const Iterator &other) const
Definition: mem_root_deque.h:481
size_t m_generation
Definition: mem_root_deque.h:501
difference_type operator-(const Iterator &other) const
Definition: mem_root_deque.h:471
std::random_access_iterator_tag iterator_category
Definition: mem_root_deque.h:371
Iterator(const mem_root_deque *deque, size_t physical_idx)
Definition: mem_root_deque.h:376
bool operator<=(const Iterator &other) const
Definition: mem_root_deque.h:487
Iterator_element_type * pointer
Definition: mem_root_deque.h:369
Iterator_element_type & operator[](size_t idx) const
Definition: mem_root_deque.h:477
Iterator & operator--()
Definition: mem_root_deque.h:436
Iterator operator-(difference_type offset) const
Definition: mem_root_deque.h:466
void assert_not_invalidated() const
Definition: mem_root_deque.h:504
Iterator & operator++()
Definition: mem_root_deque.h:406
Iterator_element_type & operator*() const
Definition: mem_root_deque.h:402
Iterator & operator-=(difference_type diff)
Definition: mem_root_deque.h:455
Iterator_element_type & reference
Definition: mem_root_deque.h:370
Iterator operator++(int)
Definition: mem_root_deque.h:428
Iterator(const T &other)
For const_iterator: Implicit conversion from iterator.
Definition: mem_root_deque.h:394
Iterator & operator+=(difference_type diff)
Definition: mem_root_deque.h:449
const mem_root_deque * m_deque
Definition: mem_root_deque.h:498
ptrdiff_t difference_type
Definition: mem_root_deque.h:367
Iterator_element_type value_type
Definition: mem_root_deque.h:368
size_t m_physical_idx
Definition: mem_root_deque.h:499
bool operator!=(const Iterator &other) const
Definition: mem_root_deque.h:420
A (partial) implementation of std::deque allocating its blocks on a MEM_ROOT.
Definition: mem_root_deque.h:171
uint8_t m_back_growth_exp
Growth exponent for back expansion.
Definition: mem_root_deque.h:670
Element_type & operator[](size_t idx) const
Definition: mem_root_deque.h:241
reverse_iterator rbegin()
Definition: mem_root_deque.h:518
iterator insert(const_iterator pos, Element_type &&value)
Insert an element at a given position.
Definition: mem_root_deque.h:601
std::reverse_iterator< const_iterator > reverse_const_iterator
Definition: mem_root_deque.h:514
const Element_type * const_pointer
Definition: mem_root_deque.h:179
void invalidate_iterators()
Definition: mem_root_deque.h:685
size_t m_first_block_idx
Index in m_blocks where the first initialized block is stored.
Definition: mem_root_deque.h:665
iterator erase(const_iterator first, const_iterator last)
Erase all the elements in the specified range.
Definition: mem_root_deque.h:548
size_t size() const
Definition: mem_root_deque.h:537
iterator insert(const_iterator pos, ForwardIt first, ForwardIt last)
Definition: mem_root_deque.h:610
uint8_t back_growth_exp() const
Definition: mem_root_deque.h:360
size_t m_block_slots
Number of Block slots allocated in m_blocks array.
Definition: mem_root_deque.h:659
bool push_back(Element_type &&element)
Adds the given element to the end of the deque.
Definition: mem_root_deque.h:266
static constexpr size_t block_elements
Number of elements in each block.
Definition: mem_root_deque.h:622
reverse_const_iterator rbegin() const
Definition: mem_root_deque.h:530
size_t size_type
Used to conform to STL algorithm demands.
Definition: mem_root_deque.h:174
reverse_const_iterator rend() const
Definition: mem_root_deque.h:533
reverse_const_iterator crbegin() const
Definition: mem_root_deque.h:524
Element_type & front()
Returns the first element in the deque.
Definition: mem_root_deque.h:326
bool add_initial_block()
Adds the first block of elements.
Definition: mem_root_deque.h:691
bool add_block_back()
Definition: mem_root_deque.h:725
const_iterator cend()
Definition: mem_root_deque.h:521
size_t capacity() const
Definition: mem_root_deque.h:362
size_t m_begin_idx
Physical index to the first valid element.
Definition: mem_root_deque.h:646
uint8_t front_growth_exp() const
Definition: mem_root_deque.h:361
const Element_type & back() const
Definition: mem_root_deque.h:342
uint8_t m_front_growth_exp
Growth exponent for front expansion.
Definition: mem_root_deque.h:674
mem_root_deque(const mem_root_deque &other)
Definition: mem_root_deque.h:187
size_t m_end_idx
Physical index one past the last valid element.
Definition: mem_root_deque.h:650
Element_type value_type
Definition: mem_root_deque.h:176
bool push_back(const Element_type &element)
Adds the given element to the end of the deque.
Definition: mem_root_deque.h:249
void clear()
Removes all elements from the deque.
Definition: mem_root_deque.h:350
reverse_iterator rend()
Definition: mem_root_deque.h:519
ptrdiff_t difference_type
Definition: mem_root_deque.h:175
std::reverse_iterator< iterator > reverse_iterator
Definition: mem_root_deque.h:513
iterator erase(const_iterator position)
Removes a single element from the array.
Definition: mem_root_deque.h:571
const Element_type & front() const
Definition: mem_root_deque.h:331
const Element_type & const_reference
Definition: mem_root_deque.h:180
Block * m_blocks
Pointer to the first block.
Definition: mem_root_deque.h:643
mem_root_deque & operator=(mem_root_deque &&other)
Definition: mem_root_deque.h:231
size_t first_block_idx() const
Definition: mem_root_deque.h:359
const_iterator end() const
Definition: mem_root_deque.h:523
size_t block_slots() const
Definition: mem_root_deque.h:358
iterator insert(const_iterator pos, const Element_type &value)
Insert an element at a given position.
Definition: mem_root_deque.h:584
bool push_front(const Element_type &element)
Adds the given element to the beginning of the deque.
Definition: mem_root_deque.h:283
mem_root_deque(MEM_ROOT *mem_root)
Constructor. Leaves the array in an empty, valid state.
Definition: mem_root_deque.h:183
mem_root_deque & operator=(const mem_root_deque &other)
Definition: mem_root_deque.h:204
size_t m_capacity
Number of blocks, multiplied by block_elements.
Definition: mem_root_deque.h:654
Element_type & reference
Definition: mem_root_deque.h:178
Element_type & back()
Returns the last element in the deque.
Definition: mem_root_deque.h:337
Element_type * pointer
Definition: mem_root_deque.h:177
reverse_const_iterator crend() const
Definition: mem_root_deque.h:527
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:714
const_iterator begin() const
Definition: mem_root_deque.h:522
~mem_root_deque()
Definition: mem_root_deque.h:239
const_iterator cbegin()
Definition: mem_root_deque.h:520
void pop_front()
Removes the first element from the deque.
Definition: mem_root_deque.h:319
bool empty() const
Definition: mem_root_deque.h:538
bool push_front(Element_type &&element)
Adds the given element to the end of the deque.
Definition: mem_root_deque.h:299
size_t num_blocks() const
Definition: mem_root_deque.h:709
iterator begin()
Definition: mem_root_deque.h:516
size_t generation() const
Definition: mem_root_deque.h:720
MEM_ROOT * m_root
Pointer to the MEM_ROOT that we store our blocks and elements on.
Definition: mem_root_deque.h:677
void pop_back()
Removes the last element from the deque.
Definition: mem_root_deque.h:312
bool add_block_front()
Definition: mem_root_deque.h:767
iterator end()
Definition: mem_root_deque.h:517
size_t m_generation
Incremented each time we make an operation that would invalidate iterators.
Definition: mem_root_deque.h:684
mem_root_deque(mem_root_deque &&other)
Definition: mem_root_deque.h:215
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:628
Element_type * elements
Definition: mem_root_deque.h:629
bool init(MEM_ROOT *mem_root)
Definition: mem_root_deque.h:631