SleakEngine 1.0.0
C++23 multi-backend game engine
Loading...
Searching...
No Matches
Heap.hpp
Go to the documentation of this file.
1#ifndef _HEAP_H_
2#define _HEAP_H_
3
4namespace Sleak
5{
6 /**
7 * @class Heap
8 * @brief Implements a binary heap data structure for efficient priority queue operations.
9 *
10 * The Heap class maintains a collection of elements with a specific ordering property
11 * (min-heap or max-heap). It is suitable for scenarios where you need to efficiently
12 * retrieve the minimum or maximum element. Use this class when you need to implement
13 * priority queues, scheduling algorithms, or heap-based sorting.
14 *
15 * Example Use Cases:
16 * - Implementing a priority queue for task scheduling.
17 * - Finding the kth largest element in a data stream.
18 * - Implementing Dijkstra's shortest path algorithm.
19 * - Heap sort implementation.
20 *
21 * Implementation Details:
22 * - Uses an array-based representation to store the heap elements.
23 * - Provides methods for inserting elements and extracting the minimum or maximum element.
24 * - Maintains the heap property after each operation.
25 * @ingroup utility
26 */
27 template <typename T, bool isMinHeap = true>
28 class Heap
29 {
30 private:
31 T* data;
32 size_t capacity;
33 size_t size;
34
35 /// Bubbles the element at index up until the heap property holds.
36 void heapifyUp(size_t index)
37 {
38 while (index > 0)
39 {
40 size_t parent = (index - 1) / 2;
41 if ((isMinHeap && data[index] < data[parent]) || (!isMinHeap && data[index] > data[parent]))
42 {
43 swap(data[index], data[parent]);
44 index = parent;
45 }
46 else
47 break;
48 }
49 }
50
51 /// Sinks the element at index down until the heap property holds.
52 void heapifyDown(size_t index)
53 {
54 while (true)
55 {
56 size_t left = 2 * index + 1;
57 size_t right = 2 * index + 2;
58 size_t target = index;
59
60 if (left < size && ((isMinHeap && data[left] < data[target]) || (!isMinHeap && data[left] > data[target])))
61 target = left;
62
63 if (right < size && ((isMinHeap && data[right] < data[target]) || (!isMinHeap && data[right] > data[target])))
64 target = right;
65
66 if (target != index)
67 {
68 swap(data[index], data[target]);
69 index = target;
70 }
71 else
72 break;
73 }
74 }
75
76 void swap(T& a, T& b)
77 {
78 T temp = a;
79 a = b;
80 b = temp;
81 }
82
83 /// Doubles the backing array's capacity.
84 void resize()
85 {
86 capacity *= 2;
87 T* newData = new T[capacity];
88 for (size_t i = 0; i < size; ++i)
89 newData[i] = data[i];
90 delete[] data;
91 data = newData;
92 }
93
94 public:
95 Heap(size_t initCapacity = 16) : size(0), capacity(initCapacity)
96 {
97 data = new T[capacity];
98 }
99
101 {
102 delete[] data;
103 }
104
105 /// Inserts value and restores the heap property.
106 void push(const T& value)
107 {
108 if (size == capacity)
109 resize();
110 data[size] = value;
111 heapifyUp(size);
112 ++size;
113 }
114
115 /// Removes and returns the root (min or max, per isMinHeap).
116 T pop()
117 {
118 if (size == 0)
119 throw "Heap is empty";
120
121 T top = data[0];
122 data[0] = data[--size];
123 heapifyDown(0);
124 return top;
125 }
126
127 /// Returns the root without removing it.
128 T top() const
129 {
130 if (size == 0)
131 throw "Heap is empty";
132 return data[0];
133 }
134
135 size_t getSize() const { return size; }
136 };
137}
138
139#endif // _HEAP_H_
void push(const T &value)
Inserts value and restores the heap property.
Definition Heap.hpp:106
size_t getSize() const
Definition Heap.hpp:135
T top() const
Returns the root without removing it.
Definition Heap.hpp:128
T pop()
Removes and returns the root (min or max, per isMinHeap).
Definition Heap.hpp:116
Heap(size_t initCapacity=16)
Definition Heap.hpp:95
Root namespace for everything the engine exposes.
Definition Camera.hpp:10