FastLED 3.9.15
Loading...
Searching...
No Matches
priority_queue.h
Go to the documentation of this file.
1#pragma once
2
3#include "fl/stl/stdint.h" // for u64 // IWYU pragma: keep
4#include "fl/stl/vector.h" // for vector
5#include "fl/stl/move.h" // for remove_reference
6#include "fl/stl/utility.h" // for greater, less
7#include "fl/stl/int.h" // for size // IWYU pragma: keep
9#include "fl/stl/noexcept.h"
10
11namespace fl {
12
13// Custom heap operations
14template <typename Iterator, typename Compare>
15void sift_down(Iterator first, Iterator last, Iterator start, Compare comp) {
16 auto root = start;
17 auto child = first + 2 * (root - first) + 1;
18
19 while (child < last) {
20 if (child + 1 < last && comp(*child, *(child + 1))) {
21 ++child;
22 }
23
24 if (comp(*root, *child)) {
25 auto tmp = *root;
26 *root = *child;
27 *child = tmp;
28
29 root = child;
30 child = first + 2 * (root - first) + 1;
31 } else {
32 break;
33 }
34 }
35}
36
37template <typename Iterator, typename Compare>
38void push_heap(Iterator first, Iterator last, Compare comp) {
39 auto pos = last - 1;
40 auto parent = first + ((pos - first) - 1) / 2;
41
42 while (pos > first && comp(*parent, *pos)) {
43 auto tmp = *parent;
44 *parent = *pos;
45 *pos = tmp;
46
47 pos = parent;
48 parent = first + ((pos - first) - 1) / 2;
49 }
50}
51
52template <typename Iterator> void push_heap(Iterator first, Iterator last) {
53 using value_type = typename fl::remove_reference<decltype(*first)>::type;
54 push_heap(first, last, [](const value_type &a, const value_type &b) { return a < b; });
55}
56
57template <typename Iterator, typename Compare>
58void pop_heap(Iterator first, Iterator last, Compare comp) {
59 --last;
60 auto tmp = *first;
61 *first = *last;
62 *last = tmp;
63
64 sift_down(first, last, first, comp);
65}
66
67template <typename Iterator> void pop_heap(Iterator first, Iterator last) {
68 using value_type = typename fl::remove_reference<decltype(*first)>::type;
69 pop_heap(first, last, [](const value_type &a, const value_type &b) { return a < b; });
70}
71
72template <typename T, typename Compare = fl::less<T>,
73 typename VectorT = vector<T>>
75 public:
76 using value_type = T;
77 using size_type = fl::size;
78 using compare_type = Compare;
79
81 explicit PriorityQueue(memory_resource* resource) : _data(resource) {}
82 explicit PriorityQueue(const Compare &comp) : _comp(comp) {}
83 PriorityQueue(const Compare &comp, memory_resource* resource) : _data(resource), _comp(comp) {}
84
85 void push(const T &value) {
86 _data.push_back(value);
87 push_heap(_data.begin(), _data.end(), _comp);
88 }
89
90 void push(T &&value) {
91 _data.push_back(fl::move(value));
92 push_heap(_data.begin(), _data.end(), _comp);
93 }
94
95 template<typename... Args>
96 void emplace(Args&&... args) {
97 _data.emplace_back(fl::forward<Args>(args)...);
98 push_heap(_data.begin(), _data.end(), _comp);
99 }
100
101 void pop() {
102 pop_heap(_data.begin(), _data.end(), _comp);
103 _data.pop_back();
104 }
105
106 const T &top() const { return _data.front(); }
107
108 bool empty() const { return _data.size() == 0; }
109 fl::size size() const { return _data.size(); }
110
111 const Compare &compare() const { return _comp; }
112 memory_resource* get_memory_resource() const { return _data.get_resource(); }
113
115 bool operator==(const PriorityQueue& other) const {
116 return _data == other._data;
117 }
118
120 bool operator!=(const PriorityQueue& other) const {
121 return _data != other._data;
122 }
123
125 bool operator<(const PriorityQueue& other) const {
126 return _data < other._data;
127 }
128
130 bool operator<=(const PriorityQueue& other) const {
131 return _data <= other._data;
132 }
133
135 bool operator>(const PriorityQueue& other) const {
136 return _data > other._data;
137 }
138
140 bool operator>=(const PriorityQueue& other) const {
141 return _data >= other._data;
142 }
143
144 private:
145 VectorT _data;
146 Compare _comp;
147};
148
179template<typename T, typename Compare = fl::less<T>>
181private:
185
186 // Comparison: primary by value, secondary by sequence (FIFO)
187 bool operator<(const StableElement& other) const {
188 Compare comp;
189 // Use the Compare function to determine ordering
190 if (comp(mValue, other.mValue)) return true;
191 if (comp(other.mValue, mValue)) return false;
192 // Values are equal according to comparator - use sequence for FIFO ordering
193 // INVERTED: smaller sequence should be "greater" (pop first)
194 return mSequence > other.mSequence;
195 }
196 };
197
198public:
199 using value_type = T;
200 using size_type = fl::size;
201
203
208 void push(const T& value) {
209 mQueue.push({value, mNextSequence++});
210 }
211
212 template<typename... Args>
213 void emplace(Args&&... args) {
214 mQueue.push({T(fl::forward<Args>(args)...), mNextSequence++});
215 }
216
222 void pop() {
223 mQueue.pop();
224 }
225
232 const T& top() const {
233 return mQueue.top().mValue;
234 }
235
240 bool empty() const {
241 return mQueue.empty();
242 }
243
248 fl::size size() const {
249 return mQueue.size();
250 }
251
255 void clear() {
256 while (!empty()) {
257 pop();
258 }
259 mNextSequence = 0;
260 }
261
263 bool operator==(const priority_queue_stable& other) const {
264 return mQueue == other.mQueue;
265 }
266
268 bool operator!=(const priority_queue_stable& other) const {
269 return mQueue != other.mQueue;
270 }
271
273 bool operator<(const priority_queue_stable& other) const {
274 return mQueue < other.mQueue;
275 }
276
278 bool operator<=(const priority_queue_stable& other) const {
279 return mQueue <= other.mQueue;
280 }
281
283 bool operator>(const priority_queue_stable& other) const {
284 return mQueue > other.mQueue;
285 }
286
288 bool operator>=(const priority_queue_stable& other) const {
289 return mQueue >= other.mQueue;
290 }
291
292private:
295};
296
297} // namespace fl
uint8_t pos
Definition Blur.ino:11
PriorityQueue(const Compare &comp)
memory_resource * get_memory_resource() const
bool operator<=(const PriorityQueue &other) const
Less-than-or-equal comparison.
bool operator>=(const PriorityQueue &other) const
Greater-than-or-equal comparison.
PriorityQueue(const Compare &comp, memory_resource *resource)
fl::size size() const
PriorityQueue() FL_NOEXCEPT=default
bool operator>(const PriorityQueue &other) const
Greater-than comparison.
void emplace(Args &&... args)
bool operator==(const PriorityQueue &other) const
Equality comparison.
bool operator!=(const PriorityQueue &other) const
Inequality comparison.
void push(T &&value)
const T & top() const
const Compare & compare() const
bool operator<(const PriorityQueue &other) const
Lexicographic comparison.
void push(const T &value)
Polymorphic memory resource base class (PMR-style).
bool operator<=(const priority_queue_stable &other) const
Less-than-or-equal comparison.
void pop()
Remove the top element from the queue.
bool empty() const
Check if the queue is empty.
fl::PriorityQueue< StableElement > mQueue
void push(const T &value)
Push an element into the priority queue.
fl::size size() const
Get the number of elements in the queue.
bool operator==(const priority_queue_stable &other) const
Equality comparison.
priority_queue_stable() FL_NOEXCEPT
bool operator<(const priority_queue_stable &other) const
Lexicographic comparison.
bool operator>=(const priority_queue_stable &other) const
Greater-than-or-equal comparison.
const T & top() const
Access the top element (highest priority)
void clear()
Clear all elements from the queue.
void emplace(Args &&... args)
bool operator>(const priority_queue_stable &other) const
Greater-than comparison.
bool operator!=(const priority_queue_stable &other) const
Inequality comparison.
PMR-style polymorphic memory resource for type-erased allocation.
constexpr T && forward(typename remove_reference< T >::type &t) FL_NOEXCEPT
Definition s16x16x4.h:234
constexpr remove_reference< T >::type && move(T &&t) FL_NOEXCEPT
Definition s16x16x4.h:28
constexpr int type_rank< T >::value
void sift_down(Iterator first, Iterator last, Iterator start, Compare comp)
void pop_heap(Iterator first, Iterator last, Compare comp)
fl::u64 u64
Definition s16x16x4.h:221
void push_heap(Iterator first, Iterator last, Compare comp)
Base definition for an LED controller.
Definition crgb.hpp:179
corkscrew_args args
Definition old.h:149
#define FL_NOEXCEPT
bool operator<(const StableElement &other) const