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_
#define _PriorityQueue_h_
#ifndef _PRIORITYQUEUE_H_
#define _PRIORITYQUEUE_H_
#include <algorithm>
namespace vcg {
/** Implements a bounded-size max priority queue using a heap
*/
@ -35,6 +39,26 @@ class HeapMaxPriorityQueue
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:
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:
int mCount;
@ -126,4 +158,5 @@ protected:
Element* mpOffsetedElements;
};
}
#endif