Although collections known as polymorphic arrays bear a similarity to arrays used in C and Pascal, they are different in subtle and substantial ways. For example polymorphic arrays are dynamic; they can grow in size automatically to accommodate new elements when they become full.
Dynamic arrays also provide bounds checking and other safety features which protect clients that try to access or change a collection element with an index value too small or too large for the collection. As with C arrays, indices for dynamic arrays start at 0. Array indices can be thought of as keys used to access values contained inside an array.
One major difference between an English language dictionary and a dictionary collection is that there can be no duplicate keys in a dictionary collection. Allowing no duplicate keys in a symbol table makes sense as this avoids the ambiguity for the definition of a word. Ambiguity is tolerable in spoken language but would cause chaos in a programming language. Note that the idea of duplicate may also be ambiguous. Are two different variables which contain the same integer values considered to be duplicates? The default answer is no. Unless you specify otherwise, duplicate items are items which have the same pointer addresses in memory. As a programmer you can override this definition of duplicate for a particular collection by defining a comparison mechanism (a comparator) used by a specific collection instance.
Another characteristic of sets is that they are unordered. When you scan through--or iterate over--a set, you are not likely to retrieve elements in the same order they are added to the set. The following sections will clarify the different characteristics of sets, dictionaries and sequences. You will also see sequences like queues, deques, and sorted sequences which are ordered collections. The ordering of elements in queues, deques (including stacks), and sorted sequences is different than the ordering of elements in arrays.
Figure 2 shows a breakdown of the conceptual behavior of different kinds of collection protocols supported by the CommonPoint application system. The diagram uses the general names of collection objects commonly associated with traditional data structures. You can follow the lines in this diagram as a decision tree to categorize collections according to use.
Figure 2 shows, that from a conceptual point of view, collections break down into two categories; they are either ordered, or unordered. In the CommonPoint application system ordered collections are called sequences and are more complex in nature than unordered collections. Once you have determined that you want a collection with a sense of order, the question is do you want the collection be ordered or sorted automatically according to some lexical or numerical comparison? If so, you want a sorted collection. If, on the other hand, you want a collection that is ordered according to how you put elements in the collection, you want either a queue, or an array. You would use a queue only if you need to retrieve elements from the beginning or end of the collection.
For example a stack is a special form of queue (often called a LIFO queue because the item which is the Last In to the queue will be the First Out when a request to retrieve the next element is made). Event queues and message queues are often implemented as FIFO queues because the order in which events are processed usually depends on the time of their occurrence. The item which is First In to a queue will be the First Out of the queue when a request is made to retrieve the next element.
In event processing systems, it is sometimes useful to prioritize events according to their urgency. For example a disk access may be given higher priority than a screen repaint. A system timer event would probably be given higher priority than a user menu command or keystroke. The events for such a prioritized queue would be sorted primarily by an internal mechanism (the rank of the event) and secondarily by external ordering (events of the same priority rank are processed in the order they arrive in the queue--that is the order in which the events occur in time).
Each of the CommonPoint classes implementing the behaviors described in the behavior diagram are covered in turn. First the general semantics for creating collections and for interacting with collections are discussed. The general protocol for collection management applies to all collection classes.
For an overview of general collection behavior, the sorting, ordering, and duplication characteristics of CommonPoint collection classes are listed in the following table. Only concrete classes are included.
Type of collection | Ordered | Allows Duplicates | |
TDequeOf | by insertion sequence | yes | |
TSortedSequenceOf | automatically | yes | |
TArrayOf | by numeric index | yes | |
TSetOf | no | no | |
TDictionaryOf | no | keys no, values yes | |
TPriorityQueueOf | partially | yes |