19#ifndef MTCORE_QUEUE_HPP
20#define MTCORE_QUEUE_HPP
71 [[nodiscard]]
bool is_full() const noexcept {
return buffer.size() >= buffer.capacity(); }
77 [[nodiscard]]
bool is_empty() const noexcept {
return size() == 0; }
82 [[nodiscard]]
size_t size() const noexcept {
return buffer.size(); }
87 [[nodiscard]]
size_t capacity() const noexcept {
return buffer.capacity(); }
97 if (buffer.size() > 0) {
110 return buffer.pop_front();
122 return buffer.push_back(elem);
135 return buffer.push_back(alloc, elem);
142 [[nodiscard]]
bool is_initialized() const noexcept {
return buffer.capacity() > 0; }
155 template<
typename T,
size_t Capacity>
177 [[nodiscard]]
size_t size() const noexcept {
return buffer.size(); }
182 [[nodiscard]]
size_t capacity() const noexcept {
return Capacity; }
189 if (buffer.size() > 0) {
201 return buffer.pop_front();
212 return buffer.push_back(elem);
219 [[nodiscard]]
bool is_full() const noexcept {
return size() >= Capacity; }
constexpr auto nullopt
Placeholder value for an empty Optional.
#define ensure(check,...)
Ensures that a check holds true, aborts the program if not true Will print error if the condition is ...
#define mtdefer
Defer statement that will mtdefer execution until the scope is left, at which point the code will run...
Core library for C++ with Zig-related functionality.
Represents a memory allocator Exact behavior depends on the underlying VTable used Should use the a_*...
Statically allocated FIFO queue with fixed maximum capacity.
Result< void, CollectionAddNoAllocationError > push(const T &elem) noexcept
Tries to add an element to the Queue Fails if Queue is empty.
bool is_full() const noexcept
Checks if ring buffer is full.
Optional< T > pop() noexcept
Removes and returns the next element in the Queue.
Result< void, AllocationError > init()
Initializes Queue with capacity.
Result< void, AllocationError > init(std::initializer_list< T > init)
Initializes Queue with initial items.
bool is_empty() const noexcept
Checks if ring buffer is empty.
size_t size() const noexcept
Optional< T > peek() const noexcept
Peeks at first element of Queue.
size_t capacity() const noexcept
Represents a ring buffer with static memory allocation Can be used as a FIFO or LIFO queue Allows mem...
Represents a value that may or may not exist (an "Optional" value) Similar concept to std::optional,...
FIFO Queue (First In, First Out) Dynamically allocated, can be resized.
Optional< T > pop() noexcept
Removes and returns the next element in the Queue.
Result< void, AllocationError > push(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 l...
Result< void, AllocationError > init(Allocator &alloc, std::initializer_list< T > init, size_t capacity=10)
Initializes Queue with initial items.
Result< void, AllocationError > shrink_to_fit(Allocator &alloc)
bool is_full() const noexcept
Checks if ring buffer is full.
bool is_initialized() const noexcept
Checks if an Queue is initialized.
Result< void, AllocationError > init(Allocator &alloc, size_t capacity=10)
Initializes Queue with capacity.
Result< void, CollectionAddNoAllocationError > push(const T &elem) noexcept
Tries to add an element to the Queue Fails if Queue is empty.
void deinit(Allocator &alloc)
Cleans up allocated memory.
size_t capacity() const noexcept
Optional< T > peek() const noexcept
Peeks at first element of Queue.
size_t size() const noexcept
bool is_empty() const noexcept
Checks if ring buffer is empty.
Represents a Result that may have an error (error code) or a success value A type of "void" means the...
Represents a ring buffer with dynamically allocated memory Can be used as a FIFO or LIFO queue Allows...