|
|||||
|
|||||
Stacks and queues |
OmniMark shelves, in addition to having all of the characteristics of arrays and associative arrays, also have the properties of stacks and queues.
A stack is a type of data container which operates under the basic "FILO" (First In Last Out) principle. When you add two items to a stack, for example, you have to remove the second item before you can access the first. The default behavior of the currently selected item on a shelf makes it easy to create stack-like shelves in OmniMark. Quite simply, if you do not explicitly state that actions should be performed on a different shelf item, actions will be carried out on the default currently selected item which is the lastmost item on a shelf.
For example, the following program illustrates how OmniMark shelves act like stacks:
process local stream name-stack variable initial {"Tom", "Dick"} output "The stack now contains%n" repeat over name-stack output name-stack || "%n" again output "%n" set new name-stack to "Harry" output "Pushed " || name-stack || " on to the stack.%n%n" output "The stack now contains%n" repeat over name-stack output name-stack || "%n" again output "%n" ; Pop all of the items off a stack repeat exit when number of name-stack = 0 output "Popping " || name-stack || " from the stack.%n%n" remove name-stack output "The stack now contains%n" repeat over name-stack output name-stack || "%n" again output "%n" again
You will notice that this program simply adds and removes items from the "name-stack" shelf at the default item.
A queue is like a stack except it operates under the "FIFO" (First In First Out) principle. If you add two items to a queue, you have to remove the first item before you can access the second. To create a queue-like shelf in OmniMark, you need only specify that all actions are performed on the first item on the shelf (as opposed to the default lastmost item). Any new items should still be added to the shelf at the default lastmost position.
The following program illustrates an OmniMark shelf acting like a queue:
process local stream name-queue variable initial {"Tom", "Dick"} output "The queue now contains%n" repeat over name-queue output name-queue || "%n" again output "%n" set new name-queue to "Harry" output "Pushed " || name-queue || " on to the queue.%n%n" output "The queue now contains%n" repeat over name-queue output name-queue || "%n" again output "%n" ; Pop all of the items off a stack repeat exit when number of name-queue = 0 output "Popping " || name-queue[1] || " from the queue.%n%n" remove name-queue[1] output "The queue now contains%n" repeat over name-queue output name-queue || "%n" again output "%n" again
The only real difference between the programs is that the first program removed items from the shelf which were at the default lastmost position, while the second removed items from the shelf which existed at position "1" on the shelf.
Copyright © Stilo International plc, 1988-2010.