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,109 +21,142 @@
* *
****************************************************************************/
#ifndef _PriorityQueue_h_
#define _PriorityQueue_h_
#ifndef _PRIORITYQUEUE_H_
#define _PRIORITYQUEUE_H_
/** Implements a bounded-size max priority queue using a heap
*/
template <typename Index, typename Weight>
class HeapMaxPriorityQueue
{
struct Element
#include <algorithm>
namespace vcg {
/** Implements a bounded-size max priority queue using a heap
*/
template <typename Index, typename Weight>
class HeapMaxPriorityQueue
{
struct Element
{
Weight weight;
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)
{
mElements = 0;
mMaxSize = 0;
}
~HeapMaxPriorityQueue()
{
if (mElements)
delete[] mElements;
}
inline void setMaxSize(int maxSize)
{
if (mMaxSize!=maxSize)
{
mMaxSize = maxSize;
delete[] mElements;
mElements = new Element[mMaxSize];
mpOffsetedElements = (mElements-1);
}
init();
}
inline void init() { mCount = 0; }
inline bool isFull() const { return mCount == mMaxSize; }
/** returns number of elements inserted in queue
*/
inline int getNofElements() const { return mCount; }
inline Weight getWeight(int i) const { return mElements[i].weight; }
inline Index getIndex(int i) const { return mElements[i].index; }
inline Weight getTopWeight() const { return mElements[0].weight; }
inline void insert(Index index, Weight weight)
{
if (mCount==mMaxSize)
{
if (weight<mElements[0].weight)
{
register int j, k;
j = 1;
k = 2;
while (k <= mMaxSize)
{
Element* z = &(mpOffsetedElements[k]);
if ((k < mMaxSize) && (z->weight < mpOffsetedElements[k+1].weight))
z = &(mpOffsetedElements[++k]);
if(weight >= z->weight)
break;
mpOffsetedElements[j] = *z;
j = k;
k = 2 * j;
}
mpOffsetedElements[j].weight = weight;
mpOffsetedElements[j].index = index;
}
}
else
{
int i, j;
i = ++mCount;
while (i >= 2)
{
j = i >> 1;
Element& y = mpOffsetedElements[j];
if(weight <= y.weight)
break;
mpOffsetedElements[i] = y;
i = j;
}
mpOffsetedElements[i].index = index;
mpOffsetedElements[i].weight = weight;
}
}
inline void sort(bool ascending = true)
{
if (ascending)
std::sort(mElements, mElements + mCount, lessElement);
else
std::sort(mElements, mElements + mCount, greaterElement);
}
protected:
int mCount;
int mMaxSize;
Element* mElements;
Element* mpOffsetedElements;
};
public:
HeapMaxPriorityQueue(void)
{
mElements = 0;
mMaxSize = 0;
}
~HeapMaxPriorityQueue()
{
if (mElements)
delete[] mElements;
}
inline void setMaxSize(int maxSize)
{
if (mMaxSize!=maxSize)
{
mMaxSize = maxSize;
delete[] mElements;
mElements = new Element[mMaxSize];
mpOffsetedElements = (mElements-1);
}
init();
}
inline void init() { mCount = 0; }
inline bool isFull() const { return mCount == mMaxSize; }
/** returns number of elements inserted in queue
*/
inline int getNofElements() const { return mCount; }
inline Weight getWeight(int i) const { return mElements[i].weight; }
inline Index getIndex(int i) const { return mElements[i].index; }
inline Weight getTopWeight() const { return mElements[0].weight; }
inline void insert(Index index, Weight weight)
{
if (mCount==mMaxSize)
{
if (weight<mElements[0].weight)
{
register int j, k;
j = 1;
k = 2;
while (k <= mMaxSize)
{
Element* z = &(mpOffsetedElements[k]);
if ((k < mMaxSize) && (z->weight < mpOffsetedElements[k+1].weight))
z = &(mpOffsetedElements[++k]);
if(weight >= z->weight)
break;
mpOffsetedElements[j] = *z;
j = k;
k = 2 * j;
}
mpOffsetedElements[j].weight = weight;
mpOffsetedElements[j].index = index;
}
}
else
{
int i, j;
i = ++mCount;
while (i >= 2)
{
j = i >> 1;
Element& y = mpOffsetedElements[j];
if(weight <= y.weight)
break;
mpOffsetedElements[i] = y;
i = j;
}
mpOffsetedElements[i].index = index;
mpOffsetedElements[i].weight = weight;
}
}
protected:
int mCount;
int mMaxSize;
Element* mElements;
Element* mpOffsetedElements;
};
}
#endif