Corium 1.1.0
High-Performance Zero-Heap C++20 MPSC Application Runtime
Loading...
Searching...
No Matches
StaticMinHeap.hpp
Go to the documentation of this file.
1
7#pragma once
8
9#include <array>
10#include <cassert>
11#include <cstddef>
12#include <functional>
13#include <utility>
14
15namespace corium::internal {
16
22template <
23 typename T,
24 std::size_t Capacity,
25 typename Compare = std::greater<T>
26>
28public:
29 constexpr StaticMinHeap() = default;
30
33 template <typename U>
34 bool push(U&& value) {
35 if (_size >= Capacity) {
36 return false;
37 }
38
39 _data[_size] = std::forward<U>(value);
40 siftUp(_size);
41 _size++;
42 return true;
43 }
44
47 bool pop() noexcept {
48 if (_size == 0) {
49 return false;
50 }
51
52 _size--;
53 if (_size > 0) {
54 _data[0] = std::move(_data[_size]);
55 siftDown(0);
56 }
57 return true;
58 }
59
61 [[nodiscard]] const T& top() const noexcept {
62 assert(_size > 0 && "Accessing top of empty StaticMinHeap");
63 return _data[0];
64 }
65
67 [[nodiscard]] T& top() noexcept {
68 assert(_size > 0 && "Accessing top of empty StaticMinHeap");
69 return _data[0];
70 }
71
73 [[nodiscard]] constexpr std::size_t size() const noexcept {
74 return _size;
75 }
76
78 [[nodiscard]] static constexpr std::size_t capacity() noexcept {
79 return Capacity;
80 }
81
83 [[nodiscard]] constexpr bool empty() const noexcept {
84 return _size == 0;
85 }
86
88 [[nodiscard]] constexpr bool full() const noexcept {
89 return _size >= Capacity;
90 }
91
93 void clear() noexcept {
94 _size = 0;
95 }
96
98 [[nodiscard]] T* data() noexcept {
99 return _data.data();
100 }
101
103 [[nodiscard]] const T* data() const noexcept {
104 return _data.data();
105 }
106
108 void siftDown(std::size_t index) noexcept {
109 Compare comp;
110 std::size_t current = index;
111
112 while (true) {
113 std::size_t left = 2 * current + 1;
114 std::size_t right = 2 * current + 2;
115 std::size_t smallest = current;
116
117 if (left < _size && comp(_data[smallest], _data[left])) {
118 smallest = left;
119 }
120 if (right < _size && comp(_data[smallest], _data[right])) {
121 smallest = right;
122 }
123
124 if (smallest != current) {
125 using std::swap;
126 swap(_data[current], _data[smallest]);
127 current = smallest;
128 } else {
129 break;
130 }
131 }
132 }
133
135 void siftUp(std::size_t index) noexcept {
136 Compare comp;
137 std::size_t current = index;
138
139 while (current > 0) {
140 std::size_t parent = (current - 1) / 2;
141 if (comp(_data[parent], _data[current])) {
142 using std::swap;
143 swap(_data[parent], _data[current]);
144 current = parent;
145 } else {
146 break;
147 }
148 }
149 }
150
151private:
152 std::array<T, Capacity> _data{};
153 std::size_t _size{0};
154};
155
156} // namespace corium::internal
Fixed-capacity statically-allocated binary min-heap. Zero dynamic heap allocations.
Definition StaticMinHeap.hpp:27
bool push(U &&value)
Push an element into the heap.
Definition StaticMinHeap.hpp:34
const T & top() const noexcept
Access top element.
Definition StaticMinHeap.hpp:61
constexpr bool full() const noexcept
Check if heap is full.
Definition StaticMinHeap.hpp:88
static constexpr std::size_t capacity() noexcept
Maximum capacity of the heap.
Definition StaticMinHeap.hpp:78
constexpr std::size_t size() const noexcept
Current number of elements in the heap.
Definition StaticMinHeap.hpp:73
T * data() noexcept
Direct access to underlying data array.
Definition StaticMinHeap.hpp:98
const T * data() const noexcept
Direct const access to underlying data array.
Definition StaticMinHeap.hpp:103
void siftDown(std::size_t index) noexcept
Sift down an element at specified index (e.g. after in-place modification).
Definition StaticMinHeap.hpp:108
void siftUp(std::size_t index) noexcept
Sift up an element at specified index.
Definition StaticMinHeap.hpp:135
constexpr bool empty() const noexcept
Check if heap is empty.
Definition StaticMinHeap.hpp:83
void clear() noexcept
Clear all elements.
Definition StaticMinHeap.hpp:93
constexpr StaticMinHeap()=default
T & top() noexcept
Access mutable top element.
Definition StaticMinHeap.hpp:67
bool pop() noexcept
Remove the top element from the heap.
Definition StaticMinHeap.hpp:47
Definition CallableTraits.hpp:12