SourcePro C++ 12.0 |
SourcePro® C++ API Reference Guide |
SourcePro C++ Documentation Home |
Maintains a collection of keys, each with an associated item stored according to a hash object. More...
#include <rw/tvhdict.h>
Inherits RW_VMapAssoc< rw_hashmap< K, T, H, EQ,A >, RWTValHashMap< K, T, H, EQ,A >, K, T, std::less< K > >.
Inherited by RWStringToStreamType, and RWXmlStringToStreamType.
Public Types | |
typedef rw_hashmap< K, T, H, EQ,A > | container_type |
typedef container_type::iterator | iterator |
typedef container_type::const_iterator | const_iterator |
typedef container_type::size_type | size_type |
typedef container_type::value_type | value_type |
typedef container_type::key_type | key_type |
typedef T | data_type |
typedef container_type::reference | reference |
typedef container_type::const_reference | const_reference |
Public Member Functions | |
container_type & | std () |
const container_type & | std () const |
void | apply (void(*fn)(const K &, const T &, void *), void *d) const |
void | apply (void(*fn)(const K &, T &, void *), void *d) |
iterator | begin () |
const_iterator | begin () const |
iterator | end () |
const_iterator | end () const |
bool | isEmpty () const |
size_type | entries () const |
bool | operator== (const container_type &m) const |
bool | operator== (const RWTValHashMap< K, T, H, EQ, A > &m) const |
bool | operator!= (const container_type &stdc) const |
bool | operator!= (const RWTValHashMap< K, T, H, EQ, A > &rwc) const |
void | clear () |
bool | contains (bool(*fn)(const_reference, void *), void *d) const |
bool | contains (const K &key) const |
bool | find (bool(*fn)(const_reference, void *), void *d, std::pair< K, T > &r) const |
bool | find (const K &key, K &r) const |
bool | findValue (const K &key, T &r) const |
bool | findKeyAndValue (const K &key, K &kr, T &tr) const |
const K & | minElement () const |
const K & | maxElement () const |
size_type | occurrencesOf (bool(*fn)(const_reference, void *), void *d) const |
size_type | occurrencesOf (const K &key) const |
bool | remove (bool(*fn)(const_reference, void *), void *d) |
bool | remove (const K &key) |
size_type | removeAll (bool(*fn)(const_reference, void *), void *d) |
size_type | removeAll (const K &key) |
bool | insert (const K &key, const T &a) |
bool | insert (K &&key, T &&a) |
T & | operator[] (const K &key) |
size_type | capacity () const |
float | fillRatio () const |
void | resize (size_type sz) |
void | swap (RWTValHashMap< K, T, H, EQ, A > &other) |
RWTValHashMap< K, T, H, EQ,A > & | operator= (const RWTValHashMap< K, T, H, EQ, A > &m) |
RWTValHashMap< K, T, H, EQ,A > & | operator= (const rw_hashmap< K, T, H, EQ, A > &m) |
RWTValHashMap< K, T, H, EQ,A > & | operator= (RWTValHashMap< K, T, H, EQ, A > &&rhs) |
RWTValHashMap () | |
RWTValHashMap (const rw_hashmap< K, T, H, EQ, A > &m) | |
RWTValHashMap (const RWTValHashMap< K, T, H, EQ, A > &rws) | |
RWTValHashMap (RWTValHashMap< K, T, H, EQ, A > &&rws) | |
RWTValHashMap (const value_type *first, const value_type *last) | |
RWTValHashMap (const H &h, size_type sz=64) | |
Related Functions | |
(Note that these are not member functions.) | |
template<class K , class T , class H , class EQ , class A > | |
RWvostream & | operator<< (RWvostream &strm, const RWTValHashMap< K, T, H, EQ, A > &coll) |
template<class K , class T , class H , class EQ , class A > | |
RWFile & | operator<< (RWFile &strm, const RWTValHashMap< K, T, H, EQ, A > &coll) |
template<class K , class T , class H , class EQ , class A > | |
RWvistream & | operator>> (RWvistream &strm, RWTValHashMap< K, T, H, EQ, A > &coll) |
template<class K , class T , class H , class EQ , class A > | |
RWFile & | operator>> (RWFile &strm, RWTValHashMap< K, T, H, EQ, A > &coll) |
template<class K , class T , class H , class EQ , class A > | |
RWvistream & | operator>> (RWvistream &strm, RWTValHashMap< K, T, H, EQ, A > *&p) |
template<class K , class T , class H , class EQ , class A > | |
RWFile & | operator>> (RWFile &strm, RWTValHashMap< K, T, H, EQ, A > *&p) |
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
. Class A
is an allocator of objects of class T
. For more information about using custom allocators, please see the Essential Tools Module User's Guide.
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 are 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.
RWTValHashMap<K,T,H,EQ,A> will not accept a key that is equivalent to any key already in the collection. (RWTValHashMultiMap<K,T,H,EQ,A> 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 std::equal_to
function object provided by the C++ Standard Library; otherwise, you must define your own equality object.
The value
type must have operator==
defined. This requirement is imposed by the C++ Standard Library.
#include <rw/tvhdict.h> RWTValHashMap<K,T,H,EQ,A> m;
Class RWTValHashMultiMap<K,T,H,EQ,A> offers the same interface to a collection that accepts multiple keys that compare equal to each other.
Class rw_hashmap<K,T,H,EQ,A> is the C++ Standard compliant collection that serves as the underlying implementation for this collection.
Isomorphic
#include <iostream> #include <functional> #include <rw/tvhdict.h> #include <rw/cstring.h> #include <rw/tools/datetime.h> struct simple_hash { unsigned long operator()(const RWCString& ref) const { size_t n = ref.length (); return (unsigned long)(n ? n * size_t (ref [0]) : 0); } }; typedef RWTValHashMap<RWCString, RWDateTime, simple_hash, std::equal_to<RWCString> > RW; int main() { RW birthdays; birthdays.insertKeyAndValue( "John", RWDateTime("April 12, 1975", RWDateTime::setDate) ); birthdays.insertKeyAndValue("Ivan", RWDateTime("Nov 2, 1980", RWDateTime::setDate)); // Alternative syntax: birthdays["Susan"] = RWDateTime("June 30, 1955", RWDateTime::setDate); birthdays["Gene"] = RWDateTime("Jan 5, 1981", RWDateTime::setDate); // Print a birthday: std::cout << birthdays["John"].asString('x') << std::endl; return 0; }
Program Output:
04/12/75
typedef container_type::const_iterator RWTValHashMap< K, T, H, EQ, A >::const_iterator |
Typedef for the const
iterator.
typedef container_type::const_reference RWTValHashMap< K, T, H, EQ, A >::const_reference |
Typedef for a const
reference to a value in this container.
typedef rw_hashmap<K,T,H,EQ ,A > RWTValHashMap< K, T, H, EQ, A >::container_type |
Typedef for the C++ Standard collection that serves as the underlying implementation for this class.
typedef T RWTValHashMap< K, T, H, EQ, A >::data_type |
Typedef for the type of value.
typedef container_type::iterator RWTValHashMap< K, T, H, EQ, A >::iterator |
Typedef for the non-const
iterator.
typedef container_type::key_type RWTValHashMap< K, T, H, EQ, A >::key_type |
Typedef for the type of key.
typedef container_type::reference RWTValHashMap< K, T, H, EQ, A >::reference |
Typedef for a non-const
reference to a value in this container.
typedef container_type::size_type RWTValHashMap< K, T, H, EQ, A >::size_type |
Typedef for the type used to index into this container.
typedef container_type::value_type RWTValHashMap< K, T, H, EQ, A >::value_type |
Typedef for the type of elements in this container.
RWTValHashMap< K, T, H, EQ, A >::RWTValHashMap | ( | ) | [inline] |
Constructs an empty map.
RWTValHashMap< K, T, H, EQ, A >::RWTValHashMap | ( | const rw_hashmap< K, T, H, EQ, A > & | m | ) | [inline] |
Constructs a map by copying all elements of m.
RWTValHashMap< K, T, H, EQ, A >::RWTValHashMap | ( | const RWTValHashMap< K, T, H, EQ, A > & | rws | ) | [inline] |
Copy constructor.
RWTValHashMap< K, T, H, EQ, A >::RWTValHashMap | ( | RWTValHashMap< K, T, H, EQ, A > && | rws | ) | [inline] |
Move constructor. The constructed hash map takes ownership of the data owned by rws.
RWTValHashMap< K, T, H, EQ, A >::RWTValHashMap | ( | const value_type * | first, | |
const value_type * | last | |||
) | [inline] |
Constructs a map by copying elements from the array of value_type
pairs pointed to by first, up to, but not including, the pair pointed to by last.
RWTValHashMap< K, T, H, EQ, A >::RWTValHashMap | ( | const H & | h, | |
size_type | sz = 64 | |||
) | [inline] |
Creates an empty hashed map which uses the hash object h and has an initial capacity of sz.
void RWTValHashMap< K, T, H, EQ, A >::apply | ( | void(*)(const K &, T &, void *) | fn, | |
void * | d | |||
) | [inline] |
Applies the user-defined function pointed to by fn to every association in the collection. Client data may be passed through parameter d.
void RWTValHashMap< K, T, H, EQ, A >::apply | ( | void(*)(const K &, const T &, void *) | fn, | |
void * | d | |||
) | const [inline] |
Applies the user-defined function pointed to by fn to every association in the collection. Client data may be passed through parameter d.
const_iterator RWTValHashMap< K, T, H, EQ, A >::begin | ( | ) | const [inline] |
Returns an iterator positioned at the first pair in self.
iterator RWTValHashMap< K, T, H, EQ, A >::begin | ( | ) | [inline] |
Returns an iterator positioned at the first pair in self.
size_type RWTValHashMap< K, T, H, EQ, A >::capacity | ( | ) | const [inline] |
Returns the number of buckets (slots) available in the underlying hash representation. See resize().
void RWTValHashMap< K, T, H, EQ, A >::clear | ( | void | ) | [inline] |
Clears the collection by removing all items from self. Each key and its associated item will have its destructor called.
bool RWTValHashMap< K, T, H, EQ, A >::contains | ( | const K & | key | ) | const [inline] |
Returns true
if there exists a key j
in self that compares equal to key, otherwise returns false
.
bool RWTValHashMap< K, T, H, EQ, A >::contains | ( | bool(*)(const_reference, void *) | fn, | |
void * | d | |||
) | const [inline] |
Returns true
if there exists an association a
in self such that the expression ((*fn)(a,d))
is true
, otherwise returns false
. Client data may be passed through parameter d.
const_iterator RWTValHashMap< K, T, H, EQ, A >::end | ( | ) | const [inline] |
Returns an iterator positioned "just past" the last association in self.
iterator RWTValHashMap< K, T, H, EQ, A >::end | ( | ) | [inline] |
Returns an iterator positioned "just past" the last association in self.
size_type RWTValHashMap< K, T, H, EQ, A >::entries | ( | void | ) | const [inline] |
Returns the number of associations in self.
float RWTValHashMap< K, T, H, EQ, A >::fillRatio | ( | ) | const [inline] |
Returns the ratio entries() / capacity().
bool RWTValHashMap< K, T, H, EQ, A >::find | ( | const K & | key, | |
K & | r | |||
) | const [inline] |
If there exists a key j
in self that compares equal to key, assigns j
to r and returns true
. Otherwise, returns false
and leaves the value of r unchanged.
bool RWTValHashMap< K, T, H, EQ, A >::find | ( | bool(*)(const_reference, void *) | fn, | |
void * | d, | |||
std::pair< K, T > & | r | |||
) | const [inline] |
If there exists an association a
in self such that the expression ((*fn)(a,d))
is true
, assigns a
to r and returns true
. Otherwise, returns false
and leaves the value of r unchanged. Client data may be passed through parameter d.
bool RWTValHashMap< K, T, H, EQ, A >::findKeyAndValue | ( | const K & | key, | |
K & | kr, | |||
T & | tr | |||
) | const [inline] |
If there exists a key j
in self that compares equal to key, assigns j
to kr, assigns the item associated with j
to tr, and returns true
. Otherwise, returns false
and leaves the values of kr and tr unchanged.
bool RWTValHashMap< K, T, H, EQ, A >::findValue | ( | const K & | key, | |
T & | r | |||
) | const [inline] |
If there exists a key j
in self that compares equal to key, assigns the item associated with j
to r and returns true
. Otherwise, returns false
and leaves the value of r unchanged.
bool RWTValHashMap< K, T, H, EQ, A >::insert | ( | K && | key, | |
T && | a | |||
) | [inline] |
Adds key with associated item a to the collection. Returns true
if the insertion is successful, otherwise returns false
. Insertion will fail if the collection already holds an association with the equivalent key.
bool RWTValHashMap< K, T, H, EQ, A >::insert | ( | const K & | key, | |
const T & | a | |||
) | [inline] |
Adds key with associated item a to the collection. Returns true
if the insertion is successful, otherwise returns false
. Insertion will fail if the collection already holds an association with the equivalent key.
bool RWTValHashMap< K, T, H, EQ, A >::isEmpty | ( | ) | const [inline] |
Returns true
if there are no items in the collection, otherwise false
.
const K& RWTValHashMap< K, T, H, EQ, A >::maxElement | ( | ) | const [inline] |
Returns a reference to the maximum key in the collection. Type K
must have well-defined less-than semantics (K::operator<(const K&)
or equivalent).
const K& RWTValHashMap< K, T, H, EQ, A >::minElement | ( | ) | const [inline] |
Returns a reference to the minimum key in the collection. Type K
must have well-defined less-than semantics (K::operator<(const K&)
or equivalent).
size_type RWTValHashMap< K, T, H, EQ, A >::occurrencesOf | ( | const K & | key | ) | const [inline] |
Returns the number of keys j
in self that compare equal to key.
size_type RWTValHashMap< K, T, H, EQ, A >::occurrencesOf | ( | bool(*)(const_reference, void *) | fn, | |
void * | d | |||
) | const [inline] |
Returns the number of associations a
in self such that the expression ((*fn)(a,d))
is true
. Client data may be passed through parameter d.
bool RWTValHashMap< K, T, H, EQ, A >::operator!= | ( | const RWTValHashMap< K, T, H, EQ, A > & | rwc | ) | const [inline] |
Returns the negation of operator==().
bool RWTValHashMap< K, T, H, EQ, A >::operator!= | ( | const container_type & | stdc | ) | const [inline] |
Returns the negation of operator==().
RWTValHashMap<K,T,H,EQ ,A >& RWTValHashMap< K, T, H, EQ, A >::operator= | ( | RWTValHashMap< K, T, H, EQ, A > && | rhs | ) | [inline] |
Move assignment. Self takes ownership of the data owned by rhs.
RWTValHashMap<K,T,H,EQ ,A >& RWTValHashMap< K, T, H, EQ, A >::operator= | ( | const rw_hashmap< K, T, H, EQ, A > & | m | ) | [inline] |
Destroys all elements of self and replaces them by copying all associations from m.
RWTValHashMap<K,T,H,EQ ,A >& RWTValHashMap< K, T, H, EQ, A >::operator= | ( | const RWTValHashMap< K, T, H, EQ, A > & | m | ) | [inline] |
Destroys all elements of self and replaces them by copying all associations from m.
bool RWTValHashMap< K, T, H, EQ, A >::operator== | ( | const RWTValHashMap< K, T, H, EQ, A > & | m | ) | const [inline] |
Returns true
if self compares equal to m, otherwise returns false
. Two collections are equal if both have the same number of entries, and iterating through both collections produces, in turn, individual pairs that compare equal to each other.
bool RWTValHashMap< K, T, H, EQ, A >::operator== | ( | const container_type & | m | ) | const [inline] |
Returns true
if self compares equal to m, otherwise returns false
. Two collections are equal if both have the same number of entries, and iterating through both collections produces, in turn, individual pairs that compare equal to each other.
T& RWTValHashMap< K, T, H, EQ, A >::operator[] | ( | const K & | key | ) | [inline] |
Looks up key and returns a reference to its associated item. If the key is not in the dictionary, then it is added with an associated item provided by the default constructor for type T
.
bool RWTValHashMap< K, T, H, EQ, A >::remove | ( | const K & | key | ) | [inline] |
Removes the first association with key j
in self such that the expression (j == key)
is true
, and returns true
. Returns false
if there is no such association.
bool RWTValHashMap< K, T, H, EQ, A >::remove | ( | bool(*)(const_reference, void *) | fn, | |
void * | d | |||
) | [inline] |
Removes the first association a
in self such that the expression ((*fn)(a,d))
is true
, and returns true
. Returns false
if there is no such element. Client data may be passed through parameter d.
size_type RWTValHashMap< K, T, H, EQ, A >::removeAll | ( | const K & | key | ) | [inline] |
Removes all elements j
in self that compare equal to key. Returns the number of items removed.
size_type RWTValHashMap< K, T, H, EQ, A >::removeAll | ( | bool(*)(const_reference, void *) | fn, | |
void * | d | |||
) | [inline] |
Removes all associations a
in self such that the expression ((*fn)(a,d))
is true
. Returns the number of items removed. Client data may be passed through parameter d.
void RWTValHashMap< K, T, H, EQ, A >::resize | ( | size_type | sz | ) | [inline] |
Changes the capacity of self by creating a new hashed map with a capacity of sz. This method copies every element of self into the new container and finally swaps the internal representation of the new container with the internal representation of self.
const container_type& RWTValHashMap< K, T, H, EQ, A >::std | ( | ) | const [inline] |
Returns a reference to the underlying collection that serves as the implementation for self. This reference may be used freely, providing access to the C++ Standard interface as well as interoperability with other software components that make use of the C++ Standard compliant collections.
container_type& RWTValHashMap< K, T, H, EQ, A >::std | ( | ) | [inline] |
Returns a reference to the underlying collection that serves as the implementation for self. This reference may be used freely, providing access to the C++ Standard interface as well as interoperability with other software components that make use of the C++ Standard compliant collections.
void RWTValHashMap< K, T, H, EQ, A >::swap | ( | RWTValHashMap< K, T, H, EQ, A > & | other | ) | [inline] |
Swaps the data held by self with the data held by other, including the H
and EQ
objects. This method does not copy or destroy any of the items swapped; rather, it swaps the underlying hash tables.
RWFile & operator<< | ( | RWFile & | strm, | |
const RWTValHashMap< K, T, H, EQ, A > & | coll | |||
) | [related] |
Saves the collection coll onto the RWFile strm, or a reference to it if it has already been saved.
RWvostream & operator<< | ( | RWvostream & | strm, | |
const RWTValHashMap< K, T, H, EQ, A > & | coll | |||
) | [related] |
Saves the collection coll onto the output stream strm, or a reference to it if it has already been saved.
RWFile & operator>> | ( | RWFile & | strm, | |
RWTValHashMap< K, T, H, EQ, A > *& | p | |||
) | [related] |
Looks at the next object on the RWFile strm and either creates a new collection off the heap and sets p to point to it, or sets p to point to a previously read instance. If a collection is created off the heap, then the application is responsible for deleting it.
RWvistream & operator>> | ( | RWvistream & | strm, | |
RWTValHashMap< K, T, H, EQ, A > *& | p | |||
) | [related] |
Looks at the next object on the input stream strm and either creates a new collection off the heap and sets p to point to it, or sets p to point to a previously read instance. If a collection is created off the heap, then the application is responsible for deleting it.
RWFile & operator>> | ( | RWFile & | strm, | |
RWTValHashMap< K, T, H, EQ, A > & | coll | |||
) | [related] |
Restores the contents of the collection coll from the RWFile strm.
RWvistream & operator>> | ( | RWvistream & | strm, | |
RWTValHashMap< K, T, H, EQ, A > & | coll | |||
) | [related] |
Restores the contents of the collection coll from the input stream strm.
© Copyright Rogue Wave Software, Inc. All Rights Reserved.
Rogue Wave and SourcePro are registered trademarks of Rogue Wave Software, Inc. in the United States and other countries. All other trademarks are the property of their respective owners.
Contact Rogue Wave about documentation or support issues.