SleakEngine 1.0.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 "List.hpp"
5#include <sstream>
6
7namespace Sleak
8{
9
10 /**
11 * @class Queue
12 * @brief Implements a FIFO (First-In, First-Out) queue data structure.
13 *
14 * The Queue class maintains a collection of elements in the order they were added. It
15 * is suitable for scenarios where you need to process elements in a first-come,
16 * first-served manner. Use this class when you need to implement task queues,
17 * message queues, or breadth-first search algorithms.
18 *
19 * Example Use Cases:
20 * - Implementing a task queue for background processing.
21 * - Managing message queues in a network communication system.
22 * - Performing breadth-first search in graph algorithms.
23 * - Simulating real-world queues, such as waiting lines.
24 *
25 * Implementation Details:
26 * - Uses an underlying List to store the queue elements.
27 * - Provides methods for enqueueing (adding) and dequeueing (removing) elements.
28 * - Maintains the FIFO order of elements.
29 * @ingroup utility
30 */
31 template <typename T>
32 class Queue {
33 public:
34 Queue() = default;
35
36 /// Adds value to the back of the queue.
37 void push(const T& value) {
38 data.add(value);
39 }
40
41 /// Removes and returns the front element; throws if empty.
42 T pop() {
43 if (isEmpty()) {
45 }
46
47 T front = std::move(data[0]);
48
49 data.erase(0);
50
51 return front;
52 }
53
54 /// Front element without removing it; throws if empty.
55 T& front() {
56 if (isEmpty()) {
58 }
59 return data[0];
60 }
61
62 const T& front() const {
63 if (isEmpty()) {
65 }
66 return data[0];
67 }
68
69 /// Reverses the queue's element order in place.
70 const void reverse() {
71 data.reverse();
72 }
73
74 bool isEmpty() const {
75 return data.GetSize() == 0;
76 }
77
78 size_t size() const { return data.GetSize(); }
79
80 void clear() {
81 data.clear();
82 }
83
84 /// Copies out the underlying List, front-to-back.
85 List<T> GetData() { return data; }
86
87 // Iterators
88 T* begin() { return data.begin(); }
89 const T* begin() const { return data.begin(); }
90 T* end() { return data.end(); }
91 const T* end() const { return data.end(); }
92
93 private:
94 List<T> data;
95 };
96}
97
98#endif // _QUEUE_H_
Implements a dynamic array-like list for storing and managing a collection of elements.
Definition List.hpp:20
const void reverse()
Reverses the queue's element order in place.
Definition Queue.hpp:70
T * begin()
Definition Queue.hpp:88
List< T > GetData()
Copies out the underlying List, front-to-back.
Definition Queue.hpp:85
T * end()
Definition Queue.hpp:90
const T * end() const
Definition Queue.hpp:91
const T * begin() const
Definition Queue.hpp:89
bool isEmpty() const
Definition Queue.hpp:74
size_t size() const
Definition Queue.hpp:78
void clear()
Definition Queue.hpp:80
T pop()
Removes and returns the front element; throws if empty.
Definition Queue.hpp:42
void push(const T &value)
Adds value to the back of the queue.
Definition Queue.hpp:37
Queue()=default
const T & front() const
Definition Queue.hpp:62
T & front()
Front element without removing it; throws if empty.
Definition Queue.hpp:55
Root namespace for everything the engine exposes.
Definition Camera.hpp:10