Queue< T > Class Template Reference

#include <queue.h>

Detailed Description

template<typename T>
class maxon::Queue< T >

A Queue that holds elements of type T, stored in a circular buffer. Supports access from both ends.

Public Types

using ValueType = T
 

Public Member Functions

 Queue ()=default
 
 Queue (const Queue &)=delete
 
Queueoperator= (const Queue &)=delete
 
 Queue (Queue &&src)
 
Queueoperator= (Queue &&src)
 
Int GetCount () const
 
Bool IsEmpty () const
 
Bool IsPopulated () const
 
Optional< T & > Peek ()
 
Optional< const T & > Peek () const
 
Optional< T & > PeekBack ()
 
Optional< const T & > PeekBack () const
 
ResultMem Push (T value)
 
ResultMem PushFront (T value)
 
template<typename ... ARGS>
ResultMem Emplace (ARGS &&... args)
 
template<typename ... ARGS>
ResultMem EmplaceFront (ARGS &&... args)
 
Optional< T > Pop ()
 
Optional< T > PopBack ()
 
Bool Truncate ()
 
Bool TruncateBack ()
 
void Flush ()
 
void Reset ()
 
Block< T > AlignAsBlock ()
 
 ~Queue ()
 
Result< void > CopyFrom (const Queue &src)
 
ResultMem EnsureCapacity (Int requestedSize)
 

Private Member Functions

void Linearize ()
 
Bool IsAtFullCapacity () const
 
Int CalcNewCapacity (Int targetSize) const
 
Bool Grow (Int targetSize=0)
 
Result< void > CopyDataToNewBuffer (T *destPtr) const
 
void MoveDataToNewBuffer (T *destPtr)
 
void PushToHead (T &&value)
 
void PushToTail (T &&value)
 
template<typename ... ARGS>
void EmplaceAtHead (ARGS &&... args)
 
template<typename ... ARGS>
void EmplaceAtTail (ARGS &&... args)
 
PopFromTail ()
 
PopFromHead ()
 
void TruncateTail ()
 
void TruncateHead ()
 

Static Private Member Functions

static MAXON_ATTRIBUTE_FORCE_INLINE void IncrementIndex (Int &index, Int capacity)
 
static MAXON_ATTRIBUTE_FORCE_INLINE void DecrementIndex (Int &index, Int capacity)
 

Private Attributes

AutoMem< T > _data
 
Int _headIndex
 
Int _tailIndex
 
Int _count
 
Int _capacity
 

Static Private Attributes

static const Int GROW_INIT
 
static const Int GROW_FACTOR
 

Member Typedef Documentation

◆ ValueType

using ValueType = T

Constructor & Destructor Documentation

◆ Queue() [1/3]

Queue ( )
default

Default constructor. Creates an empty queue.

◆ Queue() [2/3]

Queue ( const Queue< T > &  )
delete

Deleted copy constructor.

◆ Queue() [3/3]

Queue ( Queue< T > &&  src)

Move constructor.

◆ ~Queue()

~Queue ( )

The destructor resets the queue.

Member Function Documentation

◆ operator=() [1/2]

Queue& operator= ( const Queue< T > &  )
delete

Deleted copy assignment.

◆ operator=() [2/2]

Queue& operator= ( Queue< T > &&  src)

Move assignment.

◆ GetCount()

Int GetCount ( void  ) const

Returns the number of elements in the queue.

◆ IsEmpty()

Bool IsEmpty ( ) const

Checks if the queue contains no elements.

◆ IsPopulated()

Bool IsPopulated ( void  ) const

Checks if the queue contains any elements.

◆ Peek() [1/2]

Optional<T&> Peek ( )

Returns a reference to the element at the front of the queue without removing it, or NO_VALUE if the queue was empty.

◆ Peek() [2/2]

Optional<const T&> Peek ( ) const

◆ PeekBack() [1/2]

Optional<T&> PeekBack ( )

