18#include <initializer_list>
24#include "pw_allocator/allocator.h"
25#include "pw_assert/assert.h"
26#include "pw_containers/internal/count_and_capacity.h"
27#include "pw_containers/internal/generic_deque.h"
28#include "pw_numeric/saturating_arithmetic.h"
56template <
typename ValueType,
typename SizeType = u
int16_t>
58 DynamicDeque<ValueType, SizeType>,
60 containers::internal::CountAndCapacity<SizeType>> {
65 containers::internal::CountAndCapacity<SizeType>>;
68 using typename Base::const_iterator;
69 using typename Base::const_pointer;
70 using typename Base::const_reference;
71 using typename Base::difference_type;
72 using typename Base::iterator;
73 using typename Base::pointer;
74 using typename Base::reference;
75 using typename Base::size_type;
76 using typename Base::value_type;
85 :
Base(0), allocator_(&allocator), buffer_(
nullptr) {}
94 : allocator_(other.allocator_), buffer_(other.buffer_) {
95 other.buffer_ =
nullptr;
96 Base::MoveAssignIndices(other);
106 using Base::try_emplace_back;
107 using Base::try_emplace_front;
109 using Base::try_push_back;
110 using Base::try_push_front;
111 using Base::try_resize;
116 template <
typename InputIt,
117 typename = containers::internal::EnableIfInputIterator<InputIt>>
118 iterator insert(const_iterator pos, InputIt first, InputIt last);
120 iterator insert(const_iterator pos,
const value_type& value) {
124 iterator insert(const_iterator pos, value_type&& value) {
128 iterator insert(const_iterator pos,
130 const value_type& value) {
134 iterator insert(const_iterator pos, std::initializer_list<value_type> ilist) {
163 return new_capacity <=
Base::capacity() || IncreaseCapacity(new_capacity);
174 constexpr size_type max_size() const noexcept {
175 return std::numeric_limits<size_type>::max();
183 Base::SwapIndices(other);
184 std::swap(allocator_, other.allocator_);
185 std::swap(buffer_, other.buffer_);
191 template <
typename,
typename>
194 static constexpr bool kFixedCapacity =
false;
196 pointer data() {
return std::launder(
reinterpret_cast<pointer
>(buffer_)); }
197 const_pointer data()
const {
198 return std::launder(
reinterpret_cast<const_pointer
>(buffer_));
201 [[nodiscard]]
bool IncreaseCapacity(size_type new_capacity);
203 size_type GetNewCapacity(
const size_type new_size) {
206 return std::max(size_type{4 *
sizeof(
void*) /
sizeof(value_type)},
213 bool ReallocateBuffer(size_type new_capacity);
215 Allocator* allocator_;
219template <
typename ValueType,
typename SizeType>
220DynamicDeque<ValueType, SizeType>& DynamicDeque<ValueType, SizeType>::operator=(
221 DynamicDeque&& other)
noexcept {
223 allocator_->Deallocate(buffer_);
225 allocator_ = other.allocator_;
226 buffer_ = other.buffer_;
227 other.buffer_ =
nullptr;
229 Base::MoveAssignIndices(other);
233template <
typename ValueType,
typename SizeType>
234DynamicDeque<ValueType, SizeType>::~DynamicDeque() {
236 allocator_->Deallocate(buffer_);
239template <
typename ValueType,
typename SizeType>
241 return new_capacity <= Base::capacity() ||
242 IncreaseCapacity(GetNewCapacity(new_capacity)) ||
243 IncreaseCapacity(new_capacity);
246template <
typename ValueType,
typename SizeType>
248 size_type new_capacity) {
250 if (buffer_ !=
nullptr && Base::CanExtendBuffer() &&
251 allocator_->Resize(buffer_, new_capacity *
sizeof(value_type))) {
252 Base::HandleExtendedBuffer(new_capacity);
257 return ReallocateBuffer(new_capacity);
260template <
typename ValueType,
typename SizeType>
262 if (Base::size() == Base::capacity()) {
267 allocator_->Deallocate(buffer_);
269 Base::HandleShrunkBuffer(0);
277 if (Base::CanShrinkBuffer() &&
278 allocator_->Resize(buffer_, Base::size() *
sizeof(value_type))) {
279 Base::HandleShrunkBuffer(Base::size());
281 ReallocateBuffer(Base::size());
285template <
typename ValueType,
typename SizeType>
287 size_type new_capacity) {
288 std::byte* new_buffer =
static_cast<std::byte*
>(
289 allocator_->Allocate(allocator::Layout::Of<value_type[]>(new_capacity)));
290 if (new_buffer ==
nullptr) {
294 pointer dest = std::launder(
reinterpret_cast<pointer
>(new_buffer));
295 auto [data_1, data_2] = Base::contiguous_data();
297 if constexpr (std::is_move_constructible_v<value_type>) {
298 dest = std::uninitialized_move(data_1.begin(), data_1.end(), dest);
299 std::uninitialized_move(data_2.begin(), data_2.end(), dest);
301 dest = std::uninitialized_copy(data_1.begin(), data_1.end(), dest);
302 std::uninitialized_copy(data_2.begin(), data_2.end(), dest);
305 std::destroy(data_1.begin(), data_1.end());
306 std::destroy(data_2.begin(), data_2.end());
308 allocator_->Deallocate(buffer_);
309 buffer_ = new_buffer;
311 Base::HandleNewBuffer(new_capacity);
315template <
typename ValueType,
typename SizeType>
316template <
typename InputIt,
typename>
317typename DynamicDeque<ValueType, SizeType>::iterator
318DynamicDeque<ValueType, SizeType>::insert(const_iterator pos,
322 if constexpr (std::is_same_v<std::input_iterator_tag,
323 typename std::iterator_traits<
324 InputIt>::iterator_category>) {
328 DynamicDeque temp(*allocator_);
329 temp.assign(first, last);
330 return Base::insert(pos,
331 std::make_move_iterator(temp.data()),
332 std::make_move_iterator(temp.data() + temp.size()));
334 return Base::insert(pos, first, last);
Definition: allocator.h:36
Definition: dynamic_deque.h:60
Definition: dynamic_vector.h:53
Definition: generic_deque.h:180
constexpr size_type capacity() const noexcept
Returns the maximum number of elements in the deque.
Definition: generic_deque.h:69
void reserve(size_type new_capacity)
Increases capacity() to at least new_capacity. Crashes on failure.
Definition: dynamic_deque.h:151
void reserve_exact(size_type new_capacity)
Increases capacity() to exactly new_capacity. Crashes on failure.
Definition: dynamic_deque.h:167
constexpr DynamicDeque(DynamicDeque &&other) noexcept
Definition: dynamic_deque.h:93
void swap(DynamicDeque &other) noexcept
Swaps the contents of two deques. No allocations occur.
Definition: dynamic_deque.h:182
bool try_reserve(size_type new_capacity)
Definition: dynamic_deque.h:240
bool try_assign(size_type count, const value_type &value)
Definition: generic_deque.h:615
void shrink_to_fit()
Attempts to reduce capacity() to size(). Not guaranteed to succeed.
Definition: dynamic_deque.h:261
constexpr allocator_type & get_allocator() const
Returns the deque's allocator.
Definition: dynamic_deque.h:179
bool try_reserve_exact(size_type new_capacity)
Definition: dynamic_deque.h:162
constexpr DynamicDeque(Allocator &allocator) noexcept
Definition: dynamic_deque.h:84
iterator insert(const_iterator pos, const value_type &value)
Definition: generic_deque.h:355
std::optional< iterator > try_emplace(const_iterator pos, Args &&... args)
std::optional< iterator > try_insert(const_iterator pos, const value_type &value)
Definition: generic_deque.h:465
constexpr T mul_sat(T lhs, T rhs) noexcept
Definition: saturating_arithmetic.h:75
The Pigweed namespace.
Definition: alignment.h:27