Class org.as2lib.data.holder.stack.SimpleStack

org.as2lib.core.BasicClass
   +--org.as2lib.data.holder.stack.SimpleStack

Implemented Interfaces

Stack

Description

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"]); // ..

Method Index

new SimpleStack()

getStringifier(), isEmpty(), iterator(), peek(), pop(), push(), setStringifier(), size(), toArray(), toString()

Constructor Detail

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

Parameters

source(optional) an array that contains values to populate this stack with

Method Detail

getStringifier

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.

Return

the stringifier that stringifies stacks

setStringifier

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.

Parameters

stackStringifierthe new stack stringifier

push

public function push(value):Void

Pushes the passed-in value to this stack.

null or undefined values are allowed.

Parameters

valuethe value to push to this stack

Specified By

push() in org.as2lib.data.holder.Stack

pop

public function pop(Void)

Removes and returns the lastly pushed value.

Return

the lastly pushed value

Throws

EmptyDataHolderExceptionif this stack is empty

Specified By

pop() in org.as2lib.data.holder.Stack

peek

public function peek(Void)

Returns the lastly pushed value without removing it.

Return

the lastly pushed value

Throws

EmptyDataHolderExceptionif this stack is empty

Specified By

peek() in org.as2lib.data.holder.Stack

iterator

public function iterator(Void):Iterator

Returns an iterator to iterate over the values of this stack.

Return

an iterator to iterate over this stack

Specified By

iterator() in org.as2lib.data.holder.Stack

isEmpty

public function isEmpty(Void):Boolean

Returns whether this stack is empty.

Return

true if this stack is empty else false

Specified By

isEmpty() in org.as2lib.data.holder.Stack

size

public function size(Void):Number

Returns the number of pushed values.

Return

the number of pushed values

Specified By

size() in org.as2lib.data.holder.Stack

toArray

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.

Return

the array representation of this stack

Specified By

toArray() in org.as2lib.data.holder.Stack

toString

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.

Return

the string representation of this stack

Specified By

toString() in org.as2lib.core.BasicInterface

Overrides

toString() in org.as2lib.core.BasicClass