Previous Table of Contents Next


A SortedCollection, as discussed previously, is a collection that supports the removal of elements based on their index within the collection but does not allow an index to be specified when elements are added. This is necessary because the index of an item in a SortedCollection represents its position in the sort order of all the elements in a collection. It is not possible to determine an item’s sort position in the sort order until it is actually added. SortedCollection supports the protocol <extensibleCollection> for adding and remove elements without specifying an index and <sequencedContractibleCollection> for removing elements based on their index position. SortedCollection also defines the protocol for accessing and changing the sort blocks that determine the collection sort order:

Protocol <SortedCollection> Conforms to: <extensibleCollection>, <sequencedContractibleCollection>
Messages:
sortBlock ^ <block2> Returns the sort block that determines the order of the collection
sortBlock: <block2> Installs a new sort block and reorders the elements using it

An OrderedCollection is a dynamically sized, indexable collection that supports both the removal and the insertion of elements based on index positions. Like SortedCollection, the protocol for OrderedCollection conforms to <extensibleCollection> and <sequenced ContractibleCollection>. It adds messages for inserting elements based on index positions:

Protocol <OrderedCollection> Conforms to: <extensibleCollection>, <sequencedContractibleCollection>
Messages:
add: <Object> after: <Object> ^ <Object> Adds an object immediately after the first occurrence of another
add: <Object> afterIndex: <integer> ^ <Object> Adds an object immediately after a specified index position
add: <Object> before: <Object> ^ <Object> Adds an object immediately before the first occurrence of another
add: <Object> beforeIndex: <integer> ^ <Object> Adds an object immediately before a specified index position
addAll: <collection> after: <Object> ^ <collection> Adds all elements of the argument collection immediate after the first occurrence of the argument object
addAll: <collection> afterIndex: <integer> ^ <collection> Adds all elements of the argument collection immediate after the specified index position
addAll: <collection> before: <Object> ^ <collection> Adds all elements of the argument collection immediate before the first occurrence of the argument object
addAll: <collection> beforeIndex: <integer> ^ <Collection> Adds all elements of the argument collection immediate before the specified index position
addAllFirst: <collection> ^ <collection> Adds all elements of the argument collection to the front
addAllLast: <collection> ^ <collection> Adds all elements of the argument collection to the back
addFirst: <Object> ^ <Object> Adds an object to the front of the receiver collection
addLast: <Object> ^ <Object> Adds an object to the back of the receiver collection

OrderedCollection can be thought of as a double-ended queue with interior access. When viewed as a queue, the message addLast: adds an element at the back of the queue and the message removeLast removes elements from the front of the queue. OrderedCollections can also be used as a stack. In this case, addLast: serves as the push operation and removeLast serves as the pop operation.

4.8. Smalltalk Program Structure

We have now described the nature of Smalltalk objects and classes, the syntax of the Smalltalk language, and some of the predefined classes that are available to Smalltalk programmers. What remains to be described is how these elements are organized to support complete programs that are implemented using Smalltalk.

Smalltalk implementations that are modeled after Smalltalk-80 are structured around a virtual image. We will use the term classic Smalltalk to refer to such implementations. A virtual image is a persistent collection of dynamic Smalltalk objects. It includes the object’s executable behavior. A virtual image is used in conjunction with a virtual machine, which is a Smalltalk execution engine implemented as a systems program on some computer platform. When a virtual image is loaded onto a running virtual machine, a predetermined Smalltalk method is selected for execution. The virtual machine executes this method, and the method can then send messages that propagate execution throughout the methods of the objects within the virtual image. At any point, the state of the executing virtual image can be saved to a file in a persistent external form. A saved virtual image can be subsequently reloaded onto a virtual machine, and execution will continue from the point at which it was suspended when the virtual image was saved.

Using classic Smalltalk, an application program is usually distributed as a saved virtual image that contains all the classes, methods, and objects necessary to represent the program. A user executes such a program by loading it onto an appropriate virtual machine.


Previous Table of Contents Next