org.as2lib.core.BasicClass +--org.as2lib.data.holder.queue.LinearQueue
Queue
LinearQueue
stores values in a 'first-in, first-out' manner.
This class is a linear implementation of the Queue
interface. This
means that enqueued values are stored in a linear manner and that you can store
as many values as you please. There are also queues that store values in a cyclic
manner. These queues can normally only hold a prescribed number of values and
overwrite old values or throw an exception if you try to enqueue more values.
'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.
You can modify the string representation that is returned by the toString method with the static setStringifier method.
Example:
// construct the queue
var queue:Queue = new LinearQueue();
queue.enqueue("value1");
queue.enqueue("value2");
queue.enqueue("value3");
// use the queue
trace(queue.peek());
while (!queue.isEmpty()) {
trace(queue.pop());
}
Output:
value1 value1 value2 value3
You can alternatively pass-in the content of the queue on construction.
var queue:Queue = new LinearQueue(["value1", "value2", "value3"]);
// ..
new LinearQueue()
public function LinearQueue(source:Array)
Constructs a new LinearQueue
instance.
The queue steps through the passed-in source
beginning at position 0
and enqueues all contained elements.
Example:
var queue:LinearQueue = new LinearQueue([1, 2, 3]);
while (!queue.isEmpty()) {
trace(queue.dequeue());
}
The output is made in the following order: 1, 2, 3
source | (optional) an array that contains values to populate this queue with |
static public function getStringifier(Void):Stringifier
Returns the stringifier that stringifies queues.
If no stringifier has been set manually via the static setStringifier method an instance of class QueueStringifier will be returned.
the stringifier that stringifies queues
static public function setStringifier(queueStringifier:Stringifier):Void
Sets the new stringifier that stringifies queues.
If the passed-in queueStringifier
is null
or undefined
,
the static getStringifier method will return the default stringifier.
queueStringifier | the new queue stringifier |
public function enqueue(value):Void
Adds the passed-in value
to this queue.
null
and undefined
values are allowed.
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 that can be used 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 an 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
public function toString():String
Returns the string representation of this queue.
The string representation is obtained via the stringifier returned by the static getStringifier method.
the string representation of this queue
toString() in org.as2lib.core.BasicInterface