SleakEngine 1.0.0
C++23 multi-backend game engine
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
7namespace 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>
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 */
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 */
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_
Implements a dynamic array-like list for storing and managing a collection of elements.
Definition List.hpp:20
A simple container for storing two values as a pair.
Definition Pair.hpp:30
A list where elements are stored with associated priorities.
void removeHighestPriorityElement()
Removes the element with the highest priority.
T getHighestPriorityElement() const
Returns the element with the highest priority.
void insert(const T &element, int priority)
Inserts an element with the given priority.
Root namespace for everything the engine exposes.
Definition Camera.hpp:10