Click on the banner to return to the Class Reference home page.

©Copyright 1996 Rogue Wave Software

RWHashTable


RWHashTableRWCollectionRWCollectable

Synopsis

#include <rw/hashtab.h>
RWHashTable h ;

Description

This class is a simple hash table for objects inheriting from RWCollectable. It uses chaining (as implemented by class RWSlistCollectables) to resolve hash collisions. Duplicate objects are allowed.

An object stored by RWHashTable must inherit from the abstract base class RWCollectable, with suitable definition for virtual functions hash() and isEqual() (see class RWCollectable).

To find an object that matches a key, the key's virtual function hash() is first called to determine in which bucket the object occurs. The bucket is then searched linearly by calling the virtual function isEqual() for each candidate, with the key as the argument. The first object to return TRUE is the returned object.

The initial number of buckets in the table is set by the constructor. There is a default value. If the number of items in the collection greatly exceeds the number of buckets then efficiency will sag because each bucket must be searched linearly. The number of buckets can be changed by calling member function resize(). This will require that all objects be rehashed.

The iterator for this class is RWHashTableIterator.

Persistence

None

Example

#include <rw/hashtab.h>
#include <rw/colldate.h>
#include <rw/rstream.h>

main(){
 RWHashTable table;
 RWCollectableDate *july 
      = new RWCollectableDate(7, "July", 1990);
 RWCollectableDate *may
      = new RWCollectableDate (1, "May", 1977);
 RWCollectableDate *feb 
      = new RWCollectableDate (22, "Feb", 1983);
 RWCollectableDate *aug
      = new RWCollectableDate (2, "Aug", 1966);

 table.insert(july);
 table.insert(may);
 table.insert(feb);
 table.insert(aug);

 cout << "Table contains " << table.entries() << " entries.\n";
 RWCollectableDate key(22, "Feb", 1983);
 cout << "It does ";
 if (!table.contains(&key)) cout << "not ";
 cout << "contain the key " << key << endl;
 
 delete july;
 delete may;
 delete feb;
 delete aug;
 return 0;
}

Program output:

Table contains 4 entries.
It does contain the key February 22, 1983

Public Constructors

RWHashTable(size_t N = RWCollection::DEFAULT_CAPACITY);
RWHashTable(const RWHashTable& t);

Public Operators

void
operator=(const RWHashTable& t);
RWBoolean 
operator==(const RWHashTable& t) const;
RWBoolean 
operator<=(const RWHashTable& t) const;
RWBoolean 
operator!=(const RWHashTable&) const;

Member Functions

virtual void
apply(RWapplyCollectable ap, void*);
virtual RWspace
binaryStoreSize() const;
virtual void
clear();
virtual void
clearAndDestroy();
virtual int
compareTo(const RWCollectable*) const;
virtual RWBoolean
contains(const RWCollectable*) const;
virtual size_t
entries() const;
virtual RWCollectable*
find(const RWCollectable*) const;
virtual unsigned
hash() const;
virtual RWCollectable*
insert(RWCollectable* a);
virtual RWClassID
isA() const;
virtual RWBoolean
isEmpty() const;
virtual RWBoolean
isEqual(const RWCollectable*) const;
virtual RWCollectable*
newSpecies() const;
virtual size_t
occurrencesOf(const RWCollectable*) const;
virtual RWCollectable*
remove(const RWCollectable*);
virtual void
removeAndDestroy(const RWCollectable*); 
virtual void
resize(size_t n = 0);
virtual void
restoreGuts(RWvistream&);
virtual void
restoreGuts(RWFile&);
virtual void
saveGuts(RWvostream&) const;
virtual void
saveGuts(RWFile&) const;
RWStringID
stringID();