CTaskHeap Class Reference

#include <externs.h>

Collaboration diagram for CTaskHeap:

Collaboration graph
[legend]

Public Member Functions

 CTaskHeap ()
 ~CTaskHeap ()
bool Insert (PTASK_RECORD, SCHCMP *)
PTASK_RECORD PeekAtTopmost (void)
PTASK_RECORD RemoveTopmost (SCHCMP *)
void CancelTask (FTASK *fpTask, void *arg_voidptr, int arg_Integer)
int TraverseUnordered (SCHLOOK *pfLook, SCHCMP *pfCompare)
int TraverseOrdered (SCHLOOK *pfLook, SCHCMP *pfCompare)

Private Member Functions

bool Grow (void)
void SiftDown (int, SCHCMP *)
void SiftUp (int, SCHCMP *)
PTASK_RECORD Remove (int, SCHCMP *)
void Update (int iNode, SCHCMP *pfCompare)
void Sort (SCHCMP *pfCompare)
void Remake (SCHCMP *pfCompare)

Private Attributes

int m_nAllocated
int m_nCurrent
PTASK_RECORDm_pHeap
int m_iVisitedMark

Detailed Description

Definition at line 894 of file externs.h.


Constructor & Destructor Documentation

CTaskHeap::CTaskHeap (  ) 

Definition at line 326 of file timer.cpp.

References INITIAL_TASKS, m_iVisitedMark, m_nAllocated, m_nCurrent, and m_pHeap.

00327 {
00328     m_nCurrent = 0;
00329     m_iVisitedMark = 0;
00330     m_nAllocated = INITIAL_TASKS;
00331     m_pHeap = new PTASK_RECORD[m_nAllocated];
00332     if (!m_pHeap)
00333     {
00334         m_nAllocated = 0;
00335     }
00336 }

CTaskHeap::~CTaskHeap (  ) 

Definition at line 338 of file timer.cpp.

References m_nCurrent, and m_pHeap.

00339 {
00340     while (m_nCurrent--)
00341     {
00342         PTASK_RECORD pTask = m_pHeap[m_nCurrent];
00343         if (pTask)
00344         {
00345             delete pTask;
00346         }
00347         m_pHeap[m_nCurrent] = NULL;
00348     }
00349     if (m_pHeap)
00350     {
00351         delete [] m_pHeap;
00352     }
00353 }


Member Function Documentation

void CTaskHeap::CancelTask ( FTASK fpTask,
void *  arg_voidptr,
int  arg_Integer 
)

Definition at line 405 of file timer.cpp.

References TASK_RECORD::fpTask, m_nCurrent, and m_pHeap.

Referenced by CScheduler::CancelTask().

00406 {
00407     for (int i = 0; i < m_nCurrent; i++)
00408     {
00409         PTASK_RECORD p = m_pHeap[i];
00410         if (  p->fpTask == fpTask
00411            && p->arg_voidptr == arg_voidptr
00412            && p->arg_Integer == arg_Integer)
00413         {
00414             p->fpTask = NULL;
00415         }
00416     }
00417 }

bool CTaskHeap::Grow ( void   )  [private]

Definition at line 372 of file timer.cpp.

References EXTRA_TASKS, m_nAllocated, and m_pHeap.

Referenced by Insert().

00373 {
00374     // Grow the heap.
00375     //
00376     int n = m_nAllocated + EXTRA_TASKS;
00377     PTASK_RECORD *p = new PTASK_RECORD[n];
00378     if (!p)
00379     {
00380         return false;
00381     }
00382 
00383     memcpy(p, m_pHeap, sizeof(PTASK_RECORD)*m_nAllocated);
00384     m_nAllocated = n;
00385     delete [] m_pHeap;
00386     m_pHeap = p;
00387 
00388     return true;
00389 }

bool CTaskHeap::Insert ( PTASK_RECORD  ,
SCHCMP  
)

Definition at line 355 of file timer.cpp.

References Grow(), m_iVisitedMark, TASK_RECORD::m_iVisitedMark, m_nAllocated, m_nCurrent, m_pHeap, and SiftUp().

Referenced by CScheduler::DeferImmediateTask(), CScheduler::DeferTask(), and CScheduler::ReadyTasks().

00356 {
00357     if (m_nCurrent == m_nAllocated)
00358     {
00359         if (!Grow())
00360         {
00361             return false;
00362         }
00363     }
00364     pTask->m_iVisitedMark = m_iVisitedMark-1;
00365 
00366     m_pHeap[m_nCurrent] = pTask;
00367     m_nCurrent++;
00368     SiftUp(m_nCurrent-1, pfCompare);
00369     return true;
00370 }

