WWC snapshot of http://www.alw.nih.gov/Docs/NIHCL/nihcl_44.html taken on Sat Jun 10 19:14:01 1995

Go to the previous, next section.

Set--Unordered Collection of Non-Duplicate Objects

SYNOPSIS

#include <nihcl/Set.h>

BASE CLASS

Collection

DERIVED CLASSES

Dictionary, IdentSet

RELATED CLASSES

Iterator

DESCRIPTION

A Set is an unordered collection of objects. The objects cannot be accessed by a key as can the objects in a Dictionary, for example. Unlike a Bag, in which equal objects may occur more than once, a Set ignores attempts to add any object that duplicates one already in the Set. A Set considers two objects to be duplicates if they are isEqual() to one another.

Class Set is implemented using a hash table with open addressing. Set::add() calls the virtual member function hash() and uses the number returned to compute an index to the hash table at which to begin searching, and isEqual() is called to check for equality.

CONSTRUCTORS

Set(unsigned capacity =DEFAULT_CAPACITY)
Constructs an empty Set that can hold up to capacity objects. An NIHCL_ALLOCSIZE error is raised if capacity is not greater than 0. If an attempt is made to add more than capacity objects to a Set, reSize() is called to increase its capacity. Since this is an expensive operation, it is wise to supply a reasonable estimate for capacity.

ADDING OBJECTS

virtual Object* add(Object& ob)
Adds the object ob to this Set and returns a pointer to either a duplicate (i.e., isEqual()) object already in this Set, or a pointer to ob if there was not already a duplicate object in this Set.

REMOVING OBJECTS

virtual Object* remove(const Object& ob)
Removes the object that is isEqual() to ob from this Set and returns a pointer to the object removed. If an object equal to ob does not occur in the Set, an NIHCL_REMOVEERR is raised.

virtual void removeAll()
Removes all occurrences of all objects in this Set.

SEARCHING

virtual Object* findObjectWithKey(const Object& ob) const
Searches this Set to find an object that is isEqual() to ob. Returns a pointer to the Nil object if an equal object is not found.

virtual unsigned hash() const
Returns a number that classes such as Set can use as a hash table probe. Class Set implements hash() by exclusive ORing the results of applying hash() to all objects in the Set.

virtual unsigned occurrencesOf(const Object& ob) const
Returns 1 if an object that is isEqual() to ob occurs in this Set; otherwise,
occurrencesOf() returns 0.

RELATIONAL OPERATORS

bool operator==(const Set&) const
bool operator!=(const Set&) const
Returns YES if the right Set operand is equal to (or not equal to) the left Set operand. Two Sets a and b are equal if they contain the same number of objects and if, for each object in a, there is an isEqual() object in b.

LOGICAL OPERATORS

Set operator&(const Set&) const
Returns a Set containing all objects that are in both the left and right operand Sets. For example:

Set s,t;
Integer i(0),j(1),k(2);
s.add(i); s.add(j);     // s contains 0,1
t.add(j); t.add(k);     // t contains 1,2
cout << (s&t);          // prints 1

Set operator|(const Set&) const
Returns a Set containing all objects that are in either the left or right operand Sets. For example:

Set s,t;
Integer i(0),j(1),k(2);
s.add(i); s.add(j);     // s contains 0,1
t.add(j); t.add(k);     // t contains 1,2
cout << (s|t);          // prints 0 1 2

Set operator-(const Set&) const
Returns a Set that contains all objects that are in the left operand Set and not in the right operand Set. For example:

Set s,t;
Integer i(0),j(1),k(2);
s.add(i); s.add(j);     // s contains 0,1
t.add(j); t.add(k);     // t contains 1,2
cout << (s-t);          // prints 0

TESTING SETS

virtual bool isEqual(const Object& a) const
Returns YES if this Set and the argument object are comparable and they have equal values. The argument object a is comparable if it returns the address of class Set's class descriptor for its species(). Class Set implements isEqual() by applying isEqual() to all objects in a Set.

COPYING SETS

virtual void deepenShallowCopy()
Converts this shallow copy to a deep copy by applying deepCopy() to all the object pointers, replacing each pointer with a pointer to its copy.

INDEXING SETS

virtual Object*& at(int i)
virtual const Object *const& at(int i) const
Return a reference to the ith object pointer in the hash table used by this Set. This will be a pointer to either the Nil object (if the ith hash table position is unoccupied), or a pointer to the object that hashes to the ith slot. This is an implementation dependent function not recommended for general use.

SET CAPACITY AND SIZE

virtual unsigned capacity() const
Returns the number of objects that this Set can hold before it will need to be expanded.

virtual unsigned size() const
Returns the number of objects in this Set.

virtual void reSize(unsigned newSize)
Changes the capacity of this Set to newSize. If newSize is less than or equal to capacity(), then the capacity is not changed.

SUPPORT FOR ITERATORS

virtual Object* doNext(Iterator& pos) const
Returns a pointer to the next object in this Set, or 0 if no more objects remain. The Iterator argument pos maintains the current position in the Set.

SET SPECIES

virtual const Class* species() const
Returns a pointer to the class descriptor for class Set.

PROTECTED MEMBERS

Member Variables

unsigned mask
The capacity of this Set, which must be a power of 2, minus 1.

ArrayOb contents
The ArrayOb used as a hash table by this Set.

Object I/O

virtual void storer(OIOofd& fd) const
virtual void storer(OIOout& strm) const
Store the objects contained in this Set to fd or strm by applying storeOn() to each of them.

Miscellaneous

virtual int findIndexOf(const Object& ob) const
Searches this Set for an object that is isEqual() to ob and returns the index of this object, if found, or the index of an empty slot in the hash table where ob should be inserted. Derived classes such as IdentDict and IdentSet redefine findIndexOf() to implement searching and comparison based on other criteria, such as an object's address.

int h(unsigned long k) const
Converts a hash table probe, as returned by hash(), into a hash table index.

virtual unsigned hashOf(const Object& ob) const
Computes a number suitable for use as a hash table probe by applying hash() to ob. Derived classes such as IdentDict and IdentSet redefine hashOf() to implement hashing based on other criteria, such as an object's address.

unsigned setCapacity(unsigned capacity)
Establishes the capacity of this Set. Rounds capacity up to the next higher power of two, if necessary.

DISABLED MEMBER FUNCTIONS

virtual int compare(const Object&) const
This function is implemented as shouldNotImplement().

EXCEPTIONS RAISED

NIHCL_ALLOCSIZE, NIHCL_REMOVEERR

Go to the previous, next section.