|
Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
|
Fixed-capacity circular FIFO buffer over contiguous storage. More...
#include <tpl_ring_buffer.H>
Classes | |
| class | basic_iterator |
| Random-access iterator over the logical window. More... | |
Public Types | |
| using | Item_Type = T |
| Aleph convention: element type. | |
| using | value_type = T |
| STL convention: value type. | |
| using | size_type = size_t |
| STL convention: size type. | |
| using | iterator = basic_iterator< false > |
| Mutable iterator. | |
| using | const_iterator = basic_iterator< true > |
| Const iterator. | |
Public Member Functions | |
| RingBuffer (const size_t cap) | |
| Construct a buffer with an exact capacity. | |
| RingBuffer (const RingBuffer &rb) | |
Copy constructor (requires copyable T). | |
| RingBuffer (RingBuffer &&rb) noexcept | |
| Move constructor: steals the storage in O(1). | |
| RingBuffer & | operator= (const RingBuffer &rb) |
Copy assignment (requires copyable T). | |
| RingBuffer & | operator= (RingBuffer &&rb) noexcept |
| Move assignment: steals the storage in O(1). | |
| ~RingBuffer () | |
| void | swap (RingBuffer &rb) noexcept |
Swap contents with rb in O(1). | |
| size_t | size () const noexcept |
| Return the number of stored elements. O(1). | |
| size_t | capacity () const noexcept |
| Return the fixed capacity chosen at construction. O(1). | |
| size_t | available () const noexcept |
| Return the number of free slots. O(1). | |
| bool | is_empty () const noexcept |
Return true if no elements are stored. O(1). | |
| bool | is_full () const noexcept |
Return true if the buffer holds capacity() elements. O(1). | |
| void | empty () noexcept |
| Destroy all elements (Aleph convention). | |
| void | clear () noexcept |
Destroy all elements. Alias of empty(). Capacity is kept. | |
| T & | operator[] (const size_t i) |
| Checked access to the i-th logical element (0 = oldest). | |
| const T & | operator[] (const size_t i) const |
| Checked const access to the i-th logical element (0 = oldest). | |
| T & | operator() (const size_t i) noexcept |
Unchecked access to the i-th logical element (must be < size()). | |
| const T & | operator() (const size_t i) const noexcept |
Unchecked const access to the i-th logical element (must be < size()). | |
| T & | get_first () |
| Oldest element — the next to leave (checked). | |
| const T & | get_first () const |
| T & | get_last () |
| Newest element — the last one inserted (checked). | |
| const T & | get_last () const |
| T & | front () |
Oldest element (checked). Alias of get_first(). | |
| const T & | front () const |
| T & | back () |
Newest element (checked). Alias of get_last(). | |
| const T & | back () const |
| template<class... Args> | |
| T & | emplace (Args &&...args) |
| Construct an element in place at the tail. | |
| T & | put (const T &item) |
Append a copy of item at the tail. | |
| T & | put (T &&item) |
Append item at the tail by moving. | |
| T & | push (const T &item) |
Queue-style alias of put(const T &). | |
| T & | push (T &&item) |
Queue-style alias of put(T &&). | |
| bool | put_overwrite (const T &item) |
| Append at the tail, evicting the oldest element when full. | |
| bool | put_overwrite (T &&item) |
| T | get () |
| Extract the oldest element from the head. | |
| T | pop () |
Queue-style alias of get(). | |
| iterator | begin () noexcept |
| Iterator on the oldest element. O(1). | |
| iterator | end () noexcept |
| Iterator past the newest element. O(1). | |
| const_iterator | begin () const noexcept |
| Const iterator on the oldest element. O(1). | |
| const_iterator | end () const noexcept |
| Const iterator past the newest element. O(1). | |
| const_iterator | cbegin () const noexcept |
| Const iterator on the oldest element. O(1). | |
| const_iterator | cend () const noexcept |
| Const iterator past the newest element. O(1). | |
| template<class Operation > | |
| bool | traverse (Operation operation) |
Traverse from oldest to newest while operation returns true. | |
| template<class Operation > | |
| bool | traverse (Operation operation) const |
| Const traversal from oldest to newest. | |
| bool | operator== (const RingBuffer &rb) const |
| Equality: same logical contents (capacity is not compared). | |
| bool | operator!= (const RingBuffer &rb) const |
Inequality: negation of operator==. | |
Private Member Functions | |
| size_t | phys (const size_t i) const noexcept |
| void | destroy_all () noexcept |
Static Private Member Functions | |
| static T * | allocate (size_t m) |
| static void | deallocate (T *p, size_t m) noexcept |
Private Attributes | |
| T * | buf_ = nullptr |
| size_t | cap_ = 0 |
| size_t | head_ = 0 |
| size_t | n_ = 0 |
Fixed-capacity circular FIFO buffer over contiguous storage.
Elements enter at the tail (put) and leave from the head (get) in FIFO order. The capacity is chosen at construction (any positive value, not rounded) and never changes; no operation reallocates. When the buffer is full, put reports overflow while put_overwrite evicts the oldest element — a sliding window.
| T | Element type. Must be move-constructible and move-assignable. No default constructor is required; move-only types work. |
operator[](i) and iterators address the logical window: index 0 is the oldest element (next to leave), size() - 1 the newest. Iterators are random access and remain valid until any element is added, removed or the buffer is destroyed.Array), empty() clears the buffer and is_empty() is the emptiness predicate.put/emplace offer the strong guarantee (the element is constructed before any state changes). put_overwrite on a full buffer assigns over the oldest element (basic guarantee if T's assignment throws).Definition at line 114 of file tpl_ring_buffer.H.
| using Aleph::RingBuffer< T >::const_iterator = basic_iterator<true> |
Const iterator.
Definition at line 338 of file tpl_ring_buffer.H.
Aleph convention: element type.
Definition at line 151 of file tpl_ring_buffer.H.
| using Aleph::RingBuffer< T >::iterator = basic_iterator<false> |
Mutable iterator.
Definition at line 337 of file tpl_ring_buffer.H.
| using Aleph::RingBuffer< T >::size_type = size_t |
STL convention: size type.
Definition at line 153 of file tpl_ring_buffer.H.
STL convention: value type.
Definition at line 152 of file tpl_ring_buffer.H.
|
inlineexplicit |
Construct a buffer with an exact capacity.
| cap | Maximum number of elements (not rounded; must be positive). |
| std::invalid_argument | if cap == 0. |
| std::bad_alloc | if the storage cannot be allocated. |
Definition at line 345 of file tpl_ring_buffer.H.
References ah_invalid_argument_if, Aleph::RingBuffer< T >::allocate(), Aleph::RingBuffer< T >::buf_, and Aleph::RingBuffer< T >::cap_.
|
inline |
Copy constructor (requires copyable T).
The copy is normalized (its oldest element sits at physical index 0).
Definition at line 353 of file tpl_ring_buffer.H.
References Aleph::RingBuffer< T >::allocate(), Aleph::blossom_maximum_cardinality_matching(), Aleph::RingBuffer< T >::buf_, Aleph::RingBuffer< T >::cap_, Aleph::RingBuffer< T >::deallocate(), Aleph::RingBuffer< T >::destroy_all(), and Aleph::RingBuffer< T >::n_.
|
inlinenoexcept |
Move constructor: steals the storage in O(1).
The source is left valid but empty with null storage; it may only be destroyed or assigned to.
Definition at line 374 of file tpl_ring_buffer.H.
References Aleph::blossom_maximum_cardinality_matching(), and Aleph::RingBuffer< T >::swap().
|
inline |
Definition at line 397 of file tpl_ring_buffer.H.
References Aleph::RingBuffer< T >::buf_, Aleph::RingBuffer< T >::cap_, Aleph::RingBuffer< T >::deallocate(), and Aleph::RingBuffer< T >::destroy_all().
Definition at line 125 of file tpl_ring_buffer.H.
References m.
Referenced by Aleph::RingBuffer< T >::RingBuffer(), and Aleph::RingBuffer< T >::RingBuffer().
|
inlinenoexcept |
Return the number of free slots. O(1).
Definition at line 432 of file tpl_ring_buffer.H.
References Aleph::RingBuffer< T >::cap_, and Aleph::RingBuffer< T >::n_.
|
inline |
Newest element (checked). Alias of get_last().
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Definition at line 543 of file tpl_ring_buffer.H.
References Aleph::RingBuffer< T >::get_last().
Referenced by TEST().
Definition at line 549 of file tpl_ring_buffer.H.
References Aleph::RingBuffer< T >::get_last().
|
inlinenoexcept |
Const iterator on the oldest element. O(1).
Definition at line 683 of file tpl_ring_buffer.H.
|
inlinenoexcept |
Iterator on the oldest element. O(1).
Definition at line 671 of file tpl_ring_buffer.H.
Referenced by Aleph::RingBuffer< T >::cbegin(), and TEST().
|
inlinenoexcept |
Return the fixed capacity chosen at construction. O(1).
Definition at line 426 of file tpl_ring_buffer.H.
References Aleph::RingBuffer< T >::cap_.
|
inlinenoexcept |
Const iterator on the oldest element. O(1).
Definition at line 695 of file tpl_ring_buffer.H.
References Aleph::RingBuffer< T >::begin().
|
inlinenoexcept |
Const iterator past the newest element. O(1).
Definition at line 701 of file tpl_ring_buffer.H.
References Aleph::RingBuffer< T >::end().
|
inlinenoexcept |
Destroy all elements. Alias of empty(). Capacity is kept.
Definition at line 459 of file tpl_ring_buffer.H.
References Aleph::RingBuffer< T >::destroy_all().
Referenced by TEST().
|
inlinestaticprivatenoexcept |
Definition at line 130 of file tpl_ring_buffer.H.
References m.
Referenced by Aleph::RingBuffer< T >::RingBuffer(), and Aleph::RingBuffer< T >::~RingBuffer().
|
inlineprivatenoexcept |
Definition at line 142 of file tpl_ring_buffer.H.
References Aleph::RingBuffer< T >::buf_, Aleph::RingBuffer< T >::head_, Aleph::RingBuffer< T >::n_, and Aleph::RingBuffer< T >::phys().
Referenced by Aleph::RingBuffer< T >::RingBuffer(), Aleph::RingBuffer< T >::~RingBuffer(), Aleph::RingBuffer< T >::clear(), and Aleph::RingBuffer< T >::empty().
|
inline |
Construct an element in place at the tail.
| Args | Argument types forwarded to T's constructor. |
| args | Arguments forwarded to T's constructor. |
| std::overflow_error | if the buffer is full. |
Definition at line 564 of file tpl_ring_buffer.H.
References ah_overflow_error_if, Aleph::blossom_maximum_cardinality_matching(), Aleph::RingBuffer< T >::buf_, Aleph::RingBuffer< T >::is_full(), Aleph::RingBuffer< T >::n_, and Aleph::RingBuffer< T >::phys().
Referenced by Aleph::RingBuffer< T >::put(), Aleph::RingBuffer< T >::put(), TEST(), TEST(), and TEST().
|
inlinenoexcept |
Destroy all elements (Aleph convention).
Capacity is kept.
Array::empty()), this mutates the buffer; use is_empty() to test emptiness. Definition at line 453 of file tpl_ring_buffer.H.
References Aleph::RingBuffer< T >::destroy_all().
Referenced by TEST().
|
inlinenoexcept |
Const iterator past the newest element. O(1).
Definition at line 689 of file tpl_ring_buffer.H.
References Aleph::RingBuffer< T >::n_.
|
inlinenoexcept |
Iterator past the newest element. O(1).
Definition at line 677 of file tpl_ring_buffer.H.
References Aleph::RingBuffer< T >::n_.
Referenced by Aleph::RingBuffer< T >::cend(), and TEST().
|
inline |
Oldest element (checked). Alias of get_first().
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Definition at line 531 of file tpl_ring_buffer.H.
References Aleph::RingBuffer< T >::get_first().
Referenced by TEST().
Definition at line 537 of file tpl_ring_buffer.H.
References Aleph::RingBuffer< T >::get_first().
|
inline |
Extract the oldest element from the head.
| std::underflow_error | if the buffer is empty. |
Definition at line 651 of file tpl_ring_buffer.H.
References ah_underflow_error_if, Aleph::blossom_maximum_cardinality_matching(), Aleph::RingBuffer< T >::buf_, Aleph::RingBuffer< T >::cap_, Aleph::RingBuffer< T >::head_, and Aleph::RingBuffer< T >::n_.
Referenced by Aleph::RingBuffer< T >::pop(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().
|
inline |
Oldest element — the next to leave (checked).
| std::underflow_error | if the buffer is empty. |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Definition at line 500 of file tpl_ring_buffer.H.
References ah_underflow_error_if, Aleph::RingBuffer< T >::buf_, Aleph::RingBuffer< T >::head_, and Aleph::RingBuffer< T >::n_.
Referenced by Aleph::RingBuffer< T >::front(), Aleph::RingBuffer< T >::front(), TEST(), TEST(), TEST(), and TEST().
Definition at line 507 of file tpl_ring_buffer.H.
References ah_underflow_error_if, Aleph::RingBuffer< T >::buf_, Aleph::RingBuffer< T >::head_, and Aleph::RingBuffer< T >::n_.
|
inline |
Newest element — the last one inserted (checked).
| std::underflow_error | if the buffer is empty. |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Definition at line 517 of file tpl_ring_buffer.H.
References ah_underflow_error_if, Aleph::RingBuffer< T >::buf_, Aleph::RingBuffer< T >::n_, and Aleph::RingBuffer< T >::phys().
Referenced by Aleph::RingBuffer< T >::back(), Aleph::RingBuffer< T >::back(), TEST(), and TEST().
Definition at line 524 of file tpl_ring_buffer.H.
References ah_underflow_error_if, Aleph::RingBuffer< T >::buf_, Aleph::RingBuffer< T >::n_, and Aleph::RingBuffer< T >::phys().
|
inlinenoexcept |
Return true if no elements are stored. O(1).
Definition at line 438 of file tpl_ring_buffer.H.
References Aleph::RingBuffer< T >::n_.
|
inlinenoexcept |
Return true if the buffer holds capacity() elements. O(1).
Definition at line 444 of file tpl_ring_buffer.H.
References Aleph::RingBuffer< T >::cap_, and Aleph::RingBuffer< T >::n_.
Referenced by Aleph::RingBuffer< T >::emplace(), Aleph::RingBuffer< T >::put_overwrite(), Aleph::RingBuffer< T >::put_overwrite(), TEST(), TEST(), and TEST().
|
inline |
Inequality: negation of operator==.
Definition at line 756 of file tpl_ring_buffer.H.
References Aleph::blossom_maximum_cardinality_matching().
|
inlinenoexcept |
Unchecked const access to the i-th logical element (must be < size()).
Definition at line 491 of file tpl_ring_buffer.H.
References Aleph::RingBuffer< T >::buf_, and Aleph::RingBuffer< T >::phys().
|
inlinenoexcept |
Unchecked access to the i-th logical element (must be < size()).
Definition at line 485 of file tpl_ring_buffer.H.
References Aleph::RingBuffer< T >::buf_, and Aleph::RingBuffer< T >::phys().
|
inline |
Copy assignment (requires copyable T).
Definition at line 380 of file tpl_ring_buffer.H.
References Aleph::blossom_maximum_cardinality_matching(), and Aleph::RingBuffer< T >::swap().
|
inlinenoexcept |
Move assignment: steals the storage in O(1).
Definition at line 391 of file tpl_ring_buffer.H.
References Aleph::blossom_maximum_cardinality_matching(), and Aleph::RingBuffer< T >::swap().
|
inline |
Equality: same logical contents (capacity is not compared).
| rb | Buffer to compare against. |
true if both buffers hold equal elements in FIFO order. T equality comparable. Definition at line 745 of file tpl_ring_buffer.H.
References Aleph::blossom_maximum_cardinality_matching(), and Aleph::RingBuffer< T >::n_.
|
inline |
Checked access to the i-th logical element (0 = oldest).
| i | Logical index from the head. |
| std::out_of_range | if i >= size(). |
Definition at line 471 of file tpl_ring_buffer.H.
References ah_out_of_range_error_if, Aleph::RingBuffer< T >::buf_, Aleph::RingBuffer< T >::n_, and Aleph::RingBuffer< T >::phys().
|
inline |
Checked const access to the i-th logical element (0 = oldest).
Definition at line 478 of file tpl_ring_buffer.H.
References ah_out_of_range_error_if, Aleph::RingBuffer< T >::buf_, Aleph::RingBuffer< T >::n_, and Aleph::RingBuffer< T >::phys().
|
inlineprivatenoexcept |
Definition at line 136 of file tpl_ring_buffer.H.
References Aleph::RingBuffer< T >::cap_, and Aleph::RingBuffer< T >::head_.
Referenced by Aleph::RingBuffer< T >::destroy_all(), Aleph::RingBuffer< T >::emplace(), Aleph::RingBuffer< T >::get_last(), Aleph::RingBuffer< T >::get_last(), Aleph::RingBuffer< T >::operator()(), Aleph::RingBuffer< T >::operator()(), Aleph::RingBuffer< T >::operator[](), Aleph::RingBuffer< T >::operator[](), Aleph::RingBuffer< T >::traverse(), and Aleph::RingBuffer< T >::traverse().
|
inline |
Queue-style alias of get().
Definition at line 663 of file tpl_ring_buffer.H.
References Aleph::RingBuffer< T >::get().
Queue-style alias of put(const T &).
Definition at line 597 of file tpl_ring_buffer.H.
References Aleph::RingBuffer< T >::put().
Queue-style alias of put(T &&).
Definition at line 604 of file tpl_ring_buffer.H.
References Aleph::RingBuffer< T >::put().
Append a copy of item at the tail.
| item | Element to copy. |
| std::overflow_error | if the buffer is full. |
Definition at line 579 of file tpl_ring_buffer.H.
References Aleph::RingBuffer< T >::emplace().
Referenced by Aleph::RingBuffer< T >::push(), Aleph::RingBuffer< T >::push(), Aleph::RingBuffer< T >::put_overwrite(), Aleph::RingBuffer< T >::put_overwrite(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().
Append item at the tail by moving.
| item | Element to move in. |
| std::overflow_error | if the buffer is full. |
Definition at line 591 of file tpl_ring_buffer.H.
References Aleph::RingBuffer< T >::emplace().
Append at the tail, evicting the oldest element when full.
This is the sliding-window insertion: on a full buffer the oldest element is overwritten (by assignment) and the head advances.
| item | Element to copy in. |
true if an old element was evicted, false if the buffer still had room. T's copy assignment throws.This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Definition at line 619 of file tpl_ring_buffer.H.
References Aleph::blossom_maximum_cardinality_matching(), Aleph::RingBuffer< T >::buf_, Aleph::RingBuffer< T >::cap_, Aleph::RingBuffer< T >::head_, Aleph::RingBuffer< T >::is_full(), and Aleph::RingBuffer< T >::put().
Definition at line 633 of file tpl_ring_buffer.H.
References Aleph::blossom_maximum_cardinality_matching(), Aleph::RingBuffer< T >::buf_, Aleph::RingBuffer< T >::cap_, Aleph::RingBuffer< T >::head_, Aleph::RingBuffer< T >::is_full(), and Aleph::RingBuffer< T >::put().
|
inlinenoexcept |
Return the number of stored elements. O(1).
Definition at line 420 of file tpl_ring_buffer.H.
References Aleph::RingBuffer< T >::n_.
|
inlinenoexcept |
Swap contents with rb in O(1).
| rb | Buffer to swap with. |
Definition at line 409 of file tpl_ring_buffer.H.
References Aleph::blossom_maximum_cardinality_matching(), Aleph::RingBuffer< T >::buf_, Aleph::RingBuffer< T >::cap_, Aleph::RingBuffer< T >::head_, and Aleph::RingBuffer< T >::n_.
Referenced by Aleph::RingBuffer< T >::RingBuffer(), Aleph::RingBuffer< T >::operator=(), and Aleph::RingBuffer< T >::operator=().
|
inline |
Traverse from oldest to newest while operation returns true.
Aleph-style bounded traversal: operation receives each element in FIFO order; returning false stops the walk.
| Operation | Callable bool(T &). |
| operation | Operation to apply. |
true if all elements were visited, false if stopped early. Definition at line 716 of file tpl_ring_buffer.H.
References Aleph::blossom_maximum_cardinality_matching(), Aleph::RingBuffer< T >::buf_, Aleph::RingBuffer< T >::n_, and Aleph::RingBuffer< T >::phys().
Referenced by TEST().
|
inline |
Const traversal from oldest to newest.
| Operation | Callable bool(const T &). |
| operation | Operation to apply. |
true if all elements were visited, false if stopped early. Definition at line 730 of file tpl_ring_buffer.H.
References Aleph::blossom_maximum_cardinality_matching(), Aleph::RingBuffer< T >::buf_, Aleph::RingBuffer< T >::n_, and Aleph::RingBuffer< T >::phys().
|
private |
Definition at line 120 of file tpl_ring_buffer.H.
Referenced by Aleph::RingBuffer< T >::RingBuffer(), Aleph::RingBuffer< T >::RingBuffer(), Aleph::RingBuffer< T >::~RingBuffer(), Aleph::RingBuffer< T >::destroy_all(), Aleph::RingBuffer< T >::emplace(), Aleph::RingBuffer< T >::get(), Aleph::RingBuffer< T >::get_first(), Aleph::RingBuffer< T >::get_first(), Aleph::RingBuffer< T >::get_last(), Aleph::RingBuffer< T >::get_last(), Aleph::RingBuffer< T >::operator()(), Aleph::RingBuffer< T >::operator()(), Aleph::RingBuffer< T >::operator[](), Aleph::RingBuffer< T >::operator[](), Aleph::RingBuffer< T >::put_overwrite(), Aleph::RingBuffer< T >::put_overwrite(), Aleph::RingBuffer< T >::swap(), Aleph::RingBuffer< T >::traverse(), and Aleph::RingBuffer< T >::traverse().
|
private |
Definition at line 121 of file tpl_ring_buffer.H.
Referenced by Aleph::RingBuffer< T >::RingBuffer(), Aleph::RingBuffer< T >::RingBuffer(), Aleph::RingBuffer< T >::~RingBuffer(), Aleph::RingBuffer< T >::available(), Aleph::RingBuffer< T >::capacity(), Aleph::RingBuffer< T >::get(), Aleph::RingBuffer< T >::is_full(), Aleph::RingBuffer< T >::phys(), Aleph::RingBuffer< T >::put_overwrite(), Aleph::RingBuffer< T >::put_overwrite(), and Aleph::RingBuffer< T >::swap().
|
private |
Definition at line 122 of file tpl_ring_buffer.H.
Referenced by Aleph::RingBuffer< T >::destroy_all(), Aleph::RingBuffer< T >::get(), Aleph::RingBuffer< T >::get_first(), Aleph::RingBuffer< T >::get_first(), Aleph::RingBuffer< T >::phys(), Aleph::RingBuffer< T >::put_overwrite(), Aleph::RingBuffer< T >::put_overwrite(), and Aleph::RingBuffer< T >::swap().
|
private |
Definition at line 123 of file tpl_ring_buffer.H.
Referenced by Aleph::RingBuffer< T >::RingBuffer(), Aleph::RingBuffer< T >::available(), Aleph::RingBuffer< T >::destroy_all(), Aleph::RingBuffer< T >::emplace(), Aleph::RingBuffer< T >::end(), Aleph::RingBuffer< T >::end(), Aleph::RingBuffer< T >::get(), Aleph::RingBuffer< T >::get_first(), Aleph::RingBuffer< T >::get_first(), Aleph::RingBuffer< T >::get_last(), Aleph::RingBuffer< T >::get_last(), Aleph::RingBuffer< T >::is_empty(), Aleph::RingBuffer< T >::is_full(), Aleph::RingBuffer< T >::operator==(), Aleph::RingBuffer< T >::operator[](), Aleph::RingBuffer< T >::operator[](), Aleph::RingBuffer< T >::size(), Aleph::RingBuffer< T >::swap(), Aleph::RingBuffer< T >::traverse(), and Aleph::RingBuffer< T >::traverse().