4#ifndef GREM_CORE_DATA_DOUBLE_ENDED_QUEUE_HPP
5#define GREM_CORE_DATA_DOUBLE_ENDED_QUEUE_HPP
14#include <initializer_list>
18#include <memory_resource>
29class DoubleEndedQueueIterator {
31 using difference_type = std::ptrdiff_t;
35 using iterator_category = std::random_access_iterator_tag;
37 DoubleEndedQueueIterator() =
default;
39 constexpr DoubleEndedQueueIterator(pointer ringBuffer, std::size_t mask, std::size_t index) noexcept
40 : ringBuffer(ringBuffer)
44 constexpr operator DoubleEndedQueueIterator<const T>() const noexcept requires(!std::is_const_v<T>) {
45 return DoubleEndedQueueIterator<const T>{ringBuffer, mask, index};
48 [[nodiscard]]
constexpr reference operator*()
const {
49 return ringBuffer[index & mask];
52 [[nodiscard]]
constexpr pointer operator->()
const {
53 return &ringBuffer[index & mask];
56 [[nodiscard]]
constexpr reference operator[](difference_type n)
const {
57 return ringBuffer[(index +
static_cast<std::size_t
>(n)) & mask];
60 constexpr DoubleEndedQueueIterator& operator++() {
65 constexpr DoubleEndedQueueIterator& operator--() {
70 constexpr DoubleEndedQueueIterator operator++(
int) {
71 return DoubleEndedQueueIterator{ringBuffer, mask, index++};
74 constexpr DoubleEndedQueueIterator operator--(
int) {
75 return DoubleEndedQueueIterator{ringBuffer, mask, index--};
78 constexpr DoubleEndedQueueIterator& operator+=(difference_type n) {
79 index +=
static_cast<std::size_t
>(n);
83 constexpr DoubleEndedQueueIterator& operator-=(difference_type n) {
84 index -=
static_cast<std::size_t
>(n);
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)};
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};
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)};
100 [[nodiscard]]
friend constexpr difference_type operator-(DoubleEndedQueueIterator a, DoubleEndedQueueIterator b) {
102 return static_cast<difference_type
>(a.index - b.index);
105 [[nodiscard]]
friend constexpr bool operator==(DoubleEndedQueueIterator a, DoubleEndedQueueIterator b) {
107 return a.index == b.index;
110 [[nodiscard]]
friend constexpr auto operator<=>(DoubleEndedQueueIterator a, DoubleEndedQueueIterator b) {
112 return a.index <=> b.index;
123template <
typename T,
typename Allocator = std::allocator<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>;
142 : allocator(allocator) {}
152 while (
count-- > 0) {
157 template <std::input_iterator InputIterator, std::sentinel_for<InputIterator> Sentinel>
158 constexpr DoubleEndedQueue(InputIterator first, Sentinel last,
const Allocator& allocator = Allocator())
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()) {}
178 if constexpr (!std::allocator_traits<Allocator>::is_always_equal::value) {
179 if (allocator != other.get_allocator()) {
181 for (T& element : other) {
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});
194 constexpr DoubleEndedQueue(std::initializer_list<T> ilist,
const Allocator& allocator = Allocator())
202 if (
this == &other) {
205 if constexpr (std::allocator_traits<Allocator>::propagate_on_container_copy_assignment::value) {
206 if constexpr (!std::allocator_traits<Allocator>::is_always_equal::value) {
218 std::allocator_traits<Allocator>::is_always_equal::value) {
219 if (
this == &other) {
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()) {
227 for (T& element : other) {
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();
252 if constexpr (std::is_copy_assignable_v<T>) {
257 for (T& element : *
this) {
265 while (
count-- > 0) {
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>) {
274 if constexpr (std::is_copy_assignable_v<T>) {
280 (*this)[i] = first[i];
292 while (first != last) {
298 constexpr void assign(std::initializer_list<T> ilist) {
299 assign(ilist.begin(), ilist.end());
302 template <
typename R>
304 assign(std::begin(r), std::end(r));
313 throw std::out_of_range{
"pos >= size()"};
320 throw std::out_of_range{
"pos >= size()"};
327 return ringBuffer[(beginIndex + pos) & mask];
332 return ringBuffer[(beginIndex + pos) & mask];
337 return ringBuffer[beginIndex & mask];
342 return ringBuffer[beginIndex & mask];
347 return ringBuffer[(endIndex - 1) & mask];
352 return ringBuffer[(endIndex - 1) & mask];
356 return iterator{ringBuffer, mask, beginIndex};
368 return iterator{ringBuffer, mask, endIndex};
403 [[nodiscard]]
constexpr bool empty() const noexcept {
404 return beginIndex == endIndex;
408 return endIndex - beginIndex;
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)),
421 for (std::size_t i = 1; i <
sizeof(newCapacity) * CHAR_BIT; i *= 2) {
422 newCapacity |= newCapacity >> i;
425 if (newCapacity == 0) {
426 throw std::length_error{
"Maximum capacity exceeded."};
428 reallocate(std::max(
size_type{2}, newCapacity));
432 return (mask == 0) ? 0 : mask + 1;
442 while (beginIndex < endIndex) {
443 std::allocator_traits<Allocator>::destroy(allocator, std::to_address(ringBuffer) + (beginIndex & mask));
460 std::rotate(it, itEnd - 1, itEnd);
474 std::rotate(it, itEnd - 1, itEnd);
482 while (
count-- > 0) {
496 template <std::input_iterator InputIterator, std::sentinel_for<InputIterator> Sentinel>
501 while (first != last) {
512 return insert(pos, ilist.begin(), ilist.end());
515 template <
typename R>
517 return insert(pos, std::begin(r), std::end(r));
520 template <
typename R>
525 template <
typename... Args>
536 std::rotate(it, itEnd - 1, itEnd);
548 std::shift_left(it,
end(), 1);
581 template <
typename... Args>
584 throw std::length_error{
"size() >= max_size()"};
591 T*
const p = std::to_address(ringBuffer) + (endIndex & mask);
592 std::allocator_traits<Allocator>::construct(allocator, p, std::forward<Args>(args)...);
594 return *std::launder(p);
600 std::allocator_traits<Allocator>::destroy(allocator, std::to_address(ringBuffer) + (endIndex & mask));
611 template <
typename... Args>
614 throw std::length_error{
"size() >= max_size()"};
621 T*
const p = std::to_address(ringBuffer) + ((beginIndex - 1) & mask);
622 std::allocator_traits<Allocator>::construct(allocator, p, std::forward<Args>(args)...);
624 return *std::launder(p);
629 std::allocator_traits<Allocator>::destroy(allocator, std::to_address(ringBuffer) + (beginIndex & mask));
658 std::allocator_traits<Allocator>::is_always_equal::value) {
659 if (
this == &other) {
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) {
679 return std::lexicographical_compare(
begin(),
end(), other.
begin(), other.
end());
683 return !(other < *
this);
687 return other < *
this;
691 return !(*
this < other);
698 template <
typename U>
706 template <
typename Predicate>
715 constexpr void reset() noexcept {
718 std::allocator_traits<Allocator>::deallocate(allocator, ringBuffer,
capacity());
719 ringBuffer =
nullptr;
724 constexpr void growCapacity() {
725 const size_type newCapacity = (mask + 1) * 2;
726 if (newCapacity == 0) {
727 throw std::length_error{
"Maximum capacity exceeded."};
729 reallocate(newCapacity);
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);
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]));
742 std::allocator_traits<Allocator>::construct(allocator, pNewBegin + i, std::move(ringBuffer[(beginIndex + i) & mask]));
745 std::allocator_traits<Allocator>::destroy(allocator, pNewBegin + i);
747 std::allocator_traits<Allocator>::deallocate(allocator, newRingBuffer, newCapacity);
753 ringBuffer = newRingBuffer;
754 mask = newCapacity - 1;
763 [[no_unique_address]] Allocator allocator;
#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
constexpr auto count(input_range auto &&range, const auto &value)
Definition algorithms.hpp:234