org.as2lib.core.BasicClass +--org.as2lib.data.holder.stack.SimpleStack
Stack
SimpleStack
holds data in a 'last-in, first-out' manner.
It is a simple implementation of the Stack
interface and realizes all
its basic concepts.
'last-in, first-out' means that the last value that has been pushed to the stack is the first that is popped from the stack.
The usage of a stack is quite simple. You have one method to push values, push, and one method to pop values, pop. You can also peek at the top of the stack to see what's the last value that has been pushed to the stack without removing it peek.
If you want to iterate over the values of the stack you can either use the iterator returned by the iterator method or the array that contains the stack's values returned by the toArray method.
The two methods isEmpty and size let you find out whether the stack contains values and how many values it contains.
You can modify the string representation that is returned by the toString method using the static setStringifier method.
Example:
// the stack is constructed somewhere
var stack:Stack = new SimpleStack();
stack.push("value1");
stack.push("value2");
stack.push("value3");
// the stack is used
trace(stack.peek());
while (!stack.isEmpty()) {
trace(stack.pop());
}
Output:
value3 value3 value2 value1
You can alternatively pass-in the content of the stack on construction.
var stack:Stack = new SimpleStack(["value1", "value2", "value3"]);
// ..
new SimpleStack()
public function SimpleStack(source:Array)
Constructs a new SimpleStack
instance.
The stack steps through the passed-in source
beginning at position
0 and pushes all contained elements to this stack.
var stack:SimpleStack = new SimpleStack([1, 2, 3]);
while (!stack.isEmpty()) {
trace(stack.pop());
}
The output is made in the following order: 3, 2, 1
source | (optional) an array that contains values to populate this stack with |
static public function getStringifier(Void):Stringifier
Returns the stringifier that stringifies stacks.
If no stringifier has been set manually the default stringifier will be returned which is an instance of class StackStringifier.
the stringifier that stringifies stacks
static public function setStringifier(stackStringifier:Stringifier):Void
Sets the new stringifier that stringifies stacks.
If the passed-in stackStringifier
is null
or undefined
the static getStringifier method will return the default stringifier.
stackStringifier | the new stack stringifier |
public function push(value):Void
Pushes the passed-in value
to this stack.
null
or undefined
values are allowed.
value | the value to push to this stack |
public function pop(Void)
Removes and returns the lastly pushed value.
the lastly pushed value
EmptyDataHolderException | if this stack is empty |
public function peek(Void)
Returns the lastly pushed value without removing it.
the lastly pushed value
EmptyDataHolderException | if this stack is empty |
public function iterator(Void):Iterator
Returns an iterator to iterate over the values of this stack.
an iterator to iterate over this stack
public function isEmpty(Void):Boolean
Returns whether this stack is empty.
true
if this stack is empty else false
public function size(Void):Number
Returns the number of pushed values.
the number of pushed values
public function toArray(Void):Array
Returns the array representation of this stack.
The elements are copied onto the array in a 'last-in, first-out' order, similar to the order of the elements returned by a succession of calls to the pop method.
the array representation of this stack
public function toString():String
Returns the string representation of this stack.
The string representation is obtained using the stringifier returned by the static getStringifier method.
the string representation of this stack
toString() in org.as2lib.core.BasicInterface