SourcePro® API Reference Guide

 
List of all members | Public Member Functions | Static Public Member Functions

Corresponds to the Smalltalk class Bag, representing a group of unordered elements not accessible by an external key. More...

#include <rw/rwbag.h>

Inheritance diagram for RWBag:
RWCollection RWCollectable

Public Member Functions

 RWBag ()
 
 RWBag (size_t n)
 
 RWBag (const RWBag &b)
 
 RWBag (RWBag &&b)
 
virtual ~RWBag ()
 
virtual void apply (RWapplyCollectable ap, void *)
 
size_t buckets () const
 
virtual void clear ()
 
virtual void clearAndDestroy ()
 
virtual RWCollectablecopy () const
 
virtual size_t entries () const
 
virtual RWCollectablefind (const RWCollectable *target) const
 
virtual RWCollectableinsert (RWCollectable *c)
 
RWCollectableinsertWithOccurrences (RWCollectable *c, int n)
 
virtual RWClassID isA () const
 
virtual bool isEmpty () const
 
virtual bool isEqual (const RWCollectable *a) const
 
virtual RWConstIteratornewConstIterator () const
 
virtual RWIteratornewIterator ()
 
virtual RWCollectablenewSpecies () const
 
virtual size_t occurrencesOf (const RWCollectable *target) const
 
RWBagoperator= (const RWBag &b)
 
RWBagoperator= (RWBag &&b)
 
bool operator== (const RWBag &b) const
 
virtual RWCollectableremove (const RWCollectable *target)
 
virtual void removeAndDestroy (const RWCollectable *target)
 
void resize (size_t n=0)
 
void swap (RWBag &b)
 
- Public Member Functions inherited from RWCollection
virtual ~RWCollection ()
 
RWBag asBag () const
 
RWBinaryTree asBinaryTree () const
 
RWOrdered asOrderedCollection () const
 
RWSet asSet () const
 
RWBinaryTree asSortedCollection () const
 
virtual RWspace binaryStoreSize () const
 
virtual bool contains (const RWCollectable *target) const
 
void operator+= (const RWCollection &c)
 
void operator-= (const RWCollection &c)
 
virtual void restoreGuts (RWvistream &)
 
virtual void restoreGuts (RWFile &)
 
virtual void saveGuts (RWvostream &) const
 
virtual void saveGuts (RWFile &) const
 
RWCollectionselect (RWtestCollectable tst, void *vp) const
 
- Public Member Functions inherited from RWCollectable
virtual ~RWCollectable ()
 
virtual int compareTo (const RWCollectable *) const
 
virtual unsigned hash () const
 
RWspace recursiveStoreSize () const
 
RWStringID stringID () const
 

Static Public Member Functions

static RWClassID classIsA ()
 
- Static Public Member Functions inherited from RWCollectable
static RWClassID classID (const RWStringID &name)
 
static RWClassID classIsA ()
 
static bool isAtom (RWClassID id)
 
static RWspace nilStoreSize ()
 

Additional Inherited Members

- Static Public Attributes inherited from RWCollection
static size_t DEFAULT_CAPACITY
 

Detailed 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 RWCollectable::hash() and RWCollectable::isEqual(). 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.

Synopsis
typedef RWBag Bag; // Smalltalk typedef .
#include <rw/rwbag.h>
Persistence
Polymorphic

Constructor & Destructor Documentation

RWBag::RWBag ( )

Constructs an empty bag with RWCollection::DEFAULT_CAPACITY buckets.

RWBag::RWBag ( size_t  n)

Constructs an empty bag with n buckets.

RWBag::RWBag ( const RWBag b)

Copy constructor. Makes a shallow copy of b.

RWBag::RWBag ( RWBag &&  b)

Move constructor. The constructed RWBag takes ownership of the data owned by b.

Condition:
This method is available only on platforms with rvalue reference support.
virtual RWBag::~RWBag ( )
virtual

