5 #ifndef BITCOIN_UTIL_VECDEQUE_H 6 #define BITCOIN_UTIL_VECDEQUE_H 12 #include <type_traits> 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>
317 #endif // BITCOIN_UTIL_VECDEQUE_H 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.
friend void swap(VecDeque &a, VecDeque &b) noexcept
Non-member version of swap.
size_t FirstPart() const noexcept
Returns the number of populated objects between m_offset and the end of the buffer.
size_t size() const noexcept
Get the number of elements in this deque.
size_t BufferIndex(size_t pos) const noexcept
What index in the buffer does logical entry number pos have?
Data structure largely mimicking std::deque, but using single preallocated ring buffer.
void resize(size_t size)
Resize the deque to be exactly size size (adding default-constructed elements if needed).
T & front() noexcept
Get a mutable reference to the first element of the deque.
void pop_back()
Remove the last element of the deque.
T & back() noexcept
Get a mutable reference to the last element of the deque.
T & operator[](size_t idx) noexcept
Get a mutable reference to the element in the deque at the given index.
void push_front(T &&elem)
Move-construct a new element at the beginning of the deque.
const T & operator[](size_t idx) const noexcept
Get a const reference to the element in the deque at the given index.
VecDeque(const VecDeque &other)
Copy-construct a deque.
bool empty() const noexcept
Test whether the contents of this deque is empty.
~VecDeque()
Destroy a deque.
VecDeque & operator=(const VecDeque &other)
Copy-assign a deque.
void emplace_back(Args &&... args)
Construct a new element at the end of the deque.
size_t m_offset
m_buffer + m_offset points to first object in queue.
VecDeque() noexcept=default
void reserve(size_t capacity)
Increase the capacity to capacity.
void shrink_to_fit()
Make the capacity equal to the size.
size_t m_size
Number of objects in the container.
#define Assume(val)
Assume is the identity function.
VecDeque & operator=(VecDeque &&other) noexcept
Move-assign a deque.
VecDeque(VecDeque &&other) noexcept
Move-construct a deque.
const T & back() const noexcept
Get a const reference to the last element of the deque.
void pop_front()
Remove the first element of the deque.
void Reallocate(size_t capacity)
size_t m_capacity
The size of m_buffer, expressed as a multiple of the size of T.
void clear() noexcept
Resize the deque to be size 0.
void swap(VecDeque &other) noexcept
Swap two deques.
void ResizeDown(size_t size) noexcept
Specialization of resize() that can only shrink.
void push_front(const T &elem)
Copy-construct a new element at the beginning of the deque.
size_t capacity() const noexcept
Get the capacity of this deque (maximum size it can have without reallocating).
void push_back(const T &elem)
Copy-construct a new element at the end of the deque.
void push_back(T &&elem)
Move-construct a new element at the end of the deque.
bool friend operator==(const VecDeque &a, const VecDeque &b)
Equality comparison between two deques (only compares size+contents, not capacity).
T * m_buffer
Pointer to allocated memory.