Previous Table of Contents Next


Although <collection> provides messages for iterating over the elements of a collection, it does not provide any messages for adding, removing, or accessing individual objects in a collection. This is because the applicabil-ity of these operations and the appropriate messages for performing them depends on the characteristics of the collection. Several different protocols are used to define these sets of messages.

The protocol <extensibleCollection> defines the messages that can be used to add and remove elements from non-keyed, variable-sized collection:

Protocol <extensibleCollection> Conforms to: <collection>
Messages:
add: <Object> Adds the argument to the receiver
addAll: <collection> Adds all elements of the argument to the receiver
remove: <Object> ^ <Object> Removes the argument from the receiver
remove: <Object> ifAbsent: <block0>^ <Object> Removes the argument from the receiver or evaluate the block
removeAll: <collection> Removes all elements of the argument from the receiver

Instances of Bag, Set, OrderedCollection, and SortedCollection all support the <extensibleCollection> protocol. The message add: is used to add individual elements, and addAll: is used to add a group of elements. How the elements are processed by the collection depends on the class of the collection. The remove: messages deletes objects from collections. Here’s an example:

  b := Bag new.
  b addAll: #( 6 4 2 2 4 6).
  ^b
      Bag(2 2 4 4 6 6) “ordering is implementation dependent”
  s := Set new.
  s addAll: #( 6 4 2 2 4 6).
  ^s
      Set(2 4 6) “ordering is implementation dependent”
  b remove: 4.0
      Bag(2 2 4 6 6)

How the elements are processed by the collection depends on the class of the collection. Adding duplicate elements to a Bag adds the elements, whereas adding them to a Set ignores the duplicates. Objects are selected using = as an equality test. In the last example, the integer object 4 is removed from the Bag because it compares equal to the floating-point object 4.0.

Set and Bag are classes whose protocols conform precisely to <extensibleCollection>:

Protocol <Bag> Conforms to: <extensibleCollection>

Protocol <Set> Conforms to: <extensibleCollection>

Both Bag and Set are unordered collections, which means that there is no natural ordering of their elements. Any observed ordering is an implementation artifact on which programmers should not depend. Bag and Set differ only in how they deal with duplicate elements. Interestingly, Set does not provide protocols for set theoretic operations and does not have messages for union, intersection, or other functions on mathematical sets.

Dictionaries are also unordered collections. Dictionaries differ from bags and sets in that each element of a dictionary has an associated key that identifies it. The key is an object that is associated with an element when it is added to a dictionary and is used to subsequently access or delete the element. <abstractDictionary> defines the common protocol for all dictionaries:

Protocol <abstractDictionary> Conforms to: <Collection>
Messages:
addAll: <abstractDictionary> Adds each key/value pair in the argument to the receiver
at: <Object> ^ <Object> Returns the value associated with the argument key
at: <Object> ifAbsent: <block0> ^ <Object> Returns the value associated with the argument key; if there is no such key in the collection, evaluates the block
at: <Object> ifAbsentPut: <block0> ^ <Object> Returns the value associated with the argument key; if there is no such key, evaluates the block and puts it into the dictionary using the key
at: <Object> put: <Object> ^ <Object> The second argument is stored using the first argument as its key
includesKey: <Object> ^ <boolean> Tests if the receiver contains an element with this key
keyAtValue: <Object> ^ <Object> If the argument is an element of the dictionary, returns its key
keyAtValue: <Object> ifAbsent: <block0> ^ <Object> If the argument is an element of the dictionary, returns its key; if not, evaluates the block and returns its value
keys ^ <collection> Returns a collection containing all the keys in the receiver
keysAndValuesDo: <block2> The argument is a two-argument block; for each element of the receiver, evaluates the block using its key as the first argument and the element as the second’s argument
keysDo: <block1> Evaluates the block using each key as its argument
removeKey: <Object> ^ <Object> Removes the element whose key is the argument
removeKey: <Object> ifAbsent: <block0> ^ <Object> Removes the element whose key is the argument; if no such key exists, evaluates the block
values ^ <collection> Returns a collection of all elements in the receiver


Previous Table of Contents Next