Rogue Wave banner
Previous fileTop of documentContentsIndexNext file

RWTValHashDictionary<K,V>

Alternative interface: no Standard C++ Library

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

Synopsis

#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.


Description

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.

Persistence

None

Example

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

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

  birthdays.insertKeyAndValue(
    "John",
    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>& 
                          dict);

Public Operators

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

Public Member Functions

void
applyToKeyAndValue(void (*applyFun)(const K&, V&,void*),
                   void* d);
void
clear();
RWBoolean
contains(const K& key) const;
size_t
entries() const;
RWBoolean
find(const K& target, K& retKey) const;
RWBoolean
findValue(const K& key, V& retVal) const;
RWBoolean
findKeyAndValue(const K& key, K& retKey,V& retVal) const;
void
insertKeyAndValue(const K& key, const V& value);
RWBoolean
isEmpty() const;
RWBoolean
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.