diff --git a/vcg/space/index/kdtree/priorityqueue.h b/vcg/space/index/kdtree/priorityqueue.h index 956dc54f..b5bf1c38 100755 --- a/vcg/space/index/kdtree/priorityqueue.h +++ b/vcg/space/index/kdtree/priorityqueue.h @@ -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 -class HeapMaxPriorityQueue -{ - struct Element +#include + +namespace vcg { + + /** Implements a bounded-size max priority queue using a heap + */ + template + 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 (weightweight < 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 (weightweight < 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