PTASK_RECORD CTaskHeap::PeekAtTopmost ( void   ) 

Definition at line 391 of file timer.cpp.

References m_nCurrent, and m_pHeap.

Referenced by CScheduler::ReadyTasks(), CScheduler::RunTasks(), and CScheduler::WhenNext().

00392 {
00393     if (m_nCurrent <= 0)
00394     {
00395         return NULL;
00396     }
00397     return m_pHeap[0];
00398 }

void CTaskHeap::Remake ( SCHCMP pfCompare  )  [private]

Definition at line 776 of file timer.cpp.

References m_nCurrent, and SiftUp().

Referenced by TraverseOrdered().

00777 {
00778     int s_nCurrent = m_nCurrent;
00779     m_nCurrent = 0;
00780     while (s_nCurrent--)
00781     {
00782         m_nCurrent++;
00783         SiftUp(m_nCurrent-1, pfCompare);
00784     }
00785 }

PTASK_RECORD CTaskHeap::Remove ( int  ,
SCHCMP  
) [private]

Definition at line 644 of file timer.cpp.

References m_nCurrent, m_pHeap, SiftDown(), and SiftUp().

Referenced by RemoveTopmost(), and TraverseUnordered().

00645 {
00646     if (iNode < 0 || m_nCurrent <= iNode) return NULL;
00647 
00648     PTASK_RECORD pTask = m_pHeap[iNode];
00649 
00650     m_nCurrent--;
00651     m_pHeap[iNode] = m_pHeap[m_nCurrent];
00652     SiftDown(iNode, pfCompare);
00653     SiftUp(iNode, pfCompare);
00654 
00655     return pTask;
00656 }

PTASK_RECORD CTaskHeap::RemoveTopmost ( SCHCMP  ) 

Definition at line 400 of file timer.cpp.

References Remove().

Referenced by CScheduler::ReadyTasks(), and CScheduler::RunTasks().

00401 {
00402     return Remove(0, pfCompare);
00403 }

void CTaskHeap::SiftDown ( int  ,
SCHCMP  
) [private]

Definition at line 600 of file timer.cpp.

References HEAP_LEFT_CHILD, HEAP_RIGHT_CHILD, m_nCurrent, and m_pHeap.

Referenced by Remove(), Sort(), and Update().

00601 {
00602     int parent = iSubRoot;
00603     int child = HEAP_LEFT_CHILD(parent);
00604 
00605     PTASK_RECORD Ref = m_pHeap[parent];
00606 
00607     while (child < m_nCurrent)
00608     {
00609         int rightchild = HEAP_RIGHT_CHILD(parent);
00610         if (rightchild < m_nCurrent)
00611         {
00612             if (pfCompare(m_pHeap[rightchild], m_pHeap[child]) < 0)
00613             {
00614                 child = rightchild;
00615             }
00616         }
00617         if (pfCompare(Ref, m_pHeap[child]) <= 0)
00618             break;
00619 
00620         m_pHeap[parent] = m_pHeap[child];
00621         parent = child;
00622         child = HEAP_LEFT_CHILD(parent);
00623     }
00624     m_pHeap[parent] = Ref;
00625 }

void CTaskHeap::SiftUp ( int  ,
SCHCMP  
) [private]

Definition at line 627 of file timer.cpp.

References HEAP_PARENT, and m_pHeap.

Referenced by Insert(), Remake(), Remove(), and Update().

00628 {
00629     while (child)
00630     {
00631         int parent = HEAP_PARENT(child);
00632         if (pfCompare(m_pHeap[parent], m_pHeap[child]) <= 0)
00633             break;
00634 
00635         PTASK_RECORD Tmp;
00636         Tmp = m_pHeap[child];
00637         m_pHeap[child] = m_pHeap[parent];
00638         m_pHeap[parent] = Tmp;
00639 
00640         child = parent;
00641     }
00642 }

void CTaskHeap::Sort ( SCHCMP pfCompare  )  [private]

Definition at line 763 of file timer.cpp.

References m_nCurrent, m_pHeap, and SiftDown().

Referenced by TraverseOrdered().

00764 {
00765     int s_nCurrent = m_nCurrent;
00766     while (m_nCurrent--)
00767     {
00768         PTASK_RECORD p = m_pHeap[m_nCurrent];
00769         m_pHeap[m_nCurrent] = m_pHeap[0];
00770         m_pHeap[0] = p;
00771         SiftDown(0, pfCompare);
00772     }
00773     m_nCurrent = s_nCurrent;
00774 }

