RWBag(3C++) RWBag(3C++)
NameRWBag - Rogue Wave library class
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 that 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.
Persistence
Polymorphic
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.
Public Member Operators
void
operator=(const RWBag& b);
Page 1
RWBag(3C++) RWBag(3C++)
Assignment operator. A shallow copy of b will be made.
RWBoolean
operator==(const RWBag& b) const;
Returns TRUE if self and bag b have the same number of total entries and
if for every key in self there is a corresponding key in b which isEqual
and which has the same number of entries.
Public Member Functions
virtual void
apply(RWapplyCollectable ap, void*);
Redefined from class RWCollection. This function has been redefined to
apply the user-supplied function pointed to by ap to each member of the
collection in a generally unpredictable order. If an item has been
inserted more than once (i.e., more than one item isEqual), then apply()
will be called that many times. The user-supplied function should not do
anything that could change the hash value or the meaning of "isEqual" of
the items.
virtual RWspace
binaryStoreSize() const;
Inherited from class RWCollection.
virtual void
clear();
Redefined from class RWCollection.
virtual void
clearAndDestroy();
Inherited from class RWCollection.
virtual int
compareTo(const RWCollectable* a) const;
Inherited from class RWCollectable.
virtual RWBoolean
contains(const RWCollectable* target) const;
Inherited from class RWCollection.
Page 2
RWBag(3C++) RWBag(3C++)
virtual size_t
entries() const;
Redefined from class RWCollection.
virtual RWCollectable*
find(const RWCollectable* target) const;
Redefined from class RWCollection. The first item that was inserted into
the Bag and which equals target is returned or nil if no item is found.
Hashing is used to narrow the search.
virtual unsigned
hash() const;
Inherited from class RWCollectable.
virtual RWCollectable*
insert(RWCollectable* c);
Redefined from class RWCollection. Inserts the item c into the
collection and returns it, or if an item was already in the collection
that isEqual to c, then returns the old item and increments its count.
RWCollectable*
insertWithOccurrences(RWCollectable* c,size_t n);
Inserts the item c into the collection with count n and returns it, or if
an item was already in the collection that isEqual to c, then returns the
old item and increments its count by n.
virtual RWClassID
isA() const;
Redefined from class RWCollectable to return __RWBAG.
virtual RWBoolean
isEmpty() const;
Redefined from class RWCollection.
virtual RWBoolean
isEqual(const RWCollectable* a) const;
Page 3
RWBag(3C++) RWBag(3C++)
Inherited from class RWCollectable.
virtual size_t
occurrencesOf(const RWCollectable* target) const;
Redefined from class RWCollection. Returns the number of items that are
equal to the item pointed to by target.
virtual RWCollectable*
remove(const RWCollectable* target);
Redefined from class RWCollection. Removes and returns the item that
isEqual to the item pointed to by target. Returns nil if no item was
found.
virtual void
removeAndDestroy(const RWCollectable* target);
Redefined from class RWCollection. Removes the item that isEqual to the
item pointed to by target. Destroys the item as well if it is the last
occurrence in the collection.
void
resize(size_t n = 0);
Resizes the internal hash table to have n buckets. The overhead for this
function is the hashing of every element in the collection. If n is
zero, then an appropriate size will be picked automatically.
virtual void
restoreGuts(RWvistream&);
virtual void
restoreGuts(RWFile&);
virtual void
saveGuts(RWvostream&) const;
virtual void
saveGuts(RWFile&) const;
Inherited from class RWCollection.
RWStringID
stringID();
(acts virtual) Inherited from class RWCollectable.
Page 4