Returns a reference to the element at the back of the queue without removing it, or NO_VALUE if the queue was empty.

◆ PeekBack() [2/2]

Optional<const T&> PeekBack ( ) const

◆ Push()

ResultMem Push ( value)

Pushes an element to the back of the queue.

◆ PushFront()

ResultMem PushFront ( value)

Pushes an element to the front of the queue.

◆ Emplace()

ResultMem Emplace ( ARGS &&...  args)

Constructs an element in-place at the back of the queue with the given arguments as constructor arguments.

◆ EmplaceFront()

ResultMem EmplaceFront ( ARGS &&...  args)

Constructs an element in-place at the front of the queue with the given arguments.

◆ Pop()

Optional<T> Pop ( )

Returns the element at the front of the queue and removes it, or NO_VALUE if the queue was empty.

◆ PopBack()

Optional<T> PopBack ( )

Returns the element at the back of the queue and removes it, or NO_VALUE if the queue was empty.

◆ Truncate()

Bool Truncate ( )

Removes the element at the front of the queue without returning it. Returns true if the operation succeeded, or false if the queue was empty.

◆ TruncateBack()

Bool TruncateBack ( )

Removes the element at the back of the queue without returning it. Returns true if the operation succeeded, or false if the queue was empty.

◆ Flush()

void Flush ( void  )

Removes all elements from the queue by calling their destructors, but does not de-allocate the memory yet.

◆ Reset()

void Reset ( void  )

Removes all elements from the queue by calling their destructors and frees the allocated memory.

◆ AlignAsBlock()

Block<T> AlignAsBlock ( )

Linearizes the internal data storage and returns the elements as a block, from front to back.

◆ CopyFrom()

Result<void> CopyFrom ( const Queue< T > &  src)

Copies the queue.

◆ EnsureCapacity()

ResultMem EnsureCapacity ( Int  requestedSize)

Ensures that the queue can hold requestedSize items without allocating additional memory. Can be used for optimization to pre-allocate a sufficiently large internal buffer instead of growing on-demand.

◆ IncrementIndex()

static MAXON_ATTRIBUTE_FORCE_INLINE void IncrementIndex ( Int index,
Int  capacity 
)
staticprivate

◆ DecrementIndex()

static MAXON_ATTRIBUTE_FORCE_INLINE void DecrementIndex ( Int index,
Int  capacity 
)
staticprivate

◆ Linearize()

void Linearize ( )
private

◆ IsAtFullCapacity()

Bool IsAtFullCapacity ( ) const
private

◆ CalcNewCapacity()

Int CalcNewCapacity ( Int  targetSize) const
private

◆ Grow()

Bool Grow ( Int  targetSize = 0)
private

◆ CopyDataToNewBuffer()

Result<void> CopyDataToNewBuffer ( T *  destPtr) const
private

◆ MoveDataToNewBuffer()

void MoveDataToNewBuffer ( T *  destPtr)
private

◆ PushToHead()

void PushToHead ( T &&  value)
private

◆ PushToTail()

void PushToTail ( T &&  value)
private

◆ EmplaceAtHead()

void EmplaceAtHead ( ARGS &&...  args)
private

◆ EmplaceAtTail()

void EmplaceAtTail ( ARGS &&...  args)
private

◆ PopFromTail()

T PopFromTail ( )
private

◆ PopFromHead()

T PopFromHead ( )
private

◆ TruncateTail()

void TruncateTail ( )
private

◆ TruncateHead()

void TruncateHead ( )
private

Member Data Documentation

◆ GROW_INIT

const Int GROW_INIT
staticprivate

◆ GROW_FACTOR

const Int GROW_FACTOR
staticprivate

◆ _data

AutoMem<T> _data
private

◆ _headIndex

Int _headIndex
private

◆ _tailIndex

Int _tailIndex
private

◆ _count

Int _count
private

◆ _capacity

Int _capacity
private