18#include <initializer_list>
24#include "pw_allocator/allocator.h"
25#include "pw_assert/assert.h"
26#include "pw_containers/internal/generic_deque.h"
27#include "pw_numeric/saturating_arithmetic.h"
31namespace containers::internal {
32template <
typename,
typename>
70template <
typename ValueType,
typename SizeType = u
int16_t>
72 :
public containers::internal::
73 GenericDeque<DynamicDeque<ValueType, SizeType>, ValueType, SizeType> {
75 using Base = containers::internal::
76 GenericDeque<DynamicDeque<ValueType, SizeType>, ValueType, SizeType>;
79 using typename Base::const_iterator;
80 using typename Base::const_pointer;
81 using typename Base::const_reference;
82 using typename Base::difference_type;
83 using typename Base::iterator;
84 using typename Base::pointer;
85 using typename Base::reference;
86 using typename Base::size_type;
87 using typename Base::value_type;
96 :
Base(0), allocator_(&allocator), buffer_(
nullptr) {}
105 :
Base(0), allocator_(other.allocator_), buffer_(other.buffer_) {
106 other.buffer_ =
nullptr;
107 Base::MoveAssignIndices(other);
117 using Base::try_emplace_back;
118 using Base::try_emplace_front;
120 using Base::try_push_back;
121 using Base::try_push_front;
122 using Base::try_resize;
127 template <
typename InputIt,
128 typename = containers::internal::EnableIfInputIterator<InputIt>>
129 iterator insert(const_iterator pos, InputIt first, InputIt last);
131 iterator insert(const_iterator pos,
const value_type& value) {
135 iterator insert(const_iterator pos, value_type&& value) {
139 iterator insert(const_iterator pos,
141 const value_type& value) {
145 iterator insert(const_iterator pos, std::initializer_list<value_type> ilist) {
174 return new_capacity <=
Base::capacity() || IncreaseCapacity(new_capacity);
200 constexpr size_type max_size() const noexcept {
201 return std::numeric_limits<size_type>::max();
209 Base::SwapIndices(other);
210 std::swap(allocator_, other.allocator_);
211 std::swap(buffer_, other.buffer_);
217 template <
typename,
typename>
220 template <
typename,
typename>
223 static constexpr bool kFixedCapacity =
false;
234 pointer data() {
return std::launder(
reinterpret_cast<pointer
>(buffer_)); }
235 const_pointer data()
const {
236 return std::launder(
reinterpret_cast<const_pointer
>(buffer_));
239 [[nodiscard]]
bool IncreaseCapacity(size_type new_capacity);
241 size_type GetNewCapacity(
const size_type new_size) {
244 return std::max(size_type{4 *
sizeof(
void*) /
sizeof(value_type)},
251 bool ReallocateBuffer(size_type new_capacity);
253 void DeallocateBuffer() {
256 Base::HandleShrunkBuffer(0);
259 Allocator* allocator_;
263template <
typename ValueType,
typename SizeType>
264DynamicDeque<ValueType, SizeType>& DynamicDeque<ValueType, SizeType>::operator=(
265 DynamicDeque&& other)
noexcept {
267 allocator_->Deallocate(buffer_);
269 allocator_ = other.allocator_;
270 buffer_ = std::exchange(other.buffer_,
nullptr);
272 Base::MoveAssignIndices(other);
276template <
typename ValueType,
typename SizeType>
277DynamicDeque<ValueType, SizeType>::~DynamicDeque() {
279 allocator_->Deallocate(buffer_);
282template <
typename ValueType,
typename SizeType>
284 return new_capacity <= Base::capacity() ||
285 IncreaseCapacity(GetNewCapacity(new_capacity)) ||
286 IncreaseCapacity(new_capacity);
289template <
typename ValueType,
typename SizeType>
291 size_type new_capacity) {
293 if (buffer_ !=
nullptr && Base::CanExtendBuffer() &&
294 allocator_->Resize(buffer_, new_capacity *
sizeof(value_type))) {
295 Base::HandleExtendedBuffer(new_capacity);
300 return ReallocateBuffer(new_capacity);
303template <
typename ValueType,
typename SizeType>
305 if (Base::size() == Base::capacity()) {
318 if (Base::CanShrinkBuffer() &&
319 allocator_->Resize(buffer_, Base::size() *
sizeof(value_type))) {
320 Base::HandleShrunkBuffer(Base::size());
323 ReallocateBuffer(Base::size());
327template <
typename ValueType,
typename SizeType>
329 size_type new_capacity) {
330 std::byte* new_buffer =
static_cast<std::byte*
>(
331 allocator_->Allocate(allocator::Layout::Of<value_type[]>(new_capacity)));
332 if (new_buffer ==
nullptr) {
336 pointer dest = std::launder(
reinterpret_cast<pointer
>(new_buffer));
337 auto [data_1, data_2] = Base::contiguous_data();
339 if constexpr (std::is_move_constructible_v<value_type>) {
340 dest = std::uninitialized_move(data_1.begin(), data_1.end(), dest);
341 std::uninitialized_move(data_2.begin(), data_2.end(), dest);
343 dest = std::uninitialized_copy(data_1.begin(), data_1.end(), dest);
344 std::uninitialized_copy(data_2.begin(), data_2.end(), dest);
347 std::destroy(data_1.begin(), data_1.end());
348 std::destroy(data_2.begin(), data_2.end());
350 allocator_->Deallocate(buffer_);
351 buffer_ = new_buffer;
353 Base::HandleNewBuffer(new_capacity);
357template <
typename ValueType,
typename SizeType>
358template <
typename InputIt,
typename>
359typename DynamicDeque<ValueType, SizeType>::iterator
360DynamicDeque<ValueType, SizeType>::insert(const_iterator pos,
364 if constexpr (std::is_same_v<std::input_iterator_tag,
365 typename std::iterator_traits<
366 InputIt>::iterator_category>) {
370 DynamicDeque temp(*allocator_);
371 temp.assign(first, last);
372 return Base::insert(pos,
373 std::make_move_iterator(temp.data()),
374 std::make_move_iterator(temp.data() + temp.size()));
376 return Base::insert(pos, first, last);
Definition: allocator.h:42
Definition: dynamic_deque.h:73
Definition: dynamic_vector.h:63
Definition: generic_deque.h:195
constexpr size_type capacity() const noexcept
Returns the maximum number of elements in the deque.
Definition: generic_deque.h:72
Definition: generic_queue.h:25
void Deallocate(void *ptr)
Definition: deallocator.h:314
iterator insert(const_iterator pos, const value_type &value)
Definition: generic_deque.h:424
void push_back_overwrite(const value_type &value)
Definition: generic_deque.h:340
void reserve(size_type new_capacity)
Increases capacity() to at least new_capacity. Crashes on failure.
Definition: dynamic_deque.h:162
std::optional< iterator > try_insert(const_iterator pos, const value_type &value)
Definition: generic_deque.h:542
bool try_assign(size_type count, const value_type &value)
Definition: generic_deque.h:691
void emplace_front_overwrite(Args &&... args)
Definition: generic_deque.h:396
void reserve_exact(size_type new_capacity)
Increases capacity() to exactly new_capacity. Crashes on failure.
Definition: dynamic_deque.h:178
constexpr DynamicDeque(DynamicDeque &&other) noexcept
Definition: dynamic_deque.h:104
std::optional< iterator > try_emplace(const_iterator pos, Args &&... args)
void swap(DynamicDeque &other) noexcept
Swaps the contents of two deques. No allocations occur.
Definition: dynamic_deque.h:208
bool try_reserve(size_type new_capacity)
Definition: dynamic_deque.h:283
void shrink_to_fit()
Definition: dynamic_deque.h:304
constexpr allocator_type & get_allocator() const
Returns the deque's allocator.
Definition: dynamic_deque.h:205
void reset()
Clears the deque and deallocates its buffer.
Definition: dynamic_deque.h:195
bool try_reserve_exact(size_type new_capacity)
Definition: dynamic_deque.h:173
constexpr DynamicDeque(Allocator &allocator) noexcept
Definition: dynamic_deque.h:95
void emplace_back_overwrite(Args &&... args)
Definition: generic_deque.h:353
void push_front_overwrite(const value_type &value)
Definition: generic_deque.h:383
constexpr T mul_sat(T lhs, T rhs) noexcept
Definition: saturating_arithmetic.h:75
The Pigweed namespace.
Definition: alignment.h:27