Grafalgo
Library of useful data structures and algorithms
/Users/jst/src/grafalgo/cpp/include/HeapSet.h
Go to the documentation of this file.
00001 
00009 #ifndef HEAPSET_H
00010 #define HEAPSET_H
00011 
00012 #include <list>
00013 #include "stdinc.h"
00014 #include "Util.h"
00015 
00016 namespace grafalgo {
00017 
00027 class HeapSet : public Adt {
00028 public:         HeapSet(int,int);
00029                 ~HeapSet();
00030 
00031         // common methods
00032         void    clear();
00033         void    resize(int);
00034         void    expand(int);
00035         void    copyFrom(const List&);
00036 
00037         // access methods
00038         index   findMin(int) const;
00039         uint64_t getKey(index) const;
00040         int     heapSize(int) const;
00041 
00042         // predicates 
00043         bool    empty(int);     
00044 
00045         // modifiers 
00046         bool    insert(index, uint64_t, int);
00047         index   deleteMin(int);
00048         void    changeKeyMin(uint64_t, int);
00049 
00050         string& toString(int, string&) const;
00051 private:
00052         static const int d = 8;         
00053         int     maxHeap;                
00054 
00055         index   *heaps;                 
00056         uint64_t *key;                  
00057 
00058         int     *root;                  
00059         int     *bot;                   
00060         int     *hSize;                 
00061         
00062         int     *child;                 
00063         int     *parent;                
00064         int     *pred;                  
00065         
00066         int     free;                   
00067 
00068         index   nodeMinPos(int) const;  
00069         void    siftup(index,int);      
00070         void    siftdown(index,int);    
00071 };
00072 
00073 inline int HeapSet::nodeMinPos(int p) const {
00074         if (p == -1 || heaps[p] == 0) return -1;
00075         int minPos = p;
00076         for (int q = p+1; q < p+D && heaps[q] != 0; q++)
00077                 if (key[heaps[q]] < key[heaps[minPos]])
00078                         minPos = q;
00079         return minPos;
00080 }
00081 
00082 // Return item at top of heap
00083 inline int HeapSet::findMin(int h) const {
00084         if (hSize[h] == 0) return 0;
00085 if (root[h] == -1)
00086 cerr << "HeapSet::findMin: non-empty heap has root=-1\n";
00087         int p = nodeMinPos(root[h]);
00088 if (p < 0) {
00089 cerr << "HeapSet::findMin: nodeMinPos returned " << p
00090      << " h=" << h << " root[h]=" << root[h] << " heaps[" << root[h] << "]="
00091      << heaps[root[h]] << " hSize[h]=" << hSize[h] << endl;
00092 }
00093         return (p < 0 ? 0 : heaps[p]);
00094 }
00095 
00096 // Return key of i.
00097 inline uint64_t HeapSet::getKey(index i) const { return key[i]; }
00098 
00099 // Return true if heap is empty, else false.
00100 inline bool HeapSet::empty(int h) { return hSize[h] == 0; };
00101 
00102 // Return the size of a heap.
00103 inline int HeapSet::heapSize(int h) const { return hSize[h]; };
00104 
00105 } // ends namespace
00106 
00107 #endif
 All Classes Files Functions Variables Typedefs Friends