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/count_and_capacity.h"
29#include "pw_containers/internal/generic_deque.h"
30#include "pw_containers/storage.h"
31#include "pw_polyfill/language_feature_macros.h"
32#include "pw_span/span.h"
33
34namespace pw {
35
37
56template <typename T, typename SizeType = uint16_t>
58 Deque<T, SizeType>,
59 T,
60 containers::internal::CountAndCapacity<SizeType>> {
61 private:
64 T,
65 containers::internal::CountAndCapacity<SizeType>>;
66
67 public:
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;
77
84 explicit constexpr Deque(span<std::byte> unaligned_buffer) noexcept
85 : Deque(Aligned::Align(unaligned_buffer)) {}
86
89 template <size_t kAlignment, size_t kSizeBytes>
90 explicit constexpr Deque(
92 : Deque(Aligned(buffer.data(), buffer.size())) {
93 static_assert(kAlignment >= alignof(value_type));
94 }
95
97 Deque(const Deque&) = delete;
98 Deque& operator=(const Deque&) = delete;
99
101 Deque(Deque&&) = delete;
102 Deque& operator=(Deque&&) = delete;
103
104 PW_CONSTEXPR_CPP20 ~Deque() { Base::DestroyAll(); }
105
106 constexpr size_type max_size() const noexcept { return Base::capacity(); }
107
108 private:
109 friend Base;
110
111 template <typename, typename>
112 friend class Queue;
113
114 template <typename, size_t, typename>
115 friend class FixedDeque;
116
117 static constexpr bool kFixedCapacity = true; // uses a fixed buffer
118
119 // Span that is known to be aligned as T.
120 class Aligned {
121 public:
122 // Construct from an UNALIGNED span. Adjusts alignment if necessary.
123 static constexpr Aligned Align(span<std::byte> unaligned_buffer) {
124 Aligned buffer(unaligned_buffer.data(), unaligned_buffer.size());
125
126 if constexpr (alignof(value_type) > alignof(std::byte)) {
127 void* data_void = buffer.data_;
128 buffer.data_ = static_cast<std::byte*>(std::align(
129 alignof(value_type), sizeof(value_type), data_void, buffer.size_));
130 if (buffer.data_ == nullptr) {
131 buffer.size_ = 0; // cannot fit any items
132 }
133 }
134
135 return buffer;
136 }
137
138 // Asserts that a buffer is aligned at least as T.
139 static constexpr Aligned Assert(std::byte* data, size_t size) {
140 void* buffer_start = data;
141 PW_ASSERT(reinterpret_cast<uintptr_t>(buffer_start) % alignof(T) == 0);
142 return Aligned(data, size);
143 }
144
145 constexpr Aligned(std::byte* aligned_data, size_t size_bytes)
146 : data_(aligned_data), size_(size_bytes) {}
147
148 constexpr std::byte* data() const { return data_; }
149 constexpr size_t capacity() const { return size_ / sizeof(value_type); }
150
151 private:
152 std::byte* data_;
153 size_t size_;
154 };
155
156 explicit constexpr Deque(Aligned buffer) noexcept
157 : Base(static_cast<size_type>(buffer.capacity())),
158 buffer_(buffer.data()) {}
159
160 constexpr void MoveBufferFrom(Deque& other) noexcept {
161 Base::DestroyAll();
162 buffer_ = cpp20::exchange(other.buffer_, nullptr);
163 Base::MoveAssignIndices(other);
164 }
165
166 void MoveItemsFrom(Deque& other) {
167 this->assign(std::make_move_iterator(other.begin()),
168 std::make_move_iterator(other.end()));
169 other.clear();
170 }
171
172 void SwapBufferWith(Deque& other) noexcept {
173 Base::SwapIndices(other);
174 std::swap(buffer_, other.buffer_);
175 }
176
177 void SwapValuesWith(Deque& other);
178
179 pointer data() { return std::launder(reinterpret_cast<pointer>(buffer_)); }
180 const_pointer data() const {
181 return std::launder(reinterpret_cast<const_pointer>(buffer_));
182 }
183
184 // Pointer to the buffer used for the deque. This may differ from the pointer
185 // passed into the constructor if it had to be shifted for alignment.
186 std::byte* buffer_;
187};
188
203template <typename T,
204 size_t kInlineCapacity = containers::kExternalStorage,
205 typename S = typename Deque<T>::size_type>
206class FixedDeque final
207 : private containers::internal::ArrayStorage<T, kInlineCapacity>,
208 public Deque<T, S> {
209 public:
212 constexpr FixedDeque()
213 : containers::internal::ArrayStorage<T, kInlineCapacity>{},
214 Deque<T, S>(this->storage_array) {}
215
216 FixedDeque(const FixedDeque&) = delete;
217 FixedDeque& operator=(const FixedDeque&) = delete;
218
219 constexpr FixedDeque(FixedDeque&& other) noexcept : FixedDeque() {
220 this->MoveItemsFrom(other);
221 }
222
223 constexpr FixedDeque& operator=(FixedDeque&& other) noexcept {
224 this->clear();
225 this->MoveItemsFrom(other);
226 return *this;
227 }
228
231 template <size_t kOtherCapacity,
232 typename = std::enable_if_t<kOtherCapacity <= kInlineCapacity>>
233 FixedDeque(FixedDeque<T, kOtherCapacity>&& other) noexcept : FixedDeque() {
234 this->MoveItemsFrom(other);
235 }
236
237 template <size_t kOtherCapacity,
238 typename = std::enable_if_t<kOtherCapacity <= kInlineCapacity>>
239 FixedDeque& operator=(FixedDeque<T, kOtherCapacity>&& other) noexcept {
240 this->clear();
241 this->MoveItemsFrom(other);
242 return *this;
243 }
244
247 template <size_t kOtherCapacity>
249 this->SwapValuesWith(other);
250 }
251
253 constexpr Deallocator* deallocator() const { return nullptr; }
254
255 private:
256 static_assert(
257 kInlineCapacity <= std::numeric_limits<S>::max(),
258 "The capacity is too large for the size_type; use a larger size_type");
259};
260
269template <typename T, typename S>
270class FixedDeque<T, containers::kExternalStorage, S> final
271 : public Deque<T, S> {
272 public:
278 static FixedDeque Allocate(Allocator& allocator, const S capacity) {
279 FixedDeque deque = TryAllocate(allocator, capacity);
280 PW_ASSERT(deque.capacity() == capacity);
281 return deque;
282 }
283
290 static FixedDeque TryAllocate(Allocator& allocator, S capacity) {
291 const allocator::Layout layout = allocator::Layout::Of<T[]>(capacity);
292 std::byte* array = static_cast<std::byte*>(allocator.Allocate(layout));
293 if (array == nullptr) {
294 return FixedDeque(Aligned(array, 0u), nullptr);
295 }
296 return FixedDeque(Aligned(array, layout.size()), &allocator);
297 }
298
303 static FixedDeque WithStorage(UniquePtr<std::byte[]>&& storage) {
304 const size_t size = storage.size();
305 Deallocator* deallocator = storage.deallocator();
306 return FixedDeque(Aligned::Assert(storage.Release(), size), deallocator);
307 }
308
316 explicit constexpr FixedDeque(span<std::byte> unaligned_buffer) noexcept
317 : Deque<T, S>(unaligned_buffer), deallocator_(nullptr) {}
318
323 template <size_t kAlignment, size_t kSizeBytes>
324 explicit constexpr FixedDeque(
326 : Deque<T, S>(buffer), deallocator_(nullptr) {}
327
329 FixedDeque(const FixedDeque&) = delete;
330 FixedDeque& operator=(const FixedDeque&) = delete;
331
336 constexpr FixedDeque(FixedDeque&& other) noexcept
337 : Deque<T, S>({}),
338 deallocator_(cpp20::exchange(other.deallocator_, nullptr)) {
339 this->MoveBufferFrom(other);
340 }
341
342 constexpr FixedDeque& operator=(FixedDeque&& other) noexcept {
343 if (this == &other) {
344 return *this;
345 }
346 T* const data = this->data();
347 this->MoveBufferFrom(other);
348
349 if (deallocator_ != nullptr) {
350 deallocator_->Deallocate(data);
351 }
352 deallocator_ = cpp20::exchange(other.deallocator_, nullptr);
353 return *this;
354 }
355
356 ~FixedDeque() {
357 // Clear the deque so that the base `Deque` destructor will not need to
358 // access its buffer, which will be freed.
359 this->clear();
360 if (deallocator_ != nullptr) {
361 deallocator_->Deallocate(this->data());
362 }
363 }
364
367 void swap(FixedDeque& other) noexcept {
368 this->SwapBufferWith(other);
369 std::swap(deallocator_, other.deallocator_);
370 }
371
376 template <size_t kInlineCapacity>
378 other.swap(*this); // Use the swap impl for statically allocated deques.
379 }
380
383 constexpr Deallocator* deallocator() const { return deallocator_; }
384
385 private:
386 using typename Deque<T, S>::Aligned;
387
388 constexpr FixedDeque(Aligned buffer, Deallocator* deallocator)
389 : Deque<T, S>(buffer), deallocator_(deallocator) {}
390
391 Deallocator* deallocator_;
392};
393
394template <typename T, typename SizeType>
395void Deque<T, SizeType>::SwapValuesWith(Deque& other) {
396 Deque* smaller;
397 Deque* larger;
398
399 if (this->size() < other.size()) {
400 smaller = this;
401 larger = &other;
402 } else {
403 smaller = &other;
404 larger = this;
405 }
406
407 const SizeType smaller_size = smaller->size();
408 for (auto it =
409 std::swap_ranges(smaller->begin(), smaller->end(), larger->begin());
410 it != larger->end();
411 ++it) {
412 smaller->push_back(std::move(*it));
413 }
414
415 while (larger->size() > smaller_size) {
416 larger->pop_back();
417 }
418}
419
420} // namespace pw
Definition: allocator.h:36
void * Allocate(Layout layout)
Definition: allocator.h:44
Abstract interface for releasing memory.
Definition: deallocator.h:29
Definition: deque.h:60
Definition: deque.h:208
Definition: unique_ptr.h:43
Definition: layout.h:58
Definition: storage.h:36
Definition: generic_deque.h:185
constexpr size_type size() const noexcept
Returns the number of elements in the deque.
Definition: generic_deque.h:69
constexpr size_type capacity() const noexcept
Returns the maximum number of elements in the deque.
Definition: generic_deque.h:74
Definition: span_impl.h:235
constexpr FixedDeque()
Definition: deque.h:212
FixedDeque(const FixedDeque &)=delete
Copying is not supported since it can fail.
void swap(FixedDeque< T, kOtherCapacity, S > &other)
Definition: deque.h:248
constexpr Deallocator * deallocator() const
Definition: deque.h:383
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:253
constexpr Deque(span< std::byte > unaligned_buffer) noexcept
Definition: deque.h:84
static FixedDeque Allocate(Allocator &allocator, const S capacity)
Definition: deque.h:278
constexpr FixedDeque(FixedDeque &&other) noexcept
Definition: deque.h:336
MoveItemsFrom(other)
constexpr Deque(containers::Storage< kAlignment, kSizeBytes > &buffer) noexcept
Definition: deque.h:90
static FixedDeque WithStorage(UniquePtr< std::byte[]> &&storage)
Definition: deque.h:303
constexpr FixedDeque(containers::Storage< kAlignment, kSizeBytes > &buffer) noexcept
Definition: deque.h:324
Deque(Deque &&)=delete
Move is not supported to avoid confusion about deque/buffer pairings.
void assign(size_type count, const value_type &value)
Sets the contents to count copies of value. Crashes if cannot fit.
Definition: generic_deque.h:216
void swap(FixedDeque< T, kInlineCapacity, S > &other)
Definition: deque.h:377
static FixedDeque TryAllocate(Allocator &allocator, S capacity)
Definition: deque.h:290
void swap(FixedDeque &other) noexcept
Definition: deque.h:367
constexpr FixedDeque(span< std::byte > unaligned_buffer) noexcept
Definition: deque.h:316
constexpr size_t kExternalStorage
Reserved capacity value for container specializations with external storage.
Definition: storage.h:76
#define PW_CONSTEXPR_CPP20
Definition: language_feature_macros.h:27
The Pigweed namespace.
Definition: alignment.h:27