GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
DoubleEndedQueue.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Ivar Härnqvist
2// SPDX-License-Identifier: MIT
3
4#ifndef GREM_CORE_DATA_DOUBLE_ENDED_QUEUE_HPP
5#define GREM_CORE_DATA_DOUBLE_ENDED_QUEUE_HPP
6
8
10
11#include <algorithm> // std::min, std::rotate, std::shift_left, std::equal, std::lexicographical_compare, std::remove, std::remove_if
12#include <climits> // CHAR_BIT
13#include <cstddef> // std::size_t, std::ptrdiff_t
14#include <initializer_list> // std::initializer_list
15#include <iterator> // std::iterator_traits, std::random_access_iterator_tag, std::reverse_iterator, std::input_iterator, std::sentinel_for, std::begin, std::end
16#include <limits> // std::numeric_limits
17#include <memory> // std::allocator, std::allocator_traits, std::to_address
18#include <memory_resource> // std::pmr::polymorphic_allocator
19#include <new> // std::launder
20#include <stdexcept> // std::out_of_range, std::length_error
21#include <type_traits> // std::is_..._v
22#include <utility> // std::move, std::forward, std::exchange, std::swap
23
24namespace grem {
25
26namespace detail {
27
28template <typename T>
29class DoubleEndedQueueIterator {
30public:
31 using difference_type = std::ptrdiff_t;
32 using value_type = T;
33 using pointer = T*;
34 using reference = T&;
35 using iterator_category = std::random_access_iterator_tag;
36
37 DoubleEndedQueueIterator() = default;
38
39 constexpr DoubleEndedQueueIterator(pointer ringBuffer, std::size_t mask, std::size_t index) noexcept
40 : ringBuffer(ringBuffer)
41 , mask(mask)
42 , index(index) {}
43
44 constexpr operator DoubleEndedQueueIterator<const T>() const noexcept requires(!std::is_const_v<T>) {
45 return DoubleEndedQueueIterator<const T>{ringBuffer, mask, index};
46 }
47
48 [[nodiscard]] constexpr reference operator*() const {
49 return ringBuffer[index & mask];
50 }
51
52 [[nodiscard]] constexpr pointer operator->() const {
53 return &ringBuffer[index & mask];
54 }
55
56 [[nodiscard]] constexpr reference operator[](difference_type n) const {
57 return ringBuffer[(index + static_cast<std::size_t>(n)) & mask];
58 }
59
60 constexpr DoubleEndedQueueIterator& operator++() {
61 ++index;
62 return *this;
63 }
64
65 constexpr DoubleEndedQueueIterator& operator--() {
66 --index;
67 return *this;
68 }
69
70 constexpr DoubleEndedQueueIterator operator++(int) {
71 return DoubleEndedQueueIterator{ringBuffer, mask, index++};
72 }
73
74 constexpr DoubleEndedQueueIterator operator--(int) {
75 return DoubleEndedQueueIterator{ringBuffer, mask, index--};
76 }
77
78 constexpr DoubleEndedQueueIterator& operator+=(difference_type n) {
79 index += static_cast<std::size_t>(n);
80 return *this;
81 }
82
83 constexpr DoubleEndedQueueIterator& operator-=(difference_type n) {
84 index -= static_cast<std::size_t>(n);
85 return *this;
86 }
87
88 [[nodiscard]] friend constexpr DoubleEndedQueueIterator operator+(DoubleEndedQueueIterator a, difference_type b) {
89 return DoubleEndedQueueIterator{a.ringBuffer, a.mask, a.index + static_cast<std::size_t>(b)};
90 }
91
92 [[nodiscard]] friend constexpr DoubleEndedQueueIterator operator+(difference_type a, DoubleEndedQueueIterator b) {
93 return DoubleEndedQueueIterator{b.ringBuffer, b.mask, static_cast<std::size_t>(a) + b.index};
94 }
95
96 [[nodiscard]] friend constexpr DoubleEndedQueueIterator operator-(DoubleEndedQueueIterator a, difference_type b) {
97 return DoubleEndedQueueIterator{a.ringBuffer, a.mask, a.index - static_cast<std::size_t>(b)};
98 }
99
100 [[nodiscard]] friend constexpr difference_type operator-(DoubleEndedQueueIterator a, DoubleEndedQueueIterator b) {
101 GREM_ASSERT(a.ringBuffer == b.ringBuffer);
102 return static_cast<difference_type>(a.index - b.index);
103 }
104
105 [[nodiscard]] friend constexpr bool operator==(DoubleEndedQueueIterator a, DoubleEndedQueueIterator b) {
106 GREM_ASSERT(a.ringBuffer == b.ringBuffer);
107 return a.index == b.index;
108 }
109
110 [[nodiscard]] friend constexpr auto operator<=>(DoubleEndedQueueIterator a, DoubleEndedQueueIterator b) {
111 GREM_ASSERT(a.ringBuffer == b.ringBuffer);
112 return a.index <=> b.index;
113 }
114
115private:
116 pointer ringBuffer;
117 std::size_t mask;
118 std::size_t index;
119};
120
121} // namespace detail
122
123template <typename T, typename Allocator = std::allocator<T>>
125public:
126 using value_type = T;
127 using allocator_type = Allocator;
128 using size_type = std::size_t;
129 using difference_type = std::ptrdiff_t;
130 using reference = T&;
131 using const_reference = const T&;
132 using pointer = typename std::allocator_traits<Allocator>::pointer;
133 using const_pointer = typename std::allocator_traits<Allocator>::const_pointer;
134 using iterator = detail::DoubleEndedQueueIterator<T>;
135 using const_iterator = detail::DoubleEndedQueueIterator<const T>;
136 using reverse_iterator = std::reverse_iterator<iterator>;
137 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
138
139 constexpr DoubleEndedQueue() = default;
140
141 constexpr explicit DoubleEndedQueue(const Allocator& allocator) noexcept
142 : allocator(allocator) {}
143
144 constexpr DoubleEndedQueue(size_type count, const T& value, const Allocator& allocator = Allocator())
145 : DoubleEndedQueue(allocator) {
146 assign(count, value);
147 }
148
149 constexpr explicit DoubleEndedQueue(size_type count, const Allocator& allocator = Allocator())
150 : DoubleEndedQueue(allocator) {
151 reserve(count);
152 while (count-- > 0) {
153 emplace_back();
154 }
155 }
156
157 template <std::input_iterator InputIterator, std::sentinel_for<InputIterator> Sentinel>
158 constexpr DoubleEndedQueue(InputIterator first, Sentinel last, const Allocator& allocator = Allocator())
159 : DoubleEndedQueue(allocator) {
160 assign(first, last);
161 }
162
163 constexpr DoubleEndedQueue(const DoubleEndedQueue& other)
164 : DoubleEndedQueue(other, std::allocator_traits<Allocator>::select_on_container_copy_construction(other.get_allocator())) {}
165
166 constexpr DoubleEndedQueue(const DoubleEndedQueue& other, const Allocator& allocator)
167 : DoubleEndedQueue(other.begin(), other.end(), allocator) {}
168
169 constexpr DoubleEndedQueue(DoubleEndedQueue&& other) noexcept
170 : ringBuffer(std::exchange(other.ringBuffer, nullptr))
171 , mask(std::exchange(other.mask, size_type{0}))
172 , beginIndex(std::exchange(other.beginIndex, size_type{0}))
173 , endIndex(std::exchange(other.endIndex, size_type{0}))
174 , allocator(other.get_allocator()) {}
175
176 constexpr DoubleEndedQueue(DoubleEndedQueue&& other, const Allocator& allocator) // NOLINT(cppcoreguidelines-rvalue-reference-param-not-moved)
177 : DoubleEndedQueue(allocator) {
178 if constexpr (!std::allocator_traits<Allocator>::is_always_equal::value) {
179 if (allocator != other.get_allocator()) {
180 reserve(other.size());
181 for (T& element : other) {
182 push_back(std::move(element));
183 }
184 other.clear();
185 return;
186 }
187 }
188 ringBuffer = std::exchange(other.ringBuffer, nullptr);
189 mask = std::exchange(other.mask, size_type{0});
190 beginIndex = std::exchange(other.beginIndex, size_type{0});
191 endIndex = std::exchange(other.endIndex, size_type{0});
192 }
193
194 constexpr DoubleEndedQueue(std::initializer_list<T> ilist, const Allocator& allocator = Allocator())
195 : DoubleEndedQueue(ilist.begin(), ilist.end(), allocator) {}
196
197 constexpr ~DoubleEndedQueue() {
198 reset();
199 }
200
201 constexpr DoubleEndedQueue& operator=(const DoubleEndedQueue& other) {
202 if (this == &other) {
203 return *this;
204 }
205 if constexpr (std::allocator_traits<Allocator>::propagate_on_container_copy_assignment::value) {
206 if constexpr (!std::allocator_traits<Allocator>::is_always_equal::value) {
207 if (allocator != other.get_allocator()) {
208 reset();
209 }
210 }
211 allocator = other.get_allocator();
212 }
213 assign(other.begin(), other.end());
214 return *this;
215 }
216
217 constexpr DoubleEndedQueue& operator=(DoubleEndedQueue&& other) noexcept(
218 std::allocator_traits<Allocator>::is_always_equal::value) { // NOLINT(cppcoreguidelines-noexcept-move-operations, performance-noexcept-move-constructor)
219 if (this == &other) {
220 return *this;
221 }
222 if constexpr (!std::allocator_traits<Allocator>::propagate_on_container_move_assignment::value) {
223 if constexpr (!std::allocator_traits<Allocator>::is_always_equal::value) {
224 if (allocator != other.get_allocator()) {
225 clear();
226 reserve(other.size());
227 for (T& element : other) {
228 push_back(std::move(element));
229 }
230 other.clear();
231 return *this;
232 }
233 }
234 }
235 reset();
236 ringBuffer = std::exchange(other.ringBuffer, nullptr);
237 mask = std::exchange(other.mask, size_type{0});
238 beginIndex = std::exchange(other.beginIndex, size_type{0});
239 endIndex = std::exchange(other.endIndex, size_type{0});
240 if constexpr (std::allocator_traits<Allocator>::propagate_on_container_move_assignment::value) {
241 allocator = other.get_allocator();
242 }
243 return *this;
244 }
245
246 constexpr DoubleEndedQueue& operator=(std::initializer_list<T> ilist) {
247 assign(ilist);
248 return *this;
249 }
250
251 constexpr void assign(size_type count, const T& value) {
252 if constexpr (std::is_copy_assignable_v<T>) {
253 if (count <= size()) {
254 while (size() > count) {
255 pop_back();
256 }
257 for (T& element : *this) {
258 element = value;
259 }
260 return;
261 }
262 }
263 clear();
264 reserve(count);
265 while (count-- > 0) {
266 push_back(value);
267 }
268 }
269
270 template <std::input_iterator InputIterator, std::sentinel_for<InputIterator> Sentinel>
271 constexpr void assign(InputIterator first, Sentinel last) {
272 if constexpr (std::is_convertible_v<typename std::iterator_traits<InputIterator>::iterator_category, std::random_access_iterator_tag>) {
273 const size_type count = static_cast<size_type>(last - first);
274 if constexpr (std::is_copy_assignable_v<T>) {
275 if (count <= size()) {
276 while (size() > count) {
277 pop_back();
278 }
279 for (size_type i = 0; i < count; ++i) {
280 (*this)[i] = first[i];
281 }
282 return;
283 }
284 }
285 clear();
286 reserve(count);
287 for (size_type i = 0; i < count; ++i) {
288 push_back(first[i]);
289 }
290 } else {
291 clear();
292 while (first != last) {
293 push_back(*first++);
294 }
295 }
296 }
297
298 constexpr void assign(std::initializer_list<T> ilist) {
299 assign(ilist.begin(), ilist.end());
300 }
301
302 template <typename R>
303 constexpr void assign_range(R&& r) { // NOLINT(cppcoreguidelines-missing-std-forward)
304 assign(std::begin(r), std::end(r));
305 }
306
307 [[nodiscard]] constexpr allocator_type get_allocator() const noexcept {
308 return allocator;
309 }
310
311 [[nodiscard]] constexpr reference at(size_type pos) {
312 if (pos >= size()) {
313 throw std::out_of_range{"pos >= size()"};
314 }
315 return (*this)[pos];
316 }
317
318 [[nodiscard]] constexpr const_reference at(size_type pos) const {
319 if (pos >= size()) {
320 throw std::out_of_range{"pos >= size()"};
321 }
322 return (*this)[pos];
323 }
324
325 [[nodiscard]] constexpr reference operator[](size_type pos) {
326 GREM_ASSERT(pos < size());
327 return ringBuffer[(beginIndex + pos) & mask];
328 }
329
330 [[nodiscard]] constexpr const_reference operator[](size_type pos) const {
331 GREM_ASSERT(pos < size());
332 return ringBuffer[(beginIndex + pos) & mask];
333 }
334
335 [[nodiscard]] constexpr reference front() {
336 GREM_ASSERT(!empty());
337 return ringBuffer[beginIndex & mask];
338 }
339
340 [[nodiscard]] constexpr const_reference front() const {
341 GREM_ASSERT(!empty());
342 return ringBuffer[beginIndex & mask];
343 }
344
345 [[nodiscard]] constexpr reference back() {
346 GREM_ASSERT(!empty());
347 return ringBuffer[(endIndex - 1) & mask];
348 }
349
350 [[nodiscard]] constexpr const_reference back() const {
351 GREM_ASSERT(!empty());
352 return ringBuffer[(endIndex - 1) & mask];
353 }
354
355 [[nodiscard]] constexpr iterator begin() noexcept {
356 return iterator{ringBuffer, mask, beginIndex};
357 }
358
359 [[nodiscard]] constexpr const_iterator begin() const noexcept {
360 return const_iterator{ringBuffer, mask, beginIndex};
361 }
362
363 [[nodiscard]] constexpr const_iterator cbegin() const noexcept {
364 return begin();
365 }
366
367 [[nodiscard]] constexpr iterator end() noexcept {
368 return iterator{ringBuffer, mask, endIndex};
369 }
370
371 [[nodiscard]] constexpr const_iterator end() const noexcept {
372 return const_iterator{ringBuffer, mask, endIndex};
373 }
374
375 [[nodiscard]] constexpr const_iterator cend() const noexcept {
376 return end();
377 }
378
379 [[nodiscard]] constexpr reverse_iterator rbegin() noexcept {
380 return reverse_iterator{end()};
381 }
382
383 [[nodiscard]] constexpr const_reverse_iterator rbegin() const noexcept {
384 return const_reverse_iterator{end()};
385 }
386
387 [[nodiscard]] constexpr const_reverse_iterator crbegin() const noexcept {
388 return rbegin();
389 }
390
391 [[nodiscard]] constexpr reverse_iterator rend() noexcept {
392 return reverse_iterator{begin()};
393 }
394
395 [[nodiscard]] constexpr const_reverse_iterator rend() const noexcept {
397 }
398
399 [[nodiscard]] constexpr const_reverse_iterator crend() const noexcept {
400 return rend();
401 }
402
403 [[nodiscard]] constexpr bool empty() const noexcept {
404 return beginIndex == endIndex;
405 }
406
407 [[nodiscard]] constexpr size_type size() const noexcept {
408 return endIndex - beginIndex;
409 }
410
411 [[nodiscard]] constexpr size_type max_size() const noexcept {
412 return std::min({
413 static_cast<size_type>(std::numeric_limits<difference_type>::max()),
414 static_cast<size_type>(std::numeric_limits<size_type>::max() / sizeof(T)),
415 static_cast<size_type>(std::allocator_traits<Allocator>::max_size(allocator)),
416 });
417 }
418
419 constexpr void reserve(size_type newCapacity) {
420 --newCapacity;
421 for (std::size_t i = 1; i < sizeof(newCapacity) * CHAR_BIT; i *= 2) {
422 newCapacity |= newCapacity >> i;
423 }
424 ++newCapacity;
425 if (newCapacity == 0) {
426 throw std::length_error{"Maximum capacity exceeded."};
427 }
428 reallocate(std::max(size_type{2}, newCapacity));
429 }
430
431 [[nodiscard]] constexpr size_type capacity() const noexcept {
432 return (mask == 0) ? 0 : mask + 1;
433 }
434
435 constexpr void shrink_to_fit() {
436 if (size() < capacity()) {
437 *this = DoubleEndedQueue{*this};
438 }
439 }
440
441 constexpr void clear() noexcept {
442 while (beginIndex < endIndex) {
443 std::allocator_traits<Allocator>::destroy(allocator, std::to_address(ringBuffer) + (beginIndex & mask));
444 ++beginIndex;
445 }
446 beginIndex = 0;
447 endIndex = 0;
448 }
449
450 constexpr iterator insert(const_iterator pos, const T& value) {
451 const difference_type offset = pos - cbegin();
452 GREM_ASSERT(offset <= static_cast<difference_type>(size()));
453 if (offset == 0) {
454 push_front(value);
455 return begin();
456 }
457 push_back(value);
458 const iterator it = begin() + offset;
459 const iterator itEnd = end();
460 std::rotate(it, itEnd - 1, itEnd);
461 return it;
462 }
463
464 constexpr iterator insert(const_iterator pos, T&& value) {
465 const difference_type offset = pos - cbegin();
466 GREM_ASSERT(offset <= static_cast<difference_type>(size()));
467 if (offset == 0) {
468 push_front(std::move(value));
469 return begin();
470 }
471 push_back(std::move(value));
472 const iterator it = begin() + offset;
473 const iterator itEnd = end();
474 std::rotate(it, itEnd - 1, itEnd);
475 return it;
476 }
477
478 constexpr iterator insert(const_iterator pos, size_type count, const T& value) {
479 const difference_type offset = pos - cbegin();
480 GREM_ASSERT(offset <= static_cast<difference_type>(size()));
481 if (offset == 0) {
482 while (count-- > 0) {
483 push_front(value);
484 }
485 return begin();
486 }
487 for (size_type i = 0; i < count; ++i) {
488 push_back(value);
489 }
490 const iterator it = begin() + offset;
491 const iterator itEnd = end();
492 std::rotate(it, itEnd - static_cast<difference_type>(count), itEnd);
493 return it;
494 }
495
496 template <std::input_iterator InputIterator, std::sentinel_for<InputIterator> Sentinel>
497 constexpr iterator insert(const_iterator pos, InputIterator first, Sentinel last) {
498 const difference_type offset = pos - cbegin();
499 GREM_ASSERT(offset <= static_cast<difference_type>(size()));
500 size_type count = 0;
501 while (first != last) {
502 push_back(*first++);
503 ++count;
504 }
505 const iterator it = begin() + offset;
506 const iterator itEnd = end();
507 std::rotate(it, itEnd - static_cast<difference_type>(count), itEnd);
508 return it;
509 }
510
511 constexpr iterator insert(const_iterator pos, std::initializer_list<T> ilist) {
512 return insert(pos, ilist.begin(), ilist.end());
513 }
514
515 template <typename R>
516 constexpr iterator insert_range(const_iterator pos, R&& r) { // NOLINT(cppcoreguidelines-missing-std-forward)
517 return insert(pos, std::begin(r), std::end(r));
518 }
519
520 template <typename R>
521 constexpr void append_range(R&& r) {
522 insert_range(end(), std::forward<R>(r));
523 }
524
525 template <typename... Args>
526 constexpr iterator emplace(const_iterator pos, Args&&... args) {
527 const difference_type offset = pos - cbegin();
528 GREM_ASSERT(offset <= static_cast<difference_type>(size()));
529 if (offset == 0) {
530 emplace_front(std::forward<Args>(args)...);
531 return begin();
532 }
533 emplace_back(std::forward<Args>(args)...);
534 const iterator it = begin() + offset;
535 const iterator itEnd = end();
536 std::rotate(it, itEnd - 1, itEnd);
537 return it;
538 }
539
541 const difference_type offset = pos - cbegin();
542 GREM_ASSERT(offset < static_cast<difference_type>(size()));
543 if (offset == 0) {
544 pop_front();
545 return begin();
546 }
547 const iterator it = begin() + offset;
548 std::shift_left(it, end(), 1);
549 pop_back();
550 return it;
551 }
552
554 const difference_type offset = first - cbegin();
555 const difference_type count = last - first;
556 GREM_ASSERT(offset <= static_cast<difference_type>(size()));
557 GREM_ASSERT(count >= 0);
558 GREM_ASSERT(offset + count <= static_cast<difference_type>(size()));
559 if (offset == 0) {
560 for (difference_type i = 0; i < count; ++i) {
561 pop_front();
562 }
563 return begin();
564 }
565 const iterator it = begin() + offset;
566 std::shift_left(it, end(), count);
567 for (difference_type i = 0; i < count; ++i) {
568 pop_back();
569 }
570 return it;
571 }
572
573 constexpr void push_back(const T& value) {
574 emplace_back(value);
575 }
576
577 constexpr void push_back(T&& value) {
578 emplace_back(std::move(value));
579 }
580
581 template <typename... Args>
582 constexpr reference emplace_back(Args&&... args) {
583 if (size() >= max_size()) {
584 throw std::length_error{"size() >= max_size()"};
585 }
586 if (size() >= capacity()) {
587 GREM_ASSERT(size() == capacity());
588 growCapacity();
589 }
591 T* const p = std::to_address(ringBuffer) + (endIndex & mask);
592 std::allocator_traits<Allocator>::construct(allocator, p, std::forward<Args>(args)...);
593 ++endIndex;
594 return *std::launder(p);
595 }
596
597 constexpr void pop_back() {
598 GREM_ASSERT(!empty());
599 --endIndex;
600 std::allocator_traits<Allocator>::destroy(allocator, std::to_address(ringBuffer) + (endIndex & mask));
601 }
602
603 constexpr void push_front(const T& value) {
604 emplace_front(value);
605 }
606
607 constexpr void push_front(T&& value) {
608 emplace_front(std::move(value));
609 }
610
611 template <typename... Args>
612 constexpr reference emplace_front(Args&&... args) {
613 if (size() >= max_size()) {
614 throw std::length_error{"size() >= max_size()"};
615 }
616 if (size() >= capacity()) {
617 GREM_ASSERT(size() == capacity());
618 growCapacity();
619 }
621 T* const p = std::to_address(ringBuffer) + ((beginIndex - 1) & mask);
622 std::allocator_traits<Allocator>::construct(allocator, p, std::forward<Args>(args)...);
623 --beginIndex;
624 return *std::launder(p);
625 }
626
627 constexpr void pop_front() {
628 GREM_ASSERT(!empty());
629 std::allocator_traits<Allocator>::destroy(allocator, std::to_address(ringBuffer) + (beginIndex & mask));
630 ++beginIndex;
631 }
632
633 constexpr void resize(size_type count) {
634 if (count < size()) {
635 for (size_type i = size() - count; i-- > 0;) {
636 pop_back();
637 }
638 } else {
639 for (size_type i = count - size(); i-- > 0;) {
640 emplace_back();
641 }
642 }
643 }
644
645 constexpr void resize(size_type count, const T& value) {
646 if (count < size()) {
647 for (size_type i = size() - count; i-- > 0;) {
648 pop_back();
649 }
650 } else {
651 for (size_type i = count - size(); i-- > 0;) {
652 push_back(value);
653 }
654 }
655 }
656
657 constexpr void swap(DoubleEndedQueue& other) noexcept(
658 std::allocator_traits<Allocator>::is_always_equal::value) { // NOLINT(cppcoreguidelines-noexcept-swap, performance-noexcept-swap)
659 if (this == &other) {
660 return;
661 }
662 using std::swap;
663 swap(ringBuffer, other.ringBuffer);
664 swap(mask, other.mask);
665 swap(beginIndex, other.beginIndex);
666 swap(endIndex, other.endIndex);
667 if constexpr (std::allocator_traits<Allocator>::propagate_on_container_swap::value) {
668 swap(allocator, other.allocator);
669 } else if constexpr (!std::allocator_traits<Allocator>::is_always_equal::value) {
670 GREM_ASSERT(allocator == other.allocator);
671 }
672 }
673
674 [[nodiscard]] constexpr bool operator==(const DoubleEndedQueue& other) const {
675 return std::equal(begin(), end(), other.begin(), other.end());
676 }
677
678 [[nodiscard]] constexpr bool operator<(const DoubleEndedQueue& other) const {
679 return std::lexicographical_compare(begin(), end(), other.begin(), other.end());
680 }
681
682 [[nodiscard]] constexpr bool operator<=(const DoubleEndedQueue& other) const {
683 return !(other < *this);
684 }
685
686 [[nodiscard]] constexpr bool operator>(const DoubleEndedQueue& other) const {
687 return other < *this;
688 }
689
690 [[nodiscard]] constexpr bool operator>=(const DoubleEndedQueue& other) const {
691 return !(*this < other);
692 }
693
694 friend constexpr void swap(DoubleEndedQueue& a, DoubleEndedQueue& b) noexcept(noexcept(a.swap(b))) { // NOLINT(cppcoreguidelines-noexcept-swap, performance-noexcept-swap)
695 a.swap(b);
696 }
697
698 template <typename U>
699 friend constexpr size_type erase(DoubleEndedQueue& c, const U& value) {
700 const iterator it = std::remove(c.begin(), c.end(), value);
701 const size_type result = static_cast<size_type>(c.end() - it);
702 c.erase(it, c.end());
703 return result;
704 }
705
706 template <typename Predicate>
707 friend constexpr size_type erase_if(DoubleEndedQueue& c, Predicate predicate) {
708 const iterator it = std::remove_if(c.begin(), c.end(), predicate);
709 const size_type result = static_cast<size_type>(c.end() - it);
710 c.erase(it, c.end());
711 return result;
712 }
713
714private:
715 constexpr void reset() noexcept {
716 clear();
717 if (ringBuffer) {
718 std::allocator_traits<Allocator>::deallocate(allocator, ringBuffer, capacity());
719 ringBuffer = nullptr;
720 mask = 0;
721 }
722 }
723
724 constexpr void growCapacity() {
725 const size_type newCapacity = (mask + 1) * 2;
726 if (newCapacity == 0) {
727 throw std::length_error{"Maximum capacity exceeded."};
728 }
729 reallocate(newCapacity);
730 }
731
732 constexpr void reallocate(size_type newCapacity) {
733 GREM_ASSERT(newCapacity >= 2 && (newCapacity & (newCapacity - 1)) == 0);
734 const pointer newRingBuffer = std::allocator_traits<Allocator>::allocate(allocator, newCapacity);
735 T* const pNewBegin = std::to_address(newRingBuffer);
736 const size_type count = size();
737 for (size_type i = 0; i < count; ++i) {
738 if constexpr (std::is_nothrow_move_constructible_v<T>) {
739 std::allocator_traits<Allocator>::construct(allocator, pNewBegin + i, std::move(ringBuffer[(beginIndex + i) & mask]));
740 } else {
741 try {
742 std::allocator_traits<Allocator>::construct(allocator, pNewBegin + i, std::move(ringBuffer[(beginIndex + i) & mask]));
743 } catch (...) {
744 while (i-- > 0) {
745 std::allocator_traits<Allocator>::destroy(allocator, pNewBegin + i);
746 }
747 std::allocator_traits<Allocator>::deallocate(allocator, newRingBuffer, newCapacity);
748 throw;
749 }
750 }
751 }
752 reset();
753 ringBuffer = newRingBuffer;
754 mask = newCapacity - 1;
755 beginIndex = 0;
756 endIndex = count;
757 }
758
759 pointer ringBuffer = nullptr;
760 size_type mask = 0;
761 size_type beginIndex = 0;
762 size_type endIndex = 0;
763 [[no_unique_address]] Allocator allocator;
764};
765
766} // namespace grem
767
768namespace grem::pmr {
769
770template <typename T>
772
773} // namespace grem::pmr
774
775#endif
#define GREM_ASSERT(expression)
Definition assertions.hpp:77
Definition DoubleEndedQueue.hpp:124
constexpr size_type capacity() const noexcept
Definition DoubleEndedQueue.hpp:431
constexpr reference front()
Definition DoubleEndedQueue.hpp:335
std::reverse_iterator< iterator > reverse_iterator
Definition DoubleEndedQueue.hpp:136
constexpr void shrink_to_fit()
Definition DoubleEndedQueue.hpp:435
constexpr iterator emplace(const_iterator pos, Args &&... args)
Definition DoubleEndedQueue.hpp:526
constexpr bool operator==(const DoubleEndedQueue &other) const
Definition DoubleEndedQueue.hpp:674
constexpr iterator insert(const_iterator pos, std::initializer_list< T > ilist)
Definition DoubleEndedQueue.hpp:511
friend constexpr size_type erase(DoubleEndedQueue &c, const U &value)
Definition DoubleEndedQueue.hpp:699
constexpr size_type max_size() const noexcept
Definition DoubleEndedQueue.hpp:411
constexpr void pop_back()
Definition DoubleEndedQueue.hpp:597
T value_type
Definition DoubleEndedQueue.hpp:126
constexpr void assign(size_type count, const T &value)
Definition DoubleEndedQueue.hpp:251
constexpr bool operator>=(const DoubleEndedQueue &other) const
Definition DoubleEndedQueue.hpp:690
std::size_t size_type
Definition DoubleEndedQueue.hpp:128
std::ptrdiff_t difference_type
Definition DoubleEndedQueue.hpp:129
constexpr reverse_iterator rend() noexcept
Definition DoubleEndedQueue.hpp:391
constexpr DoubleEndedQueue(const Allocator &allocator) noexcept
Definition DoubleEndedQueue.hpp:141
constexpr const_reverse_iterator crend() const noexcept
Definition DoubleEndedQueue.hpp:399
constexpr const_reference back() const
Definition DoubleEndedQueue.hpp:350
constexpr void swap(DoubleEndedQueue &other) noexcept(std::allocator_traits< Allocator >::is_always_equal::value)
Definition DoubleEndedQueue.hpp:657
constexpr const_reverse_iterator rbegin() const noexcept
Definition DoubleEndedQueue.hpp:383
constexpr DoubleEndedQueue(DoubleEndedQueue &&other) noexcept
Definition DoubleEndedQueue.hpp:169
constexpr void pop_front()
Definition DoubleEndedQueue.hpp:627
detail::DoubleEndedQueueIterator< const T > const_iterator
Definition DoubleEndedQueue.hpp:135
T & reference
Definition DoubleEndedQueue.hpp:130
constexpr void push_front(T &&value)
Definition DoubleEndedQueue.hpp:607
constexpr const_iterator begin() const noexcept
Definition DoubleEndedQueue.hpp:359
constexpr DoubleEndedQueue(size_type count, const Allocator &allocator=Allocator())
Definition DoubleEndedQueue.hpp:149
constexpr reference at(size_type pos)
Definition DoubleEndedQueue.hpp:311
constexpr reference emplace_back(Args &&... args)
Definition DoubleEndedQueue.hpp:582
constexpr reference back()
Definition DoubleEndedQueue.hpp:345
constexpr DoubleEndedQueue & operator=(const DoubleEndedQueue &other)
Definition DoubleEndedQueue.hpp:201
constexpr void clear() noexcept
Definition DoubleEndedQueue.hpp:441
constexpr void resize(size_type count, const T &value)
Definition DoubleEndedQueue.hpp:645
constexpr const_iterator cbegin() const noexcept
Definition DoubleEndedQueue.hpp:363
detail::DoubleEndedQueueIterator< T > iterator
Definition DoubleEndedQueue.hpp:134
constexpr const_reference front() const
Definition DoubleEndedQueue.hpp:340
friend constexpr void swap(DoubleEndedQueue &a, DoubleEndedQueue &b) noexcept(noexcept(a.swap(b)))
Definition DoubleEndedQueue.hpp:694
constexpr reverse_iterator rbegin() noexcept
Definition DoubleEndedQueue.hpp:379
constexpr void push_back(const T &value)
Definition DoubleEndedQueue.hpp:573
constexpr void resize(size_type count)
Definition DoubleEndedQueue.hpp:633
constexpr iterator insert(const_iterator pos, size_type count, const T &value)
Definition DoubleEndedQueue.hpp:478
constexpr const_reverse_iterator crbegin() const noexcept
Definition DoubleEndedQueue.hpp:387
constexpr void assign(std::initializer_list< T > ilist)
Definition DoubleEndedQueue.hpp:298
constexpr iterator begin() noexcept
Definition DoubleEndedQueue.hpp:355
constexpr void push_back(T &&value)
Definition DoubleEndedQueue.hpp:577
constexpr iterator insert(const_iterator pos, const T &value)
Definition DoubleEndedQueue.hpp:450
constexpr void reserve(size_type newCapacity)
Definition DoubleEndedQueue.hpp:419
constexpr void append_range(R &&r)
Definition DoubleEndedQueue.hpp:521
constexpr reference operator[](size_type pos)
Definition DoubleEndedQueue.hpp:325
constexpr reference emplace_front(Args &&... args)
Definition DoubleEndedQueue.hpp:612
constexpr iterator insert_range(const_iterator pos, R &&r)
Definition DoubleEndedQueue.hpp:516
constexpr bool operator<=(const DoubleEndedQueue &other) const
Definition DoubleEndedQueue.hpp:682
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition DoubleEndedQueue.hpp:137
Allocator allocator_type
Definition DoubleEndedQueue.hpp:127
constexpr DoubleEndedQueue(InputIterator first, Sentinel last, const Allocator &allocator=Allocator())
Definition DoubleEndedQueue.hpp:158
constexpr void assign_range(R &&r)
Definition DoubleEndedQueue.hpp:303
constexpr bool empty() const noexcept
Definition DoubleEndedQueue.hpp:403
constexpr DoubleEndedQueue()=default
typename std::allocator_traits< Allocator >::const_pointer const_pointer
Definition DoubleEndedQueue.hpp:133
constexpr DoubleEndedQueue(const DoubleEndedQueue &other, const Allocator &allocator)
Definition DoubleEndedQueue.hpp:166
constexpr iterator end() noexcept
Definition DoubleEndedQueue.hpp:367
constexpr DoubleEndedQueue(std::initializer_list< T > ilist, const Allocator &allocator=Allocator())
Definition DoubleEndedQueue.hpp:194
constexpr void assign(InputIterator first, Sentinel last)
Definition DoubleEndedQueue.hpp:271
constexpr iterator erase(const_iterator first, const_iterator last)
Definition DoubleEndedQueue.hpp:553
constexpr DoubleEndedQueue(DoubleEndedQueue &&other, const Allocator &allocator)
Definition DoubleEndedQueue.hpp:176
constexpr bool operator>(const DoubleEndedQueue &other) const
Definition DoubleEndedQueue.hpp:686
constexpr DoubleEndedQueue(size_type count, const T &value, const Allocator &allocator=Allocator())
Definition DoubleEndedQueue.hpp:144
constexpr void push_front(const T &value)
Definition DoubleEndedQueue.hpp:603
constexpr bool operator<(const DoubleEndedQueue &other) const
Definition DoubleEndedQueue.hpp:678
constexpr iterator erase(const_iterator pos)
Definition DoubleEndedQueue.hpp:540
constexpr iterator insert(const_iterator pos, T &&value)
Definition DoubleEndedQueue.hpp:464
constexpr iterator insert(const_iterator pos, InputIterator first, Sentinel last)
Definition DoubleEndedQueue.hpp:497
constexpr allocator_type get_allocator() const noexcept
Definition DoubleEndedQueue.hpp:307
constexpr DoubleEndedQueue & operator=(std::initializer_list< T > ilist)
Definition DoubleEndedQueue.hpp:246
constexpr const_iterator end() const noexcept
Definition DoubleEndedQueue.hpp:371
constexpr size_type size() const noexcept
Definition DoubleEndedQueue.hpp:407
constexpr const_reverse_iterator rend() const noexcept
Definition DoubleEndedQueue.hpp:395
constexpr DoubleEndedQueue & operator=(DoubleEndedQueue &&other) noexcept(std::allocator_traits< Allocator >::is_always_equal::value)
Definition DoubleEndedQueue.hpp:217
constexpr const_iterator cend() const noexcept
Definition DoubleEndedQueue.hpp:375
constexpr const_reference operator[](size_type pos) const
Definition DoubleEndedQueue.hpp:330
constexpr DoubleEndedQueue(const DoubleEndedQueue &other)
Definition DoubleEndedQueue.hpp:163
typename std::allocator_traits< Allocator >::pointer pointer
Definition DoubleEndedQueue.hpp:132
constexpr const_reference at(size_type pos) const
Definition DoubleEndedQueue.hpp:318
const T & const_reference
Definition DoubleEndedQueue.hpp:131
friend constexpr size_type erase_if(DoubleEndedQueue &c, Predicate predicate)
Definition DoubleEndedQueue.hpp:707
constexpr ~DoubleEndedQueue()
Definition DoubleEndedQueue.hpp:197
Definition concepts.hpp:106
@ T
't'
Definition keyboard.hpp:90
Definition Allocation.hpp:554
grem::DoubleEndedQueue< T, std::pmr::polymorphic_allocator< T > > DoubleEndedQueue
Definition DoubleEndedQueue.hpp:771
Definition aliases.hpp:9
constexpr auto count(input_range auto &&range, const auto &value)
Definition algorithms.hpp:234