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

©Copyright 1996 Rogue Wave Software


Alternate template: Standard C++ Library not required


#include <rw/tvhdict.h>
unsigned hashFun(const K&);
RWTValHashDictionary<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 RWTValHashMap described in the Class Reference.


RWTValHashDictionary<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 value based collection: 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. Classes K and V must have:

In addition, class K must have

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 an expensive proposition because not only must all the items be copied into the new buckets, but all of the keys must be rehashed.

If you wish 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.




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

main()  { 
  RWTValHashDictionary<RWCString, RWDate>     birthdays(RWCString::hash);

    RWDate(12, "April", 1975)
  birthdays.insertKeyAndValue("Ivan", RWDate(2, "Nov", 1980));

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

  // Print a birthday:
  cout << birthdays["John"] << endl;
  return 0;

Program output:

April 12, 1975

Public Constructors

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

Public Operators

operator=(const RWTValHashDictionary<K,V>& dict);
operator[](const K& key);

Public Member Functions

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