SleakEngine
0.1.0
C++23 multi-backend game engine
Toggle main menu visibility
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
7
namespace
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
()) {
44
throw
Sleak::EmptyContainerException
();
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
()) {
57
throw
Sleak::EmptyContainerException
();
58
}
59
return
data[0];
60
}
61
62
const
T&
front
()
const
{
63
if
(
isEmpty
()) {
64
throw
Sleak::EmptyContainerException
();
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_
List.hpp
Sleak::EmptyContainerException
Definition
Exception.hpp:46
Sleak::List
Implements a dynamic array-like list for storing and managing a collection of elements.
Definition
List.hpp:20
Sleak::Queue::reverse
const void reverse()
Reverses the queue's element order in place.
Definition
Queue.hpp:70
Sleak::Queue::begin
T * begin()
Definition
Queue.hpp:88
Sleak::Queue::GetData
List< T > GetData()
Copies out the underlying List, front-to-back.
Definition
Queue.hpp:85
Sleak::Queue::end
T * end()
Definition
Queue.hpp:90
Sleak::Queue::end
const T * end() const
Definition
Queue.hpp:91
Sleak::Queue::begin
const T * begin() const
Definition
Queue.hpp:89
Sleak::Queue::isEmpty
bool isEmpty() const
Definition
Queue.hpp:74
Sleak::Queue::size
size_t size() const
Definition
Queue.hpp:78
Sleak::Queue::clear
void clear()
Definition
Queue.hpp:80
Sleak::Queue::pop
T pop()
Removes and returns the front element; throws if empty.
Definition
Queue.hpp:42
Sleak::Queue::push
void push(const T &value)
Adds value to the back of the queue.
Definition
Queue.hpp:37
Sleak::Queue::Queue
Queue()=default
Sleak::Queue::front
const T & front() const
Definition
Queue.hpp:62
Sleak::Queue::front
T & front()
Front element without removing it; throws if empty.
Definition
Queue.hpp:55
Sleak
Root namespace for everything the engine exposes.
Definition
Camera.hpp:10
include
public
Utility
Container
Queue.hpp
Generated on
for SleakEngine by
1.18.0