Rogue Wave banner
Previous fileTop of documentContentsIndexNext file

RWTValHashMap<K,T,H,EQ>

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

Synopsis

#include <rw/tvhdict.h> 
RWTValHashMap<K,T,H,EQ> m;

Please Note!


If you have the Standard C++ Library, use the interface described here. Otherwise, use the interface for RWTValHashDictionary described in Appendix A.


Description

This class maintains a collection of keys, each with an associated item of type T. These pairs are stored according to a hash object of type H. H must provide a const hash function on elements of type K via public member

unsigned long operator()(const K& x) const;

Equivalent keys within the collection will be grouped together based on an equality object of type EQ. EQ must ensure this grouping via public member

bool operator()(const K& x, const K& y) const;

which should return true if x and y are equivalent.

Note: Any two keys that are equivalent must hash to the same value.

RWTValHashMap<K,T,H,EQ> will not accept a key that is equivalent to any key already in the collection. (RWTValHashMultiMap<K,T,H,EQ> may contain multiple keys that are equivalent to each other.) Equality is based on an equality object and not on the == operator. If your type has an == operator, then you may want to use the templatized equal_to function object provided by the Standard C++ Library; otherwise, you must define your own equality object.

The value type must have operator==() defined. This requirement is imposed by the Standard C++ Library.

Persistence

Isomorphic

Example

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

struct silly_h{
   unsigned long operator()(const RWCString& x) const
   { return x.length() * (long)x(0); }
};

main()  { 
  RWTValHashDictionary
    <RWCString, RWDate, silly_hash, equal_to<RWCString> > 
                              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

Related Classes

Class RWTValHashMultiMap<K,T,H,EQ> offers the same interface to a collection that accepts multiple keys that compare equal to each other.

Class rw_hashmap<K,T,H,EQ> is the C++-standard compliant collection that serves as the underlying implementation for this collection.

Public Typedefs

typedef rw_hashmap<K,T,H,EQ>                   container_type; 
typedef container_type::iterator               iterator;
typedef container_type::const_iterator         const_iterator;
typedef container_type::size_type              size_type;
typedef pair <const K,T>                       value_type; 
typedef K                                      key_type; 
typedef T                                      data_type; 
typedef pair <const K,T>&                      reference; 
typedef pair <const K,T>&                      const_reference; 

Public Constructors

RWTValHashMap<K,T,H,EQ>();
RWTValHashMap<K,T,H,EQ>(const rw_hashmap<K,T,H,EQ>& m);
RWTValHashMap<K,T,H,EQ>
(const H& h, size_type sz = RWDEFAULT_CAPACITY);
RWTValHashMap<K,T,H,EQ>(const RWTValHashMap<K,T,H,EQ>& rwm);
RWTValHashMap<K,T,H,EQ>(const value_type* first, 
     const value_type* last);

Public Member Operators

RWTValHashMap<K,T,H,EQ>&
operator=(const RWTValHashMap<K,T,H,EQ>& m); 
RWTValHashMap<K,T,H,EQ>&
operator=(const rw_hashmap<K,T,H,EQ>& m);
bool
operator==(const RWTValHashMap<K,T,H,EQ>& m) const; 
bool
operator==(const rw_hashmap<K,T,H,EQ>& m) const;
T&
operator[](const K& key); 

Public Member Functions

void
apply(void (*fn)(const K&, T&, void*),void* d); 
void
apply(void (*fn)(const K&,const T&,void*),void* d) const;
void
applyToKeyAndValue(void (*fn)(const K&, T&,void*),void* d); 
void
applyToKeyAndValue
(void (*fn)(const K&, const T, void*),void* d) const;
iterator
begin();
const_iterator
begin() const;
size_type
capacity() const; 
void
clear();
bool
contains(const K& key) const; 
bool
contains(bool (*fn)(const_reference,void*), void* d) const;
iterator
end();
const_iterator
end() const;
size_type
entries() const; 
float
fillRatio() const; 
bool
find(const K& key, K& r) const; 
bool
find(bool (*fn)(const_reference,void*),void* d,
                pair<K,T>& r) const;
bool
findValue(const K& key, T& r) const; 
bool
findKeyValue(const K& key, K& kr, T& tr) const; 
bool
insert(const K& key, const T& a); 
bool
insertKeyAndValue(const K& key,const T& a); 
bool
isEmpty() const; 
size_type
occurrencesOf(const K& key) const; 
size_type
occurrencesOf(bool (*fn)(const_reference,void*),void* d) const;
bool
remove(const K& key); 
bool
remove(bool (*fn)(const_reference,void*), void* d);
size_type
removeAll(const K& key); 
size_type
removeAll(bool (*fn)(const_reference,void*), void* d);
void
resize(size_type sz); 
rw_hashmap<K,T,H,EQ>&
std();
const rw_hashmap<K,T,H,EQ>&
std() const;

Related Global Operators

RWvostream&
operator<<(RWvostream& strm, 
       const RWTValHashMap<K,T,H,EQ>& coll);
RWFile&
operator<<(RWFile& strm, const RWTValHashMap<K,T,H,EQ>& coll);
RWvistream&
operator>>(RWvistream& strm, RWTValHashMap<K,T,H,EQ>& coll);
RWFile&
operator>>(RWFile& strm, RWTValHashMap<K,T,H,EQ>& coll);
RWvistream&
operator>>(RWvistream& strm, RWTValHashMap<K,T,H,EQ>*& p);
RWFile&
operator>>(RWFile& strm, RWTValHashMap<K,T,H,EQ>*& p);


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