Added method "sort(bool)" to sort the element of the queue in ascending or descending order

This commit is contained in:
Gianpaolo Palma 2014-07-11 11:35:15 +00:00
parent 65336cfe7b
commit 0491ceedeb
1 changed files with 132 additions and 99 deletions

View File

@ -21,8 +21,12 @@
* * * *
****************************************************************************/ ****************************************************************************/
#ifndef _PriorityQueue_h_ #ifndef _PRIORITYQUEUE_H_
#define _PriorityQueue_h_ #define _PRIORITYQUEUE_H_
#include <algorithm>
namespace vcg {
/** Implements a bounded-size max priority queue using a heap /** Implements a bounded-size max priority queue using a heap
*/ */
@ -35,6 +39,26 @@ class HeapMaxPriorityQueue
Index index; Index index;
}; };
struct
{
bool operator()(const Element& a, const Element& b) const
{
return a.weight < b.weight;
}
} lessElement;
struct
{
bool operator()(const Element& a, const Element& b) const
{
return a.weight > b.weight;
}
} greaterElement;
public: public:
HeapMaxPriorityQueue(void) HeapMaxPriorityQueue(void)
@ -118,6 +142,14 @@ public:
} }
} }
inline void sort(bool ascending = true)
{
if (ascending)
std::sort(mElements, mElements + mCount, lessElement);
else
std::sort(mElements, mElements + mCount, greaterElement);
}
protected: protected:
int mCount; int mCount;
@ -126,4 +158,5 @@ protected:
Element* mpOffsetedElements; Element* mpOffsetedElements;
}; };
}
#endif #endif