// QueType.cpp
// Class is templated; array of items is in dynamically allocated storage.

#include "ansiscreen.h"   //macros for cursor positioning and color for View() 

template<class ItemType>
QueType<ItemType>::QueType(int max)	
// Post: maxQue, front, and rear have been initialized.
//       The array to hold the queue elements has been dynamically allocated.
{
    maxQue = max + 1;
    front = maxQue - 1;
    rear = maxQue - 1;
    items = new ItemType[maxQue];
}

template<class ItemType>
QueType<ItemType>::QueType()	  // default class constructor
// Post: maxQue, front, and rear have been initialized.
//       The array to hold the queue elements has been dynamically allocated.
{
    maxQue = 501;
    front = maxQue - 1;
    rear = maxQue - 1;
    items = new ItemType[maxQue];
}

template<class ItemType>
QueType<ItemType>::~QueType()			// Class destructor
{
    delete [] items;
}

template<class ItemType>
void QueType<ItemType>::MakeEmpty()
// Post: front and rear have been reset to the empty state.
  //empty is when front equals rear
{
    front = maxQue - 1;
    rear = maxQue - 1;
}

template<class ItemType>
bool QueType<ItemType>::IsEmpty() const
// Returns true if the queue is empty; false otherwise.
{
    return (rear == front);
}

template<class ItemType>
bool QueType<ItemType>::IsFull() const
// Returns true if the queue is full; false otherwise.
  //Front is one element ahead of Rear.
  //One element of the array can not be used by the Queue.
{
    return ((rear + 1) % maxQue == front);
}

template<class ItemType>
void QueType<ItemType>::Enqueue(ItemType newItem)
// Post: newItem is at the rear of the queue.
  //Rear is the index of the last item enqueued.
  //pre-increment rear, wrap-around to 0 if necessary
{
    rear = (rear +1) % maxQue;
    items[rear] = newItem;
}

template<class ItemType>
void QueType<ItemType>::Dequeue(ItemType& item)
// Post: The front of the queue has been removed and a copy returned in item.
  //Front is the index of the last item dequeued.
  //pre-increment front, wrap-around to 0 if necessary
{
    front = (front + 1) % maxQue;
    item = items[front];
}


template<class ItemType>
void QueType<ItemType>::View() const {
  //display everything in the array.  - for unused element, F front index,
  //red is rear element.
  //assumes ItemType can be sent to output stream
  int i;
  cout << endl;
  for (i=0; i<maxQue; i++) 
    if (front<rear)
      if (i<front || i>rear)
	cout << " - ";
      else if (i == front)
	cout << " F ";
      else if (i == rear) {
	color_red();
	cout << " " << items[i] << " ";   
	color_white();
      }
      else
	cout << " " << items[i] << " ";
    else if (rear < front)
      if (i<rear || i>front)
	cout << " " << items[i] << " ";
      else if (i == front)
	cout << " F ";
      else if (i == rear) {
	color_red();
	cout << " " << items[i] << " ";
	color_white();
      }
      else 
	cout << " - ";
    else if (i != front)  //front==rear
      cout << " - ";
    else
      cout << " E ";
  cout << endl;
}
