00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifndef __UT_PriorityQueue_h__
00027 #define __UT_PriorityQueue_h__
00028
00029 #include "UT_RefArray.h"
00030
00031
00032 template <class utPtr, class utCompare, bool need_changed_position>
00033 class UT_PriorityQueue
00034 {
00035
00036 public:
00037
00038 explicit UT_PriorityQueue(unsigned int sz=0)
00039 : myArray(sz)
00040 { }
00041 virtual ~UT_PriorityQueue() {}
00042
00043
00044
00045 UT_PriorityQueue(const UT_PriorityQueue<utPtr, utCompare,
00046 need_changed_position> &a) : myArray(a.myArray) { }
00047
00048 unsigned int insert(utPtr t)
00049 {
00050 unsigned int temp;
00051 temp = myArray.append(t);
00052 if (need_changed_position)
00053 changedPosition( myArray( temp ), temp );
00054 return bubbleUp( temp );
00055 }
00056
00057
00058
00059 void destroy(unsigned int entry)
00060 {
00061 utPtr ptr = myArray(entry);
00062 remove( entry );
00063 delete ptr;
00064 }
00065
00066 void remove(unsigned int entry)
00067 {
00068 if( myArray.entries()>1 )
00069 {
00070 myArray( entry ) = myArray.last();
00071 if (need_changed_position)
00072 changedPosition( myArray( entry ), entry );
00073 myArray.entries( myArray.entries()-1 );
00074 bubbleDown( entry );
00075 }
00076 else
00077 {
00078 myArray.entries( 0 );
00079 }
00080 }
00081
00082 unsigned int entries(void) const { return myArray.entries(); }
00083
00084 int64 getMemoryUsage() const
00085 { return sizeof(*this) + myArray.getMemoryUsage(); }
00086
00087 unsigned short isEmpty(void) const { return myArray.isEmpty(); }
00088
00089
00090 utPtr head() const { return myArray(0); };
00091
00092
00093
00094
00095 utPtr getEntry(int idx) const { return myArray(idx); };
00096
00097
00098
00099
00100
00101
00102
00103 virtual void changedPosition( utPtr ,
00104 unsigned int ) const
00105 { }
00106
00107 unsigned int bubbleDown( unsigned int entry )
00108 {
00109 utCompare comp;
00110 for( ;; )
00111 {
00112 unsigned int l = leftChild( entry );
00113 unsigned int r = l+1;
00114 unsigned int newplace;
00115
00116 if( nodeExists( r ) )
00117 {
00118 if( comp.isLess(myArray(entry), myArray(l)))
00119 {
00120 if (comp.isLess(myArray(l), myArray(r)))
00121 newplace = r;
00122 else
00123 newplace = l;
00124 }
00125 else if (comp.isLess(myArray(entry),
00126 myArray(r)))
00127 {
00128 newplace = r;
00129 }
00130 else break;
00131
00132 }
00133 else if( nodeExists( l ) )
00134 {
00135 if (comp.isLess(myArray(entry), myArray(l)))
00136 newplace = l;
00137 else break;
00138 }
00139 else
00140 {
00141 break;
00142 }
00143
00144 swap( entry, newplace );
00145 entry = newplace;
00146 }
00147 return entry;
00148 }
00149
00150 unsigned int bubbleUp( unsigned int entry )
00151 {
00152 utCompare comp;
00153 while( entry )
00154 {
00155 int nextentry = parent( entry );
00156 if (!comp.isLess(myArray(entry),
00157 myArray(nextentry)))
00158 {
00159 swap( entry, nextentry );
00160 entry = nextentry;
00161 }
00162 else
00163 {
00164 break;
00165 }
00166 }
00167 return entry;
00168 }
00169
00170 private:
00171 static unsigned int parent( unsigned int node )
00172 {
00173 return (node-1)>>1;
00174 }
00175
00176 static unsigned int leftChild( unsigned int node )
00177 {
00178 return (node<<1) + 1;
00179 }
00180
00181 static unsigned int rightChild( unsigned int node )
00182 {
00183 return (node<<1) + 2;
00184 }
00185
00186 bool nodeExists( unsigned int node )
00187 {
00188 return node < myArray.entries();
00189 }
00190
00191 void swap( unsigned int a, unsigned int b )
00192 {
00193 utPtr temp = myArray( a );
00194 myArray( a ) = myArray( b );
00195 myArray( b ) = temp;
00196
00197 if (need_changed_position)
00198 {
00199 changedPosition( myArray( a ), a );
00200 changedPosition( myArray( b ), b );
00201 }
00202 }
00203
00204 protected:
00205
00206
00207
00208
00209 UT_RefArray< utPtr > myArray;
00210 };
00211
00212
00213 #endif