4#ifndef GREM_CORE_DATA_RING_BUFFER_HPP
5#define GREM_CORE_DATA_RING_BUFFER_HPP
14#include <initializer_list>
18#include <memory_resource>
28class RingBufferIterator {
30 using difference_type = std::ptrdiff_t;
34 using iterator_category = std::random_access_iterator_tag;
36 RingBufferIterator() =
default;
38 constexpr RingBufferIterator(pointer ringBuffer, std::size_t mask, std::size_t index) noexcept
39 : ringBuffer(ringBuffer)
43 constexpr operator RingBufferIterator<const T>() const noexcept requires(!std::is_const_v<T>) {
44 return RingBufferIterator<const T>{ringBuffer, mask, index};
47 [[nodiscard]]
constexpr reference operator*()
const {
48 return ringBuffer[index & mask];
51 [[nodiscard]]
constexpr pointer operator->()
const {
52 return &ringBuffer[index & mask];
55 [[nodiscard]]
constexpr reference operator[](difference_type n)
const {
56 return ringBuffer[(index +
static_cast<std::size_t
>(n)) & mask];
59 constexpr RingBufferIterator& operator++() {
64 constexpr RingBufferIterator& operator--() {
69 constexpr RingBufferIterator operator++(
int) {
70 return RingBufferIterator{ringBuffer, mask, index++};
73 constexpr RingBufferIterator operator--(
int) {
74 return RingBufferIterator{ringBuffer, mask, index--};
77 constexpr RingBufferIterator& operator+=(difference_type n) {
78 index +=
static_cast<std::size_t
>(n);
82 constexpr RingBufferIterator& operator-=(difference_type n) {
83 index -=
static_cast<std::size_t
>(n);
87 [[nodiscard]]
friend constexpr RingBufferIterator operator+(RingBufferIterator a, difference_type b) {
88 return RingBufferIterator{a.ringBuffer, a.mask, a.index +
static_cast<std::size_t
>(b)};
91 [[nodiscard]]
friend constexpr RingBufferIterator operator+(difference_type a, RingBufferIterator b) {
92 return RingBufferIterator{b.ringBuffer, b.mask,
static_cast<std::size_t
>(a) + b.index};
95 [[nodiscard]]
friend constexpr RingBufferIterator operator-(RingBufferIterator a, difference_type b) {
96 return RingBufferIterator{a.ringBuffer, a.mask, a.index -
static_cast<std::size_t
>(b)};
99 [[nodiscard]]
friend constexpr difference_type operator-(RingBufferIterator a, RingBufferIterator b) {
101 return static_cast<difference_type
>(a.index - b.index);
104 [[nodiscard]]
friend constexpr bool operator==(RingBufferIterator a, RingBufferIterator b) {
106 return a.index == b.index;
109 [[nodiscard]]
friend constexpr auto operator<=>(RingBufferIterator a, RingBufferIterator b) {
111 return a.index <=> b.index;
122template <
typename T,
typename Allocator = std::allocator<T>>
131 using pointer =
typename std::allocator_traits<Allocator>::pointer;
132 using const_pointer =
typename std::allocator_traits<Allocator>::const_pointer;
140 constexpr explicit RingBuffer(
const Allocator& allocator) noexcept
141 : allocator(allocator) {}
151 while (
count-- > 0) {
156 template <std::input_iterator InputIterator, std::sentinel_for<InputIterator> Sentinel>
157 constexpr RingBuffer(InputIterator first, Sentinel last,
const Allocator& allocator = Allocator())
163 :
RingBuffer(other, std::allocator_traits<Allocator>::select_on_container_copy_construction(other.
get_allocator())) {}
169 : ringBuffer(std::exchange(other.ringBuffer,
nullptr))
170 , mask(std::exchange(other.mask,
size_type{0}))
171 , beginIndex(std::exchange(other.beginIndex,
size_type{0}))
172 , endIndex(std::exchange(other.endIndex,
size_type{0}))
173 , allocator(other.get_allocator()) {}
177 if constexpr (!std::allocator_traits<Allocator>::is_always_equal::value) {
178 if (allocator != other.get_allocator()) {
180 for (T& element : other) {
187 ringBuffer = std::exchange(other.ringBuffer,
nullptr);
188 mask = std::exchange(other.mask,
size_type{0});
189 beginIndex = std::exchange(other.beginIndex,
size_type{0});
190 endIndex = std::exchange(other.endIndex,
size_type{0});
193 constexpr RingBuffer(std::initializer_list<T> ilist,
const Allocator& allocator = Allocator())
201 if (
this == &other) {
204 if constexpr (std::allocator_traits<Allocator>::propagate_on_container_copy_assignment::value) {
205 if constexpr (!std::allocator_traits<Allocator>::is_always_equal::value) {
217 std::allocator_traits<Allocator>::is_always_equal::value) {
218 if (
this == &other) {
221 if constexpr (!std::allocator_traits<Allocator>::propagate_on_container_move_assignment::value) {
222 if constexpr (!std::allocator_traits<Allocator>::is_always_equal::value) {
223 if (allocator != other.get_allocator()) {
226 for (T& element : other) {
235 ringBuffer = std::exchange(other.ringBuffer,
nullptr);
236 mask = std::exchange(other.mask,
size_type{0});
237 beginIndex = std::exchange(other.beginIndex,
size_type{0});
238 endIndex = std::exchange(other.endIndex,
size_type{0});
239 if constexpr (std::allocator_traits<Allocator>::propagate_on_container_move_assignment::value) {
240 allocator = other.get_allocator();
251 if constexpr (std::is_copy_assignable_v<T>) {
256 for (T& element : *
this) {
264 while (
count-- > 0) {
269 template <std::input_iterator InputIterator, std::sentinel_for<InputIterator> Sentinel>
270 constexpr void assign(InputIterator first, Sentinel last) {
271 if constexpr (std::is_convertible_v<typename std::iterator_traits<InputIterator>::iterator_category, std::random_access_iterator_tag>) {
273 if constexpr (std::is_copy_assignable_v<T>) {
279 (*this)[i] = first[i];
291 while (first != last) {
297 constexpr void assign(std::initializer_list<T> ilist) {
298 assign(ilist.begin(), ilist.end());
301 template <
typename R>
303 assign(std::begin(r), std::end(r));
312 throw std::out_of_range{
"pos >= size()"};
319 throw std::out_of_range{
"pos >= size()"};
326 return ringBuffer[(beginIndex + pos) & mask];
331 return ringBuffer[(beginIndex + pos) & mask];
336 return ringBuffer[beginIndex & mask];
341 return ringBuffer[beginIndex & mask];
346 return ringBuffer[(endIndex - 1) & mask];
351 return ringBuffer[(endIndex - 1) & mask];
355 return iterator{ringBuffer, mask, beginIndex};
367 return iterator{ringBuffer, mask, endIndex};
402 [[nodiscard]]
constexpr bool empty() const noexcept {
403 return beginIndex == endIndex;
407 return endIndex - beginIndex;
412 static_cast<size_type>(std::numeric_limits<difference_type>::max()),
413 static_cast<size_type>(std::numeric_limits<size_type>::max() /
sizeof(T)),
414 static_cast<size_type>(std::allocator_traits<Allocator>::max_size(allocator)),
420 for (std::size_t i = 1; i <
sizeof(newCapacity) * CHAR_BIT; i *= 2) {
421 newCapacity |= newCapacity >> i;
424 if (newCapacity == 0) {
425 throw std::length_error{
"Maximum capacity exceeded."};
427 reallocate(std::max(
size_type{2}, newCapacity));
431 return (mask == 0) ? 0 : mask + 1;
455 std::rotate(it, itEnd - 1, itEnd);
469 std::rotate(it, itEnd - 1, itEnd);
477 while (
count-- > 0) {
491 template <std::input_iterator InputIterator, std::sentinel_for<InputIterator> Sentinel>
496 while (first != last) {
507 return insert(pos, ilist.begin(), ilist.end());
510 template <
typename R>
512 return insert(pos, std::begin(r), std::end(r));
515 template <
typename R>
530 std::rotate(it, itEnd - 1, itEnd);
538 while (
count-- > 0) {
560 std::rotate(it, it + 1,
end());
587 throw std::length_error{
"size() >= max_size()"};
594 T& storage = ringBuffer[endIndex & mask];
619 throw std::length_error{
"size() >= max_size()"};
626 T& storage = ringBuffer[(beginIndex - 1) & mask];
670 std::allocator_traits<Allocator>::is_always_equal::value) {
671 if (
this == &other) {
675 swap(ringBuffer, other.ringBuffer);
676 swap(mask, other.mask);
677 swap(beginIndex, other.beginIndex);
678 swap(endIndex, other.endIndex);
679 if constexpr (std::allocator_traits<Allocator>::propagate_on_container_swap::value) {
680 swap(allocator, other.allocator);
681 }
else if constexpr (!std::allocator_traits<Allocator>::is_always_equal::value) {
691 return std::lexicographical_compare(
begin(),
end(), other.
begin(), other.
end());
695 return !(other < *
this);
699 return other < *
this;
703 return !(*
this < other);
710 template <
typename U>
718 template <
typename Predicate>
727 constexpr void reset() noexcept {
731 T*
const pBegin = std::to_address(ringBuffer);
732 T*
const pEnd = pBegin + cap;
733 for (T* p = pBegin; p != pEnd; ++p) {
734 std::allocator_traits<Allocator>::destroy(allocator, p);
736 std::allocator_traits<Allocator>::deallocate(allocator, ringBuffer, cap);
737 ringBuffer =
nullptr;
742 constexpr void growCapacity() {
743 const size_type newCapacity = (mask + 1) * 2;
744 if (newCapacity == 0) {
745 throw std::length_error{
"Maximum capacity exceeded."};
747 reallocate(newCapacity);
750 constexpr void reallocate(
size_type newCapacity) {
751 GREM_ASSERT(newCapacity >= 2 && (newCapacity & (newCapacity - 1)) == 0);
752 const pointer newRingBuffer = std::allocator_traits<Allocator>::allocate(allocator, newCapacity);
753 T*
const pNewBegin = std::to_address(newRingBuffer);
757 if constexpr (std::is_nothrow_move_constructible_v<T>) {
758 std::allocator_traits<Allocator>::construct(allocator, pNewBegin + i, std::move(ringBuffer[(beginIndex + i) & mask]));
761 std::allocator_traits<Allocator>::construct(allocator, pNewBegin + i, std::move(ringBuffer[(beginIndex + i) & mask]));
764 std::allocator_traits<Allocator>::destroy(allocator, pNewBegin + i);
766 std::allocator_traits<Allocator>::deallocate(allocator, newRingBuffer, newCapacity);
771 for (
size_type i = cap; i < newCapacity; ++i) {
772 if constexpr (std::is_nothrow_default_constructible_v<T>) {
773 std::allocator_traits<Allocator>::construct(allocator, pNewBegin + i);
776 std::allocator_traits<Allocator>::construct(allocator, pNewBegin + i);
779 std::allocator_traits<Allocator>::destroy(allocator, pNewBegin + i);
781 std::allocator_traits<Allocator>::deallocate(allocator, newRingBuffer, newCapacity);
787 ringBuffer = newRingBuffer;
788 mask = newCapacity - 1;
797 [[no_unique_address]] Allocator allocator;
#define GREM_ASSERT(expression)
Definition assertions.hpp:77
Definition RingBuffer.hpp:123
constexpr void resize_and_overwrite_added_values(size_type count, auto overwrite)
Definition RingBuffer.hpp:657
constexpr const_reverse_iterator crend() const noexcept
Definition RingBuffer.hpp:398
typename std::allocator_traits< Allocator >::const_pointer const_pointer
Definition RingBuffer.hpp:132
constexpr void assign(InputIterator first, Sentinel last)
Definition RingBuffer.hpp:270
constexpr void push_front(const T &value)
Definition RingBuffer.hpp:636
constexpr const_reference back() const
Definition RingBuffer.hpp:349
detail::RingBufferIterator< T > iterator
Definition RingBuffer.hpp:133
std::size_t size_type
Definition RingBuffer.hpp:127
constexpr reference operator[](size_type pos)
Definition RingBuffer.hpp:324
constexpr void shrink_to_fit()
Definition RingBuffer.hpp:434
std::reverse_iterator< iterator > reverse_iterator
Definition RingBuffer.hpp:135
constexpr void swap(RingBuffer &other) noexcept(std::allocator_traits< Allocator >::is_always_equal::value)
Definition RingBuffer.hpp:669
constexpr RingBuffer & operator=(RingBuffer &&other) noexcept(std::allocator_traits< Allocator >::is_always_equal::value)
Definition RingBuffer.hpp:216
constexpr iterator erase(const_iterator first, const_iterator last)
Definition RingBuffer.hpp:565
constexpr iterator insert(const_iterator pos, T &&value)
Definition RingBuffer.hpp:459
constexpr const_reference operator[](size_type pos) const
Definition RingBuffer.hpp:329
constexpr const_iterator end() const noexcept
Definition RingBuffer.hpp:370
constexpr void push_back(const T &value)
Definition RingBuffer.hpp:604
constexpr RingBuffer(RingBuffer &&other) noexcept
Definition RingBuffer.hpp:168
constexpr RingBuffer & operator=(std::initializer_list< T > ilist)
Definition RingBuffer.hpp:245
constexpr reverse_iterator rend() noexcept
Definition RingBuffer.hpp:390
constexpr void resize(size_type count)
Definition RingBuffer.hpp:649
constexpr bool operator>=(const RingBuffer &other) const
Definition RingBuffer.hpp:702
detail::RingBufferIterator< const T > const_iterator
Definition RingBuffer.hpp:134
constexpr const_reverse_iterator rbegin() const noexcept
Definition RingBuffer.hpp:382
constexpr RingBuffer(RingBuffer &&other, const Allocator &allocator)
Definition RingBuffer.hpp:175
constexpr void resize(size_type count, const T &value)
Definition RingBuffer.hpp:653
constexpr RingBuffer(InputIterator first, Sentinel last, const Allocator &allocator=Allocator())
Definition RingBuffer.hpp:157
constexpr RingBuffer(const RingBuffer &other)
Definition RingBuffer.hpp:162
constexpr const_iterator begin() const noexcept
Definition RingBuffer.hpp:358
constexpr bool operator==(const RingBuffer &other) const
Definition RingBuffer.hpp:686
constexpr reference at(size_type pos)
Definition RingBuffer.hpp:310
constexpr iterator erase(const_iterator pos)
Definition RingBuffer.hpp:552
T & reference
Definition RingBuffer.hpp:129
constexpr iterator insert(const_iterator pos, size_type count, const T &value)
Definition RingBuffer.hpp:473
constexpr void assign(size_type count, const T &value)
Definition RingBuffer.hpp:250
constexpr RingBuffer(size_type count, const T &value, const Allocator &allocator=Allocator())
Definition RingBuffer.hpp:143
constexpr reference push_back_unspecified_value()
Definition RingBuffer.hpp:600
constexpr const_iterator cbegin() const noexcept
Definition RingBuffer.hpp:362
constexpr const_reverse_iterator rend() const noexcept
Definition RingBuffer.hpp:394
constexpr reference back()
Definition RingBuffer.hpp:344
constexpr ~RingBuffer()
Definition RingBuffer.hpp:196
constexpr reference push_front_and_overwrite(auto overwrite)
Definition RingBuffer.hpp:617
constexpr const_reverse_iterator crbegin() const noexcept
Definition RingBuffer.hpp:386
constexpr bool operator<=(const RingBuffer &other) const
Definition RingBuffer.hpp:694
constexpr iterator begin() noexcept
Definition RingBuffer.hpp:354
constexpr iterator insert_unspecified_value(const_iterator pos, size_type count)
Definition RingBuffer.hpp:534
constexpr void clear() noexcept
Definition RingBuffer.hpp:440
constexpr iterator insert(const_iterator pos, const T &value)
Definition RingBuffer.hpp:445
constexpr void push_front(T &&value)
Definition RingBuffer.hpp:640
constexpr void push_back(T &&value)
Definition RingBuffer.hpp:608
typename std::allocator_traits< Allocator >::pointer pointer
Definition RingBuffer.hpp:131
constexpr reference push_front_unspecified_value()
Definition RingBuffer.hpp:632
constexpr iterator insert(const_iterator pos, std::initializer_list< T > ilist)
Definition RingBuffer.hpp:506
constexpr allocator_type get_allocator() const noexcept
Definition RingBuffer.hpp:306
constexpr RingBuffer & operator=(const RingBuffer &other)
Definition RingBuffer.hpp:200
constexpr RingBuffer(std::initializer_list< T > ilist, const Allocator &allocator=Allocator())
Definition RingBuffer.hpp:193
constexpr const_reference at(size_type pos) const
Definition RingBuffer.hpp:317
constexpr void assign_range(R &&r)
Definition RingBuffer.hpp:302
constexpr size_type capacity() const noexcept
Definition RingBuffer.hpp:430
constexpr RingBuffer(size_type count, const Allocator &allocator=Allocator())
Definition RingBuffer.hpp:148
constexpr void pop_front()
Definition RingBuffer.hpp:644
constexpr iterator end() noexcept
Definition RingBuffer.hpp:366
constexpr void reserve(size_type newCapacity)
Definition RingBuffer.hpp:418
Allocator allocator_type
Definition RingBuffer.hpp:126
constexpr RingBuffer(const RingBuffer &other, const Allocator &allocator)
Definition RingBuffer.hpp:165
friend constexpr size_type erase_if(RingBuffer &c, Predicate predicate)
Definition RingBuffer.hpp:719
friend constexpr size_type erase(RingBuffer &c, const U &value)
Definition RingBuffer.hpp:711
constexpr void assign(std::initializer_list< T > ilist)
Definition RingBuffer.hpp:297
constexpr iterator insert_range(const_iterator pos, R &&r)
Definition RingBuffer.hpp:511
T value_type
Definition RingBuffer.hpp:125
friend constexpr void swap(RingBuffer &a, RingBuffer &b) noexcept(noexcept(a.swap(b)))
Definition RingBuffer.hpp:706
constexpr reference front()
Definition RingBuffer.hpp:334
constexpr bool operator<(const RingBuffer &other) const
Definition RingBuffer.hpp:690
constexpr bool operator>(const RingBuffer &other) const
Definition RingBuffer.hpp:698
constexpr RingBuffer(const Allocator &allocator) noexcept
Definition RingBuffer.hpp:140
constexpr iterator insert(const_iterator pos, InputIterator first, Sentinel last)
Definition RingBuffer.hpp:492
const T & const_reference
Definition RingBuffer.hpp:130
constexpr const_reference front() const
Definition RingBuffer.hpp:339
std::ptrdiff_t difference_type
Definition RingBuffer.hpp:128
constexpr size_type size() const noexcept
Definition RingBuffer.hpp:406
constexpr size_type max_size() const noexcept
Definition RingBuffer.hpp:410
constexpr iterator insert_unspecified_value(const_iterator pos)
Definition RingBuffer.hpp:520
constexpr void pop_back()
Definition RingBuffer.hpp:612
constexpr void append_range(R &&r)
Definition RingBuffer.hpp:516
constexpr reverse_iterator rbegin() noexcept
Definition RingBuffer.hpp:378
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition RingBuffer.hpp:136
constexpr reference push_back_and_overwrite(auto overwrite)
Definition RingBuffer.hpp:585
constexpr RingBuffer()=default
constexpr const_iterator cend() const noexcept
Definition RingBuffer.hpp:374
constexpr bool empty() const noexcept
Definition RingBuffer.hpp:402
Definition concepts.hpp:106
@ T
't'
Definition keyboard.hpp:90
Definition Allocation.hpp:554
grem::RingBuffer< T, std::pmr::polymorphic_allocator< T > > RingBuffer
Definition RingBuffer.hpp:805
constexpr auto count(input_range auto &&range, const auto &value)
Definition algorithms.hpp:234