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{
12
13
15template <typename T, typename P = std::less<T>>
17{
18public:
19 using value_type = T;
20 using Container = std::vector<T>;
21 using size_type = typename Container::size_type;
22
25 explicit PriorityQueue( const P& pred ) : pred_( pred ) {}
26
28 explicit PriorityQueue( const P& pred, Container&& v MR_LIFETIMEBOUND_NESTED );
29
31 bool empty() const { return c.empty(); }
32
34 size_type size() const { return c.size(); }
35
37 const T & top() const MR_LIFETIMEBOUND { return c.front(); }
38
40 void push( const value_type& value MR_LIFETIME_CAPTURE_BY_NESTED(this) ) { c.push_back( value ); onPush_(); }
41 void push( value_type&& value MR_LIFETIME_CAPTURE_BY_NESTED(this) ) { c.push_back( std::move( value ) ); onPush_(); }
42 template< class... Args >
43 void emplace( Args&&... args ) { c.emplace_back( std::forward<Args>(args)... ); onPush_(); }
44
46 void pop() { std::pop_heap( c.begin(), c.end(), pred_ ); c.pop_back(); }
47
51
52private:
53 void onPush_() { std::push_heap( c.begin(), c.end(), pred_ ); };
54
55private:
57};
58
59template <typename T, typename P>
60PriorityQueue<T, P>::PriorityQueue( const P& pred, Container&& v ) : c( std::move( v ) ), pred_( pred )
61{
63 std::make_heap( c.begin(), c.end(), pred_ );
64}
65
66}
#define MR_LIFETIMEBOUND
Definition MRMacros.h:81
#define MR_LIFETIME_CAPTURE_BY_NESTED(x)
Definition MRMacros.h:86
#define MR_LIFETIMEBOUND_NESTED
Definition MRMacros.h:84
#define MR_NO_UNIQUE_ADDRESS
Definition MRMacros.h:42
#define MR_TIMER
namespace MR
Definition MRTimer.h:56
similar to std::priority_queue, but with ability to access underlying vector to custom modify its ele...
Definition MRPriorityQueue.h:17
PriorityQueue(const P &pred, Container &&v MR_LIFETIMEBOUND_NESTED)
initializes queue elements from given vector
bool empty() const
checks if the queue has no elements
Definition MRPriorityQueue.h:31
void emplace(Args &&... args)
Definition MRPriorityQueue.h:43
void push(const value_type &value MR_LIFETIME_CAPTURE_BY_NESTED(this))
inserts element in the queue
Definition MRPriorityQueue.h:40
const T & top() const MR_LIFETIMEBOUND
accesses the top element
Definition MRPriorityQueue.h:37
T value_type
Definition MRPriorityQueue.h:19
Container c
Definition MRPriorityQueue.h:50
typename Container::size_type size_type
Definition MRPriorityQueue.h:21
PriorityQueue()
constructs empty queue
Definition MRPriorityQueue.h:24
size_type size() const
returns the number of elements
Definition MRPriorityQueue.h:34
void pop()
removes the top element from the priority queue
Definition MRPriorityQueue.h:46
std::vector< T > Container
Definition MRPriorityQueue.h:20
PriorityQueue(const P &pred)
Definition MRPriorityQueue.h:25
void push(value_type &&value MR_LIFETIME_CAPTURE_BY_NESTED(this))
Definition MRPriorityQueue.h:41
only for bindings generation
Definition MRCameraOrientationPlugin.h:8