|
|||||
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 counter value1 initial {2} output "The stack now contains %d(count1)%n" repeat output "Pushing %d(value1) on to the stack." set new count1 to value1 output " The stack now contains" repeat over count1 output " %d(count1)" again output "%n" increment value1 exit when value1 = 10 again ; Pop all of the items off a stack process repeat exit when number of count1 = 0 output "Popping %d(count1) from the stack." remove count1 output " The stack now contains" repeat over count1 output " %d(count1)" again output "%n" again
You will notice that this program simply adds and removes items from the "count1" 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:
global counter count1 variable process local counter value1 initial {2} output "The queue now contains %d(count1)%n" repeat output "Pushing %d(value1) on to the queue." set new count1 to value1 output " The queue now contains" repeat over count1 output " %d(count1)" again output "%n" increment value1 exit when value1 = 10 again ; Pop all of the items off a queue process repeat exit when number of count1 = 0 using count1 item 1 output "Popping %d(count1) from the queue." remove count1 item 1 ; this line specifies that the first item ; on the shelf should be removed ; rather than the default lastmost ; item output " The queue now contains" repeat over count1 output " %d(count1)" 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.
---- |