24#include "lib/stdcompat/utility.h"
25#include "pw_allocator/allocator.h"
26#include "pw_allocator/unique_ptr.h"
27#include "pw_assert/assert.h"
28#include "pw_containers/internal/generic_deque.h"
29#include "pw_containers/storage.h"
30#include "pw_polyfill/language_feature_macros.h"
31#include "pw_span/span.h"
55template <
typename T,
typename SizeType = u
int16_t>
56class Deque :
public containers::internal::
57 GenericDeque<Deque<T, SizeType>, T, SizeType> {
63 using typename Base::const_iterator;
64 using typename Base::const_pointer;
65 using typename Base::const_reference;
66 using typename Base::difference_type;
67 using typename Base::iterator;
68 using typename Base::pointer;
69 using typename Base::reference;
70 using typename Base::size_type;
71 using typename Base::value_type;
80 :
Deque(Aligned::Align(unaligned_buffer)) {}
84 template <
size_t kAlignment,
size_t kSizeBytes>
87 :
Deque(Aligned(buffer.data(), buffer.size())) {
88 static_assert(kAlignment >=
alignof(value_type));
101 constexpr size_type max_size() const noexcept {
return Base::capacity(); }
106 template <
typename,
typename>
109 template <
typename,
size_t,
typename>
110 friend class FixedDeque;
112 static constexpr bool kFixedCapacity =
true;
118 static constexpr Aligned Align(span<std::byte> unaligned_buffer) {
119 Aligned buffer(unaligned_buffer.data(), unaligned_buffer.size());
121 if constexpr (
alignof(value_type) >
alignof(std::byte)) {
122 void* data_void = buffer.data_;
123 buffer.data_ =
static_cast<std::byte*
>(std::align(
124 alignof(value_type),
sizeof(value_type), data_void, buffer.size_));
125 if (buffer.data_ ==
nullptr) {
134 static constexpr Aligned Assert(std::byte* data,
size_t size) {
135 void* buffer_start = data;
138 uintptr_t misalignment =
139 reinterpret_cast<uintptr_t
>(buffer_start) %
alignof(T);
140 PW_ASSERT(misalignment == 0);
141 return Aligned(data, size);
144 constexpr Aligned(std::byte* aligned_data,
size_t size_bytes)
145 : data_(aligned_data), size_(size_bytes) {}
147 constexpr std::byte* data()
const {
return data_; }
148 constexpr size_t capacity()
const {
return size_ /
sizeof(value_type); }
155 explicit constexpr Deque(Aligned buffer) noexcept
156 : Base(
static_cast<size_type
>(buffer.capacity())),
157 buffer_(buffer.data()) {}
159 constexpr void MoveBufferFrom(Deque& other)
noexcept {
161 buffer_ = cpp20::exchange(other.buffer_,
nullptr);
162 Base::MoveAssignIndices(other);
165 void MoveItemsFrom(Deque& other) {
166 this->assign(std::make_move_iterator(other.begin()),
167 std::make_move_iterator(other.end()));
171 void SwapBufferWith(Deque& other)
noexcept {
172 Base::SwapIndices(other);
173 std::swap(buffer_, other.buffer_);
176 void SwapValuesWith(Deque& other);
178 pointer data() {
return std::launder(
reinterpret_cast<pointer
>(buffer_)); }
179 const_pointer data()
const {
180 return std::launder(
reinterpret_cast<const_pointer
>(buffer_));
204 typename S =
typename Deque<T>::size_type>
211 : containers::StorageBaseFor<T, kInlineCapacity>{},
229 template <
size_t kOtherCapacity,
230 typename = std::enable_if_t<kOtherCapacity <= kInlineCapacity>>
235 template <
size_t kOtherCapacity,
236 typename = std::enable_if_t<kOtherCapacity <= kInlineCapacity>>
245 template <
size_t kOtherCapacity>
247 this->SwapValuesWith(other);
255 kInlineCapacity <= std::numeric_limits<S>::max(),
256 "The capacity is too large for the size_type; use a larger size_type");
267template <
typename T,
typename S>
269 :
public Deque<T, S> {
277 FixedDeque deque = TryAllocate(allocator, capacity);
278 PW_ASSERT(deque.capacity() == capacity);
290 std::byte* array =
static_cast<std::byte*
>(allocator.
Allocate(layout));
291 if (array ==
nullptr) {
292 return FixedDeque(Aligned(array, 0u),
nullptr);
294 return FixedDeque(Aligned(array, layout.size()), &allocator);
302 const size_t size = storage.size();
315 :
Deque<T, S>(unaligned_buffer), deallocator_(
nullptr) {}
321 template <
size_t kAlignment,
size_t kSizeBytes>
336 deallocator_(cpp20::exchange(other.deallocator_,
nullptr)) {
337 this->MoveBufferFrom(other);
341 if (
this == &other) {
344 T*
const data = this->data();
345 this->MoveBufferFrom(other);
347 if (deallocator_ !=
nullptr) {
348 deallocator_->Deallocate(data);
350 deallocator_ = cpp20::exchange(other.deallocator_,
nullptr);
358 if (deallocator_ !=
nullptr) {
359 deallocator_->Deallocate(this->data());
366 this->SwapBufferWith(other);
367 std::swap(deallocator_, other.deallocator_);
374 template <
size_t kInlineCapacity>
389 Deallocator* deallocator_;
392template <
typename T,
typename SizeType>
393void Deque<T, SizeType>::SwapValuesWith(Deque& other) {
397 if (this->size() < other.size()) {
405 const SizeType smaller_size = smaller->
size();
407 std::swap_ranges(smaller->begin(), smaller->end(), larger->begin());
410 smaller->push_back(std::move(*it));
413 while (larger->size() > smaller_size) {
Definition: allocator.h:42
void * Allocate(Layout layout)
Abstract interface for releasing memory.
Definition: deallocator.h:30
Definition: unique_ptr.h:44
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
constexpr size_type size() const noexcept
Returns the number of elements in the deque.
Definition: generic_deque.h:69
Definition: span_impl.h:235
constexpr FixedDeque()
Definition: deque.h:210
FixedDeque(const FixedDeque &)=delete
Copying is not supported since it can fail.
void swap(FixedDeque< T, kOtherCapacity, S > &other)
Definition: deque.h:246
constexpr Deallocator * deallocator() const
Definition: deque.h:381
Deque(const Deque &)=delete
Copying is not supported since it can fail.
constexpr Deallocator * deallocator() const
Returns nullptr; a FixedDeque with static storage never allocates.
Definition: deque.h:251
constexpr Deque(span< std::byte > unaligned_buffer) noexcept
Definition: deque.h:79
static FixedDeque Allocate(Allocator &allocator, const S capacity)
Definition: deque.h:276
constexpr FixedDeque(FixedDeque &&other) noexcept
Definition: deque.h:334
constexpr Deque(containers::Storage< kAlignment, kSizeBytes > &buffer) noexcept
Definition: deque.h:85
static FixedDeque WithStorage(UniquePtr< std::byte[]> &&storage)
Definition: deque.h:301
constexpr FixedDeque(containers::Storage< kAlignment, kSizeBytes > &buffer) noexcept
Definition: deque.h:322
Deque(Deque &&)=delete
Move is not supported to avoid confusion about deque/buffer pairings.
void swap(FixedDeque< T, kInlineCapacity, S > &other)
Definition: deque.h:375
static FixedDeque TryAllocate(Allocator &allocator, S capacity)
Definition: deque.h:288
void swap(FixedDeque &other) noexcept
Definition: deque.h:365
constexpr FixedDeque(span< std::byte > unaligned_buffer) noexcept
Definition: deque.h:314
constexpr size_t kExternalStorage
Reserved capacity value for container specializations with external storage.
Definition: storage.h:79
#define PW_CONSTEXPR_CPP20
Definition: language_feature_macros.h:27
The Pigweed namespace.
Definition: alignment.h:27