5#ifndef BITCOIN_UTIL_VECDEQUE_H
6#define BITCOIN_UTIL_VECDEQUE_H
46 if constexpr (std::is_trivially_copyable_v<T>) {
49 if (first_part != 0) {
52 if (first_part !=
m_size) {
53 std::memcpy(new_buffer + first_part,
m_buffer, (
m_size - first_part) *
sizeof(
T));
58 for (
size_t new_pos = 0; new_pos <
m_size; ++new_pos) {
59 std::construct_at(new_buffer + new_pos, std::move(*(
m_buffer + old_pos)));
92 if constexpr (std::is_trivially_destructible_v<T>) {
138 if (&other ==
this) [[unlikely]]
return *
this;
141 if constexpr (std::is_trivially_copyable_v<T>) {
144 if (first_part != 0) {
147 if (first_part != other.
m_size) {
163 std::swap(
m_buffer, other.m_buffer);
164 std::swap(
m_offset, other.m_offset);
165 std::swap(
m_size, other.m_size);
188 for (
size_t i = 0; i < a.
m_size; ++i) {
189 if (a[i] != b[i])
return false;
197 size_t pos_a{0}, pos_b{0};
199 auto cmp = a[pos_a++] <=> b[pos_b++];
200 if (cmp != 0)
return cmp;
218 template<
typename... Args>
233 template<
typename... Args>
#define Assume(val)
Assume is the identity function.
Data structure largely mimicking std::deque, but using single preallocated ring buffer.
std::strong_ordering friend operator<=>(const VecDeque &a, const VecDeque &b)
Comparison between two deques, implementing lexicographic ordering on the contents.
void ResizeDown(size_t size) noexcept
Specialization of resize() that can only shrink.
size_t BufferIndex(size_t pos) const noexcept
What index in the buffer does logical entry number pos have?
T & operator[](size_t idx) noexcept
Get a mutable reference to the element in the deque at the given index.
bool empty() const noexcept
Test whether the contents of this deque is empty.
void clear() noexcept
Resize the deque to be size 0.
const T & operator[](size_t idx) const noexcept
Get a const reference to the element in the deque at the given index.
T * m_buffer
Pointer to allocated memory.
bool friend operator==(const VecDeque &a, const VecDeque &b)
Equality comparison between two deques (only compares size+contents, not capacity).
VecDeque(const VecDeque &other)
Copy-construct a deque.
size_t m_offset
m_buffer + m_offset points to first object in queue.
VecDeque(VecDeque &&other) noexcept
Move-construct a deque.
void push_back(T &&elem)
Move-construct a new element at the end of the deque.
void pop_front()
Remove the first element of the deque.
size_t size() const noexcept
Get the number of elements in this deque.
size_t m_size
Number of objects in the container.
void resize(size_t size)
Resize the deque to be exactly size size (adding default-constructed elements if needed).
void pop_back()
Remove the last element of the deque.
void emplace_back(Args &&... args)
Construct a new element at the end of the deque.
friend void swap(VecDeque &a, VecDeque &b) noexcept
Non-member version of swap.
size_t m_capacity
The size of m_buffer, expressed as a multiple of the size of T.
T & front() noexcept
Get a mutable reference to the first element of the deque.
VecDeque & operator=(VecDeque &&other) noexcept
Move-assign a deque.
VecDeque() noexcept=default
void reserve(size_t capacity)
Increase the capacity to capacity.
size_t capacity() const noexcept
Get the capacity of this deque (maximum size it can have without reallocating).
const T & back() const noexcept
Get a const reference to the last element of the deque.
void push_back(const T &elem)
Copy-construct a new element at the end of the deque.
size_t FirstPart() const noexcept
Returns the number of populated objects between m_offset and the end of the buffer.
void Reallocate(size_t capacity)
VecDeque & operator=(const VecDeque &other)
Copy-assign a deque.
void emplace_front(Args &&... args)
Construct a new element at the beginning of the deque.
const T & front() const noexcept
Get a const reference to the first element of the deque.
~VecDeque()
Destroy a deque.
T & back() noexcept
Get a mutable reference to the last element of the deque.
void push_front(T &&elem)
Move-construct a new element at the beginning of the deque.
void push_front(const T &elem)
Copy-construct a new element at the beginning of the deque.
void shrink_to_fit()
Make the capacity equal to the size.
void swap(VecDeque &other) noexcept
Swap two deques.