1 #ifndef TERMOX_COMMON_PRIORITY_QUEUE_HPP
2 #define TERMOX_COMMON_PRIORITY_QUEUE_HPP
7 #include <termox/common/lockable.hpp>
13 template <
typename T,
typename Priority = std::u
int8_t>
16 using Priority_t = Priority;
20 [[nodiscard]]
auto top() const -> T const& {
return queue_.top().second; }
23 [[nodiscard]]
auto is_empty() const ->
bool {
return queue_.empty(); }
26 [[nodiscard]]
auto size() const -> std::
size_t {
return queue_.size(); }
29 void push(Priority p, T
const& x) { queue_.push(std::pair{p, x}); }
32 void push(Priority p, T&& x) { queue_.push(std::pair{p, std::move(x)}); }
37 auto result = std::move(
const_cast<T&
>(this->
top()));
39 return std::move(result);
43 using Value_t = std::pair<Priority, T>;
48 [[nodiscard]]
auto operator()(Value_t
const& x, Value_t
const& y)
const
51 return x.first < y.first;
56 using Queue_t = std::priority_queue<Value_t, std::vector<Value_t>, Compare>;
Wraps std::priority_queue to attach an int priority type to stored objects.
Definition: priority_queue.hpp:14
auto top() const -> T const &
Returns a const reference to the element with the highest priority.
Definition: priority_queue.hpp:20
auto size() const -> std::size_t
Returns the number of elements currently in the queue.
Definition: priority_queue.hpp:26
void push(Priority p, T &&x)
Move an element into the queue with the given priority.
Definition: priority_queue.hpp:32
void push(Priority p, T const &x)
Copy an element into the queue with the given priority.
Definition: priority_queue.hpp:29
auto pop() -> T
Moves top element from the queue and returns it, shrinking the queue.
Definition: priority_queue.hpp:35
auto is_empty() const -> bool
Returns true if no elements in queue.
Definition: priority_queue.hpp:23