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 "MRVector.h"
5#include "MRTimer.h"
6#include <vector>
7#include <algorithm>
8
9namespace MR
10{
13
14
16template <typename T, typename P = std::less<T>>
18{
19public:
20 using value_type = T;
23
26 explicit PriorityQueue( const P& pred ) : pred_( pred ) {}
27
29 explicit PriorityQueue( const P& pred, Container&& v MR_LIFETIMEBOUND_NESTED );
30
32 bool empty() const { return c.empty(); }
33
35 size_type size() const { return c.size(); }
36
38 const T & top() const MR_LIFETIMEBOUND { return c.front(); }
39
41 void push( const value_type& value MR_LIFETIME_CAPTURE_BY_NESTED(this) ) { c.push_back( value ); onPush_(); }
42 void push( value_type&& value MR_LIFETIME_CAPTURE_BY_NESTED(this) ) { c.push_back( std::move( value ) ); onPush_(); }
43 template< class... Args >
44 void emplace( Args&&... args ) { c.emplace_back( std::forward<Args>(args)... ); onPush_(); }
45
47 void pop() { std::pop_heap( c.begin(), c.end(), pred_ ); c.pop_back(); }
48
52
53private:
54 void onPush_() { std::push_heap( c.begin(), c.end(), pred_ ); };
55
56private:
58};
59
60template <typename T, typename P>
61PriorityQueue<T, P>::PriorityQueue( const P& pred, Container&& v ) : c( std::move( v ) ), pred_( pred )
62{
64 std::make_heap( c.begin(), c.end(), pred_ );
65}
66
67}
#define MR_LIFETIMEBOUND
Definition MRMacros.h:86
#define MR_LIFETIME_CAPTURE_BY_NESTED(x)
Definition MRMacros.h:91
#define MR_LIFETIMEBOUND_NESTED
Definition MRMacros.h:89
#define MR_NO_UNIQUE_ADDRESS
Definition MRMacros.h:40
#define MR_TIMER
Definition MRTimer.h:60
Vector<T> is indexed by size_t.
Definition MRVector.h:24
bool empty() const
checks if the queue has no elements
Definition MRPriorityQueue.h:32
void emplace(Args &&... args)
Definition MRPriorityQueue.h:44
void push(const value_type &value MR_LIFETIME_CAPTURE_BY_NESTED(this))
inserts element in the queue
Definition MRPriorityQueue.h:41
T value_type
Definition MRPriorityQueue.h:20
Container c
Definition MRPriorityQueue.h:51
typename Container::size_type size_type
Definition MRPriorityQueue.h:22
typename StdVector::size_type size_type
Definition MRVector.h:30
PriorityQueue()
constructs empty queue
Definition MRPriorityQueue.h:25
size_type size() const
returns the number of elements
Definition MRPriorityQueue.h:35
Vector< T > Container
Definition MRPriorityQueue.h:21
void pop()
removes the top element from the priority queue
Definition MRPriorityQueue.h:47
PriorityQueue(const P &pred)
Definition MRPriorityQueue.h:26
void push(value_type &&value MR_LIFETIME_CAPTURE_BY_NESTED(this))
Definition MRPriorityQueue.h:42
const T & top() const
accesses the top element
Definition MRPriorityQueue.h:38
Only strictly necessary for the bindings (to make MR::MeshIntersectionResult a complete type).
Definition MRCameraOrientationPlugin.h:8