Bags Versus Sets Versus Hash Tables
Class
RWHashTable is the easiest Smalltalk-like collection class to understand. It uses a simple hashed lookup to find the
bucket where a particular object occurs, then does a linear search of the bucket to find the object. A key concept is that multiple objects that test
isEqual to each other can be inserted into a hash table.
Class
RWBag is similar to
RWHashTable, except that it
counts occurrences of multiple objects with the same value; that is, it retains only the first occurrence and merely increments an occurrence count for subsequent ones.
RWBag is implemented as a dictionary, where the key is the inserted object and the value is the occurrence count. This is the same way the Smalltalk
Bag object is implemented. Note that this implementation differs significantly from many other C++
Bag classes which are closer to the
RWHashTable class and not true
Bags.
Class
RWSet is similar to its base class
RWHashTable, except that it doesn't allow duplicates. If you try to insert an object that
isEqual to an object already in
RWSet, the object will be rejected.
Class
RWIdentitySet, which inherits from
RWSet, retrieves objects on the basis of
identity instead of value. Because
RWIdentitySet is a
Set, it can take only one
instance of a given object.
Note that the ordering of objects in any of these classes based on hash tables is not meaningful. If ordering is important, you should choose a sequenceable class.