register is deprecated
This commit is contained in:
parent
59ffba5af3
commit
c27a19e3d1
|
@ -28,135 +28,135 @@
|
||||||
|
|
||||||
namespace vcg {
|
namespace vcg {
|
||||||
|
|
||||||
/** Implements a bounded-size max priority queue using a heap
|
/** Implements a bounded-size max priority queue using a heap
|
||||||
*/
|
*/
|
||||||
template <typename Index, typename Weight>
|
template <typename Index, typename Weight>
|
||||||
class HeapMaxPriorityQueue
|
class HeapMaxPriorityQueue
|
||||||
{
|
{
|
||||||
struct Element
|
struct Element
|
||||||
{
|
{
|
||||||
Weight weight;
|
Weight weight;
|
||||||
Index index;
|
Index index;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
struct
|
struct
|
||||||
{
|
{
|
||||||
bool operator()(const Element& a, const Element& b) const
|
bool operator()(const Element& a, const Element& b) const
|
||||||
{
|
{
|
||||||
return a.weight < b.weight;
|
return a.weight < b.weight;
|
||||||
}
|
}
|
||||||
} lessElement;
|
} lessElement;
|
||||||
|
|
||||||
|
|
||||||
struct
|
struct
|
||||||
{
|
{
|
||||||
bool operator()(const Element& a, const Element& b) const
|
bool operator()(const Element& a, const Element& b) const
|
||||||
{
|
{
|
||||||
return a.weight > b.weight;
|
return a.weight > b.weight;
|
||||||
}
|
}
|
||||||
} greaterElement;
|
} greaterElement;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
HeapMaxPriorityQueue(void)
|
HeapMaxPriorityQueue(void)
|
||||||
{
|
{
|
||||||
mElements = 0;
|
mElements = 0;
|
||||||
mMaxSize = 0;
|
mMaxSize = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
~HeapMaxPriorityQueue()
|
~HeapMaxPriorityQueue()
|
||||||
{
|
{
|
||||||
if (mElements)
|
if (mElements)
|
||||||
delete[] mElements;
|
delete[] mElements;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
inline void setMaxSize(int maxSize)
|
inline void setMaxSize(int maxSize)
|
||||||
{
|
{
|
||||||
if (mMaxSize!=maxSize)
|
if (mMaxSize!=maxSize)
|
||||||
{
|
{
|
||||||
mMaxSize = maxSize;
|
mMaxSize = maxSize;
|
||||||
delete[] mElements;
|
delete[] mElements;
|
||||||
mElements = new Element[mMaxSize];
|
mElements = new Element[mMaxSize];
|
||||||
mpOffsetedElements = (mElements-1);
|
mpOffsetedElements = (mElements-1);
|
||||||
}
|
}
|
||||||
init();
|
init();
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void init() { mCount = 0; }
|
inline void init() { mCount = 0; }
|
||||||
|
|
||||||
inline bool isFull() const { return mCount == mMaxSize; }
|
inline bool isFull() const { return mCount == mMaxSize; }
|
||||||
|
|
||||||
/** returns number of elements inserted in queue
|
/** returns number of elements inserted in queue
|
||||||
*/
|
*/
|
||||||
inline int getNofElements() const { return mCount; }
|
inline int getNofElements() const { return mCount; }
|
||||||
|
|
||||||
inline Weight getWeight(int i) const { return mElements[i].weight; }
|
inline Weight getWeight(int i) const { return mElements[i].weight; }
|
||||||
inline Index getIndex(int i) const { return mElements[i].index; }
|
inline Index getIndex(int i) const { return mElements[i].index; }
|
||||||
|
|
||||||
inline Weight getTopWeight() const { return mElements[0].weight; }
|
inline Weight getTopWeight() const { return mElements[0].weight; }
|
||||||
|
|
||||||
inline void insert(Index index, Weight weight)
|
inline void insert(Index index, Weight weight)
|
||||||
{
|
{
|
||||||
if (mCount==mMaxSize)
|
if (mCount==mMaxSize)
|
||||||
{
|
{
|
||||||
if (weight<mElements[0].weight)
|
if (weight<mElements[0].weight)
|
||||||
{
|
{
|
||||||
register int j, k;
|
int j, k;
|
||||||
j = 1;
|
j = 1;
|
||||||
k = 2;
|
k = 2;
|
||||||
while (k <= mMaxSize)
|
while (k <= mMaxSize)
|
||||||
{
|
{
|
||||||
Element* z = &(mpOffsetedElements[k]);
|
Element* z = &(mpOffsetedElements[k]);
|
||||||
if ((k < mMaxSize) && (z->weight < mpOffsetedElements[k+1].weight))
|
if ((k < mMaxSize) && (z->weight < mpOffsetedElements[k+1].weight))
|
||||||
z = &(mpOffsetedElements[++k]);
|
z = &(mpOffsetedElements[++k]);
|
||||||
|
|
||||||
if(weight >= z->weight)
|
if(weight >= z->weight)
|
||||||
break;
|
break;
|
||||||
mpOffsetedElements[j] = *z;
|
mpOffsetedElements[j] = *z;
|
||||||
j = k;
|
j = k;
|
||||||
k = 2 * j;
|
k = 2 * j;
|
||||||
}
|
}
|
||||||
mpOffsetedElements[j].weight = weight;
|
mpOffsetedElements[j].weight = weight;
|
||||||
mpOffsetedElements[j].index = index;
|
mpOffsetedElements[j].index = index;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
int i, j;
|
int i, j;
|
||||||
i = ++mCount;
|
i = ++mCount;
|
||||||
while (i >= 2)
|
while (i >= 2)
|
||||||
{
|
{
|
||||||
j = i >> 1;
|
j = i >> 1;
|
||||||
Element& y = mpOffsetedElements[j];
|
Element& y = mpOffsetedElements[j];
|
||||||
if(weight <= y.weight)
|
if(weight <= y.weight)
|
||||||
break;
|
break;
|
||||||
mpOffsetedElements[i] = y;
|
mpOffsetedElements[i] = y;
|
||||||
i = j;
|
i = j;
|
||||||
}
|
}
|
||||||
mpOffsetedElements[i].index = index;
|
mpOffsetedElements[i].index = index;
|
||||||
mpOffsetedElements[i].weight = weight;
|
mpOffsetedElements[i].weight = weight;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void sort(bool ascending = true)
|
inline void sort(bool ascending = true)
|
||||||
{
|
{
|
||||||
if (ascending)
|
if (ascending)
|
||||||
std::sort(mElements, mElements + mCount, lessElement);
|
std::sort(mElements, mElements + mCount, lessElement);
|
||||||
else
|
else
|
||||||
std::sort(mElements, mElements + mCount, greaterElement);
|
std::sort(mElements, mElements + mCount, greaterElement);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
int mCount;
|
int mCount;
|
||||||
int mMaxSize;
|
int mMaxSize;
|
||||||
Element* mElements;
|
Element* mElements;
|
||||||
Element* mpOffsetedElements;
|
Element* mpOffsetedElements;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue