Rogue Wave banner
Previous fileTop of documentContentsIndexNext file

RWTPtrHashDictionary<K,V>

Alternative interface: no Standard C++ Library

Data Type and Member Function Indexes
(exclusive of constructors and destructors)

Synopsis

#include <rw/tphdict.h>
unsigned hashFun(const K&);
RWTPtrHashDictionary<K,V> dictionary(hashFun);

Please Note!


If you do not have the Standard C++ Library, use the interface described here. Otherwise, use the interface to RWTPtrHashMap described in the Class Reference.


Description

RWTPtrHashDictionary<K,V> is a dictionary of keys of type K and values of type V, implemented using a hash table. While duplicates of values are allowed, duplicates of keys are not.

It is a pointer based collection: pointers to the keys and values are copied in and out of the hash buckets.

Parameters K and V represent the type of the key and the type of the value, respectively, to be inserted into the table. These can be either classes or fundamental types. Class K must have

Class V can be of any type.

A user-supplied hashing function for type K must be supplied to the constructor when creating a new table. If K is a Rogue Wave class, then this requirement is usually trivial because most Rogue Wave objects know how to return a hashing value. In fact, classes RWCString, RWDate, RWTime, and RWWString contain static member functions called hash that can be supplied to the constructor as is. The function must have prototype:

unsigned hFun(const K& a);

and should return a suitable hash value for the object a.

To find a value, the key is first hashed to determine in which bucket the key and value can be found. The bucket is then searched for an object that is equal (as determined by the equality operator) to the key.

The initial number of buckets in the table is set by the constructor. There is a default value. If the number of (key/value) pairs 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 is relatively expensive because all of the keys must be rehashed.

If you wish for this to be done automatically, then you can subclass from this class and implement your own special insert() and remove() functions which perform a resize() as necessary.

Persistence

None

Example

#include <rw/tphdict.h>
#include <rw/cstring.h>
#include <rw/rwdate.h>
#include <rw/rstream.h>

main()  { 
  RWTPtrHashDictionary<RWCString, RWDate>
    birthdays(RWCString::hash);
  birthdays.insertKeyAndValue
    (new RWCString("John"),
     new RWDate(12, "April", 1975)
    );
  birthdays.insertKeyAndValue
    (new RWCString("Ivan"),
     new RWDate(2, "Nov", 1980)
    );

  // Alternative syntax:
  birthdays[new RWCString("Susan")] = 
    new RWDate(30, "June", 1955);
  birthdays[new RWCString("Gene")] =
    new RWDate(5, "Jan", 1981);

  // Print a birthday:
  RWCString key("John");
  cout << *birthdays[&key] << endl;

  birthdays.clearAndDestroy();
  return 0;
}

Program output:

April 12, 1975

Public Constructors

RWTPtrHashDictionary<K,V>(unsigned (*hashKey)(const K&),
                       size_t buckets = RWDEFAULT_CAPACITY);
RWTPtrHashDictionary<K,V>(const RWTPtrHashDictionary<K,V>& c);

Public Operators

RWTPtrHashDictionary<K,V>&
operator=(const RWTPtrHashDictionary<K,V>& c);
V*&
operator[](K* key);

Public Member Functions

void
applyToKeyAndValue( void (*applyFun)(K*,V*&,void*),void* d);
void
clear();
void
clearAndDestroy();
RWBoolean
contains(const K* key) const;
size_t
entries() const;
K*
find(const K* key) const;
V*
findValue(const K* key) const;
K*
findKeyAndValue(const K* key, V*& retVal) const;
void
insertKeyAndValue(K* key, V* value);
RWBoolean
isEmpty() const;
K*
remove(const K* key);
void
resize(size_t N);


Previous fileTop of documentContentsIndexNext file
Return to the main class reference
©Copyright 1999, Rogue Wave Software, Inc.
Send mail to report errors or comment on the documentation.