C/C++ API Reference
Loading...
Searching...
No Matches
deque.h
1// Copyright 2025 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14#pragma once
15
16#include <algorithm>
17#include <cstddef>
18#include <cstdint>
19#include <iterator>
20#include <limits>
21#include <type_traits>
22#include <utility>
23
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"
32
33namespace pw {
34
36
55template <typename T, typename SizeType = uint16_t>
56class Deque : public containers::internal::
57 GenericDeque<Deque<T, SizeType>, T, SizeType> {
58 private:
59 using Base =
61
62 public:
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;
72
79 explicit constexpr Deque(span<std::byte> unaligned_buffer) noexcept
80 : Deque(Aligned::Align(unaligned_buffer)) {}
81
84 template <size_t kAlignment, size_t kSizeBytes>
85 explicit constexpr Deque(
87 : Deque(Aligned(buffer.data(), buffer.size())) {
88 static_assert(kAlignment >= alignof(value_type));
89 }
90
92 Deque(const Deque&) = delete;
93 Deque& operator=(const Deque&) = delete;
94
96 Deque(Deque&&) = delete;
97 Deque& operator=(Deque&&) = delete;
98
99 PW_CONSTEXPR_CPP20 ~Deque() { Base::DestroyAll(); }
100
101 constexpr size_type max_size() const noexcept { return Base::capacity(); }
102
103 private:
104 friend Base;
105
106 template <typename, typename>
107 friend class Queue;
108
109 template <typename, size_t, typename>
110 friend class FixedDeque;
111
112 static constexpr bool kFixedCapacity = true; // uses a fixed buffer
113
114 // Span that is known to be aligned as T.
115 class Aligned {
116 public:
117 // Construct from an UNALIGNED span. Adjusts alignment if necessary.
118 static constexpr Aligned Align(span<std::byte> unaligned_buffer) {
119 Aligned buffer(unaligned_buffer.data(), unaligned_buffer.size());
120
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) {
126 buffer.size_ = 0; // cannot fit any items
127 }
128 }
129
130 return buffer;
131 }
132
133 // Asserts that a buffer is aligned at least as T.
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);
138 }
139
140 constexpr Aligned(std::byte* aligned_data, size_t size_bytes)
141 : data_(aligned_data), size_(size_bytes) {}
142
143 constexpr std::byte* data() const { return data_; }
144 constexpr size_t capacity() const { return size_ / sizeof(value_type); }
145
146 private:
147 std::byte* data_;
148 size_t size_;
149 };
150
151 explicit constexpr Deque(Aligned buffer) noexcept
152 : Base(static_cast<size_type>(buffer.capacity())),
153 buffer_(buffer.data()) {}
154
155 constexpr void MoveBufferFrom(Deque& other) noexcept {
156 Base::DestroyAll();
157 buffer_ = cpp20::exchange(other.buffer_, nullptr);
158 Base::MoveAssignIndices(other);
159 }
160
161 void MoveItemsFrom(Deque& other) {
162 this->assign(std::make_move_iterator(other.begin()),
163 std::make_move_iterator(other.end()));
164 other.clear();
165 }
166
167 void SwapBufferWith(Deque& other) noexcept {
168 Base::SwapIndices(other);
169 std::swap(buffer_, other.buffer_);
170 }
171
172 void SwapValuesWith(Deque& other);
173
174 pointer data() { return std::launder(reinterpret_cast<pointer>(buffer_)); }
175 const_pointer data() const {
176 return std::launder(reinterpret_cast<const_pointer>(buffer_));
177 }
178
179 // Pointer to the buffer used for the deque. This may differ from the pointer
180 // passed into the constructor if it had to be shifted for alignment.
181 std::byte* buffer_;
182};
183
198template <typename T,
199 size_t kInlineCapacity = containers::kExternalStorage,
200 typename S = typename Deque<T>::size_type>
201class FixedDeque final : private containers::StorageBaseFor<T, kInlineCapacity>,
202 public Deque<T, S> {
203 public:
206 constexpr FixedDeque()
207 : containers::StorageBaseFor<T, kInlineCapacity>{},
208 Deque<T, S>(this->storage()) {}
209
210 FixedDeque(const FixedDeque&) = delete;
211 FixedDeque& operator=(const FixedDeque&) = delete;
212
213 constexpr FixedDeque(FixedDeque&& other) noexcept : FixedDeque() {
214 this->MoveItemsFrom(other);
215 }
216
217 constexpr FixedDeque& operator=(FixedDeque&& other) noexcept {
218 this->clear();
219 this->MoveItemsFrom(other);
220 return *this;
221 }
222
225 template <size_t kOtherCapacity,
226 typename = std::enable_if_t<kOtherCapacity <= kInlineCapacity>>
227 FixedDeque(FixedDeque<T, kOtherCapacity>&& other) noexcept : FixedDeque() {
228 this->MoveItemsFrom(other);
229 }
230
231 template <size_t kOtherCapacity,
232 typename = std::enable_if_t<kOtherCapacity <= kInlineCapacity>>
233 FixedDeque& operator=(FixedDeque<T, kOtherCapacity>&& other) noexcept {
234 this->clear();
235 this->MoveItemsFrom(other);
236 return *this;
237 }
238
241 template <size_t kOtherCapacity>
243 this->SwapValuesWith(other);
244 }
245
247 constexpr Deallocator* deallocator() const { return nullptr; }
248
249 private:
250 static_assert(
251 kInlineCapacity <= std::numeric_limits<S>::max(),
252 "The capacity is too large for the size_type; use a larger size_type");
253};
254
263template <typename T, typename S>
264class FixedDeque<T, containers::kExternalStorage, S> final
265 : public Deque<T, S> {
266 public:
272 static FixedDeque Allocate(Allocator& allocator, const S capacity) {
273 FixedDeque deque = TryAllocate(allocator, capacity);
274 PW_ASSERT(deque.capacity() == capacity);
275 return deque;
276 }
277
284 static FixedDeque TryAllocate(Allocator& allocator, S capacity) {
285 const allocator::Layout layout = allocator::Layout::Of<T[]>(capacity);
286 std::byte* array = static_cast<std::byte*>(allocator.Allocate(layout));
287 if (array == nullptr) {
288 return FixedDeque(Aligned(array, 0u), nullptr);
289 }
290 return FixedDeque(Aligned(array, layout.size()), &allocator);
291 }
292
297 static FixedDeque WithStorage(UniquePtr<std::byte[]>&& storage) {
298 const size_t size = storage.size();
299 Deallocator* deallocator = storage.deallocator();
300 return FixedDeque(Aligned::Assert(storage.Release(), size), deallocator);
301 }
302
310 explicit constexpr FixedDeque(span<std::byte> unaligned_buffer) noexcept
311 : Deque<T, S>(unaligned_buffer), deallocator_(nullptr) {}
312
317 template <size_t kAlignment, size_t kSizeBytes>
318 explicit constexpr FixedDeque(
320 : Deque<T, S>(buffer), deallocator_(nullptr) {}
321
323 FixedDeque(const FixedDeque&) = delete;
324 FixedDeque& operator=(const FixedDeque&) = delete;
325
330 constexpr FixedDeque(FixedDeque&& other) noexcept
331 : Deque<T, S>({}),
332 deallocator_(cpp20::exchange(other.deallocator_, nullptr)) {
333 this->MoveBufferFrom(other);
334 }
335
336 constexpr FixedDeque& operator=(FixedDeque&& other) noexcept {
337 if (this == &other) {
338 return *this;
339 }
340 T* const data = this->data();
341 this->MoveBufferFrom(other);
342
343 if (deallocator_ != nullptr) {
344 deallocator_->Deallocate(data);
345 }
346 deallocator_ = cpp20::exchange(other.deallocator_, nullptr);
347 return *this;
348 }
349
350 ~FixedDeque() {
351 // Clear the deque so that the base `Deque` destructor will not need to
352 // access its buffer, which will be freed.
353 this->clear();
354 if (deallocator_ != nullptr) {
355 deallocator_->Deallocate(this->data());
356 }
357 }
358
361 void swap(FixedDeque& other) noexcept {
362 this->SwapBufferWith(other);
363 std::swap(deallocator_, other.deallocator_);
364 }
365
370 template <size_t kInlineCapacity>
372 other.swap(*this); // Use the swap impl for statically allocated deques.
373 }
374
377 constexpr Deallocator* deallocator() const { return deallocator_; }
378
379 private:
380 using typename Deque<T, S>::Aligned;
381
382 constexpr FixedDeque(Aligned buffer, Deallocator* deallocator)
383 : Deque<T, S>(buffer), deallocator_(deallocator) {}
384
385 Deallocator* deallocator_;
386};
387
388template <typename T, typename SizeType>
389void Deque<T, SizeType>::SwapValuesWith(Deque& other) {
390 Deque* smaller;
391 Deque* larger;
392
393 if (this->size() < other.size()) {
394 smaller = this;
395 larger = &other;
396 } else {
397 smaller = &other;
398 larger = this;
399 }
400
401 const SizeType smaller_size = smaller->size();
402 for (auto it =
403 std::swap_ranges(smaller->begin(), smaller->end(), larger->begin());
404 it != larger->end();
405 ++it) {
406 smaller->push_back(std::move(*it));
407 }
408
409 while (larger->size() > smaller_size) {
410 larger->pop_back();
411 }
412}
413
414} // namespace pw
Definition: allocator.h:45
void * Allocate(Layout layout)
Definition: allocator.h:53
Abstract interface for releasing memory.
Definition: deallocator.h:29
Definition: deque.h:57
Definition: deque.h:202
Definition: unique_ptr.h:43
Definition: layout.h:58
Definition: storage.h:86
Definition: storage.h:39
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
MoveItemsFrom(other)
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