Tools.h++ Manual

104011 Tandem Computers Incorporated 21-3
21
RWBag
RWBag
|
RWCollection
|
RWCollectable
Synopsis
typedef RWBag Bag; // Smalltalk typedef.
#include <rw/rwbag.h>
RWBag h;
Description Class
RWBag
corresponds to the
Smalltalk
class
Bag
. It represents a group of
unordered elements, not accessible by an external key. Duplicates are allowed.
An object stored by
RWBag
must inherit abstract base class
RWCollectable
,
with suitable definition for virtual functions
hash()
and
isEqual()
(see class
RWCollectable
). The function
hash()
is used to find objects with the same
hash value, then
isEqual()
is used to confirm the match.
Class
RWBag
is implemented by using an internal hashed dictionary
(
RWHashDictionary
) which keeps track of the number of occurrences of an
item. If an item is added to the collection that compares equal (
isEqual
) to an
existing item in the collection, then the count is incremented.
Note – This means that only the first instance of a value is actually inserted:
subsequent instances cause the occurrence count to be incremented. This behavior
parallels the
Smalltalk
implementation of
Bag
.
Member function
apply()
and the iterator are called repeatedly according to
the count for an item.
See class
RWHashTable
if you want duplicates to be stored, rather than merely
counted.
Public constructors
RWBag(size_t n = RWDEFAULT_CAPACITY);
Construct an empty bag with
n
buckets.
RWBag(const RWBag& b);
Copy constructor. A shallow copy of
b
will be made.