#include <rw/tphmmap.h> RWTPtrHashMultiMap<K,T,H,EQ> m;
RWTPtrHashMultiMap requires the Standard C++ Library.
This class maintains a pointer-based collection of associatoins of type pair<K* const, 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.
RWTPtrHashMultiMap<K,T,H,EQ> may contain multiple keys that are equivalent to each other. (RWTPtrHashMap<K,T,H,EQ> will not accept a key that is equivalent to any key already in the collection.) Equality is based on the comparison object and not on the == operator.
The value type must have operator==() defined. This requirement is imposed by the Standard C++ Library.
Isomorphic
// // tphmap.cpp // #include<rw/tphmmap.h> #include<rw/cstring.h> #include<iostream.h> struct silly_hash{ unsigned long operator()(RWCString x) const { return x.length() * (long)x[0]; } }; int main(){ RWCString snd = "Second"; RWTPtrHashMultiMap<RWCString,int,silly_hash,equal_to<RWCString> > contest; contest.insert(new RWCString("First"), new int(7)); contest.insert(&snd, new int(3)); contest.insert(&snd, new int(6)); // duplicate key OK contest.insert(new RWCString("Third"), new int(2)); cout << "There were " << contest.occurrencesOf(&snd) << " second place winners." << endl; return 0; } Program Output: There were 2 second place winners.
Class RWTPtrHashMap<K,T,H,EQ> offers the same interface to a pointer-based collection that will not accept multiple keys that compare equal to each other.
rw_hashmultimap<<K*,T*,rw_deref_hash<H,K>,rw_deref_compare<EQ,K>> is the C++-standard style collection that serves as the underlying implementation for this collection.
typedef rw_deref_hash<H,K> container_hash; typedef rw_deref_compare<EQ,K> container_eq; typedef rw_hashmultimap<K*,T*,container_hash,container_eq> container_type; typedef container_type::size_type size_type; typedef container_type::difference_type difference_type; typedef container_type::iterator iterator; typedef container_type::const_iterator const_iterator; typedef pair <K* const, T*> value_type; typedef pair <K* const, T*>& reference; typedef const pair <K* const, T*>& const_reference; typedef K* value_type_key; typedef T* value_type_data; typedef K*& reference_key; typedef T*& reference_data; typedef const K*const& const_reference_key; typedef const T*const& const_reference_data;
RWTPtrHashMultiMap<K,T,H,EQ>();
Constructs an empty map.
RWTPtrHashMultiMap<K,T,H,EQ>(const container_type& m);
Constructs a multi-map by doing an element by element copy from the C++ Standard Library style hashed multi-map, m.
RWTPtrHashMultiMap<K,T,H,EQ> (const RWTPtrHashMultiMap<K,T,H,EQ>& rwm);
Copy constructor.
RWTPtrHashMultiMap<K,T,H,EQ> (value_type* first, value_type* last);
Constructs a map by copying elements from the array of pairs pointed to by first, up to, but not including, the pair pointed to by last.
RWTPtrHashMultiMap<K,T,H,EQ> (const H& h, size_type sz = RWDEFAULT_CAPACITY);
This Tools.h++ 6.x style constructor creates an empty hashed multi-map which uses the hash object h and has an initial capacity of sz.
RWTPtrHashMultiMap<K,T,H,EQ>& operator=(const container_type&jjj m); RWTPtrHashMultiMap<K,T,H,EQ>& operator=(const RWTPtrHashMultiMap<K,T,H,EQ>& m);
Destroys all associations in self and replaces them by copying all associations from m.
bool operator==(const RWTPtrHashMultiMap<K,T,H,EQ>& m);
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 keys that compare equal to each other. Keys are dereferenced before being compared.
void apply(void (*fn)(const K*, T*&,void*),void* d); void apply(void (*fn)(const K*, const T*, void*), void* d) const;
Applies the user-defined function pointed to by fn to every association in the collection. This function must have one of the prototypes:
void yourfun(const K* key, T*& a, void* d); void yourfun(const K* key, const T* a, void* d);
Client data may be passed through parameter d.
void applyToKeyAndValue(void (*fn)(const K*, T*&,void*),void* d); void applyToKeyAndValue (void (*fn)(const K*, const T*, void*), void* d) const;
This is a deprecated version of the apply member above. It behaves exactly the same as apply.
iterator begin(); const_iterator begin() const;
Returns an iterator positioned at the first pair in self.
size_type capacity() const;
Returns the number of buckets (slots) available in the underlying hash representation. See resize below.
void clear();
Clears the collection by removing all items from self.
void clearAndDestroy();
Removes all associations from the collection and uses operator delete to destroy the objects pointed to by the keys and their associated items. Do not use this method if multiple pointers to the same keys or items are stored.
bool contains(const K* key) const;
Returns true if there exists a key j in self that compares equal to *key, otherwise returns false.
bool contains(bool (*fn)(value_type,void*),void* d) const;
Returns true if there exists an association a in self such that the expression ((*fn)(a,d)) is true, otherwise returns false. fn points to a user-defined tester function which must have prototype:
bool yourTester(value_type* a, void* d);
Client data may be passed through parameter d.
iterator end(); const_iterator end() const;
Returns an iterator positioned "just past" the last association in self.
size_type entries() const;
Returns the number of associations in self.
float fillRatio() const;
Returns the ratio entries()/capacity().
const K* find(const K* key) const;
If there exists a key j in self that compares equal to *key, then j is returned. Otherwise, returns rwnil.
value_type find(bool (*fn)(value_type,void*), void* d) const;
If there exists an association a in self such that the expression ((*fn)(a,d)) is true, then returns a. Otherwise, returns pair<rwnil,rwnil>. fn points to a user-defined tester function which must have prototype:
bool yourTester(value_type a, void* d);
Client data may be passed through parameter d.
T* findValue(const K* key); const T* findValue(const K* key) const;
If there exists a key j in self that compares equal to *key, returns the item associated with j. Otherwise, returns rwnil.
const K* findKeyAndValue(const K* key, T*& tr); const K* findKeyAndValue(const K* key, const T*& tr) const;
If there exists a key j in self that compares equal to *key, assigns the item associated with j to tr, and returns j. Otherwise, returns rwnil and leaves the value of tr unchanged.
bool insert(K* key,T* a);
Adds key with associated item a to the collection. Returns true.
bool insertKeyAndValue(K* key,T* a);
This is a deprecated version of the insert member above. It behaves exactly the same as insert.
bool isEmpty() const;
Returns true if there are no items in the collection, false otherwise.
size_type occurrencesOf(const K* key) const;
Returns the number of keys j in self that compare equal to *key.
size_type occurrencesOf (bool(*fn)(value_type,void*),void* d)const;
Returns the number of associations a in self such that the expression((*fn)(a,d)) is true. fn points to a user-defined tester function which must have prototype:
bool yourTester(value_type a, void* d);
Client data may be passed through parameter d.
K* remove(const K* key);
Removes the first association with key j in self that compares equal to *key. Returns rwnil if there is no such association.
K* remove(bool (*fn)(value_type,void*), void* d);
Removes the first association a in self such that the expression ((*fn)(a,d)) is true and returns its key. Returns rwnil if there is no such association. fn points to a user-defined tester function which must have prototype:
bool yourTester(value_type a, void* d);
Client data may be passed through parameter d.
size_type removeAll(const K* key);
Removes all associations with key j in self that compare equal to *key. Returns the number of associations removed.
size_type removeAll(bool (*fn)(value_type,void*), void* d);
Removes all associations a in self such that the expression ((*fn)(a,d))is true. Returns the number removed. fn points to a user-defined tester function which must have prototype:
bool yourTester(value_type a, void* d);
Client data may be passed through parameter d.
void resize(size_type sz);
Changes the capacity of self by creating a new hashed multi-map with a capacity of sz . resize then copies every element of self into the new container and finally swaps the internal representation of the new container with self.
container_type& std(); const container_type& std() const;
Returns a reference to the underlying C++-standard collection that serves as the implementation for self.
RWvostream& operator<<(RWvostream& strm, const RWTPtrHashMultiMap<K,T,H,EQ>& coll); RWFile& operator<<(RWFile& strm, const RWTPtrHashMultiMap<K,T,H,EQ>& coll);
Saves the collection coll onto the output stream strm, or a reference to it if it has already been saved.
RWvistream& operator>>(RWvistream& strm, RWTPtrHashMultiMap<K,T,H,EQ>& coll); RWFile& operator>>(RWFile& strm, RWTPtrHashMultiMap<K,T,H,EQ>& coll);
Restores the contents of the collection coll from the input stream strm.
RWvistream& operator>>(RWvistream& strm, RWTPtrHashMultiMap<K,T,H,EQ>*& p); RWFile& operator>>(RWFile& strm, RWTPtrHashMultiMap<K,T,H,EQ>*& p);
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 you are responsible for deleting it.