Calls the clear() method.

Member Function Documentation

virtual void RWBag::apply ( RWapplyCollectable  ap,
void *   
)
virtual

Invokes the function pointer ap on each item in 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 function pointed to by ap should take no action that could change the hash value or the meaning of isEqual() for the items.

Implements RWCollection.

size_t RWBag::buckets ( ) const
inline

Returns the current number of buckets allocated to the internal hash table.

static RWClassID RWBag::classIsA ( )
static

Returns the RWClassID of this class.

virtual void RWBag::clear ( )
virtual

Removes all objects from the collection. Does not delete the objects themselves.

Implements RWCollection.

virtual void RWBag::clearAndDestroy ( )
virtual

Removes all objects from the collection and deletes them. Takes into account duplicate objects within a collection and only deletes them once. However, it does not take into account objects shared between different collections. Either do not use this function if you are sharing objects between separate collections, or put all collections that could be sharing objects into one single "super-collection" and call clearAndDestroy() on that.

Reimplemented from RWCollection.

virtual RWCollectable* RWBag::copy ( ) const
virtual

Returns a new, copy-constructed object of the same type as self. The caller is responsible for deleting the object.

Reimplemented from RWCollectable.

virtual size_t RWBag::entries ( ) const
inlinevirtual

Returns the total number of items in the collection.

Implements RWCollection.

virtual RWCollectable* RWBag::find ( const RWCollectable target) const
virtual

The first item that was inserted into the Bag and which equals target is returned, or rwnil if no item is found. Hashing is used to narrow the search.

Implements RWCollection.

virtual RWCollectable* RWBag::insert ( RWCollectable c)
virtual

Inserts the item c into the collection and returns it, or if an item was already in the collection that isEqual() to c, returns the old item and increments its count.

Implements RWCollection.

RWCollectable* RWBag::insertWithOccurrences ( RWCollectable c,
int  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 RWBag::isA ( ) const
virtual

Returns the unique id for RWBag.

Reimplemented from RWCollection.

virtual bool RWBag::isEmpty ( ) const
inlinevirtual

Returns true if the collection is empty, otherwise returns false.

Implements RWCollection.

virtual bool RWBag::isEqual ( const RWCollectable t) const
virtual

Behaves as if compareTo(t) was invoked, returning true if the result equals 0, false otherwise.

Reimplemented from RWCollectable.

virtual RWConstIterator* RWBag::newConstIterator ( ) const
virtual

Returns a const pointer to a dynamically allocated iterator for the collection.

Implements RWCollection.

virtual RWIterator* RWBag::newIterator ( )
virtual

Returns a dynamically allocated iterator for the collection.

Implements RWCollection.

virtual RWCollectable* RWBag::newSpecies ( ) const
virtual

Returns a new, default-constructed object of the same type as self. The caller is responsible for deleting the object.

Reimplemented from RWCollectable.

virtual size_t RWBag::occurrencesOf ( const RWCollectable target) const
virtual

Returns the number of items that are equal to the item pointed to by target.

Implements RWCollection.

RWBag& RWBag::operator= ( const RWBag b)

Assignment operator. Makes a shallow copy of b.

RWBag& RWBag::operator= ( RWBag &&  b)

Move assignment. Self takes ownership of the data owned by b.

Condition:
This method is available only on platforms with rvalue reference support.
bool RWBag::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.

virtual RWCollectable* RWBag::remove ( const RWCollectable target)
virtual

Removes and returns the item that isEqual() to the item pointed to by target. Returns rwnil if no item was found.

Implements RWCollection.

virtual void RWBag::removeAndDestroy ( const RWCollectable target)
virtual

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.

Reimplemented from RWCollection.

void RWBag::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 is picked automatically.

void RWBag::swap ( RWBag b)

Swaps the data owned by self with the data owned by b.

Copyright © 2023 Rogue Wave Software, Inc., a Perforce company. All Rights Reserved.