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 :
Base(0), 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;
199 pointer data() {
return std::launder(
reinterpret_cast<pointer
>(buffer_)); }
200 const_pointer data()
const {
201 return std::launder(
reinterpret_cast<const_pointer
>(buffer_));
204 [[nodiscard]]
bool IncreaseCapacity(size_type new_capacity);
206 size_type GetNewCapacity(
const size_type new_size) {
209 return std::max(size_type{4 *
sizeof(
void*) /
sizeof(value_type)},
216 bool ReallocateBuffer(size_type new_capacity);
218 Allocator* allocator_;
222template <
typename ValueType,
typename SizeType>
223DynamicDeque<ValueType, SizeType>& DynamicDeque<ValueType, SizeType>::operator=(
224 DynamicDeque&& other)
noexcept {
226 allocator_->Deallocate(buffer_);
228 allocator_ = other.allocator_;
229 buffer_ = std::exchange(other.buffer_,
nullptr);
231 Base::MoveAssignIndices(other);
235template <
typename ValueType,
typename SizeType>
236DynamicDeque<ValueType, SizeType>::~DynamicDeque() {
238 allocator_->Deallocate(buffer_);
241template <
typename ValueType,
typename SizeType>
243 return new_capacity <= Base::capacity() ||
244 IncreaseCapacity(GetNewCapacity(new_capacity)) ||
245 IncreaseCapacity(new_capacity);
248template <
typename ValueType,
typename SizeType>
250 size_type new_capacity) {
252 if (buffer_ !=
nullptr && Base::CanExtendBuffer() &&
253 allocator_->Resize(buffer_, new_capacity *
sizeof(value_type))) {
254 Base::HandleExtendedBuffer(new_capacity);
259 return ReallocateBuffer(new_capacity);
262template <
typename ValueType,
typename SizeType>
264 if (Base::size() == Base::capacity()) {
269 allocator_->Deallocate(buffer_);
271 Base::HandleShrunkBuffer(0);
279 if (Base::CanShrinkBuffer() &&
280 allocator_->Resize(buffer_, Base::size() *
sizeof(value_type))) {
281 Base::HandleShrunkBuffer(Base::size());
283 ReallocateBuffer(Base::size());
287template <
typename ValueType,
typename SizeType>
289 size_type new_capacity) {
290 std::byte* new_buffer =
static_cast<std::byte*
>(
291 allocator_->Allocate(allocator::Layout::Of<value_type[]>(new_capacity)));
292 if (new_buffer ==
nullptr) {
296 pointer dest = std::launder(
reinterpret_cast<pointer
>(new_buffer));
297 auto [data_1, data_2] = Base::contiguous_data();
299 if constexpr (std::is_move_constructible_v<value_type>) {
300 dest = std::uninitialized_move(data_1.begin(), data_1.end(), dest);
301 std::uninitialized_move(data_2.begin(), data_2.end(), dest);
303 dest = std::uninitialized_copy(data_1.begin(), data_1.end(), dest);
304 std::uninitialized_copy(data_2.begin(), data_2.end(), dest);
307 std::destroy(data_1.begin(), data_1.end());
308 std::destroy(data_2.begin(), data_2.end());
310 allocator_->Deallocate(buffer_);
311 buffer_ = new_buffer;
313 Base::HandleNewBuffer(new_capacity);
317template <
typename ValueType,
typename SizeType>
318template <
typename InputIt,
typename>
319typename DynamicDeque<ValueType, SizeType>::iterator
320DynamicDeque<ValueType, SizeType>::insert(const_iterator pos,
324 if constexpr (std::is_same_v<std::input_iterator_tag,
325 typename std::iterator_traits<
326 InputIt>::iterator_category>) {
330 DynamicDeque temp(*allocator_);
331 temp.assign(first, last);
332 return Base::insert(pos,
333 std::make_move_iterator(temp.data()),
334 std::make_move_iterator(temp.data() + temp.size()));
336 return Base::insert(pos, first, last);
Definition: allocator.h:36
Definition: dynamic_deque.h:60
Definition: dynamic_vector.h:53
Definition: generic_deque.h:185
constexpr size_type capacity() const noexcept
Returns the maximum number of elements in the deque.
Definition: generic_deque.h:74
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:242
bool try_assign(size_type count, const value_type &value)
Definition: generic_deque.h:624
void shrink_to_fit()
Attempts to reduce capacity() to size(). Not guaranteed to succeed.
Definition: dynamic_deque.h:263
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:358
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:474
constexpr T mul_sat(T lhs, T rhs) noexcept
Definition: saturating_arithmetic.h:75
The Pigweed namespace.
Definition: alignment.h:27