#include <rw/tphasht.h> RWTPtrHashMultiSet<T,H,EQ> hmset;
If you have the Standard C++ Library, use the interface described here. Otherwise, use the interface for RWTPtrHashTable described in Appendix A.
This class maintains a pointer-based collection of values, which are stored according to a hash object of type H. Class T is the type pointed to by the items in the collection. H must provide a const hash function on elements of type T via public member
unsigned long operator()(const T& x) const;
Objects 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 T& x, const T& y) const;
which should return true if x and y are equivalent, false otherwise.
Note: Any two keys that are equivalent must hash to the same value.
RWTPtrHashMultiSet<T,H,EQ> may contain multiple items that are equivalent to each other. (RWTPtrHashSet<T,H,EQ> will not accept an item that is equivalent to an item already in the collection.)
The value type must have operator==() defined. This requirement is imposed by the Standard C++ Library.
Isomorphic
// // tphasht.cpp // #include <rw/tphasht.h> #include <rw/cstring.h> #include <iostream.h> struct silly_hash{ unsigned long operator()(RWCString x) const { return x.length() * (long)x(0); } }; main(){ RWTPtrHashMultiSet<RWCString,silly_hash,equal_to<RWCString> > set1; RWTPtrHashMultiSet<RWCString,silly_hash,equal_to<RWCString> > set2; set1.insert(new RWCString("one")); set1.insert(new RWCString("two")); set1.insert(new RWCString("three")); set1.insert(new RWCString("one")); // OK: duplicates allowd cout << set1.entries() << endl; // Prints "4" set2 = set1; cout << ((set1.isEquivalent(set2)) ? "TRUE" : "FALSE") << endl; // Prints "TRUE" set2.difference(set1); set1.clearAndDestroy(); cout << set1.entries() << endl; // Prints "0" cout << set2.entries() << endl; // Prints "0" return 0; }
Class RWTPtrHashSet<T,H,EQ> offers the same interface to a pointer-based collection that will not accept multiple items that compare equal to each other.
Class rw_hashmultiset<T*,rw_deref_hash<H,T>,rw_deref_compare<EQ,T>> is the C++-standard collection that serves as the underlying implementation for RWTPtrHashMultiSet<T,H,EQ>.
typedef rw_deref_compare<EQ,T> container_eq; typedef rw_deref_hash<H,T> container_hash; typedef rw_hashmultiset<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 T* value_type; typedef T* const& reference; typedef T* const& const_reference;
RWTPtrHashMultiSet<T,H,EQ> (size_type sz=1024,const H& h = H(),const EQ& eq = EQ());
Constructs an empty multi set. The hash table representation used by This multi-set will have sz buckets, use h as a hashing function and eq to test for equality between stored elements.
RWTPtrHashMultiSet<T,H,EQ> (const RWTPtrHashMultiSet<T,H,EQ>& rws);
Copy constructor.
RWTPtrHashMultiSet<T,H,EQ> (const rw_hashmultiset<T*,container_hash, container_eq>& s);
Constructs a hashed multi-set, copying all element from s.
RWTPtrHashMultiSet<T,H,EQ> (const H& h,size_type sz = RWDEFAULT_CAPACITY);
This Tools.h++ 6.xstyle constructor creates an empty hashed multi-set which uses the hash object h and has an initial hash table capacity of sz.
RWTPtrHashMultiSet<T,H,EQ>(T*const* first,T*const* last, size_type sz=1024,const H& h = H(),const EQ& eq = EQ());
Constructs a set by copying elements from the array of T*s pointed to by first, up to, but not including, the element pointed to by last. The hash table representation used by this multi-set will have sz buckets, use h as a hashing function and eq to test for equality between stored elements.
RWTPtrHashMultiSet<T,H,EQ>& operator=(const RWTPtrHashMultiSet<T,H,EQ>& s);
Clears all elements of self and replaces them by copying all elements of s.
bool operator==(const RWTPtrHashMultiSet<T,H,EQ>& s) const;
Returns true if self compares equal to s, otherwise returns false. Two collections are equal if both have the same number of entries, and iterating through both collections produces, in turn, individual elements that compare equal to each other. Elements are dereferenced before being compared.
void apply(void (*fn)(const T*,void*), void* d) const;
Applies the user-defined function pointed to by fn to every item in the collection. This function must have prototype:
void yourfun(const T* a, void* d);
Client data may be passed through parameter d.
iterator begin(); const_iterator begin() const;
Returns an iterator positioned at the first element of 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 items from the collection and uses operator delete to destroy the objects pointed to by those items. Do not use this method if multiple pointers to the same object are stored.
bool contains(const T* a) const;
Returns true if there exists an element t in self that compares equal to *a, otherwise returns false.
bool contains(bool (*fn)(const T*,void*), void* d) const;
Returns true if there exists an element t in self such that the expression ((*fn)(t,d)) is true, otherwise returns false. fn points to a user-defined tester function which must have prototype:
bool yourTester(const T* a, void* d);
Client data may be passed through parameter d.
void difference(const RWTPtrHashMultiSet<T,H,EQ>& s);
Sets self to the set-theoretic difference given by (self - s). Elements from each set are dereferenced before being compared.
iterator end(); const_iterator end() const;
Returns an iterator positioned "just past" the last element in self.
size_type entries() const;
Returns the number of items in self.
float fillRatio() const;
Returns the ratio entries()/capacity().
const T* find(const T* a) const;
If there exists an element t in self that compares equal to *a, returns t. Otherwise, returns rwnil.
const T* find(bool (*fn)(const T*,void*), void* d) const;
If there exists an element t in self such that the expression ((*fn)(t,d)) is true, returns t. Otherwise, returns rwnil. fn points to a user-defined tester function which must have prototype:
bool yourTester(const T* a, void* d);
Client data may be passed through parameter d.
bool insert(T* a);
Adds the item a to the collection. Returns true.
void intersection(const RWTPtrHashMultiSet<T,H,EQ>& s);
Destructively performs a set theoretic intersection of self and s, replacing the contents of self with the result.
bool isEmpty() const;
Returns true if there are no items in the collection, false otherwise.
bool isEquivalent(const RWTPtrHashMultiSet<T,H,EQ>& s) const;
Returns true if there is set equivalence between self and s; returns false otherwise.
bool isProperSubsetOf(const RWTPtrHashMultiSet<T,H,EQ>& s) const;
Returns true if self is a proper subset of s; returns false otherwise.
bool isSubsetOf(const RWTPtrHashMultiSet<T,H,EQ>& s) const;
Returns true if self is a subset of s or if self is set equivalent to s, false otherwise.
size_type occurrencesOf(const T* a) const;
Returns the number of elements t in self that compare equal to *a.
size_type occurrencesOf(bool (*fn)(const T*,void*), void* d) const;
Returns the number of elements t in self such that the expression((*fn)(t,d)) is true. fn points to a user-defined tester function which must have prototype:
bool yourTester(const T* a, void* d);
Client data may be passed through parameter d.
T* remove(const T* a);
Removes and returns the first element t in self that compares equal to *a. Returns rwnil if there is no such element.
T* remove(bool (*fn)(const T*,void*), void* d);
Removes and returns the first element t in self such that the expression ((*fn)(t,d)) is true. Returns rwnil if there is no such element. fn points to a user-defined tester function which must have prototype:
bool yourTester(const T* a, void* d);
Client data may be passed through parameter d.
size_type removeAll(const T* a);
Removes all elements t in self that compare equal to *a. Returns the number of items removed.
size_type removeAll(bool (*fn)(const T*,void*), void* d);
Removes all elements t in self such that the expression ((*fn)(t,d))is true. Returns the number of items removed. fn points to a user-defined tester function which must have prototype:
bool yourTester(const T* 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-set with a capacity of sz . resize 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.
rw_hashset<T*,container_hash,container_eq>& std(); const rw_hashset<T*,container_hash,container_eq>& std() const;
Returns a reference to the underlying C++-standard collection that serves as the implementation for self.
void symmetricDifference(const RWTPtrHashMultiSet<T,H,EQ>& rhs);
Destructively performs a set theoretic symmetric difference operation on self and rhs. Self is replaced by the result. A symmetric difference can be informally defined as (A_B)-(A_B).
void Union(const RWTPtrHashMultiSet<T,H,EQ>& rhs);
Destructively performs a set theoretic union operation on self and rhs. Self is replaced by the result. Note the uppercase "U" in Union to avoid conflict with the C++ reserved word.
RWvostream& operator<<(RWvostream& strm, const RWTPtrHashMultiSet<T,H,EQ>& coll); RWFile& operator<<(RWFile& strm, const RWTPtrHashMultiSet<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, RWTPtrHashMultiSet<T,H,EQ>& coll); RWFile& operator>>(RWFile& strm, RWTPtrHashMultiSet<T,H,EQ>& coll);
Restores the contents of the collection coll from the input stream strm.
RWvistream& operator>>(RWvistream& strm, RWTPtrHashMultiSet<T,H,EQ>*& p); RWFile& operator>>(RWFile& strm, RWTPtrHashMultiSet<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.