Vital
Loading...
Searching...
No Matches
vital::CircularQueue< T > Class Template Reference

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_inlinepop_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_inlinepop_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_inlinefront () const
 Returns the element at the front of the queue.
 
force_inlineback () 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.
 

Detailed Description

template<class T>
class vital::CircularQueue< T >

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:

  • T: The type of elements stored in the queue.

Operations:

  • push_back, pop_back: Add and remove elements from the end.
  • push_front, pop_front: Add and remove elements from the front.
  • remove, removeAt: Remove elements at given positions or by value.
  • at, operator[]: Access elements by index.
  • Contains iterator support for iteration through elements.

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.

Constructor & Destructor Documentation

◆ CircularQueue() [1/3]

template<class T >
vital::CircularQueue< T >::CircularQueue ( int capacity)
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.

Parameters
capacityThe desired capacity.

◆ CircularQueue() [2/3]

template<class T >
vital::CircularQueue< T >::CircularQueue ( const CircularQueue< T > & other)
inline

Copy constructor.

Parameters
otherAnother CircularQueue to copy from.

◆ CircularQueue() [3/3]

template<class T >
vital::CircularQueue< T >::CircularQueue ( )
inline

Default constructor creates an empty queue with no capacity.

Member Function Documentation

◆ assign()

template<class T >
force_inline void vital::CircularQueue< T >::assign ( int num,
T value )
inline

Assigns num elements with a given value.

Resizes if necessary and sets the queue to contain num copies of value.

Parameters
numThe number of elements to assign.
valueThe value to fill.

◆ at()

template<class T >
force_inline T & vital::CircularQueue< T >::at ( std::size_t index)
inline

Accesses an element by index in the queue.

Parameters
indexThe element index [0..size()-1].
Returns
A reference to the element.

◆ back()

template<class T >
force_inline T vital::CircularQueue< T >::back ( ) const
inline

Returns the element at the back of the queue.

◆ begin()

template<class T >
force_inline iterator vital::CircularQueue< T >::begin ( ) const
inline

Returns an iterator to the first element of the queue.

◆ capacity()

template<class T >
force_inline int vital::CircularQueue< T >::capacity ( ) const
inline

Returns the maximum number of elements the queue can hold.

◆ clear()

template<class T >
force_inline void vital::CircularQueue< T >::clear ( )
inline

Clears all elements in the queue.

◆ contains()

template<class T >
bool vital::CircularQueue< T >::contains ( T entry) const
inline

Checks if the queue contains a given element.

Parameters
entryThe element to check for.
Returns
True if found, false otherwise.

◆ count()

template<class T >
int vital::CircularQueue< T >::count ( T entry) const
inline

Counts how many times entry appears in the queue.

Parameters
entryThe element to count.
Returns
The number of occurrences.

◆ end()

template<class T >
force_inline iterator vital::CircularQueue< T >::end ( ) const
inline

Returns an iterator to the past-the-end element of the queue.

◆ ensureCapacity()

template<class T >
void vital::CircularQueue< T >::ensureCapacity ( int min_capacity)
inline

Ensures the queue has at least min_capacity total capacity.

Parameters
min_capacityThe required minimum capacity.

◆ ensureSpace()

template<class T >
void vital::CircularQueue< T >::ensureSpace ( int space = 2)
inline

Ensures that there is at least space extra capacity beyond the current size.

Parameters
spaceExtra space needed.

◆ erase()

template<class T >
force_inline iterator vital::CircularQueue< T >::erase ( iterator & iter)
inline

Erases the element at the iterator position and returns an iterator to the next element.

Parameters
iterThe iterator pointing to the element to erase.
Returns
The iterator to the next element.

◆ front()

template<class T >
force_inline T vital::CircularQueue< T >::front ( ) const
inline

Returns the element at the front of the queue.

◆ operator[]() [1/2]

template<class T >
force_inline T & vital::CircularQueue< T >::operator[] ( std::size_t index)
inline

◆ operator[]() [2/2]

template<class T >
force_inline const T & vital::CircularQueue< T >::operator[] ( std::size_t index) const
inline

◆ pop_back()

template<class T >
force_inline T vital::CircularQueue< T >::pop_back ( )
inline

Pops an element from the back of the queue.

Returns
The popped element.

◆ pop_front()

template<class T >
force_inline T vital::CircularQueue< T >::pop_front ( )
inline

Pops an element from the front of the queue.

Returns
The popped element.

◆ push_back()

template<class T >
force_inline void vital::CircularQueue< T >::push_back ( T entry)
inline

Pushes an element to the back of the queue.

Parameters
entryThe element to push.

◆ push_front()

template<class T >
force_inline void vital::CircularQueue< T >::push_front ( T entry)
inline

Pushes an element to the front of the queue.

Parameters
entryThe element to push.

◆ remove()

template<class T >
force_inline void vital::CircularQueue< T >::remove ( T entry)
inline

Removes the first occurrence of an element if found.

Parameters
entryThe element to remove.

◆ removeAll()

template<class T >
void vital::CircularQueue< T >::removeAll ( T entry)
inline

Removes all occurrences of a given element.

Parameters
entryThe element to remove.

◆ removeAt()

template<class T >
force_inline void vital::CircularQueue< T >::removeAt ( int index)
inline

Removes an element at a given index.

Elements after the index are shifted left.

Parameters
indexThe index to remove at.

◆ reserve()

template<class T >
void vital::CircularQueue< T >::reserve ( int capacity)
inline

Ensures that the queue has at least the given capacity.

If the new capacity is larger, re-allocates and copies the existing elements.

Parameters
capacityThe desired minimum capacity.

◆ size()

template<class T >
force_inline int vital::CircularQueue< T >::size ( ) const
inline

Returns the current number of elements in the queue.

◆ sort()

template<class T >
template<int(*)(T, T) compare>
void vital::CircularQueue< T >::sort ( )
inline

Sorts the elements according to a compare function.

Template Parameters
compareA function that returns <0 if a<b, 0 if a==b, >0 if a>b.

The documentation for this class was generated from the following file: