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"
55template <
typename ValueType,
typename SizeType = u
int16_t>
57 :
public containers::internal::
58 GenericDeque<DynamicDeque<ValueType, SizeType>, ValueType, SizeType> {
60 using Base = containers::internal::
61 GenericDeque<DynamicDeque<ValueType, SizeType>, ValueType, SizeType>;
64 using typename Base::const_iterator;
65 using typename Base::const_pointer;
66 using typename Base::const_reference;
67 using typename Base::difference_type;
68 using typename Base::iterator;
69 using typename Base::pointer;
70 using typename Base::reference;
71 using typename Base::size_type;
72 using typename Base::value_type;
81 :
Base(0), allocator_(&allocator), buffer_(
nullptr) {}
90 :
Base(0), allocator_(other.allocator_), buffer_(other.buffer_) {
91 other.buffer_ =
nullptr;
92 Base::MoveAssignIndices(other);
102 using Base::try_emplace_back;
103 using Base::try_emplace_front;
105 using Base::try_push_back;
106 using Base::try_push_front;
107 using Base::try_resize;
112 template <
typename InputIt,
113 typename = containers::internal::EnableIfInputIterator<InputIt>>
114 iterator insert(const_iterator pos, InputIt first, InputIt last);
116 iterator insert(const_iterator pos,
const value_type& value) {
120 iterator insert(const_iterator pos, value_type&& value) {
124 iterator insert(const_iterator pos,
126 const value_type& value) {
130 iterator insert(const_iterator pos, std::initializer_list<value_type> ilist) {
159 return new_capacity <=
Base::capacity() || IncreaseCapacity(new_capacity);
170 constexpr size_type max_size() const noexcept {
171 return std::numeric_limits<size_type>::max();
179 Base::SwapIndices(other);
180 std::swap(allocator_, other.allocator_);
181 std::swap(buffer_, other.buffer_);
187 template <
typename,
typename>
190 static constexpr bool kFixedCapacity =
false;
195 pointer data() {
return std::launder(
reinterpret_cast<pointer
>(buffer_)); }
196 const_pointer data()
const {
197 return std::launder(
reinterpret_cast<const_pointer
>(buffer_));
200 [[nodiscard]]
bool IncreaseCapacity(size_type new_capacity);
202 size_type GetNewCapacity(
const size_type new_size) {
205 return std::max(size_type{4 *
sizeof(
void*) /
sizeof(value_type)},
212 bool ReallocateBuffer(size_type new_capacity);
214 Allocator* allocator_;
218template <
typename ValueType,
typename SizeType>
219DynamicDeque<ValueType, SizeType>& DynamicDeque<ValueType, SizeType>::operator=(
220 DynamicDeque&& other)
noexcept {
222 allocator_->Deallocate(buffer_);
224 allocator_ = other.allocator_;
225 buffer_ = std::exchange(other.buffer_,
nullptr);
227 Base::MoveAssignIndices(other);
231template <
typename ValueType,
typename SizeType>
232DynamicDeque<ValueType, SizeType>::~DynamicDeque() {
234 allocator_->Deallocate(buffer_);
237template <
typename ValueType,
typename SizeType>
239 return new_capacity <= Base::capacity() ||
240 IncreaseCapacity(GetNewCapacity(new_capacity)) ||
241 IncreaseCapacity(new_capacity);
244template <
typename ValueType,
typename SizeType>
246 size_type new_capacity) {
248 if (buffer_ !=
nullptr && Base::CanExtendBuffer() &&
249 allocator_->Resize(buffer_, new_capacity *
sizeof(value_type))) {
250 Base::HandleExtendedBuffer(new_capacity);
255 return ReallocateBuffer(new_capacity);
258template <
typename ValueType,
typename SizeType>
260 if (Base::size() == Base::capacity()) {
265 allocator_->Deallocate(buffer_);
267 Base::HandleShrunkBuffer(0);
275 if (Base::CanShrinkBuffer() &&
276 allocator_->Resize(buffer_, Base::size() *
sizeof(value_type))) {
277 Base::HandleShrunkBuffer(Base::size());
279 ReallocateBuffer(Base::size());
283template <
typename ValueType,
typename SizeType>
285 size_type new_capacity) {
286 std::byte* new_buffer =
static_cast<std::byte*
>(
287 allocator_->Allocate(allocator::Layout::Of<value_type[]>(new_capacity)));
288 if (new_buffer ==
nullptr) {
292 pointer dest = std::launder(
reinterpret_cast<pointer
>(new_buffer));
293 auto [data_1, data_2] = Base::contiguous_data();
295 if constexpr (std::is_move_constructible_v<value_type>) {
296 dest = std::uninitialized_move(data_1.begin(), data_1.end(), dest);
297 std::uninitialized_move(data_2.begin(), data_2.end(), dest);
299 dest = std::uninitialized_copy(data_1.begin(), data_1.end(), dest);
300 std::uninitialized_copy(data_2.begin(), data_2.end(), dest);
303 std::destroy(data_1.begin(), data_1.end());
304 std::destroy(data_2.begin(), data_2.end());
306 allocator_->Deallocate(buffer_);
307 buffer_ = new_buffer;
309 Base::HandleNewBuffer(new_capacity);
313template <
typename ValueType,
typename SizeType>
314template <
typename InputIt,
typename>
315typename DynamicDeque<ValueType, SizeType>::iterator
316DynamicDeque<ValueType, SizeType>::insert(const_iterator pos,
320 if constexpr (std::is_same_v<std::input_iterator_tag,
321 typename std::iterator_traits<
322 InputIt>::iterator_category>) {
326 DynamicDeque temp(*allocator_);
327 temp.assign(first, last);
328 return Base::insert(pos,
329 std::make_move_iterator(temp.data()),
330 std::make_move_iterator(temp.data() + temp.size()));
332 return Base::insert(pos, first, last);
Definition: allocator.h:45
Definition: dynamic_deque.h:58
Definition: dynamic_vector.h:53
Definition: generic_deque.h:178
constexpr size_type capacity() const noexcept
Returns the maximum number of elements in the deque.
Definition: generic_deque.h:72
iterator insert(const_iterator pos, const value_type &value)
Definition: generic_deque.h:351
void reserve(size_type new_capacity)
Increases capacity() to at least new_capacity. Crashes on failure.
Definition: dynamic_deque.h:147
std::optional< iterator > try_insert(const_iterator pos, const value_type &value)
Definition: generic_deque.h:469
bool try_assign(size_type count, const value_type &value)
Definition: generic_deque.h:618
void reserve_exact(size_type new_capacity)
Increases capacity() to exactly new_capacity. Crashes on failure.
Definition: dynamic_deque.h:163
constexpr DynamicDeque(DynamicDeque &&other) noexcept
Definition: dynamic_deque.h:89
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:178
bool try_reserve(size_type new_capacity)
Definition: dynamic_deque.h:238
void shrink_to_fit()
Attempts to reduce capacity() to size(). Not guaranteed to succeed.
Definition: dynamic_deque.h:259
constexpr allocator_type & get_allocator() const
Returns the deque's allocator.
Definition: dynamic_deque.h:175
bool try_reserve_exact(size_type new_capacity)
Definition: dynamic_deque.h:158
constexpr DynamicDeque(Allocator &allocator) noexcept
Definition: dynamic_deque.h:80
constexpr T mul_sat(T lhs, T rhs) noexcept
Definition: saturating_arithmetic.h:75
The Pigweed namespace.
Definition: alignment.h:27