Added method "sort(bool)" to sort the element of the queue in ascending or descending order
This commit is contained in:
parent
65336cfe7b
commit
0491ceedeb
|
|
@ -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
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue