source: GTP/trunk/Lib/Vis/Preprocessing/src/MyHeap.h @ 1099

Revision 1099, 1.9 KB checked in by mattausch, 18 years ago (diff)
Line 
1#ifndef MYHEAP_H
2#define MYHEAP_H
3
4#include <vector>
5
6/** This class implementas a heap  for
7        use as a priority queue.
8
9        The class aditionally implements efficient remove
10        of arbitrary elements.
11*/
12
13namespace GtpVisibilityPreprocessor {
14
15const static int NOT_IN_HEAP = -100;
16
17class Heapable
18{
19public:
20    Heapable() { NotInHeap(); HeapKey(0.0f); }
21
22    inline bool IsInHeap() { return mPosition != NOT_IN_HEAP; }
23    inline void NotInHeap() { mPosition = NOT_IN_HEAP; }
24    inline int GetHeapPos() { return mPosition; }
25    inline void SetHeapPos(int t) { mPosition = t; }
26
27    inline void  HeapKey(float k) { mPriority = k; }
28    inline float HeapKey() const  { return mPriority; }
29
30protected:
31
32    float mPriority;
33    int mPosition;
34};
35
36
37class MyHeap
38{
39public:
40    MyHeap() { }
41    MyHeap(unsigned int n) : mBuffer(n) { }
42
43    void Push(Heapable *t) { Push(t, t->HeapKey()); }
44    void Push(Heapable *, float);
45    bool Update(Heapable *t) { return Update(t, t->HeapKey()); }
46    bool Update(Heapable *, float);
47
48    unsigned int Size() const { return (unsigned int)mBuffer.size(); }
49        bool Empty() const {return mBuffer.empty(); }
50    Heapable       *Item(unsigned int i)       { return mBuffer[i]; }
51    const Heapable *Item(unsigned int i) const { return mBuffer[i]; }
52    Heapable *Pop();
53        Heapable *Top() { return (mBuffer.empty() ? (Heapable *)NULL : mBuffer.front()); }
54    Heapable *Erase(Heapable *);
55
56protected:
57
58    void Place(Heapable *x, unsigned int i);
59    void Swap(unsigned int i, unsigned int j);
60
61    unsigned int Parent(unsigned int i) { return (i-1)/2; }
62    unsigned int Left(unsigned int i) { return 2*i+1; }
63    unsigned int Right(unsigned int i) { return 2*i+2; }
64
65    void UpHeap(unsigned int i);
66    void DownHeap(unsigned int i);
67
68        std::vector<Heapable* > mBuffer;
69};
70
71}
72
73// MYHEAP_H
74#endif
Note: See TracBrowser for help on using the repository browser.