rwlogo
SourcePro C++ 12.0

SourcePro® C++ API Reference Guide



   SourcePro C++
Documentation Home

RWBag Class Reference
[RWCollectable-derived]

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

List of all members.

Public Member Functions

virtual RWCollectablenewSpecies () const
virtual RWClassID isA () const
 RWBag (size_t n=RWCollection::DEFAULT_CAPACITY)
 RWBag (const RWBag &b)
 RWBag (RWBag &&b)
virtual ~RWBag ()
void operator= (const RWBag &b)
void operator= (RWBag &&b)
bool operator== (const RWBag &b) const
virtual void apply (RWapplyCollectable ap, void *)
virtual void clear ()
virtual void clearAndDestroy ()
virtual size_t entries () const
virtual RWCollectablefind (const RWCollectable *target) const
virtual RWCollectableinsert (RWCollectable *c)
virtual bool isEmpty () const
virtual bool isEqual (const RWCollectable *a) const
virtual size_t occurrencesOf (const RWCollectable *target) const
virtual RWCollectableremove (const RWCollectable *target)
virtual void removeAndDestroy (const RWCollectable *target)
virtual RWIteratornewIterator ()
virtual RWConstIteratornewConstIterator () const
RWCollectableinsertWithOccurrences (RWCollectable *c, int n)
void resize (size_t n=0)
size_t buckets () const
void swap (RWBag &b)

Static Public Member Functions

static RWClassID classIsA ()

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>
 RWBag h;

Persistence

Polymorphic


Constructor & Destructor Documentation

RWBag::RWBag ( size_t  n = RWCollection::DEFAULT_CAPACITY  ) 

Construct 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 only available 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]

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() 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.

Reimplemented from RWCollectable.

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 size_t RWBag::entries (  )  const [inline, virtual]

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 [inline, virtual]

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

Implements RWCollection.

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

Returns true if the collectable object "matches" the object at address t. The default definition is:

 return this == t;

i.e., both objects have the same address (a test for identity). The definition may be redefined in any consistent way.

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]

Allocates a new object off the heap of the same type as self and returns a pointer to it. You are responsible for deleting the object when done with it.

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.

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

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

Condition:
This method is only available on platforms with rvalue reference support.
void RWBag::operator= ( const RWBag b  ) 

Assignment operator. Makes a shallow copy of b.

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.

 All Classes Functions Variables Typedefs Enumerations Enumerator Friends

© Copyright Rogue Wave Software, Inc. All Rights Reserved.
Rogue Wave and SourcePro are registered trademarks of Rogue Wave Software, Inc. in the United States and other countries. All other trademarks are the property of their respective owners.
Contact Rogue Wave about documentation or support issues.