org.as2lib.core.BasicInterface +--org.as2lib.data.holder.Queue
Queue
is the base interface for data holders that follow the 'first-in,
first-out' policy.
It offers the opposite functionality of a stack which follows the 'last-in, first-out' policy.
'first-in, first-out' means that the first value that has been enqueued/added to the queue is the first that gets dequeued/removed.
The usage of a queue is quite simple. You have one method to add/enqueue values enqueue and one method to remove/dequeue them dequeue. You can also peek at the beginning of the queue to see what value has been added/enqueued at first without removing it peek.
If you want to iterate over the values of the queue you can either use the iterator returned by the iterator method or the array that contains the queue's values returned by the toArray method.
The two methods isEmpty and size let you find out whether the queue contains values and how many values it contains.
Example:
// the queue gets set up
var queue:Queue = new MyQueue();
queue.enqueue("value1");
queue.enqueue("value2");
queue.enqueue("value3");
// the queue gets used somewhere in your application
trace(queue.peek()); // traces the first element without removing it
while (!queue.isEmpty()) {
trace(queue.dequeue());
}
Output:
value1 value1 value2 value3
public function enqueue(value):Void
Adds the passed-in value
to this queue.
value | the value to add |
public function dequeue(Void)
Removes and returns the firstly inserted value.
the firstly inserted value
EmptyDataHolderException | if this queue is empty |
public function peek(Void)
Returns the firstly inserted value.
the firstly inserted value
EmptyDataHolderException | if this queue is empty |
public function iterator(Void):Iterator
Returns an iterator to iterate over the values of this queue.
an iterator to iterate over this queue's values
public function isEmpty(Void):Boolean
Returns whether this queue contains any values.
true
if this queue contains no values else false
public function size(Void):Number
Returns the number of enqueued elements.
the number of enqueued elements
public function toArray(Void):Array
Returns the array representation of this queue.
The elements are copied onto the array in a 'first-in, first-out' order, similar to the order of the elements returned by a succession of calls to the dequeue method.
the array representation of this queue