// File: ArrayQueue.java from the package edu.colorado.collections
// Complete documentation is available from the ArrayQueue link in:
// http://www.cs.colorado.edu/~main/docs/
//package edu.colorado.collections;
import java.util.NoSuchElementException;
/******************************************************************************
* An ArrayQueue<E> is a queue of references to E objects.
*
*
add, clone,
* and union will result in an
* OutOfMemoryError when free memory is exhausted.
* Integer.MAX_VALUE). Any attempt to create a larger capacity
* results in a failure due to an arithmetic overflow.
* add method works efficiently (without needing more
* memory) until this capacity is reached.
* @param - none
* new Object[10].
**/
public ArrayQueue( )
{
final int INITIAL_CAPACITY = 10;
manyItems = 0;
data = (E[]) new Object[INITIAL_CAPACITY];
// We don't care about front and rear for an empty queue.
}
/**
* Initialize an empty queue with a specified initial capacity. Note that the
* add method works efficiently (without needing more
* memory) until this capacity is reached.
* @param initialCapacity
* the initial capacity of this queue
* initialCapacity is non-negative.
* new Object[initialCapacity].
**/
public ArrayQueue(int initialCapacity)
{
if (initialCapacity < 0)
throw new IllegalArgumentException
("initialCapacity is negative: " + initialCapacity);
manyItems = 0;
data = (E[]) new Object[initialCapacity];
// We don't care about front and rear for an empty queue.
}
/**
* Generate a copy of this queue.
* @param - none
* @return
* The return value is a copy of this queue. Subsequent changes to the
* copy will not affect the original, nor vice versa. Note that the return
* value must be type cast to an ArrayQueue before it can be used.
* @exception OutOfMemoryError
* Indicates insufficient memory for creating the clone.
**/
public ArrayQueueitem
* the item to be pushed onto this queue
* Integer.MAX_VALUE will cause the queue to fail with an
* arithmetic overflow.
**/
public void add(E item)
{
if (manyItems == data.length)
{
// Double the capacity and add 1; this works even if manyItems is 0. However, in
// case that manyItems*2 + 1 is beyond Integer.MAX_VALUE, there will be an
// arithmetic overflow and the bag will fail.
ensureCapacity(manyItems*2 + 1);
}
if (manyItems == 0)
{
front = 0;
rear = 0;
}
else
rear = nextIndex(rear);
data[rear] = item;
manyItems++;
}
/**
* Change the current capacity of this queue.
* @param minimumCapacity
* the new capacity for this queue
* minimumCapacity.
* If the capacity was already at or greater than minimumCapacity,
* then the capacity is left unchanged.
* @exception OutOfMemoryError
* Indicates insufficient memory for: new Object[minimumCapacity].
**/
public void ensureCapacity(int minimumCapacity)
{
E[ ] biggerArray;
int n1, n2;
if (data.length >= minimumCapacity)
// No change needed.
return;
else if (manyItems == 0)
// Just increase the size of the array because the queue is empty.
data = (E[]) new Object[minimumCapacity];
else if (front <= rear)
{ // Create larger array and copy data[front]...data[rear] into it.
biggerArray = (E[]) new Object[minimumCapacity];
System.arraycopy(data, front, biggerArray, front, manyItems);
data = biggerArray;
}
else
{ // Create a bigger array, but be careful about copying items into it. The queue items
// occur in two segments. The first segment goes from data[front] to the end of the
// array, and the second segment goes from data[0] to data[rear]. The variables n1
// and n2 will be set to the number of items in these two segments. We will copy
// these segments to biggerArray[0...manyItems-1].
biggerArray = (E[]) new Object[minimumCapacity];
n1 = data.length - front;
n2 = rear + 1;
System.arraycopy(data, front, biggerArray, 0, n1);
System.arraycopy(data, 0, biggerArray, n1, n2);
front = 0;
rear = manyItems-1;
data = biggerArray;
}
}
/**
* Accessor method to get the current capacity of this queue.
* The add method works efficiently (without needing
* more memory) until this capacity is reached.
* @param - none
* @return
* the current capacity of this queue
**/
public int getCapacity( )
{
return data.length;
}
/**
* Determine whether this queue is empty.
* @param - none
* @return
* true if this queue is empty;
* false otherwise.
**/
public boolean isEmpty( )
{
return (manyItems == 0);
}
private int nextIndex(int i)
// Precondition: 0 <= i and i < data.length
// Postcondition: If i+1 is data.length,
// then the return value is zero; otherwise
// the return value is i+1.
{
if (++i == data.length)
return 0;
else
return i;
}
/**
* Get the front item, removing it from this queue.
* @param - none
*