int CTaskHeap::TraverseOrdered ( SCHLOOK pfLook,
SCHCMP pfCompare 
)

Definition at line 745 of file timer.cpp.

References IU_DONE, m_nCurrent, m_pHeap, Remake(), and Sort().

Referenced by CScheduler::TraverseOrdered().

00746 {
00747     Sort(pfCompare);
00748 
00749     for (int i = m_nCurrent-1; i >= 0; i--)
00750     {
00751         PTASK_RECORD p = m_pHeap[i];
00752         int cmd = pfLook(p);
00753         if (IU_DONE == cmd)
00754         {
00755             break;
00756         }
00757     }
00758 
00759     Remake(pfCompare);
00760     return true;
00761 }

int CTaskHeap::TraverseUnordered ( SCHLOOK pfLook,
SCHCMP pfCompare 
)

Definition at line 687 of file timer.cpp.

References IU_DONE, IU_REMOVE_TASK, IU_UPDATE_TASK, TASK_RECORD::m_iVisitedMark, m_iVisitedMark, m_nCurrent, m_pHeap, Remove(), and Update().

Referenced by CScheduler::TraverseUnordered().

00688 {
00689     // Indicate that everything has not been visited.
00690     //
00691     m_iVisitedMark++;
00692     if (m_iVisitedMark == 0)
00693     {
00694         // We rolled over. Yeah, this code will probably never run, but
00695         // let's go ahead and mark all the records as visited anyway.
00696         //
00697         for (int i = 0; i < m_nCurrent; i++)
00698         {
00699             PTASK_RECORD p = m_pHeap[i];
00700             p->m_iVisitedMark = m_iVisitedMark;
00701         }
00702         m_iVisitedMark++;
00703     }
00704 
00705     int bUnvisitedRecords;
00706     do
00707     {
00708         bUnvisitedRecords = false;
00709         for (int i = 0; i < m_nCurrent; i++)
00710         {
00711             PTASK_RECORD p = m_pHeap[i];
00712 
00713             if (p->m_iVisitedMark == m_iVisitedMark)
00714             {
00715                 // We have already seen this one.
00716                 //
00717                 continue;
00718             }
00719             bUnvisitedRecords = true;
00720             p->m_iVisitedMark = m_iVisitedMark;
00721 
00722             int cmd = pfLook(p);
00723             switch (cmd)
00724             {
00725             case IU_REMOVE_TASK:
00726                 Remove(i, pfCompare);
00727                 break;
00728 
00729             case IU_DONE:
00730                 return false;
00731 
00732             case IU_UPDATE_TASK:
00733                 Update(i, pfCompare);
00734                 break;
00735             }
00736         }
00737     } while (bUnvisitedRecords);
00738     return true;
00739 }

void CTaskHeap::Update ( int  iNode,
SCHCMP pfCompare 
) [private]

Definition at line 658 of file timer.cpp.

References m_nCurrent, SiftDown(), and SiftUp().

Referenced by TraverseUnordered().

00659 {
00660     if (iNode < 0 || m_nCurrent <= iNode)
00661     {
00662         return;
00663     }
00664 
00665     SiftDown(iNode, pfCompare);
00666     SiftUp(iNode, pfCompare);
00667 }


Field Documentation

int CTaskHeap::m_iVisitedMark [private]

Definition at line 901 of file externs.h.

Referenced by CTaskHeap(), Insert(), and TraverseUnordered().

int CTaskHeap::m_nAllocated [private]

Definition at line 897 of file externs.h.

Referenced by CTaskHeap(), Grow(), and Insert().

int CTaskHeap::m_nCurrent [private]

Definition at line 898 of file externs.h.

Referenced by CancelTask(), CTaskHeap(), Insert(), PeekAtTopmost(), Remake(), Remove(), SiftDown(), Sort(), TraverseOrdered(), TraverseUnordered(), Update(), and ~CTaskHeap().

PTASK_RECORD* CTaskHeap::m_pHeap [private]

Definition at line 899 of file externs.h.

Referenced by CancelTask(), CTaskHeap(), Grow(), Insert(), PeekAtTopmost(), Remove(), SiftDown(), SiftUp(), Sort(), TraverseOrdered(), TraverseUnordered(), and ~CTaskHeap().


The documentation for this class was generated from the following files:
Generated on Mon May 28 04:40:25 2007 for MUX by  doxygen 1.4.7