4#ifndef GREM_CORE_DATA_INPLACE_DOUBLE_ENDED_QUEUE_HPP
5#define GREM_CORE_DATA_INPLACE_DOUBLE_ENDED_QUEUE_HPP
15#include <initializer_list>
27union InplaceDoubleEndedQueueStorage {
28 constexpr InplaceDoubleEndedQueueStorage()
31 ~InplaceDoubleEndedQueueStorage() =
default;
32 constexpr ~InplaceDoubleEndedQueueStorage()
requires(!std::is_trivially_destructible_v<T>) {}
34 constexpr InplaceDoubleEndedQueueStorage(
const InplaceDoubleEndedQueueStorage&) =
default;
35 constexpr InplaceDoubleEndedQueueStorage(InplaceDoubleEndedQueueStorage&&) noexcept(
36 std::is_nothrow_move_constructible_v<T>) = default;
37 constexpr InplaceDoubleEndedQueueStorage& operator=(const InplaceDoubleEndedQueueStorage&) = default;
38 constexpr InplaceDoubleEndedQueueStorage& operator=(InplaceDoubleEndedQueueStorage&&) noexcept(
39 std::is_nothrow_move_assignable_v<T>) = default;
41 template <typename... Args>
42 constexpr T& construct(Args&&... args) {
43 return *std::construct_at(&value, std::forward<Args>(args)...);
46 constexpr void destroy() noexcept {
47 std::destroy_at(&value);
54template <
typename T, std::
size_t Capacity>
55class InplaceDoubleEndedQueueIterator {
59 std::conditional_t<Capacity <= 255ull, std::uint8_t,
60 std::conditional_t<Capacity <= 65535ull, std::uint16_t,
61 std::conditional_t<Capacity <= 4294967295ull, std::uint32_t,
65 using Pointer = std::conditional_t<std::is_const_v<T>,
const InplaceDoubleEndedQueueStorage<std::remove_const_t<T>>*, InplaceDoubleEndedQueueStorage<T>*>;
68 static_assert(Capacity >= 2 && (Capacity & (Capacity - 1)) == 0,
"InplaceDoubleEndedQueueIterator capacity must be a positive power of 2!");
70 using difference_type = std::ptrdiff_t;
74 using iterator_category = std::random_access_iterator_tag;
76 InplaceDoubleEndedQueueIterator() =
default;
78 constexpr InplaceDoubleEndedQueueIterator(Pointer ringBuffer, Size index) noexcept
79 : ringBuffer(ringBuffer)
82 constexpr operator InplaceDoubleEndedQueueIterator<const T, Capacity>() const noexcept requires(!std::is_const_v<T>) {
83 return InplaceDoubleEndedQueueIterator<const T, Capacity>{ringBuffer, index};
86 [[nodiscard]]
constexpr reference operator*()
const {
87 return ringBuffer[index &
static_cast<Size
>(Capacity - 1)].value;
90 [[nodiscard]]
constexpr pointer operator->()
const {
91 return &ringBuffer[index &
static_cast<Size
>(Capacity - 1)].value;
94 [[nodiscard]]
constexpr reference operator[](difference_type n)
const {
95 return ringBuffer[
static_cast<Size
>(index +
static_cast<Size
>(n)) &
static_cast<Size
>(Capacity - 1)].value;
98 constexpr InplaceDoubleEndedQueueIterator& operator++() {
103 constexpr InplaceDoubleEndedQueueIterator& operator--() {
108 constexpr InplaceDoubleEndedQueueIterator operator++(
int) {
109 return InplaceDoubleEndedQueueIterator{ringBuffer, index++};
112 constexpr InplaceDoubleEndedQueueIterator operator--(
int) {
113 return InplaceDoubleEndedQueueIterator{ringBuffer, index--};
116 constexpr InplaceDoubleEndedQueueIterator& operator+=(difference_type n) {
117 index +=
static_cast<Size
>(n);
121 constexpr InplaceDoubleEndedQueueIterator& operator-=(difference_type n) {
122 index -=
static_cast<Size
>(n);
126 [[nodiscard]]
friend constexpr InplaceDoubleEndedQueueIterator operator+(InplaceDoubleEndedQueueIterator a, difference_type b) {
127 return InplaceDoubleEndedQueueIterator{a.ringBuffer,
static_cast<Size
>(a.index +
static_cast<Size
>(b))};
130 [[nodiscard]]
friend constexpr InplaceDoubleEndedQueueIterator operator+(difference_type a, InplaceDoubleEndedQueueIterator b) {
131 return InplaceDoubleEndedQueueIterator{b.ringBuffer,
static_cast<Size
>(
static_cast<Size
>(a) + b.index)};
134 [[nodiscard]]
friend constexpr InplaceDoubleEndedQueueIterator operator-(InplaceDoubleEndedQueueIterator a, difference_type b) {
135 return InplaceDoubleEndedQueueIterator{a.ringBuffer,
static_cast<Size
>(a.index -
static_cast<Size
>(b))};
138 [[nodiscard]]
friend constexpr difference_type operator-(InplaceDoubleEndedQueueIterator a, InplaceDoubleEndedQueueIterator b) {
140 return static_cast<difference_type
>(
static_cast<Size
>(a.index - b.index));
143 [[nodiscard]]
friend constexpr bool operator==(InplaceDoubleEndedQueueIterator a, InplaceDoubleEndedQueueIterator b) {
145 return a.index == b.index;
148 [[nodiscard]]
friend constexpr auto operator<=>(InplaceDoubleEndedQueueIterator a, InplaceDoubleEndedQueueIterator b) {
150 return a.index <=> b.index;
160template <
typename T, std::
size_t N>
163 static constexpr std::size_t Capacity = [] {
166 for (std::size_t i = 1; i <
sizeof(
capacity) * CHAR_BIT; i *= 2) {
176 std::conditional_t<Capacity <= 255ull, std::uint8_t,
177 std::conditional_t<Capacity <= 65535ull, std::uint16_t,
178 std::conditional_t<Capacity <= 4294967295ull, std::uint32_t,
182 using Storage = detail::InplaceDoubleEndedQueueStorage<T>;
192 using iterator = detail::InplaceDoubleEndedQueueIterator<T, Capacity>;
193 using const_iterator = detail::InplaceDoubleEndedQueueIterator<const T, Capacity>;
209 template <std::input_iterator InputIterator, std::sentinel_for<InputIterator> Sentinel>
223 std::is_nothrow_move_constructible_v<T>)
224 requires(!std::is_trivially_copyable_v<T>)
226 for (T& element : other) {
244 if (
this == &other) {
247 assign(other.begin(), other.end());
254 std::is_nothrow_move_constructible_v<T> &&
255 std::is_nothrow_move_assignable_v<T>) requires(!std::is_trivially_copyable_v<T>) {
256 if (
this == &other) {
259 if (
size() == other.size()) {
260 std::move(other.begin(), other.end(), begin());
265 for (T& element : other) {
278 if constexpr (std::is_copy_assignable_v<T>) {
281 for (T& element : *
this) {
288 while (
count-- > 0) {
293 template <std::input_iterator InputIterator, std::sentinel_for<InputIterator> Sentinel>
294 constexpr void assign(InputIterator first, Sentinel last) {
295 if constexpr (std::is_convertible_v<typename std::iterator_traits<InputIterator>::iterator_category, std::random_access_iterator_tag>) {
297 if constexpr (std::is_copy_assignable_v<T>) {
301 (*this)[i] = first[i];
312 while (first != last) {
318 constexpr void assign(std::initializer_list<T> ilist) {
319 assign(ilist.begin(), ilist.end());
322 template <
typename R>
324 assign(std::begin(r), std::end(r));
329 throw std::out_of_range{
"pos >= size()"};
336 throw std::out_of_range{
"pos >= size()"};
343 return ringBuffer[
static_cast<Size
>(beginIndex +
static_cast<Size
>(pos)) &
static_cast<Size
>(Capacity - 1)].value;
348 return ringBuffer[
static_cast<Size
>(beginIndex +
static_cast<Size
>(pos)) &
static_cast<Size
>(Capacity - 1)].value;
353 return ringBuffer[beginIndex &
static_cast<Size
>(Capacity - 1)].value;
358 return ringBuffer[beginIndex &
static_cast<Size
>(Capacity - 1)].value;
363 return ringBuffer[
static_cast<Size
>(endIndex - 1) &
static_cast<Size
>(Capacity - 1)].value;
368 return ringBuffer[
static_cast<Size
>(endIndex - 1) &
static_cast<Size
>(Capacity - 1)].value;
372 return iterator{ringBuffer, beginIndex};
384 return iterator{ringBuffer, endIndex};
419 [[nodiscard]]
constexpr bool empty() const noexcept {
420 return beginIndex == endIndex;
424 return static_cast<size_type>(
static_cast<Size
>(endIndex - beginIndex));
436 while (beginIndex < endIndex) {
437 ringBuffer[beginIndex &
static_cast<Size
>(Capacity - 1)].destroy();
454 std::rotate(it, itEnd - 1, itEnd);
468 std::rotate(it, itEnd - 1, itEnd);
476 while (
count-- > 0) {
490 template <std::input_iterator InputIterator, std::sentinel_for<InputIterator> Sentinel>
495 while (first != last) {
506 return insert(pos, ilist.begin(), ilist.end());
509 template <
typename R>
511 return insert(pos, std::begin(r), std::end(r));
514 template <
typename R>
519 template <
typename... Args>
530 std::rotate(it, itEnd - 1, itEnd);
542 std::shift_left(it,
end(), 1);
575 template <
typename... Args>
578 throw std::length_error{
"size() >= max_size()"};
581 Storage& storage = ringBuffer[endIndex &
static_cast<Size
>(Capacity - 1)];
582 reference result = storage.construct(std::forward<Args>(args)...);
590 ringBuffer[endIndex &
static_cast<Size
>(Capacity - 1)].destroy();
601 template <
typename... Args>
604 throw std::length_error{
"size() >= max_size()"};
607 Storage& storage = ringBuffer[(beginIndex - 1) &
static_cast<Size
>(Capacity - 1)];
608 reference result = storage.construct(std::forward<Args>(args)...);
615 ringBuffer[beginIndex &
static_cast<Size
>(Capacity - 1)].destroy();
644 if (
this == &other) {
648 *
this = std::move(other);
649 other = std::move(temporary);
657 return std::lexicographical_compare(
begin(),
end(), other.
begin(), other.
end());
661 return !(other < *
this);
665 return other < *
this;
669 return !(*
this < other);
677 template <
typename U>
685 template <
typename Predicate>
696 Storage ringBuffer[Capacity]{};
#define GREM_ASSERT(expression)
Definition assertions.hpp:77
constexpr size_type capacity() const noexcept
Definition InplaceDoubleEndedQueue.hpp:431
constexpr void push_back(const T &value)
Definition InplaceDoubleEndedQueue.hpp:567
constexpr bool operator<=(const InplaceDoubleEndedQueue &other) const
Definition InplaceDoubleEndedQueue.hpp:660
constexpr ~InplaceDoubleEndedQueue()=default
const T * const_pointer
Definition InplaceDoubleEndedQueue.hpp:191
const T & const_reference
Definition InplaceDoubleEndedQueue.hpp:189
constexpr iterator insert(const_iterator pos, InputIterator first, Sentinel last)
Definition InplaceDoubleEndedQueue.hpp:491
constexpr reverse_iterator rbegin() noexcept
Definition InplaceDoubleEndedQueue.hpp:395
constexpr InplaceDoubleEndedQueue & operator=(const InplaceDoubleEndedQueue &)=default
constexpr const_reference back() const
Definition InplaceDoubleEndedQueue.hpp:366
constexpr const_reverse_iterator rbegin() const noexcept
Definition InplaceDoubleEndedQueue.hpp:399
constexpr const_reverse_iterator crbegin() const noexcept
Definition InplaceDoubleEndedQueue.hpp:403
constexpr void clear() noexcept
Definition InplaceDoubleEndedQueue.hpp:435
constexpr void swap(InplaceDoubleEndedQueue &other) noexcept(std::is_nothrow_move_constructible_v< T >)
Definition InplaceDoubleEndedQueue.hpp:643
constexpr InplaceDoubleEndedQueue(InputIterator first, Sentinel last)
Definition InplaceDoubleEndedQueue.hpp:210
constexpr bool operator>(const InplaceDoubleEndedQueue &other) const
Definition InplaceDoubleEndedQueue.hpp:664
std::size_t size_type
Definition InplaceDoubleEndedQueue.hpp:186
constexpr bool empty() const noexcept
Definition InplaceDoubleEndedQueue.hpp:419
constexpr void resize(size_type count, const T &value)
Definition InplaceDoubleEndedQueue.hpp:631
constexpr void assign_range(R &&r)
Definition InplaceDoubleEndedQueue.hpp:323
std::ptrdiff_t difference_type
Definition InplaceDoubleEndedQueue.hpp:187
constexpr size_type size() const noexcept
Definition InplaceDoubleEndedQueue.hpp:423
detail::InplaceDoubleEndedQueueIterator< const T, Capacity > const_iterator
Definition InplaceDoubleEndedQueue.hpp:193
constexpr void pop_front()
Definition InplaceDoubleEndedQueue.hpp:613
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition InplaceDoubleEndedQueue.hpp:195
constexpr reference at(size_type pos)
Definition InplaceDoubleEndedQueue.hpp:327
constexpr reference emplace_back(Args &&... args)
Definition InplaceDoubleEndedQueue.hpp:576
constexpr void push_front(const T &value)
Definition InplaceDoubleEndedQueue.hpp:593
friend constexpr size_type erase(InplaceDoubleEndedQueue &c, const U &value)
Definition InplaceDoubleEndedQueue.hpp:678
constexpr const_reference at(size_type pos) const
Definition InplaceDoubleEndedQueue.hpp:334
constexpr void append_range(R &&r)
Definition InplaceDoubleEndedQueue.hpp:515
constexpr const_iterator begin() const noexcept
Definition InplaceDoubleEndedQueue.hpp:375
constexpr iterator end() noexcept
Definition InplaceDoubleEndedQueue.hpp:383
constexpr void resize(size_type count)
Definition InplaceDoubleEndedQueue.hpp:619
constexpr InplaceDoubleEndedQueue(std::initializer_list< T > ilist)
Definition InplaceDoubleEndedQueue.hpp:232
detail::InplaceDoubleEndedQueueIterator< T, Capacity > iterator
Definition InplaceDoubleEndedQueue.hpp:192
constexpr ~InplaceDoubleEndedQueue()
Definition InplaceDoubleEndedQueue.hpp:237
T & reference
Definition InplaceDoubleEndedQueue.hpp:188
friend constexpr size_type erase_if(InplaceDoubleEndedQueue &c, Predicate predicate)
Definition InplaceDoubleEndedQueue.hpp:686
constexpr bool operator>=(const InplaceDoubleEndedQueue &other) const
Definition InplaceDoubleEndedQueue.hpp:668
constexpr bool operator<(const InplaceDoubleEndedQueue &other) const
Definition InplaceDoubleEndedQueue.hpp:656
constexpr const_iterator cbegin() const noexcept
Definition InplaceDoubleEndedQueue.hpp:379
constexpr iterator insert_range(const_iterator pos, R &&r)
Definition InplaceDoubleEndedQueue.hpp:510
constexpr InplaceDoubleEndedQueue(const InplaceDoubleEndedQueue &other)
Definition InplaceDoubleEndedQueue.hpp:217
constexpr void assign(std::initializer_list< T > ilist)
Definition InplaceDoubleEndedQueue.hpp:318
constexpr const_reference front() const
Definition InplaceDoubleEndedQueue.hpp:356
constexpr iterator erase(const_iterator pos)
Definition InplaceDoubleEndedQueue.hpp:534
constexpr iterator insert(const_iterator pos, T &&value)
Definition InplaceDoubleEndedQueue.hpp:458
constexpr void push_front(T &&value)
Definition InplaceDoubleEndedQueue.hpp:597
constexpr InplaceDoubleEndedQueue & operator=(InplaceDoubleEndedQueue &&) noexcept=default
constexpr iterator insert(const_iterator pos, const T &value)
Definition InplaceDoubleEndedQueue.hpp:444
constexpr void assign(size_type count, const T &value)
Definition InplaceDoubleEndedQueue.hpp:277
constexpr const_reverse_iterator crend() const noexcept
Definition InplaceDoubleEndedQueue.hpp:415
constexpr const_iterator cend() const noexcept
Definition InplaceDoubleEndedQueue.hpp:391
constexpr InplaceDoubleEndedQueue(size_type count)
Definition InplaceDoubleEndedQueue.hpp:204
constexpr iterator erase(const_iterator first, const_iterator last)
Definition InplaceDoubleEndedQueue.hpp:547
constexpr InplaceDoubleEndedQueue()=default
constexpr iterator emplace(const_iterator pos, Args &&... args)
Definition InplaceDoubleEndedQueue.hpp:520
constexpr void assign(InputIterator first, Sentinel last)
Definition InplaceDoubleEndedQueue.hpp:294
constexpr InplaceDoubleEndedQueue(InplaceDoubleEndedQueue &&) noexcept=default
constexpr InplaceDoubleEndedQueue(size_type count, const T &value)
Definition InplaceDoubleEndedQueue.hpp:199
constexpr reference operator[](size_type pos)
Definition InplaceDoubleEndedQueue.hpp:341
constexpr iterator insert(const_iterator pos, std::initializer_list< T > ilist)
Definition InplaceDoubleEndedQueue.hpp:505
constexpr size_type max_size() const noexcept
Definition InplaceDoubleEndedQueue.hpp:427
constexpr iterator insert(const_iterator pos, size_type count, const T &value)
Definition InplaceDoubleEndedQueue.hpp:472
constexpr reference front()
Definition InplaceDoubleEndedQueue.hpp:351
constexpr void pop_back()
Definition InplaceDoubleEndedQueue.hpp:587
constexpr iterator begin() noexcept
Definition InplaceDoubleEndedQueue.hpp:371
constexpr InplaceDoubleEndedQueue & operator=(const InplaceDoubleEndedQueue &other)
Definition InplaceDoubleEndedQueue.hpp:243
constexpr InplaceDoubleEndedQueue(const InplaceDoubleEndedQueue &)=default
constexpr void push_back(T &&value)
Definition InplaceDoubleEndedQueue.hpp:571
constexpr InplaceDoubleEndedQueue & operator=(std::initializer_list< T > ilist)
Definition InplaceDoubleEndedQueue.hpp:272
constexpr reference back()
Definition InplaceDoubleEndedQueue.hpp:361
constexpr bool operator==(const InplaceDoubleEndedQueue &other) const
Definition InplaceDoubleEndedQueue.hpp:652
friend constexpr void swap(InplaceDoubleEndedQueue &a, InplaceDoubleEndedQueue &b) noexcept(noexcept(a.swap(b)))
Definition InplaceDoubleEndedQueue.hpp:672
constexpr const_reverse_iterator rend() const noexcept
Definition InplaceDoubleEndedQueue.hpp:411
T value_type
Definition InplaceDoubleEndedQueue.hpp:185
constexpr const_reference operator[](size_type pos) const
Definition InplaceDoubleEndedQueue.hpp:346
constexpr reverse_iterator rend() noexcept
Definition InplaceDoubleEndedQueue.hpp:407
std::reverse_iterator< iterator > reverse_iterator
Definition InplaceDoubleEndedQueue.hpp:194
constexpr reference emplace_front(Args &&... args)
Definition InplaceDoubleEndedQueue.hpp:602
constexpr const_iterator end() const noexcept
Definition InplaceDoubleEndedQueue.hpp:387
Definition concepts.hpp:106
@ T
't'
Definition keyboard.hpp:90
constexpr auto count(input_range auto &&range, const auto &value)
Definition algorithms.hpp:234