#include <rw/tphset.h> RWTPtrHashSet<T,H,EQ> s;
If you have the Standard C++ Library, use the interface described here. Otherwise, use the restricted interface to RWTPtrHashSet 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.
RWTPtrHashSet<T,H,EQ> will not accept an item that is equivalent to an item already in the collection. (RWTPtrHashMultiSet<T,H,EQ> may contain multiple items that are equivalent to each other.) Equality is based on the equality object and not on the == operator.
The value type must have operator==() defined. This requirement is imposed by the Standard C++ Library.
Isomorphic
// // tphset2.cpp // #include <rw/tphset.h> #include <rw/cstring.h> #include <iostream.h> struct silly_hash{ unsigned long operator()(RWCString x) const { return x.length() * (long)x(0); } }; main(){ RWTPtrHashSet<RWCString,silly_hash,equal_to<RWCString> > set1; RWTPtrHashSet<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")); // Duplicate insertion rejected cout << set1.entries() << endl; // Prints "3" 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 RWTPtrHashMultiSet<T,H,EQ> offers the same interface to a pointer-based collection that accepts multiple items that compare equal to each other.
Class rw_hashset<T*,rw_deref_hash<H,T>,rw_deref_compare<EQ,T>> is the C++-standard collection that serves as the underlying implementation for RWTPtrHashSet<T,H,EQ>.
typedef rw_deref_compare<EQ,T> container_eq; typedef rw_deref_hash<H,T> container_hash; typedef rw_hashset<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;
RWTPtrHashSet<T,H,EQ> (size_type sz=1024,const H& h = H(),const EQ& eq = EQ());
Constructs an empty hashed set. The underlying hash table representation will have sz buckets, will use h for its hashing function and will use eq to determine equality between elements.
RWTPtrHashSet<T,H,EQ>(const RWTPtrHashSet<T,H,EQ>& rws);
Copy constructor.
RWTPtrHashSet<T,H,EQ> (const H& h,size_type sz = RWDEFAULT_CAPACITY);
This Tools.h++ 6.xstyle constructor creates an empty hashed set which uses the hash object h and has an initial hash table capacity of sz.
RWTPtrHashSet<T,H,EQ> (const rw_hashset<T*,container_hash,container_eq>& s);
Constructs a pointer based hash set by copying all elements from s.
RWTPtrHashSet<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 underlying hash table representation will have sz buckets, will use h for its hashing function and will use eq to determine equality between elements.
RWTPtrHashSet<T,H,EQ>& operator=(const RWTPtrHashSet<T,H,EQ>& s);
Clears all elements of self and replaces them by copying all elements of s.
bool operator==(const RWTPtrHashSet<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. (If the equality operator is reflexive, the container cannot hold such multiple pointers.)
bool contains(const T* a) const;
Returns true if there exists an element t in self such that the expression(*t == *a) is true, 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 RWTPtrHashSet<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 such that *T 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 if the insertion is successful, otherwise returns false. The function will return true unless the collection already holds an element with an equivalent key.
void intersection(const RWTPtrHashSet<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 RWTPtrHashSet<T,H,EQ>& s) const;
Returns true if there is set equivalence between self and s, and returns false otherwise.
bool isProperSubsetOf(const RWTPtrHashSet<T,H,EQ>& s) const;
Returns true if self is a proper subset of s, and returns false otherwise.
bool isSubsetOf(const RWTPtrHashSet<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 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 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 RWTPtrHashSet<T,H,EQ>& s);
Destructively performs a set theoretic symmetric difference operation on self and s. Self is replaced by the result. A symmetric difference can be defined as (A_B)-(A_B).
void Union(const RWTPtrHashSet<T,H,EQ>& s);
Destructively performs a set theoretic union operation on self and s. 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 RWTPtrHashSet<T,H,EQ>& coll); RWFile& operator<<(RWFile& strm, const RWTPtrHashSet<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, RWTPtrHashSet<T,H,EQ>& coll); RWFile& operator>>(RWFile& strm, RWTPtrHashSet<T,H,EQ>& coll);
Restores the contents of the collection coll from the input stream strm.
RWvistream& operator>>(RWvistream& strm, RWTPtrHashSet<T,H,EQ>*& p); RWFile& operator>>(RWFile& strm, RWTPtrHashSet<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.