gz-cpp-util 1.3
A c++20 library containing various utilities
|
A fixed size buffer that can store data continuously without needing to move data or reallocate memory. More...
#include <ringbuffer.hpp>
Classes | |
struct | Iterator |
Bidirectonal iterator for the RingBuffer. More... | |
Public Member Functions | |
RingBuffer (size_t size=10) | |
void | push_back (T &t) |
void | push_back (T &&t) |
void | emplace_back (T &&t) |
const Iterator | cbegin () const |
Return an iterator pointing to the newest object. | |
const Iterator | cend () const |
Return an iterator poiting to the element preceeding the oldest element. | |
const Iterator | crbegin () const |
Return an iterator pointing to the oldest object. | |
const Iterator | crend () const |
Return an iterator pointing to the element following the newest element. | |
const Iterator | begin () |
const Iterator | end () |
const Iterator | rbegin () |
const Iterator | rend () |
void | resize (const size_t size) |
Resize the buffer to contain max size elements. More... | |
size_t | capacity () const |
size_t | size () const |
Private Attributes | |
size_t | writeIndex |
Points to the element that was last written. | |
std::vector< T > | buffer |
size_t | vectorCapacity |
A fixed size buffer that can store data continuously without needing to move data or reallocate memory.
A buffer with size n will store the n newest elements that were inserted. If the number of inserted elements is < n, the buffers size will also be < n.
The buffer can be resized, which potentially leads to a reallocation of memory.
The RingBuffer has its own bidirectional iterator. The normal direction is from newest to oldest element.
will produce
If the buffer is empty, all iterators point to the separator element (end() == rend() == begin() == rbegin()).
A buffer with size n will store its objects in a std::vector with size n+1, where the additional element serves as a separator between the newest and the oldest element. It is technically the real oldest element and could be accessed using end() or rend(), which will always point to this element (meaning end() == rend()), giving you a n+1 sized buffer. However, this element will be default initialized until n+1 elements have been inserted into the buffer, so it is not advisable to use this extra element.
The RingBuffer satisfies concept std::ranges::bidirectional_range and RingBuffer::Iterator satisfies std::bidirectional_iterator
The writeIndex will always point to the element that was last written.
void gz::RingBuffer< T >::resize | ( | const size_t | size | ) |
Resize the buffer to contain max size elements.
If the current size is greater than size, the buffer is reduced to the newest elements that fit size.
If the current size is smaller than size, the buffer size remains but it will be able to grow during element insertion until size is reached.