E
- the type of the elements in the queuepublic class ResizingArrayQueue<E> extends ArrayQueue<E> implements IQueue<E>
ArrayQueue
that is space efficient
at various sizes.
A plain ArrayQueue may have a maximum capacity of 1000, but many times it will only use a fraction of that space. Or it may use that entire space for a period of time, but will then drain down to a much smaller number of elements.
A ResizingArrayQueue will dynamically resize as it is used so that it does not waste space. Hence you can have a queue of max capacity 1000, but not pay for that storage unless required. Further, if the queue grows to that max capacity, but then drains down to a much smaller size, a smaller array will be used, reclaiming the extra space not being used by the too-large array.
The trick with all of this is to do it efficiently, so that it is only slightly slower than ArrayQueue
.
by cspfrederick
and garethhalladay
Fall17
inspired by Chris Wilcox
Modifier and Type | Field and Description |
---|---|
(package private) int |
maxSize
the maximum size of the queue.
|
(package private) int |
minSize
the minimum size that the queue can shrink to.
|
added, elements, removed
Constructor and Description |
---|
ResizingArrayQueue(int minSize,
int maxSize)
Create a new queue with the given constraints on size.
|
Modifier and Type | Method and Description |
---|---|
static void |
main(java.lang.String[] args) |
boolean |
offer(E e)
Adds an element to the queue.
|
E |
poll()
Removes the oldest element from the queue.
|
protected void |
resizeArrayTo(int newSize)
Replaces the current
elements array with an array of size newSize . |
final int maxSize
final int minSize
public ResizingArrayQueue(int minSize, int maxSize)
minSize
- the minimum size of the queuemaxSize
- the maximum size of the queueprotected void resizeArrayTo(int newSize)
elements
array with an array of size newSize
.
All the old elements are mapped to their proper locations in the new array.
First, check that newSize
is greater than or equal to ArrayQueue.size()
,
throw a new RuntimeException if the new size would not hold all the elements.
Next, use ArrayQueue.newArray(int)
to create an array of the appropriate size.
Then use a for
loop that ranges from ArrayQueue.removed
to ArrayQueue.added
,
and assign values from the old array into the new array.
If the loop variable is i
, then the element at elements[i % elements.length]
belongs at new_elements[i % new_elements.length]
. After copying over all the elements,
assign ArrayQueue.elements
to be the newly created array.
newSize
- the new size for the backing arrayjava.lang.RuntimeException
- if newSize
is less than the current queue sizepublic boolean offer(E e)
resizeArrayTo(int)
with
either double the length of the current array or maxSize
, whichever is smaller.
Whether you had to resize or not, now add the new element in the usual way.
Don't forget to increment added
.
offer
in interface IQueue<E>
offer
in class ArrayQueue<E>
e
- the element to be added to the queueArrayQueue.added
public E poll()
removed
variable.
If the size of the queue is less than a quarter of the length of the array,
and the array is not already at the minimum size, then it is time to shrink the array.
Call resizeArrayTo(int)
with either half the length of the current array or
minSize
, whichever is larger.
poll
in interface IQueue<E>
poll
in class ArrayQueue<E>
ArrayQueue.removed
public static void main(java.lang.String[] args)