WOscPriorityQueue Class Reference

#include <WOscPriorityQueue.h>

Inherited by WOscReceiver [private].

List of all members.

Public Member Functions

 WOscPriorityQueue (int initialCapacity)
virtual ~WOscPriorityQueue ()
void InsertItem (WOscQueueItem *item)
WOscQueueItemRemoveEarliestItem ()
WOscTimeTag GetEarliestTimeTag ()
int GetNumItems ()

Detailed Description

Sorts queue-items by their time tag when inserted. The earliest items can be queried and/or removed.

Remarks:
The queue items are stored internally only by reference, the user must control the life (allocation and deletion) of the queue items.
See also:
WOscQueueItem

Definition at line 115 of file WOscPriorityQueue.h.


Constructor & Destructor Documentation

WOscPriorityQueue::WOscPriorityQueue ( int  initialCapacity  ) 

Constructor. Constructs an empty priority queue with a capacity of "initialCapacity".

Parameters:
initialCapacity The initial capacity (how many items can be inserted until doubleCapacity() will be called.
See also:
WOscPriorityQueue::doubleCapacity()

Definition at line 58 of file WOscPriorityQueue.cpp.

00059 {
00060 
00061     /* allocate list (queue) */
00062     m_listCapacity = initialCapacity;
00063     m_list = new WOscQueueItem*[initialCapacity];
00064 
00065     /* reset list (no items) */
00066     for( int i = 0; i < initialCapacity; i++ )
00067         m_list[i] = NULL;
00068     m_itemsInList = 0;
00069 
00070 }

WOscPriorityQueue::~WOscPriorityQueue (  )  [virtual]

Destructor. All elements which are not removed from the priority-queue get deleted automatically.

Definition at line 77 of file WOscPriorityQueue.cpp.

References GetNumItems(), and RemoveEarliestItem().

00078 {
00079 
00080     /* empty queue as long there are items in the queue*/
00081     while( GetNumItems() > 0 )
00082         delete RemoveEarliestItem();
00083 
00084     delete [] m_list;
00085 }


Member Function Documentation

WOscTimeTag WOscPriorityQueue::GetEarliestTimeTag (  ) 

Returns the timetag of the earliest item. Searches by linearly traversing the queue.

Returns:
The time-tag of the earliest item. If no time-tag smaller than WOscTimeTag::getLargestTimeTag() can be found WOscTimeTag::getLargestTimeTag() will be returned.

Definition at line 143 of file WOscPriorityQueue.cpp.

References WOscTimeTag::GetLargestTimeTag().

Referenced by WOscReceiver::ManagePriorityQueue().

00144 {
00145     
00146     WOscTimeTag smallest = WOscTimeTag::GetLargestTimeTag();
00147 
00148     for ( int i = 0; i < m_itemsInList; i++ )
00149         if ( *m_list[i] < smallest )
00150             smallest = *m_list[i];
00151 
00152     return smallest;
00153 }

int WOscPriorityQueue::GetNumItems (  ) 

Returns the number of items in the queue.

Returns:
The number of items in the queue.

Definition at line 216 of file WOscPriorityQueue.cpp.

Referenced by WOscReceiver::ManagePriorityQueue(), and ~WOscPriorityQueue().

00217 {
00218     return m_itemsInList;
00219 }

void WOscPriorityQueue::InsertItem ( WOscQueueItem item  ) 

Inserts an item into the queue. Items are simply inserted and not ordered.

Remarks:
WOscPriorityQueue::getEarliestTimeTag() then checks the time-tags and returns the earliest.

Definition at line 96 of file WOscPriorityQueue.cpp.

00097 {
00098 
00099     // check capacity, double if necessary
00100     if ( m_itemsInList == m_listCapacity )
00101         DoubleCapacity();
00102 
00103     // insert pointer to element and increment population
00104     m_list[m_itemsInList++] = item;
00105 
00106 }

WOscQueueItem * WOscPriorityQueue::RemoveEarliestItem (  ) 

Removes the earliest item from the queue and returns it.

Returns:
The pointer to the earliest item removed from the queue.
Exceptions:
WOscException When trying to remove from an empty queue.
Remarks:
When removed, the user (caller) has to care about the deletion of that element, since the queue is only an itermediate buffer. When removed the queue does not know anything about this object anymore.

Definition at line 187 of file WOscPriorityQueue.cpp.

References ERR_REMOVE_FROM_EMPTY_QUEUE.

Referenced by WOscReceiver::ManagePriorityQueue(), and ~WOscPriorityQueue().

00188 {
00189 
00190     WOscQueueItem* result;
00191 
00192     int smallestIdx = 0;
00193 
00194     if ( m_itemsInList == 0 )
00195         throw WOscException(ERR_REMOVE_FROM_EMPTY_QUEUE, __FILE__, __LINE__);
00196 
00197     /* traverse the queue and */
00198     for ( int i = 0; i < m_itemsInList; i++ )
00199         /* compare time-tags */
00200         if ( *m_list[smallestIdx] > *m_list[i] )
00201             smallestIdx = i;
00202 
00203     /* save pointer before removing it */
00204     result = m_list[smallestIdx];
00205 
00206     RemoveItem(smallestIdx);
00207 
00208     return result;
00209 }


The documentation for this class was generated from the following files:
Generated on Sat Oct 23 03:05:59 2010 for WOscLib by  doxygen 1.6.3