14#include <condition_variable>
22#include "../MonotonicIdAllocator.hpp"
29template<
typename Mapper,
typename T>
32 { m(val) } -> std::convertible_to<int>;
59 struct QueueItemCompare
61 bool operator()(QueueItem
const & a, QueueItem
const & b)
const noexcept
64 auto const ap = a.priority;
65 auto const bp = b.priority;
76 using size_type = std::size_t;
77 using difference_type = std::ptrdiff_t;
78 using reference = value_type &;
79 using const_reference = value_type
const &;
80 using pointer = value_type *;
81 using const_pointer = value_type
const *;
94 void push(T item,
int priority = 0);
113 template<
typename Mapper>
115 void pushRange(std::span<T> items, Mapper && priorityMapper);
126 std::optional<T>
pop(std::stop_token stopToken = {});
158 mutable std::mutex _mutex;
159 std::vector<QueueItem> _stableQueue;
161 std::condition_variable_any _cvQueue;
168 std::lock_guard lock(_mutex);
169 _stableQueue.emplace_back(std::move(item), priority, _seqAllocator.next());
170 std::push_heap(_stableQueue.begin(), _stableQueue.end(), QueueItemCompare());
173 _cvQueue.notify_one();
183 std::lock_guard lock(_mutex);
185 for (
auto & item : items)
186 _stableQueue.emplace_back(std::move(item), priority, _seqAllocator.next());
188 std::make_heap(_stableQueue.begin(), _stableQueue.end(), QueueItemCompare{});
191 _cvQueue.notify_all();
195template<
typename Mapper>
203 std::lock_guard lock(_mutex);
205 for (
auto & item : items)
206 _stableQueue.emplace_back(std::move(item), priorityMapper(item), _seqAllocator.next());
208 std::make_heap(_stableQueue.begin(), _stableQueue.end(), QueueItemCompare{});
211 _cvQueue.notify_all();
217 std::unique_lock lock(_mutex);
218 if (!_cvQueue.wait(lock, stopToken, [
this]
noexcept { return !_stableQueue.empty(); }))
224 if (stopToken.stop_requested())
227 std::pop_heap(_stableQueue.begin(), _stableQueue.end(), QueueItemCompare());
228 auto item = std::move(_stableQueue.back().item);
229 _stableQueue.pop_back();
237 std::lock_guard lock(_mutex);
238 if (_stableQueue.empty())
241 std::pop_heap(_stableQueue.begin(), _stableQueue.end(), QueueItemCompare());
242 auto item = std::move(_stableQueue.back().item);
243 _stableQueue.pop_back();
251 std::vector<QueueItem> items;
254 std::lock_guard lock(_mutex);
255 if (_stableQueue.empty())
258 items = std::move(_stableQueue);
259 _stableQueue.clear();
263 std::sort(items.begin(), items.end(), QueueItemCompare());
265 std::reverse(items.begin(), items.end());
267 std::vector<T> result;
268 result.reserve(items.size());
269 for (
auto & qi : items)
270 result.push_back(std::move(qi.item));
278 std::lock_guard lock(_mutex);
279 _stableQueue.clear();
285 std::lock_guard lock(_mutex);
286 return _stableQueue.empty();
292 std::lock_guard lock(_mutex);
293 return _stableQueue.size();
Convenience macros to explicitly control copy and move semantics.
#define DFX_DISABLE_COPY_AND_MOVE(ClassName)
Disable both copy and move.
Definition CopyMoveControl.hpp:37
Monotonically increasing ID allocator.
Definition MonotonicIdAllocator.hpp:52
void push(T item, int priority=0)
Pushes a single value into the queue.
Definition PriorityStableQueue.hpp:165
void pushRange(std::span< T > items, int priority=0)
Pushes a range of values into the queue with a shared priority.
Definition PriorityStableQueue.hpp:177
size_type size() const
Returns the current number of elements in the queue.
Definition PriorityStableQueue.hpp:290
std::optional< T > pop(std::stop_token stopToken={})
Removes and returns the highest priority element (blocking).
Definition PriorityStableQueue.hpp:215
PriorityStableQueue()=default
Default constructor.
bool empty() const
Checks if the queue is empty.
Definition PriorityStableQueue.hpp:283
std::vector< T > drain()
Removes all elements from the queue and returns them in priority order.
Definition PriorityStableQueue.hpp:249
void clear()
Removes all elements from the queue.
Definition PriorityStableQueue.hpp:276
std::optional< T > tryPop()
Attempts to remove and return the highest priority element (non-blocking).
Definition PriorityStableQueue.hpp:235
Concept for a priority extractor.
Definition PriorityStableQueue.hpp:30
Definition SystemConfigCommandHandler.hpp:15