2 #ifndef OPENGM_PRIORITY_QUEUE_HXX
3 #define OPENGM_PRIORITY_QUEUE_HXX
19 FiFoQueue(
const size_t maxSize) : begin_(0), end_(0), maxSize_(maxSize)
21 data_.resize(maxSize);
49 return end_+maxSize_-1-begin_;
65 template<
class T,
class COMPARE = std::less<T> >
84 priorities_(maxSize_+1)
86 for(
int i = 0; i <= maxSize_; i++)
92 for(
typename std::vector<T>::iterator it = priorities_.begin(); it !=priorities_.end(); ++it)
99 for(
size_t i=0; i<=currentSize_; ++i)
100 indices_[heap_[i]] = -1;
106 return currentSize_ == 0;
111 return indices_[i] != -1;
126 void push(
const value_type i,
const priority_type p) {
129 indices_[i] = currentSize_;
130 heap_[currentSize_] = i;
132 bubbleUp(currentSize_);
141 const_reference
top()
const {
148 return priorities_[heap_[1]];
154 const int min = heap_[1];
155 swapItems(1, currentSize_--);
158 heap_[currentSize_+1] = -1;
163 return priorities_[i];
168 int ind = indices_[i];
169 swapItems(ind, currentSize_--);
179 if(_gt(p,priorities_[i])){
181 bubbleDown(indices_[i]);
183 else if(_lt(p,priorities_[i])) {
185 bubbleUp(indices_[i]);
191 void swapItems(
const int i,
const int j) {
192 std::swap(heap_[i],heap_[j]);
193 indices_[heap_[i]] = i;
194 indices_[heap_[j]] = j;
197 void bubbleUp(
int k) {
198 while(k > 1 && _gt( priorities_[heap_[k/2]],priorities_[heap_[k]])) {
204 void bubbleDown(
int k) {
206 while(2*k <= currentSize_) {
208 if(j < currentSize_ && _gt(priorities_[heap_[j]] , priorities_[heap_[j+1]]) )
210 if( _leqt(priorities_[heap_[k]] , priorities_[heap_[j]]))
218 bool _lt(
const T & a,
const T & b)
const{
221 bool _leqt(
const T & a,
const T & b)
const{
224 bool _eq(
const T & a,
const T & b)
const{
225 return !comp_(a,b) && !comp_(b,a);
227 bool _gt(
const T & a,
const T & b)
const{
228 return !_eq(a,b) && !comp_(a,b);
230 bool _geqt(
const T & a,
const T & b)
const{
237 std::vector<int> heap_;
238 std::vector<int> indices_;
239 std::vector<T> priorities_;
void reset()
reset heap - priorities are not changed
void pop()
Remove the current top element.
priority_type priority(const value_type i) const
returns the value associated with index i
ValueType const_reference
bool contains(const int i) const
check if i is an index on the PQ
Heap-based changable priority queue with a maximum number of elemements.
void changePriority(const value_type i, const priority_type p)
change priority of a given index. The index must be in the queue! Call push to auto insert / change ...
bool empty() const
check if the PQ is empty
void push(const value_type i, const priority_type p)
Insert a index with a given priority.
priority_type topPriority() const
get top priority
int size() const
return the number of elements in the PQ
ChangeablePriorityQueue(const size_t maxSize)
Create an empty ChangeablePriorityQueue which can contain atmost maxSize elements.
void deleteItem(const value_type i)
deleqte the priority associated with index i
void setPriorities(T newPriority)
set all priorities to the given value
FiFoQueue(const size_t maxSize)
const_reference top() const
get index with top priority