37 #ifndef VIGRA_PRIORITY_QUEUE_HXX
38 #define VIGRA_PRIORITY_QUEUE_HXX
42 #include "array_vector.hxx"
67 template <
class ValueType,
68 bool Ascending =
false>
77 typedef ValueType value_type;
78 typedef ValueType & reference;
79 typedef ValueType
const & const_reference;
80 typedef std::size_t size_type;
81 typedef std::ptrdiff_t priority_type;
87 : buckets_(bucket_count),
111 return (priority_type)buckets_.
size() - 1;
123 const_reference
top()
const
126 return buckets_[top_].
front();
134 buckets_[top_].pop();
136 while(top_ > 0 && buckets_[top_].
size() == 0)
142 void push(value_type
const & v, priority_type priority)
145 buckets_[priority].push(v);
152 template <
class ValueType>
153 class BucketQueue<ValueType, true>
155 ArrayVector<std::queue<ValueType> > buckets_;
161 typedef ValueType value_type;
162 typedef ValueType & reference;
163 typedef ValueType
const & const_reference;
164 typedef std::size_t size_type;
165 typedef std::ptrdiff_t priority_type;
168 : buckets_(bucket_count),
169 size_(0), top_((priority_type)bucket_count)
172 size_type
size()
const
184 return (priority_type)buckets_.
size() - 1;
192 const_reference
top()
const
195 return buckets_[top_].
front();
201 buckets_[top_].pop();
203 while(top_ < (priority_type)buckets_.
size() && buckets_[top_].
size() == 0)
207 void push(value_type
const & v, priority_type priority)
210 buckets_[priority].push(v);
229 template <
class ValueType,
230 class PriorityFunctor,
231 bool Ascending =
false>
235 PriorityFunctor get_priority_;
240 typedef typename BaseType::value_type value_type;
241 typedef typename BaseType::reference reference;
242 typedef typename BaseType::const_reference const_reference;
243 typedef typename BaseType::size_type size_type;
244 typedef typename BaseType::priority_type priority_type;
252 PriorityFunctor
const & priority = PriorityFunctor())
254 get_priority_(priority)
264 void push(value_type
const & v)
266 priority_type index = get_priority_(v);
287 template <
class ValueType,
289 bool Ascending =
false>
292 typedef std::pair<ValueType, PriorityType> ElementType;
296 typename IfBool<Ascending, std::greater<PriorityType>,
297 std::less<PriorityType> >::type cmp;
299 bool operator()(ElementType
const & l, ElementType
const & r)
const
301 return cmp(l.second, r.second);
305 typedef std::priority_queue<ElementType, std::vector<ElementType>, Compare> Heap;
311 typedef ValueType value_type;
312 typedef ValueType & reference;
313 typedef ValueType
const & const_reference;
314 typedef typename Heap::size_type size_type;
315 typedef PriorityType priority_type;
343 return NumericTraits<priority_type>::max();
350 return heap_.top().second;
355 const_reference
top()
const
358 return heap_.top().first;
370 void push(value_type
const & v, priority_type priority)
372 heap_.push(ElementType(v, priority));
376 template <
class ValueType,
378 class PriorityQueue<ValueType, unsigned char, Ascending>
379 :
public BucketQueue<ValueType, Ascending>
382 typedef BucketQueue<ValueType, Ascending> BaseType;
385 : BaseType(NumericTraits<unsigned char>::max()+1)
389 template <
class ValueType,
391 class PriorityQueue<ValueType, unsigned short, Ascending>
392 :
public BucketQueue<ValueType, Ascending>
395 typedef BucketQueue<ValueType, Ascending> BaseType;
398 : BaseType(NumericTraits<unsigned short>::max()+1)
412 template<
class T,
class COMPARE = std::less<T> >
418 typedef T priority_type;
419 typedef int ValueType;
420 typedef ValueType value_type;
421 typedef ValueType const_reference;
430 indices_(maxSize_+1, -1),
431 priorities_(maxSize_+1)
433 for(
unsigned i = 0; i <= maxSize_; i++)
440 for(
int i = 0; i <= maxSize_; i++)
445 return currentSize_ == 0;
450 for(
int i = 0; i < currentSize_; i++)
452 indices_[heap_[i+1]] = -1;
460 return indices_[i] != -1;
475 void push(
const value_type i,
const priority_type p) {
478 indices_[i] = currentSize_;
479 heap_[currentSize_] = i;
481 bubbleUp(currentSize_);
490 const_reference
top()
const {
497 return priorities_[heap_[1]];
503 const int min = heap_[1];
504 swapItems(1, currentSize_--);
507 heap_[currentSize_+1] = -1;
512 return priorities_[i];
517 int ind = indices_[i];
518 swapItems(ind, currentSize_--);
528 if(_gt(p,priorities_[i])){
530 bubbleDown(indices_[i]);
532 else if(_lt(p,priorities_[i])) {
534 bubbleUp(indices_[i]);
540 void swapItems(
const int i,
const int j) {
541 std::swap(heap_[i],heap_[j]);
542 indices_[heap_[i]] = i;
543 indices_[heap_[j]] = j;
546 void bubbleUp(
int k) {
547 while(k > 1 && _gt( priorities_[heap_[k/2]],priorities_[heap_[k]])) {
553 void bubbleDown(
int k) {
555 while(static_cast<unsigned>(2*k) <= currentSize_) {
557 if(static_cast<unsigned>(j) < currentSize_ && _gt(priorities_[heap_[j]] , priorities_[heap_[j+1]]) )
559 if( _leqt(priorities_[heap_[k]] , priorities_[heap_[j]]))
567 bool _lt(
const T & a,
const T & b)
const{
570 bool _leqt(
const T & a,
const T & b)
const{
573 bool _eq(
const T & a,
const T & b)
const{
574 return !comp_(a,b) && !comp_(b,a);
576 bool _gt(
const T & a,
const T & b)
const{
577 return !_eq(a,b) && !comp_(a,b);
579 bool _geqt(
const T & a,
const T & b)
const{
586 std::vector<int> heap_;
587 std::vector<int> indices_;
588 std::vector<T> priorities_;
596 #endif // VIGRA_PRIORITY_QUEUE_HXX
bool empty() const
Queue contains no elements. Equivalent to size() == 0.
Definition: priority_queue.hxx:333
void clear()
check if the PQ is empty
Definition: priority_queue.hxx:449
bool empty() const
check if the PQ is empty
Definition: priority_queue.hxx:444
priority_type maxIndex() const
Maximum index (i.e. priority) allowed in this queue. Equivalent to bucket_count - 1...
Definition: priority_queue.hxx:109
Priority queue implemented using bucket sort.
Definition: priority_queue.hxx:69
size_type size() const
Number of elements in this queue.
Definition: priority_queue.hxx:93
priority_type topPriority() const
get top priority
Definition: priority_queue.hxx:496
void pop()
Remove the current top element.
Definition: priority_queue.hxx:502
Heap-based priority queue compatible to BucketQueue.
Definition: priority_queue.hxx:290
void push(value_type const &v)
Insert new element.
Definition: priority_queue.hxx:264
const_reference top() const
The current top element.
Definition: priority_queue.hxx:123
priority_type topPriority() const
Priority of the current top element.
Definition: priority_queue.hxx:348
bool contains(const int i) const
check if i is an index on the PQ
Definition: priority_queue.hxx:459
BucketQueue(size_type bucket_count=256)
Create bucket queue with.
Definition: priority_queue.hxx:86
MappedBucketQueue(unsigned int bucket_count=256, PriorityFunctor const &priority=PriorityFunctor())
Create a queue with.
Definition: priority_queue.hxx:251
void pop()
Remove the current top element.
Definition: priority_queue.hxx:363
void push(value_type const &v, priority_type priority)
Insert new element.
Definition: priority_queue.hxx:142
PriorityQueue()
Create empty priority queue.
Definition: priority_queue.hxx:319
int size() const
return the number of elements in the PQ
Definition: priority_queue.hxx:464
Definition: array_vector.hxx:58
Heap-based changable priority queue with a maximum number of elemements.
Definition: priority_queue.hxx:413
void push(const value_type i, const priority_type p)
Insert a index with a given priority.
Definition: priority_queue.hxx:475
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 ...
Definition: priority_queue.hxx:527
reference front()
Definition: array_vector.hxx:307
priority_type topPriority() const
Priority of the current top element.
Definition: priority_queue.hxx:116
void pop()
Remove the current top element.
Definition: priority_queue.hxx:131
Priority queue implemented using bucket sort (STL compatible).
Definition: priority_queue.hxx:232
void push(value_type const &v, priority_type priority)
Insert new element.
Definition: priority_queue.hxx:370
bool empty() const
Queue contains no elements. Equivalent to size() == 0.
Definition: priority_queue.hxx:101
const_reference top() const
The current top element.
Definition: priority_queue.hxx:355
void deleteItem(const value_type i)
deleqte the priority associated with index i
Definition: priority_queue.hxx:516
ChangeablePriorityQueue(const size_t maxSize)
Create an empty ChangeablePriorityQueue which can contain atmost maxSize elements.
Definition: priority_queue.hxx:426
const_reference top() const
get index with top priority
Definition: priority_queue.hxx:490
priority_type maxIndex() const
Maximum index (i.e. priority) allowed in this queue. Equivalent to bucket_count - 1...
Definition: priority_queue.hxx:341
size_type size() const
Definition: array_vector.hxx:358
priority_type priority(const value_type i) const
returns the value associated with index i
Definition: priority_queue.hxx:511
size_type size() const
Number of elements in this queue.
Definition: priority_queue.hxx:325