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