/******************************************************** ServiceQueue.cpp CMSC-341 Spring 2004 Project 2 Spencer Shimko, Section 001, sshimko1 Created: 29 February 2004 Current: 02 March 2004 ServiceQueue class implementation an adpater class to the LinkedList class that priority a interface to a priority based service queue *********************************************************/ #ifndef _SERVICEQUEUE_CPP_ #define _SERVICEQUEUE_CPP_ #include "ServiceQueue.h" #include "Job.h" class ServiceQueueException { }; template ServiceQueue::ServiceQueue() { last = m_theList.zeroth (); } template ServiceQueue::ServiceQueue(const ServiceQueue &sq) { m_theList = sq.m_theList; last = sq.last; firstSm = sq.firstSm; } template ServiceQueue::~ServiceQueue() { // no code } template bool ServiceQueue::isEmpty( ) const { return m_theList.isEmpty( ); } template Object ServiceQueue::dequeue( ) { if ( isEmpty ( ) ) throw ServiceQueueException( ); // retrieve next object in queue Object tmp = m_theList.first ( ).retrieve ( ); // update last pointer if needed if ( &m_theList.first ( ) == &last ) last = m_theList.zeroth ( ); m_theList.remove( m_theList.first( ).retrieve ( ) ); return tmp; } template Object ServiceQueue::dequeuePr( ) { if ( isEmpty ( ) ) throw ServiceQueueException ( ); // if we dont have a small job call normal dequeue if ( firstSm.isPastEnd ( ) ) { return dequeue ( ); } // update last iterator if needed if ( &firstSm == &last) last = m_theList.findPrevious ( firstSm.retrieve ( )); // find next small job ListItr iter = firstSm; Job j; bool newSm=false; while ( ! iter.isPastEnd ( ) && ! newSm ){ iter.advance ( ); j = iter.retrieve( ); if (j.getJType () == SMALL_J) newSm=true; } // last iter position is new small job firstSm = iter; ListItr tmp = firstSm; m_theList.remove(tmp.retrieve ( )); return tmp.retrieve ( ); } template void ServiceQueue::enqueue(Object &x) { /* ListItr iter = m_theList.first ( ); while (! iter->next.isPastEnd ( ) ) { iter.advance ( ); }*/ // insert item (last is ListItr of last queued item m_theList.insert(x, last); // increment last last.advance ( ); // check firstSm b/c if it's pastend (no small jobs in queue) // and we are inserting a small job we need to set this pointer if ( (firstSm.isPastEnd ( ) ) && (x.getJType ( ) == SMALL_J)) firstSm = last; } template int ServiceQueue::getSize( ) { int count = 0; ListItr iter = m_theList.zeroth ( ); for ( ; ! iter.isPastEnd( ) ; iter.advance( ) ) count++; return count; } template const ServiceQueue &ServiceQueue:: operator=(const ServiceQueue &sq) { if (this != &sq){ m_theList = sq.m_theList; last = sq.last; firstSm = sq.firstSm; } return *this; } #endif