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;
136 PW_ASSERT(
reinterpret_cast<uintptr_t
>(buffer_start) %
alignof(T) == 0);
137 return Aligned(data, size);
140 constexpr Aligned(std::byte* aligned_data,
size_t size_bytes)
141 : data_(aligned_data), size_(size_bytes) {}
143 constexpr std::byte* data()
const {
return data_; }
144 constexpr size_t capacity()
const {
return size_ /
sizeof(value_type); }
151 explicit constexpr Deque(Aligned buffer) noexcept
152 : Base(
static_cast<size_type
>(buffer.capacity())),
153 buffer_(buffer.data()) {}
155 constexpr void MoveBufferFrom(Deque& other)
noexcept {
157 buffer_ = cpp20::exchange(other.buffer_,
nullptr);
158 Base::MoveAssignIndices(other);
161 void MoveItemsFrom(Deque& other) {
162 this->assign(std::make_move_iterator(other.begin()),
163 std::make_move_iterator(other.end()));
167 void SwapBufferWith(Deque& other)
noexcept {
168 Base::SwapIndices(other);
169 std::swap(buffer_, other.buffer_);
172 void SwapValuesWith(Deque& other);
174 pointer data() {
return std::launder(
reinterpret_cast<pointer
>(buffer_)); }
175 const_pointer data()
const {
176 return std::launder(
reinterpret_cast<const_pointer
>(buffer_));
200 typename S =
typename Deque<T>::size_type>
207 : containers::StorageBaseFor<T, kInlineCapacity>{},
225 template <
size_t kOtherCapacity,
226 typename = std::enable_if_t<kOtherCapacity <= kInlineCapacity>>
231 template <
size_t kOtherCapacity,
232 typename = std::enable_if_t<kOtherCapacity <= kInlineCapacity>>
241 template <
size_t kOtherCapacity>
243 this->SwapValuesWith(other);
251 kInlineCapacity <= std::numeric_limits<S>::max(),
252 "The capacity is too large for the size_type; use a larger size_type");
263template <
typename T,
typename S>
265 :
public Deque<T, S> {
273 FixedDeque deque = TryAllocate(allocator, capacity);
274 PW_ASSERT(deque.capacity() == capacity);
286 std::byte* array =
static_cast<std::byte*
>(allocator.
Allocate(layout));
287 if (array ==
nullptr) {
288 return FixedDeque(Aligned(array, 0u),
nullptr);
290 return FixedDeque(Aligned(array, layout.size()), &allocator);
298 const size_t size = storage.size();
311 :
Deque<T, S>(unaligned_buffer), deallocator_(
nullptr) {}
317 template <
size_t kAlignment,
size_t kSizeBytes>
332 deallocator_(cpp20::exchange(other.deallocator_,
nullptr)) {
333 this->MoveBufferFrom(other);
337 if (
this == &other) {
340 T*
const data = this->data();
341 this->MoveBufferFrom(other);
343 if (deallocator_ !=
nullptr) {
344 deallocator_->Deallocate(data);
346 deallocator_ = cpp20::exchange(other.deallocator_,
nullptr);
354 if (deallocator_ !=
nullptr) {
355 deallocator_->Deallocate(this->data());
362 this->SwapBufferWith(other);
363 std::swap(deallocator_, other.deallocator_);
370 template <
size_t kInlineCapacity>
385 Deallocator* deallocator_;
388template <
typename T,
typename SizeType>
389void Deque<T, SizeType>::SwapValuesWith(Deque& other) {
393 if (this->size() < other.size()) {
401 const SizeType smaller_size = smaller->
size();
403 std::swap_ranges(smaller->begin(), smaller->end(), larger->begin());
406 smaller->push_back(std::move(*it));
409 while (larger->size() > smaller_size) {
Definition: allocator.h:45
void * Allocate(Layout layout)
Definition: allocator.h:53
Abstract interface for releasing memory.
Definition: deallocator.h:29
Definition: unique_ptr.h:43
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
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:206
FixedDeque(const FixedDeque &)=delete
Copying is not supported since it can fail.
void swap(FixedDeque< T, kOtherCapacity, S > &other)
Definition: deque.h:242
constexpr Deallocator * deallocator() const
Definition: deque.h:377
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:247
constexpr Deque(span< std::byte > unaligned_buffer) noexcept
Definition: deque.h:79
static FixedDeque Allocate(Allocator &allocator, const S capacity)
Definition: deque.h:272
constexpr FixedDeque(FixedDeque &&other) noexcept
Definition: deque.h:330
constexpr Deque(containers::Storage< kAlignment, kSizeBytes > &buffer) noexcept
Definition: deque.h:85
static FixedDeque WithStorage(UniquePtr< std::byte[]> &&storage)
Definition: deque.h:297
constexpr FixedDeque(containers::Storage< kAlignment, kSizeBytes > &buffer) noexcept
Definition: deque.h:318
Deque(Deque &&)=delete
Move is not supported to avoid confusion about deque/buffer pairings.
void swap(FixedDeque< T, kInlineCapacity, S > &other)
Definition: deque.h:371
static FixedDeque TryAllocate(Allocator &allocator, S capacity)
Definition: deque.h:284
void swap(FixedDeque &other) noexcept
Definition: deque.h:361
constexpr FixedDeque(span< std::byte > unaligned_buffer) noexcept
Definition: deque.h:310
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