|
Vital
|
A generic circular buffer (FIFO) data structure that allows adding and removing elements efficiently. More...
#include <circular_queue.h>
Classes | |
| class | iterator |
| A forward and backward iterator for iterating over the elements in the CircularQueue. More... | |
Public Member Functions | |
| CircularQueue (int capacity) | |
| Constructs a CircularQueue with a given capacity. | |
| CircularQueue (const CircularQueue &other) | |
| Copy constructor. | |
| CircularQueue () | |
| Default constructor creates an empty queue with no capacity. | |
| void | reserve (int capacity) |
| Ensures that the queue has at least the given capacity. | |
| force_inline void | assign (int num, T value) |
Assigns num elements with a given value. | |
| force_inline T & | at (std::size_t index) |
| Accesses an element by index in the queue. | |
| force_inline T & | operator[] (std::size_t index) |
| force_inline const T & | operator[] (std::size_t index) const |
| force_inline void | push_back (T entry) |
| Pushes an element to the back of the queue. | |
| force_inline T | pop_back () |
| Pops an element from the back of the queue. | |
| force_inline void | push_front (T entry) |
| Pushes an element to the front of the queue. | |
| force_inline T | pop_front () |
| Pops an element from the front of the queue. | |
| force_inline void | removeAt (int index) |
| Removes an element at a given index. | |
| force_inline void | remove (T entry) |
| Removes the first occurrence of an element if found. | |
| void | removeAll (T entry) |
| Removes all occurrences of a given element. | |
| template<int(*)(T, T) compare> | |
| void | sort () |
| Sorts the elements according to a compare function. | |
| void | ensureSpace (int space=2) |
Ensures that there is at least space extra capacity beyond the current size. | |
| void | ensureCapacity (int min_capacity) |
Ensures the queue has at least min_capacity total capacity. | |
| force_inline iterator | erase (iterator &iter) |
| Erases the element at the iterator position and returns an iterator to the next element. | |
| int | count (T entry) const |
Counts how many times entry appears in the queue. | |
| bool | contains (T entry) const |
| Checks if the queue contains a given element. | |
| force_inline void | clear () |
| Clears all elements in the queue. | |
| force_inline T | front () const |
| Returns the element at the front of the queue. | |
| force_inline T | back () const |
| Returns the element at the back of the queue. | |
| force_inline int | size () const |
| Returns the current number of elements in the queue. | |
| force_inline int | capacity () const |
| Returns the maximum number of elements the queue can hold. | |
| force_inline iterator | begin () const |
| Returns an iterator to the first element of the queue. | |
| force_inline iterator | end () const |
| Returns an iterator to the past-the-end element of the queue. | |
A generic circular buffer (FIFO) data structure that allows adding and removing elements efficiently.
CircularQueue is a template-based circular buffer that can store a sequence of elements. It supports operations like push_back, pop_back, push_front, pop_front, as well as inserting, removing, and accessing elements by index. By using a circular indexing strategy, it can reuse a fixed-size buffer and avoid costly memory allocations once reserved capacity is sufficient.
Template Parameters:
Operations:
This queue uses a one-element gap strategy to differentiate between full and empty states, thus when constructing with capacity n, it effectively stores up to n-1 elements.
|
inline |
Constructs a CircularQueue with a given capacity.
Note that the effective capacity for storing elements is capacity - 1 because one slot is used to differentiate between empty and full states.
| capacity | The desired capacity. |
|
inline |
Copy constructor.
| other | Another CircularQueue to copy from. |
|
inline |
Default constructor creates an empty queue with no capacity.
|
inline |
Assigns num elements with a given value.
Resizes if necessary and sets the queue to contain num copies of value.
| num | The number of elements to assign. |
| value | The value to fill. |
|
inline |
Accesses an element by index in the queue.
| index | The element index [0..size()-1]. |
|
inline |
Returns the element at the back of the queue.
|
inline |
Returns an iterator to the first element of the queue.
|
inline |
Returns the maximum number of elements the queue can hold.
|
inline |
Clears all elements in the queue.
|
inline |
Checks if the queue contains a given element.
| entry | The element to check for. |
|
inline |
Counts how many times entry appears in the queue.
| entry | The element to count. |
|
inline |
Returns an iterator to the past-the-end element of the queue.
|
inline |
Ensures the queue has at least min_capacity total capacity.
| min_capacity | The required minimum capacity. |
|
inline |
Ensures that there is at least space extra capacity beyond the current size.
| space | Extra space needed. |
|
inline |
Erases the element at the iterator position and returns an iterator to the next element.
| iter | The iterator pointing to the element to erase. |
|
inline |
Returns the element at the front of the queue.
|
inline |
|
inline |
|
inline |
Pops an element from the back of the queue.
|
inline |
Pops an element from the front of the queue.
|
inline |
Pushes an element to the back of the queue.
| entry | The element to push. |
|
inline |
Pushes an element to the front of the queue.
| entry | The element to push. |
|
inline |
Removes the first occurrence of an element if found.
| entry | The element to remove. |
|
inline |
Removes all occurrences of a given element.
| entry | The element to remove. |
|
inline |
Removes an element at a given index.
Elements after the index are shifted left.
| index | The index to remove at. |
|
inline |
Ensures that the queue has at least the given capacity.
If the new capacity is larger, re-allocates and copies the existing elements.
| capacity | The desired minimum capacity. |
|
inline |
Returns the current number of elements in the queue.
Sorts the elements according to a compare function.
| compare | A function that returns <0 if a<b, 0 if a==b, >0 if a>b. |