tHeapBase Class Reference

#include <tHeap.h>

Inheritance diagram for tHeapBase:

Inheritance graph
[legend]
Collaboration diagram for tHeapBase:

Collaboration graph
[legend]

List of all members.

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
tHeapElementoperator() (int i)
const tHeapElementoperator() (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)
tHeapElementRemove (int i)
void Replace (int i)
void Replace (tHeapElement *e)

Friends

class tHeapElement


Detailed Description

Definition at line 46 of file tHeap.h.


Constructor & Destructor Documentation

tHeapBase::tHeapBase (  )  [inline]

Definition at line 81 of file tHeap.h.

00081 {}

tHeapBase::~tHeapBase (  ) 

Definition at line 56 of file tHeap.cpp.

References Len(), and Remove().

00057 {
00058     while(Len() > 0)
00059         Remove(0);
00060 }

Here is the call graph for this function:


Member Function Documentation

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     }

Here is the caller graph for this function:

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(); }

Here is the call graph for this function:

Here is the caller graph for this function:

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) ); }

Here is the call graph for this function:

Here is the caller graph for this function:

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); }

Here is the call graph for this function:

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 }

Here is the call graph for this function:

Here is the caller graph for this function:

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 }

Here is the call graph for this function:

Here is the caller graph for this function:

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 }

Here is the call graph for this function:

Here is the caller graph for this function:

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 }

Here is the call graph for this function:

Here is the caller graph for this function:

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 }

Here is the call graph for this function:

Here is the caller graph for this function:

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 }

Here is the call graph for this function:

Here is the caller graph for this function:

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 }

Here is the call graph for this function:

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 }

Here is the call graph for this function:

Here is the caller graph for this function:

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 }

Here is the call graph for this function:

static int tHeapBase::UpperL ( int  i  )  [inline, static]

Definition at line 78 of file tHeap.h.

Referenced by CheckHeap(), draw_eWall(), and SwapUp().

00078 {return 2*i+1;} // the elements left and

Here is the caller graph for this function:

static int tHeapBase::UpperR ( int  i  )  [inline, static]

Definition at line 79 of file tHeap.h.

Referenced by CheckHeap(), draw_eWall(), and SwapUp().

00079 {return 2*i+2;} // right above i

Here is the caller graph for this function:


Friends And Related Function Documentation

friend class tHeapElement [friend]

Definition at line 47 of file tHeap.h.


The documentation for this class was generated from the following files:
Generated on Sat Mar 15 23:57:01 2008 for Armagetron Advanced by  doxygen 1.5.4