Main Page   Packages   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Search  

C:/temp/src/j2k/DataType/Queue/ArrayQueue.hpp

Go to the documentation of this file.
00001 #ifndef __J2K__Array_Queue_HPP__
00002 #define __J2K__Array_Queue_HPP__
00003 
00004 #include <j2k/Fred/Standard.hpp>
00005 
00006 #define LIST_SIZE    100
00007 
00008 class ArrayQueue {                      // Array based queue class
00009 public:
00010 
00011   // Make list array one position
00012   // larger for an empty slot.
00013   inline ArrayQueue()                   // Default Constructor
00014     : size( LIST_SIZE + 1 ), front( 0 ), rear( 0 ) 
00015   {
00016       listarray = new Elem[size];
00017   }
00018 
00019   inline ArrayQueue( unsigned long sz ) // Constructor with Size
00020     : size( sz + 1 ), front( 0 ), rear( 0 ) 
00021   {
00022       listarray = new Elem[size];
00023   }
00024 
00025   virtual ~ArrayQueue()                 // Destructor
00026   {
00027     delete [] listarray;
00028   }
00029 
00030   void clear()                          // Clear queue
00031   {
00032     front = rear;
00033   }
00034 
00035   void enqueue( const Elem item );      // Enqueue Elem at rear
00036   Elem dequeue();                       // Dequeue Elem from front
00037 
00038   Elem firstValue() const               // Get value of front Elem
00039   {
00040     assert( !isEmpty() );
00041     return listarray[ (front+1) % size ];
00042   }
00043 
00044   BOOL isEmpty() const                  // TRUE if queue is empty
00045   {
00046     return front == rear;
00047   }
00048 
00049 private:
00050   unsigned long size;                   // Maximum size of queue
00051   unsigned long front;                  // Index prior to front item
00052   unsigned long rear;                   // Index of rear item
00053   Elem *listarray;                      // Array holding the list Elem's
00054 };
00055 
00056 // Enqueue Elem at rear of queue
00057 void ArrayQueue::enqueue( const Elem item ) 
00058 {
00059   assert( ((rear+1) % size) != front ); // Queue must not be full
00060   rear = (rear+1) % size;               // Increment rear (in circle)
00061   listarray[rear] = item;
00062 }
00063 
00064 Elem ArrayQueue::dequeue()              // Dequeue Elem from front of queue
00065 {
00066   assert( !isEmpty() );                 // There must be something to dequeue
00067   front = (front+1) % size;             // Increment front
00068   return listarray[ front ];            // Return value
00069 }
00070 
00071 #endif // End of ArrayQueue.hpp

Generated on Sun Oct 14 18:46:18 2001 for Standard J2K Library by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001