MT Core (C++)
Core library for replacing C++ standard in project usage
Loading...
Searching...
No Matches
mtcore::Queue< T > Struct Template Reference

FIFO Queue (First In, First Out) Dynamically allocated, can be resized. More...

#include <queue.hpp>

Public Member Functions

Result< void, AllocationErrorinit (Allocator &alloc, size_t capacity=10)
 Initializes Queue with capacity.
 
Result< void, AllocationErrorinit (Allocator &alloc, std::initializer_list< T > init, size_t capacity=10)
 Initializes Queue with initial items.
 
void deinit (Allocator &alloc)
 Cleans up allocated memory.
 
bool is_full () const noexcept
 Checks if ring buffer is full.
 
bool is_empty () const noexcept
 Checks if ring buffer is empty.
 
size_t size () const noexcept
 
size_t capacity () const noexcept
 
Result< void, AllocationErrorshrink_to_fit (Allocator &alloc)
 
Optional< T > peek () const noexcept
 Peeks at first element of Queue.
 
Optional< T > pop () noexcept
 Removes and returns the next element in the Queue.
 
Result< void, CollectionAddNoAllocationErrorpush (const T &elem) noexcept
 Tries to add an element to the Queue Fails if Queue is empty.
 
Result< void, AllocationErrorpush (Allocator &alloc, const T &elem) noexcept
 Tries to add an element to the Queue Will allocate more space if needed Fails if it cannot allocate larger Queue.
 
bool is_initialized () const noexcept
 Checks if an Queue is initialized.
 

Detailed Description

template<typename T>
struct mtcore::Queue< T >

FIFO Queue (First In, First Out) Dynamically allocated, can be resized.

Adding elements is worst case \(O(N)\), but generally \(O(1)\) Removing elements is \(O(1)\)

Template Parameters
TElement to store

Definition at line 37 of file queue.hpp.

Member Function Documentation

◆ capacity()

template<typename T>
size_t mtcore::Queue< T >::capacity ( ) const
inlinenodiscardnoexcept
Returns
Capacity of Queue

Definition at line 87 of file queue.hpp.

87{ return buffer.capacity(); }
Here is the caller graph for this function:

◆ deinit()

template<typename T>
void mtcore::Queue< T >::deinit ( Allocator & alloc)
inline

Cleans up allocated memory.

Parameters
allocAllocator to clean up memory with

Definition at line 65 of file queue.hpp.

65{ buffer.deinit(alloc); }
FIFO Queue (First In, First Out) Dynamically allocated, can be resized.
Definition queue.hpp:37
Here is the caller graph for this function:

◆ init() [1/2]

template<typename T>
Result< void, AllocationError > mtcore::Queue< T >::init ( Allocator & alloc,
size_t capacity = 10 )
inline

Initializes Queue with capacity.

Parameters
allocAllocator for initial allocation
capacityInitial capacity
Returns
success or failure

Definition at line 48 of file queue.hpp.

48{ return buffer.init(alloc, capacity); }
size_t capacity() const noexcept
Definition queue.hpp:87
Here is the call graph for this function:
Here is the caller graph for this function:

◆ init() [2/2]

template<typename T>
Result< void, AllocationError > mtcore::Queue< T >::init ( Allocator & alloc,
std::initializer_list< T > init,
size_t capacity = 10 )
inline

Initializes Queue with initial items.

Parameters
allocAllocator for initial allocation
initInitial elements
capacityInitial capacity
Returns
success or failure

Definition at line 57 of file queue.hpp.

57 {
58 return buffer.init(alloc, init, capacity);
59 }
Result< void, AllocationError > init(Allocator &alloc, size_t capacity=10)
Initializes Queue with capacity.
Definition queue.hpp:48
Here is the call graph for this function:

◆ is_empty()

template<typename T>
bool mtcore::Queue< T >::is_empty ( ) const
inlinenodiscardnoexcept

Checks if ring buffer is empty.

Returns
true if ring buffer is empty

Definition at line 77 of file queue.hpp.

77{ return size() == 0; }
size_t size() const noexcept
Definition queue.hpp:82
Here is the call graph for this function:

◆ is_full()

template<typename T>
bool mtcore::Queue< T >::is_full ( ) const
inlinenodiscardnoexcept

Checks if ring buffer is full.

Returns
true if ring buffer is full

Definition at line 71 of file queue.hpp.

71{ return buffer.size() >= buffer.capacity(); }
Here is the caller graph for this function:

◆ is_initialized()

template<typename T>
bool mtcore::Queue< T >::is_initialized ( ) const
inlinenodiscardnoexcept

Checks if an Queue is initialized.

Returns
true if initialized, false otherwise

Definition at line 142 of file queue.hpp.

142{ return buffer.capacity() > 0; }
Here is the caller graph for this function:

◆ peek()

template<typename T>
Optional< T > mtcore::Queue< T >::peek ( ) const
inlinenoexcept

Peeks at first element of Queue.

Returns
First elem or nullopt

Definition at line 95 of file queue.hpp.

95 {
96 ensure(is_initialized(), "Not initialized");
97 if (buffer.size() > 0) {
98 return buffer.at(0);
99 }
100 return nullopt;
101 }
#define ensure(check,...)
Ensures that a check holds true, aborts the program if not true Will print error if the condition is ...
bool is_initialized() const noexcept
Checks if an Queue is initialized.
Definition queue.hpp:142
Here is the call graph for this function:

◆ pop()

template<typename T>
Optional< T > mtcore::Queue< T >::pop ( )
inlinenoexcept

Removes and returns the next element in the Queue.

Returns
Next elem in Queue or nullopt

Definition at line 107 of file queue.hpp.

107 {
108 ensure(is_initialized(), "Not initialized");
109 mtdefer { ensure(size() <= capacity(), "SIZE MISMATCH"); };
110 return buffer.pop_front();
111 }
Here is the call graph for this function:

◆ push() [1/2]

template<typename T>
Result< void, AllocationError > mtcore::Queue< T >::push ( Allocator & alloc,
const T & elem )
inlinenoexcept

Tries to add an element to the Queue Will allocate more space if needed Fails if it cannot allocate larger Queue.

Parameters
allocAllocator to use for new allocation
elemElement to add
Returns
Success or failure

Definition at line 133 of file queue.hpp.

133 {
134 ensure(is_initialized(), "Not initialized");
135 return buffer.push_back(alloc, elem);
136 }
Here is the call graph for this function:

◆ push() [2/2]

template<typename T>
Result< void, CollectionAddNoAllocationError > mtcore::Queue< T >::push ( const T & elem)
inlinenoexcept

Tries to add an element to the Queue Fails if Queue is empty.

Parameters
elemElement to add
Returns
Success or failure

Definition at line 119 of file queue.hpp.

119 {
120 ensure(is_initialized(), "Not initialized");
121 mtdefer { ensure(size() <= capacity(), "SIZE MISMATCH"); };
122 return buffer.push_back(elem);
123 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ shrink_to_fit()

template<typename T>
Result< void, AllocationError > mtcore::Queue< T >::shrink_to_fit ( Allocator & alloc)
inline

Definition at line 89 of file queue.hpp.

89{ return buffer.shrink_to_fit(alloc); }

◆ size()

template<typename T>
size_t mtcore::Queue< T >::size ( ) const
inlinenodiscardnoexcept
Returns
Size of Queue

Definition at line 82 of file queue.hpp.

82{ return buffer.size(); }
Here is the caller graph for this function:

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