SleakEngine
0.1.0
C++23 multi-backend game engine
Toggle main menu visibility
Loading...
Searching...
No Matches
PriorityList.hpp
Go to the documentation of this file.
1
#ifndef _PRIORITYLIST_H_
2
#define _PRIORITYLIST_H_
3
4
#include <Utility/List.hpp>
5
#include <Utility/Pair.hpp>
6
7
namespace
Sleak
{
8
9
/**
10
* @class PriorityList
11
* @brief A list where elements are stored with associated priorities.
12
*
13
* This class provides a way to store elements with priorities and
14
* efficiently retrieve or remove the element with the highest priority.
15
* It is suitable for scenarios where you need to manage a collection of
16
* items with different priorities, such as task scheduling, event handling,
17
* or AI decision-making.
18
*
19
* Example Use Cases:
20
* - Task scheduling based on priority levels.
21
* - Event handling where events have different priorities.
22
* - AI decision-making where actions have different priorities.
23
*
24
* Implementation Details:
25
* - Uses a `List` to store pairs of (element, priority).
26
* - Elements are kept sorted by priority for efficient retrieval.
27
* - Provides methods for inserting elements with priorities,
28
* retrieving the highest priority element, and removing it.
29
* @ingroup utility
30
*/
31
template
<
typename
T>
32
class
PriorityList
{
33
public
:
34
/**
35
* @brief Inserts an element with the given priority.
36
*
37
* @param element The element to insert.
38
* @param priority The priority of the element.
39
*/
40
void
insert
(
const
T& element,
int
priority) {
41
// Find the correct position to insert based on priority (keep the list sorted)
42
size_t
index = 0;
43
while
(index < data.GetSize() && data[index].second > priority) {
44
++index;
45
}
46
data.insert(index, std::make_pair(element, priority));
47
}
48
49
/**
50
* @brief Returns the element with the highest priority.
51
*
52
* @return The element with the highest priority.
53
* @throw std::runtime_error if the list is empty.
54
*/
55
T
getHighestPriorityElement
()
const
{
56
if
(data.GetSize() == 0) {
57
throw
std::runtime_error(
"Priority list is empty"
);
58
}
59
return
data[0].first;
60
}
61
62
/**
63
* @brief Removes the element with the highest priority.
64
*
65
* @throw std::runtime_error if the list is empty.
66
*/
67
void
removeHighestPriorityElement
() {
68
if
(data.GetSize() == 0) {
69
throw
std::runtime_error(
"Priority list is empty"
);
70
}
71
data.insert(0,
List
<
Pair<T, int>
>().begin(),
List
<
Pair<T, int>
>().end());
72
}
73
74
private
:
75
List<Pair<T, int>
> data;
// Use Pair to store element and priority
76
};
77
}
78
79
#endif
// _PRIORITYLIST_H_
Sleak::List
Implements a dynamic array-like list for storing and managing a collection of elements.
Definition
List.hpp:20
Sleak::Pair
A simple container for storing two values as a pair.
Definition
Pair.hpp:30
Sleak::PriorityList
A list where elements are stored with associated priorities.
Definition
PriorityList.hpp:32
Sleak::PriorityList::removeHighestPriorityElement
void removeHighestPriorityElement()
Removes the element with the highest priority.
Definition
PriorityList.hpp:67
Sleak::PriorityList::getHighestPriorityElement
T getHighestPriorityElement() const
Returns the element with the highest priority.
Definition
PriorityList.hpp:55
Sleak::PriorityList::insert
void insert(const T &element, int priority)
Inserts an element with the given priority.
Definition
PriorityList.hpp:40
Sleak
Root namespace for everything the engine exposes.
Definition
Camera.hpp:10
include
public
Utility
Container
PriorityList.hpp
Generated on
for SleakEngine by
1.18.0