org.xith3d.utility.general
Class CircularArray<T>

java.lang.Object
  extended by org.xith3d.utility.general.CircularArray<T>

public class CircularArray<T>
extends java.lang.Object

Java's internal LinkedList implementation is extremely GC expensive. This Array-embedded implemention is better.
The CircularArray is a special kind of a Stack. Elements are dropped in (push) in the array at a certain position and are removed (pop) at another certain position. These position are generally not the backing array's first and last positions, but can be anywhere. When the array's last element has been written, the array's first element will be the next and so forth. So the position of an element in the backing array is (i % array.length).


Constructor Summary
CircularArray(int size)
          Creates a new CircularArray with a backing array of the specified size.
 
Method Summary
 boolean isEmpty()
           
 T peek()
          Sneaks the CircularArray, if there is any Element to be retrieved by pop(), but does not remove it.
 T pop()
          Retrieves and removes the next element in question.
 void push(T o)
          Stores the object at the next position.
 int size()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CircularArray

public CircularArray(int size)
Creates a new CircularArray with a backing array of the specified size. If (size + 1) elements are stored without removing any, the last element will overwrite the first one.

Parameters:
size -
Method Detail

size

public final int size()
Returns:
the backing array's length.

isEmpty

public final boolean isEmpty()
Returns:
true, if the CircularArray holds at least one Element.

push

public final void push(T o)
Stores the object at the next position. This might overwrite an existing Element, if the backing array is not big enough. (No auto-growing)

Parameters:
o -

peek

public final T peek()
Sneaks the CircularArray, if there is any Element to be retrieved by pop(), but does not remove it.

Returns:
the element to be retrieved by pop(), but does not remove it.
See Also:
pop()

pop

public final T pop()
Retrieves and removes the next element in question. This is the first element element added and still remaining.

See Also:
peek()