SleakEngine 0.1.0
C++23 multi-backend game engine
Loading...
Searching...
No Matches
Queue.hpp
Go to the documentation of this file.
1#ifndef _QUEUE_H_
2#define _QUEUE_H_
3
4#include <algorithm>
5#include <sstream>
6#include <utility>
7
8#include "List.hpp"
9
10namespace Sleak
11{
12
13/**
14 * @class Queue
15 * @brief Implements a FIFO (First-In, First-Out) queue data structure.
16 *
17 * The Queue class maintains a collection of elements in the order they were
18 * added. It is suitable for scenarios where you need to process elements in a
19 * first-come, first-served manner. Use this class when you need to implement
20 * task queues, message queues, or breadth-first search algorithms.
21 *
22 * Example Use Cases:
23 * - Implementing a task queue for background processing.
24 * - Managing message queues in a network communication system.
25 * - Performing breadth-first search in graph algorithms.
26 * - Simulating real-world queues, such as waiting lines.
27 *
28 * Implementation Details:
29 * - Uses an underlying List plus a head index; pop is amortized O(1).
30 * - Provides methods for enqueueing (adding) and dequeueing (removing)
31 * elements.
32 * - Maintains the FIFO order of elements.
33 * @ingroup utility
34 */
35template <typename T>
36class Queue {
37 public:
38 Queue() = default;
39 Queue(const Queue&) = default;
40 Queue& operator=(const Queue&) = default;
41
42 /// Takes other's elements, leaving it empty.
43 Queue(Queue&& other) noexcept
44 : data(std::move(other.data)), head(std::exchange(other.head, 0)) {}
45
46 /// Replaces this queue with other's elements, leaving other empty.
47 Queue& operator=(Queue&& other) noexcept {
48 if (this != &other) {
49 data = std::move(other.data);
50 head = std::exchange(other.head, 0);
51 }
52 return *this;
53 }
54
55 /// Adds value to the back of the queue.
56 void push(const T& value) { data.add(value); }
57
58 /// Moves value onto the back of the queue.
59 void push(T&& value) { data.add(std::move(value)); }
60
61 /// Removes and returns the front element; throws if empty.
62 T pop() {
63 if (isEmpty()) {
65 }
66
67 T* slots = data.GetData();
68 T front = std::move(slots[head]);
69 slots[head] = T();
70 ++head;
71 compactIfSparse();
72
73 return front;
74 }
75
76 /// Front element without removing it; throws if empty.
77 T& front() {
78 if (isEmpty()) {
80 }
81 return data.GetData()[head];
82 }
83
84 const T& front() const {
85 if (isEmpty()) {
87 }
88 return data.GetData()[head];
89 }
90
91 /// Reverses the queue's element order in place.
92 const void reverse() { std::reverse(begin(), end()); }
93
94 bool isEmpty() const { return size() == 0; }
95
96 size_t size() const { return data.GetSize() - head; }
97
98 void clear() {
99 data.clear();
100 head = 0;
101 }
102
103 /// Copies out the queued elements, front-to-back.
105 List<T> out;
106 out.resize(size());
107 for (const T& value : *this) {
108 out.add(value);
109 }
110 return out;
111 }
112
113 // Iterators
114 T* begin() { return data.begin() + head; }
115 const T* begin() const { return data.begin() + head; }
116 T* end() { return data.end(); }
117 const T* end() const { return data.end(); }
118
119 private:
120 /// Slides live elements to the front once popped slots are the majority.
121 void compactIfSparse() {
122 const size_t live = size();
123 if (head < live) {
124 return;
125 }
126 if (live == 0) {
127 clear();
128 return;
129 }
130
131 T* slots = data.GetData();
132 for (size_t i = 0; i < live; ++i) {
133 slots[i] = std::move(slots[head + i]);
134 }
135 while (data.GetSize() > live) {
136 data.erase(data.GetSize() - 1);
137 }
138 head = 0;
139 }
140
141 List<T> data;
142 size_t head = 0;
143};
144}
145
146#endif // _QUEUE_H_
Implements a dynamic array-like list for storing and managing a collection of elements.
Definition List.hpp:21
void add(const T &value)
Definition List.hpp:84
void resize()
Definition List.hpp:158
Queue & operator=(const Queue &)=default
const void reverse()
Reverses the queue's element order in place.
Definition Queue.hpp:92
T * begin()
Definition Queue.hpp:114
List< T > GetData()
Copies out the queued elements, front-to-back.
Definition Queue.hpp:104
T * end()
Definition Queue.hpp:116
const T * end() const
Definition Queue.hpp:117
const T * begin() const
Definition Queue.hpp:115
bool isEmpty() const
Definition Queue.hpp:94
size_t size() const
Definition Queue.hpp:96
void clear()
Definition Queue.hpp:98
T pop()
Removes and returns the front element; throws if empty.
Definition Queue.hpp:62
Queue(const Queue &)=default
Queue(Queue &&other) noexcept
Takes other's elements, leaving it empty.
Definition Queue.hpp:43
void push(const T &value)
Adds value to the back of the queue.
Definition Queue.hpp:56
Queue()=default
Queue & operator=(Queue &&other) noexcept
Replaces this queue with other's elements, leaving other empty.
Definition Queue.hpp:47
const T & front() const
Definition Queue.hpp:84
void push(T &&value)
Moves value onto the back of the queue.
Definition Queue.hpp:59
T & front()
Front element without removing it; throws if empty.
Definition Queue.hpp:77
Root namespace for everything the engine exposes.
Definition Camera.hpp:10