Grafalgo
Library of useful data structures and algorithms
|
00001 00009 #ifndef DHEAPSET_H 00010 #define DHEAPSET_H 00011 00012 #include <list> 00013 #include "stdinc.h" 00014 #include "Adt.h" 00015 00016 namespace grafalgo { 00017 00018 typedef int keytyp; 00019 00065 class DheapSet : public Adt { 00066 public: DheapSet(int,int,int=8); 00067 ~DheapSet(); 00068 00069 // common methods 00070 void clear(); 00071 void resize(int,int,int=8); 00072 void resize(int size) { resize(size,size,8); } 00073 void expand(int,int,int=8); 00074 void expand(int size) { expand(size,max(size,maxHeap),d); } 00075 void copyFrom(const DheapSet&); 00076 00077 // access methods 00078 index findMin(int) const; 00079 keytyp getKey(index) const; 00080 int heapSize(int) const; 00081 00082 // predicates 00083 bool empty(int) const; 00084 00085 // modifiers 00086 bool insert(index, keytyp, int); 00087 index deleteMin(int); 00088 void changeKeyMin(keytyp, int); 00089 00090 string& toString(int, string&) const; 00091 string& toString(string&) const; 00092 private: 00093 int maxHeap; 00094 int d = 8; 00095 int numNodes; 00096 00097 index *heaps; 00098 keytyp *key; 00099 00100 int *root; 00101 int *bot; 00102 int *hSize; 00103 00104 int *child; 00105 int *parent; 00106 int *pred; 00107 00108 int free; 00109 00110 index nodeMinPos(int) const; 00111 void siftup(index,int); 00112 void siftdown(index,int); 00113 00114 void makeSpace(int,int,int=8); 00115 void freeSpace(); 00116 }; 00117 00118 inline int DheapSet::nodeMinPos(int p) const { 00119 if (p == -1 || heaps[p] == 0) return -1; 00120 int minPos = p; 00121 for (int q = p+1; q < p+d && heaps[q] != 0; q++) 00122 if (key[heaps[q]] < key[heaps[minPos]]) 00123 minPos = q; 00124 return minPos; 00125 } 00126 00127 // Return item at top of heap 00128 inline int DheapSet::findMin(int h) const { 00129 if (hSize[h] == 0) return 0; 00130 int p = nodeMinPos(root[h]); 00131 return (p < 0 ? 0 : heaps[p]); 00132 } 00133 00134 // Return key of i. 00135 inline keytyp DheapSet::getKey(index i) const { return key[i]; } 00136 00137 // Return true if heap is empty, else false. 00138 inline bool DheapSet::empty(int h) const { return hSize[h] == 0; }; 00139 00140 // Return the size of a heap. 00141 inline int DheapSet::heapSize(int h) const { return hSize[h]; }; 00142 00143 } // ends namespace 00144 00145 #endif