The Basic Stack Constructor
stack returned by SimpleStack
arbitrary Maple expression (not an expression sequence)
positive integer bounding the maximums size of the stack
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·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.
macro( Stack = MeteredStack ):
macro( Stack = SimpleStack ):
For a non-object-oriented stack implementation, see the stack package.
S ≔ SimpleStack⁡:
Error, (in top) empty stack
MathFolks ≔ MeteredStack⁡:
B ≔ BoundedStack⁡10:
Error, (in push) stack is full
Download Help Document
What kind of issue would you like to report? (Optional)