Comparators in context

To understand comparators, it's helpful to see a collection in it's full context. Figure 1 shows the relationship between a collection, and it's three primary components.


A collection relies on three related components: a comparator, a streamer, and an iterator. You've seen iterators in previous sections and should be aware that iterators are used to process each element of a collection in some order related to the type of collection, and possibly the order in which elements are placed in the collection. The easiest way to create an iterator for a collection is to first create the collection. Once the collection is created you can use the CreateIterator member function to create an iterator. At that point you interact with the iterator directly to gain access to collection elements. This is possible because the iterator maintains a reference to the collection over which it is iterating.

Streamers

Streamers deserve a special mention here, because both comparators and streamers are specified as arguments in constructors used to create collections. As mentioned earlier, a NIL can be specified for the comparator and streamer alike. If NIL is specified, for either argument, it is taken to mean that the default behaviors for comparison and streaming are desired. Most of the time, however, you want to specify a behavior for comparison by providing a comparator.

It is your responsibility as a client of a collection to specify (and possibly create) both a comparator and a streamer at the time you instantiate a collection. The comparator and streamer are passed as the first and second arguments to the constructor for the collection.

        constructor(comparator, streamer); // collection adopts both

Comparators can determine order and relation in addition to equality

Once the collection is created, it has references to both the comparator and the streamer. The comparator is used to identify and order collection elements. Sets, for example, make use of their comparators every time a new element is added. Because sets do not allow duplicate elements, each time a new element is added, the set's comparator checks to see if an identical element is already in the collection. If an identical element exists, the new element being inserted into the collection replaces the old one if the replace option is turned on. You can also turn the replace option off in which case an attempts to add duplicate elements to a set have no effect. In the context of sets, comparators are used to determine equality or identity of two collection elements.

Comparators are also used to establish equality or identity when searching for collection elements, testing for membership, or counting occurrences of an element within a collection.

Sorted collections make use of their comparators each time an element is added to place the new element in the proper position in relation to other elements already in the collection. In the context of sorted collections, comparators are used to determine the order of two elements in relation to one another.


[Contents] [Previous] [Next]
Click the icon to mail questions or corrections about this material to Taligent personnel.
Copyright©1995 Taligent,Inc. All rights reserved.

Generated with WebMaker