MT Core (C++)
Core library for replacing C++ standard in project usage
Loading...
Searching...
No Matches
queue.hpp
Go to the documentation of this file.
1/*
2
3Copyright 2025 Matthew Tolman
4
5Licensed under the Apache License, Version 2.0 (the "License");
6you may not use this file except in compliance with the License.
7You may obtain a copy of the License at
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
11Unless required by applicable law or agreed to in writing, software
12distributed under the License is distributed on an "AS IS" BASIS,
13WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14See the License for the specific language governing permissions and
15limitations under the License.
16
17*/
18
19#ifndef MTCORE_QUEUE_HPP
20#define MTCORE_QUEUE_HPP
21
22#include "mtcore/core.hpp"
23#include "ring_buffer.hpp"
24
25namespace mtcore {
36 template<typename T>
37 struct Queue {
38 private:
39 RingBuffer<T> buffer = {};
40
41 public:
48 Result<void, AllocationError> init(Allocator &alloc, size_t capacity = 10) { return buffer.init(alloc, capacity); }
49
57 Result<void, AllocationError> init(Allocator &alloc, std::initializer_list<T> init, size_t capacity = 10) {
58 return buffer.init(alloc, init, capacity);
59 }
60
65 void deinit(Allocator &alloc) { buffer.deinit(alloc); }
66
71 [[nodiscard]] bool is_full() const noexcept { return buffer.size() >= buffer.capacity(); }
72
77 [[nodiscard]] bool is_empty() const noexcept { return size() == 0; }
78
82 [[nodiscard]] size_t size() const noexcept { return buffer.size(); }
83
87 [[nodiscard]] size_t capacity() const noexcept { return buffer.capacity(); }
88
89 Result<void, AllocationError> shrink_to_fit(Allocator &alloc) { return buffer.shrink_to_fit(alloc); }
90
95 Optional<T> peek() const noexcept {
96 ensure(is_initialized(), "Not initialized");
97 if (buffer.size() > 0) {
98 return buffer.at(0);
99 }
100 return nullopt;
101 }
102
107 Optional<T> pop() noexcept {
108 ensure(is_initialized(), "Not initialized");
109 mtdefer { ensure(size() <= capacity(), "SIZE MISMATCH"); };
110 return buffer.pop_front();
111 }
112
120 ensure(is_initialized(), "Not initialized");
121 mtdefer { ensure(size() <= capacity(), "SIZE MISMATCH"); };
122 return buffer.push_back(elem);
123 }
124
133 Result<void, AllocationError> push(Allocator &alloc, const T &elem) noexcept {
134 ensure(is_initialized(), "Not initialized");
135 return buffer.push_back(alloc, elem);
136 }
137
142 [[nodiscard]] bool is_initialized() const noexcept { return buffer.capacity() > 0; }
143 };
144
155 template<typename T, size_t Capacity>
156 struct FixedQueue {
157 private:
159
160 public:
165 Result<void, AllocationError> init() { return buffer.init(); }
166
172 Result<void, AllocationError> init(std::initializer_list<T> init) { return buffer.init(init); }
173
177 [[nodiscard]] size_t size() const noexcept { return buffer.size(); }
178
182 [[nodiscard]] size_t capacity() const noexcept { return Capacity; }
183
188 Optional<T> peek() const noexcept {
189 if (buffer.size() > 0) {
190 return buffer.at(0);
191 }
192 return nullopt;
193 }
194
199 Optional<T> pop() noexcept {
200 mtdefer { ensure(size() <= capacity(), "SIZE MISMATCH"); };
201 return buffer.pop_front();
202 }
203
211 mtdefer { ensure(size() <= capacity(), "SIZE MISMATCH"); };
212 return buffer.push_back(elem);
213 }
214
219 [[nodiscard]] bool is_full() const noexcept { return size() >= Capacity; }
220
225 [[nodiscard]] bool is_empty() const noexcept { return size() == 0; }
226 };
227} // namespace mtcore
228
229#endif // MTCORE_QUEUE_HPP
constexpr auto nullopt
Placeholder value for an empty Optional.
Definition optional.hpp:409
#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.
Definition queue.hpp:156
Result< void, CollectionAddNoAllocationError > push(const T &elem) noexcept
Tries to add an element to the Queue Fails if Queue is empty.
Definition queue.hpp:210
bool is_full() const noexcept
Checks if ring buffer is full.
Definition queue.hpp:219
Optional< T > pop() noexcept
Removes and returns the next element in the Queue.
Definition queue.hpp:199
Result< void, AllocationError > init()
Initializes Queue with capacity.
Definition queue.hpp:165
Result< void, AllocationError > init(std::initializer_list< T > init)
Initializes Queue with initial items.
Definition queue.hpp:172
bool is_empty() const noexcept
Checks if ring buffer is empty.
Definition queue.hpp:225
size_t size() const noexcept
Definition queue.hpp:177
Optional< T > peek() const noexcept
Peeks at first element of Queue.
Definition queue.hpp:188
size_t capacity() const noexcept
Definition queue.hpp:182
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,...
Definition optional.hpp:235
FIFO Queue (First In, First Out) Dynamically allocated, can be resized.
Definition queue.hpp:37
Optional< T > pop() noexcept
Removes and returns the next element in the Queue.
Definition queue.hpp:107
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...
Definition queue.hpp:133
Result< void, AllocationError > init(Allocator &alloc, std::initializer_list< T > init, size_t capacity=10)
Initializes Queue with initial items.
Definition queue.hpp:57
Result< void, AllocationError > shrink_to_fit(Allocator &alloc)
Definition queue.hpp:89
bool is_full() const noexcept
Checks if ring buffer is full.
Definition queue.hpp:71
bool is_initialized() const noexcept
Checks if an Queue is initialized.
Definition queue.hpp:142
Result< void, AllocationError > init(Allocator &alloc, size_t capacity=10)
Initializes Queue with capacity.
Definition queue.hpp:48
Result< void, CollectionAddNoAllocationError > push(const T &elem) noexcept
Tries to add an element to the Queue Fails if Queue is empty.
Definition queue.hpp:119
void deinit(Allocator &alloc)
Cleans up allocated memory.
Definition queue.hpp:65
size_t capacity() const noexcept
Definition queue.hpp:87
Optional< T > peek() const noexcept
Peeks at first element of Queue.
Definition queue.hpp:95
size_t size() const noexcept
Definition queue.hpp:82
bool is_empty() const noexcept
Checks if ring buffer is empty.
Definition queue.hpp:77
Represents a Result that may have an error (error code) or a success value A type of "void" means the...
Definition result.hpp:170
Represents a ring buffer with dynamically allocated memory Can be used as a FIFO or LIFO queue Allows...