SimpleStack - Maple Help

Online Help

All Products    Maple    MapleSim


Home : Support : Online Help : Programming : Operations : Stacks : SimpleStack

SimpleStack

The Basic Stack Constructor

 

Calling Sequence

Parameters

Description

Examples

Calling Sequence

SimpleStack()

MeteredStack()

BoundedStack(bound::posint)

type(e::anything, 'Stack')

s:-push(e::anything)

s:-pop()

s:-empty()

s:-top()

s:-depth()

s:-init()

 

$include <Stack.mi>

Push(s::Stack, e::anything)

Pop(s::Stack)

Top(s::Stack)

EmptyP(s::Stack)

Parameters

s

-

stack returned by SimpleStack

e

-

arbitrary Maple expression (not an expression sequence)

bound

-

positive integer bounding the maximums size of the stack

Description

• 

The procedure SimpleStack is a stack constructor. It returns a Maple expression that implements a stack object, which is of type Stack.

• 

You can test whether a Maple expression e is a Stack object by using type( e, 'Stack' ). An expression is of type Stack if it is an object with the methods empty, top, push, and pop. Specific Stack implementations may support additional methods, but all Stacks support at least these four methods. Stacks built by the constructor SimpleStack are currently represented by modules, so the message passing syntax uses the :- operator.

• 

The empty method returns the value true if no items are on the stack, and returns the value false otherwise.

• 

To insert an item e (any Maple expression) on the top of a stack, use the push method. The pushed value is returned.

• 

Items may be removed from the top of the stack by using the pop method. An error is raised if the stack is empty. This error may be caught using the exception string "empty stack".

• 

The item on the top of a non-empty stack may be examined, without changing the contents of the stack, by using the method top. If the stack is non-empty, this method returns the item on the top of the stack (that value which will be returned by the next call to the pop method), and otherwise raises the "empty stack" exception.

• 

Stacks constructed by the SimpleStack constructor also support the methods depth, which returns the number of items on the stack, and init, which initializes the stack (and empties it).

• 

The standard include file <Stack.mi> defines several inline procedures for invoking the basic Stack operations. The procedures provided are Enqueue, Dequeue, EmptyP, and Front. These procedures are not part of the Maple library, and are provided only as inlined procedures. (Note: The include file Queue&period;mi also provides an EmptyP inlined procedure, but it is compatible with the one in <Stack.mi>, so both include files may be used in the same Maple source file.

• 

The BoundedStack constructor returns a bounded stack; that is, a stack whose maximum depth is bounded by a predetermined positive integer bound specified in the call to the constructor. Bounded stacks export the additional method full, which returns the value true if the stack is full (no more items can be pushed), and the value false otherwise.

• 

A stack which records the numbers of pushes, pops and the maximum stack depth achieved can be created using the MeteredStack constructor. It returns a stack that exports the additional method stats, which returns an expression sequence of three non-negative integers. The first is the total number of pushes effected since the stack was initialized. The second is the total number of pops since the most recent initialization. The third integer in the sequence is the maximum stack depth achieved since the last time the init method was called. The init method for a metered stack resets these counters to 0. Metered stacks are intended to be used for performance monitoring; the extra overhead of tracking the stack statistics imposes a noticeable performance penalty. A useful technique is to include code like the following in your Maple source code.

$include    <Stack.mi>
 $ifdef    _DEBUG_
 macro( Stack = MeteredStack ):
 $else
 macro( Stack = SimpleStack ):
 $endif

• 

For a non-object-oriented stack implementation, see the stack package.

Examples

SSimpleStack&colon;

typeS&comma;&apos;Stack&apos;

true

(1)

S:-push1&semi;S:-push2&semi;S:-push3

1

2

3

(2)

S:-depth

3

(3)

S:-top

3

(4)

S:-pop

3

(5)

S:-top

2

(6)

whilenotS:-emptydoS:-popend do

1

(7)

S:-depth

0

(8)

S:-top

Error, (in top) empty stack

MathFolksMeteredStack&colon;

MathFolks:-pushAlex&colon;MathFolks:-pushAllan&colon;MathFolks:-pushDave&colon;MathFolks:-pushDavid&colon;MathFolks:-pushEdgardo&colon;MathFolks:-pushJames&colon;MathFolks:-pushJuergen&colon;MathFolks:-pushLaurent&colon;MathFolks:-pushMichael&colon;MathFolks:-pushPaulina&colon;MathFolks:-pushRaqeeb&colon;MathFolks:-pushStephen&colon;whilenotMathFolks:-emptydoMathFolks:-popend do

Alex

(9)

MathFolks:-stats

12&comma;12&comma;12

(10)

BBoundedStack10&colon;

forito10doB:-pushiend do&colon;B:-pushone_more

Error, (in push) stack is full

See Also

heap

module

priqueue

Queue

queue

stack

type

 


Download Help Document

Was this information helpful?



Please add your Comment (Optional)
E-mail Address (Optional)
What is ? This question helps us to combat spam