#include <tHeap.h>
Public Member Functions | |
tHeapBase () | |
~tHeapBase () | |
Static Public Member Functions | |
static int | UpperL (int i) |
static int | UpperR (int i) |
Protected Member Functions | |
int | Lower (int i) |
int | Len () const |
tHeapElement * | operator() (int i) |
const tHeapElement * | operator() (int i) const |
bool | SwapIf (int i, int j) |
void | SwapDown (int j) |
void | SwapUp (int i) |
void | CheckHeap () |
void | Insert (tHeapElement *e) |
void | Remove (tHeapElement *e) |
tHeapElement * | Remove (int i) |
void | Replace (int i) |
void | Replace (tHeapElement *e) |
Friends | |
class | tHeapElement |
Definition at line 46 of file tHeap.h.
tHeapBase::~tHeapBase | ( | ) |
int tHeapBase::Lower | ( | int | i | ) | [inline, protected] |
Definition at line 49 of file tHeap.h.
Referenced by CheckHeap(), Replace(), and SwapDown().
00049 { // the element below i in the heap 00050 if(i%2==0) // just to make sure what happens; you never know what 1/2 is.. 00051 return i/2-1; 00052 else 00053 return (i-1)/2; 00054 }
int tHeapBase::Len | ( | ) | const [inline, protected] |
Reimplemented from GrowingArrayBase.
Reimplemented in tHeap< T >, tHeap< nBandwidthArbitrator >, and tHeap< tEvent >.
Definition at line 56 of file tHeap.h.
References GrowingArrayBase::Len().
Referenced by CheckHeap(), Insert(), tHeap< tEvent >::Len(), Remove(), Replace(), SwapUp(), and ~tHeapBase().
00056 { return tList<tHeapElement>::Len(); }
tHeapElement* tHeapBase::operator() | ( | int | i | ) | [inline, protected] |
Reimplemented in tHeap< T >, tHeap< nBandwidthArbitrator >, and tHeap< tEvent >.
Definition at line 57 of file tHeap.h.
References tArray< T *, MALLOC >::operator()().
Referenced by CheckHeap(), tHeap< tEvent >::Events(), tHeap< tEvent >::operator()(), Remove(), and SwapIf().
00057 { return const_cast< tHeapElement* >( tList<tHeapElement>::operator()(i) ); }
const tHeapElement* tHeapBase::operator() | ( | int | i | ) | const [inline, protected] |
Reimplemented from tArray< tHeapElement *, false >.
Reimplemented in tHeap< T >, tHeap< nBandwidthArbitrator >, and tHeap< tEvent >.
Definition at line 58 of file tHeap.h.
References tArray< T *, MALLOC >::operator()().
00058 { return tList<tHeapElement>::operator()(i); }
bool tHeapBase::SwapIf | ( | int | i, | |
int | j | |||
) | [protected] |
Definition at line 37 of file tHeap.cpp.
References tHeapElement::hID, operator()(), Swap(), tASSERT, and tHeapElement::Val().
Referenced by SwapDown(), and SwapUp().
00038 { 00039 if (i==j || i<0) return false; // safety 00040 00041 tHeapElement *e1=operator()(i),*e2=operator()(j); 00042 00043 tASSERT( e1->hID == i ); 00044 tASSERT( e2->hID == j ); 00045 00046 if (e1->Val() > e2->Val()){ 00047 Swap(tList< tHeapElement >::operator()(i),tList< tHeapElement >::operator()(j)); 00048 e1->hID=j; 00049 e2->hID=i; 00050 return true; 00051 } 00052 else 00053 return false; 00054 }
void tHeapBase::SwapDown | ( | int | j | ) | [protected] |
Definition at line 79 of file tHeap.cpp.
References CheckHeap(), Lower(), and SwapIf().
Referenced by Insert(), and Replace().
00079 { 00080 int i=j; 00081 // e is now at position i. swap it down 00082 // as far as it goes: 00083 do{ 00084 j=i; 00085 i=Lower(j); 00086 } 00087 while(SwapIf(i,j)); // mean: relies on the fact that SwapIf returns -1 00088 // if i<0. 00089 00090 #ifdef EVENT_DEB 00091 CheckHeap(); 00092 #endif 00093 }
void tHeapBase::SwapUp | ( | int | i | ) | [protected] |
Definition at line 95 of file tHeap.cpp.
References CheckHeap(), Len(), SwapIf(), UpperL(), and UpperR().
Referenced by Replace().
00095 { 00096 #ifdef EVENT_DEB 00097 // static int su=0; 00098 // if (su%100 ==0 ) 00099 // con << "su=" << su << '\n'; 00100 // if (su > 11594 ) 00101 // con << "su=" << su << '\n'; 00102 // su ++; 00103 #endif 00104 00105 int ul,ur; 00106 bool goon=1; 00107 while(goon && UpperL(i)<Len()){ 00108 ul=UpperL(i); 00109 ur=UpperR(i); 00110 if(ur>=Len() || 00111 operator()(ul)->Val() < operator()(ur)->Val() ){ 00112 goon=SwapIf(i,ul); 00113 i=ul; 00114 } 00115 else{ 00116 goon=SwapIf(i,ur); 00117 i=ur; 00118 } 00119 } 00120 00121 #ifdef EVENT_DEB 00122 CheckHeap(); 00123 #endif 00124 00125 }
void tHeapBase::CheckHeap | ( | ) | [protected] |
Definition at line 63 of file tHeap.cpp.
References tHeapElement::hID, Len(), Lower(), operator()(), tERR_ERROR_INT, UpperL(), UpperR(), and tHeapElement::Val().
Referenced by Insert(), Remove(), Replace(), SwapDown(), SwapUp(), and tEventQueue::Timestep().
00063 { 00064 for(int i=Len()-1;i>0;i--){ 00065 tHeapElement *current=operator()(i); 00066 tHeapElement *low=operator()(Lower(i)); 00067 if (Lower(UpperL(i))!=i || Lower(UpperR(i))!=i) 00068 tERR_ERROR_INT("Error in lower/upper " << i << "!"); 00069 00070 if (low->Val() > current->Val() ) 00071 tERR_ERROR_INT("Heap structure corrupt!"); 00072 00073 if ( current->hID != i ) 00074 tERR_ERROR_INT("Heap list structure corrupt!"); 00075 } 00076 }
void tHeapBase::Insert | ( | tHeapElement * | e | ) | [protected] |
Definition at line 127 of file tHeap.cpp.
References tList< tHeapElement >::Add(), CheckHeap(), tHeapElement::hID, Len(), SwapDown(), and tASSERT.
Referenced by tHeap< tEvent >::Insert(), and tHeapElement::SetVal().
00127 { 00128 #ifdef EVENT_DEB 00129 CheckHeap(); 00130 #endif 00131 00132 Add(e,e->hID); // relies on the implementation of List: e is 00133 // put to the back of the heap. 00134 tASSERT(e->hID == Len()-1); 00135 SwapDown(Len()-1); // bring it to the right place 00136 00137 #ifdef EVENT_DEB 00138 CheckHeap(); 00139 #endif 00140 }
void tHeapBase::Remove | ( | tHeapElement * | e | ) | [protected] |
Reimplemented from tList< tHeapElement >.
Definition at line 142 of file tHeap.cpp.
References CheckHeap(), tHeapElement::Heap(), tHeapElement::hID, tERR_ERROR_INT, and tERR_WARN.
Referenced by tHeap< tEvent >::Remove(), tHeapElement::RemoveFromHeap(), and ~tHeapBase().
00142 { 00143 int i=e->hID; 00144 00145 #ifdef EVENT_DEB 00146 CheckHeap(); 00147 #endif 00148 00149 // element 00150 if(i<0 ) 00151 { 00152 #ifdef DEBUG 00153 static bool warn = true; 00154 if ( warn ) 00155 { 00156 tERR_WARN("Element to be removed from heap was already removed. Unless there is a fatal exit in process, this is not right."); 00157 warn=false; 00158 } 00159 #endif 00160 return; 00161 } 00162 00163 if( this != e->Heap()) 00164 tERR_ERROR_INT("Element is not in this heap! (Note: this usually is a followup error when the system fails to recover from another error. When reporting, please also include whatever happened before this.)"); 00165 00166 Remove(i); 00167 00168 #ifdef EVENT_DEB 00169 CheckHeap(); 00170 #endif 00171 }
tHeapElement * tHeapBase::Remove | ( | int | i | ) | [protected] |
Reimplemented in tHeap< T >, tHeap< nBandwidthArbitrator >, and tHeap< tEvent >.
Definition at line 199 of file tHeap.cpp.
References CheckHeap(), tHeapElement::hID, Len(), NULL, operator()(), tList< T, MALLOC, REFERENCE >::Remove(), Replace(), and tASSERT.
00199 { 00200 #ifdef EVENT_DEB 00201 CheckHeap(); 00202 #endif 00203 00204 if (i>=0 && i<Len()) 00205 { 00206 tHeapElement *ret=operator()(i); 00207 00208 tASSERT( ret->hID == i ); 00209 00210 tList<tHeapElement>::Remove(ret,ret->hID); 00211 00212 // now we have an misplaced element at pos i. (if i was not at the end..) 00213 if (i<Len()) 00214 Replace(i); 00215 00216 #ifdef EVENT_DEB 00217 CheckHeap(); 00218 #endif 00219 00220 return ret; 00221 } 00222 return NULL; 00223 }
void tHeapBase::Replace | ( | int | i | ) | [protected] |
Definition at line 173 of file tHeap.cpp.
References CheckHeap(), Len(), Lower(), SwapDown(), and SwapUp().
Referenced by Remove(), tHeap< tEvent >::Replace(), Replace(), and tHeapElement::SetVal().
00173 { 00174 if (i>=0 && i < Len()){ 00175 if (i==0 || operator()(i)->Val() > operator()(Lower(i))->Val() ) 00176 SwapUp(i); // put it up where it belongs 00177 else 00178 SwapDown(i); 00179 00180 #ifdef EVENT_DEB 00181 CheckHeap(); 00182 #endif 00183 } 00184 }
void tHeapBase::Replace | ( | tHeapElement * | e | ) | [protected] |
Definition at line 186 of file tHeap.cpp.
References CheckHeap(), tHeapElement::hID, Replace(), and tERR_ERROR_INT.
00186 { 00187 int i=e->hID; 00188 00189 if(i<0 || this != e->Heap()) 00190 tERR_ERROR_INT("Element is not in this heap! (Note: this usually is a followup error when the system fails to recover from another error. When reporting, please also include whatever happened before this.)"); 00191 00192 Replace(i); 00193 00194 #ifdef EVENT_DEB 00195 CheckHeap(); 00196 #endif 00197 }
static int tHeapBase::UpperL | ( | int | i | ) | [inline, static] |
Definition at line 78 of file tHeap.h.
Referenced by CheckHeap(), draw_eWall(), and SwapUp().
static int tHeapBase::UpperR | ( | int | i | ) | [inline, static] |
Definition at line 79 of file tHeap.h.
Referenced by CheckHeap(), draw_eWall(), and SwapUp().
friend class tHeapElement [friend] |