MeshLib C++ Docs
Loading...
Searching...
No Matches
MRPriorityQueue.h
Go to the documentation of this file.
1#pragma once
2
3#include "MRMacros.h"
4#include "MRTimer.h"
5#include <vector>
6#include <algorithm>
7
8namespace MR
9{
10
12template <typename T, typename P = std::less<T>>
14{
15public:
16 using value_type = T;
17 using Container = std::vector<T>;
18 using size_type = typename Container::size_type;
19
22 explicit PriorityQueue( const P& pred ) : pred_( pred ) {}
23
25 explicit PriorityQueue( const P& pred, Container&& v );
26
28 bool empty() const { return c.empty(); }
29
31 size_type size() const { return c.size(); }
32
34 const T & top() const { return c.front(); }
35
37 void push( const value_type& value ) { c.push_back( value ); onPush_(); }
38 void push( value_type&& value ) { c.push_back( std::move( value ) ); onPush_(); }
39 template< class... Args >
40 void emplace( Args&&... args ) { c.emplace_back( std::forward<Args>(args)... ); onPush_(); }
41
43 void pop() { std::pop_heap( c.begin(), c.end(), pred_ ); c.pop_back(); }
44
48
49private:
50 void onPush_() { std::push_heap( c.begin(), c.end(), pred_ ); };
51
52private:
54};
55
56template <typename T, typename P>
57PriorityQueue<T, P>::PriorityQueue( const P& pred, Container&& v ) : c( std::move( v ) ), pred_( pred )
58{
60 std::make_heap( c.begin(), c.end(), pred_ );
61}
62
63} // namespace MR
#define MR_NO_UNIQUE_ADDRESS
Definition MRMacros.h:37
#define MR_TIMER
Definition MRTimer.h:53
similar to std::priority_queue, but with ability to access underlying vector to custom modify its ele...
Definition MRPriorityQueue.h:14
void push(const value_type &value)
inserts element in the queue
Definition MRPriorityQueue.h:37
bool empty() const
checks if the queue has no elements
Definition MRPriorityQueue.h:28
void emplace(Args &&... args)
Definition MRPriorityQueue.h:40
T value_type
Definition MRPriorityQueue.h:16
Container c
Definition MRPriorityQueue.h:47
typename Container::size_type size_type
Definition MRPriorityQueue.h:18
PriorityQueue()
constructs empty queue
Definition MRPriorityQueue.h:21
size_type size() const
returns the number of elements
Definition MRPriorityQueue.h:31
void push(value_type &&value)
Definition MRPriorityQueue.h:38
void pop()
removes the top element from the priority queue
Definition MRPriorityQueue.h:43
std::vector< T > Container
Definition MRPriorityQueue.h:17
PriorityQueue(const P &pred)
Definition MRPriorityQueue.h:22
const T & top() const
accesses the top element
Definition MRPriorityQueue.h:34