#include <rw/tvhset.h> RWTValHashSet<T,H,EQ> s;
If you have the Standard C++ Library, use the interface described here. Otherwise, use the restricted interface to RWTValHashSet described in Appendix A.
This class maintains a collection of values, which are stored according to a hash object of type H. H must offer a const hash function for 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.
RWTValHashSet<T,H,EQ> will not accept an item that is equivalent to an item already in the collection. (RWTValHashMultiSet<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
// // tvhsstr.cpp // #include <rw/tvhset.h> #include <rw/cstring.h> #include <iostream.h> struct silly_hash{ unsigned long operator()(RWCString x) const { return x.length() * (long)x(0); } }; main(){ RWTValHashSet<RWCString,silly_hash,equal_to<RWCString> > set1; RWTValHashSet<RWCString,silly_hash,equal_to<RWCString> > set2; set1.insert("one"); set1.insert("two"); set1.insert("three"); //Rejected, no duplicates allowed set1.insert("one"); cout << set1.entries() << endl; // Prints "3" set2.insert("one"); set2.insert("five"); //Rejected, no duplicates allowed set2.insert("one"); cout << ((set1.isEquivalent(set2)) ? "TRUE" : "FALSE") << endl; // Prints "FALSE" set2.intersection(set1); set1.clear(); cout << set1.entries() << endl; // Prints "0" cout << set2.entries() << endl; // Prints "1" return 0; }
Class RWTValHashMultiSet<T,H,EQ> offers the same interface to a collection that accepts multiple items that compare equal to each other.
Class rw_hashset<T,H,EQ> is the C++-standard compliant collection that serves as the underlying implementation for RWTValHashSet<T,H,EQ>.
typedef rw_hashset<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 T value_type; typedef T& reference; typedef const T& const_reference;
RWTValHashSet<T,H,EQ> (size_type sz = 1024,const H& h = H(),const EQ& eq= EQ());
Constructs an empty 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
RWTValHashSet<T,H,EQ>(const rw_hashset<T,H,EQ>& s);
Constructs a set by copying all elements of s.
RWTValHashSet<T,H,EQ>(const RWTValHashSet<T,H,EQ>& rws);
Copy constructor.
RWTPtrHashSet<T,H,EQ> (const H& h,size_type sz = RWDEFAULT_CAPACITY);
Creates an empty hashed set which uses the hash object h and has an initial hash table capacity of sz.
RWTValHashSet<T,H,EQ>(const T* first,const T* last, size_type sz = 1024,const H& h = H(),const EQ& eq = EQ());
Constructs a set by copying elements from the array of Ts 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
RWTValHashSet<T,H,EQ>& operator=(const RWTValHashSet<T,H,EQ>& s); RWTValHashSet<T,H,EQ>& operator=(const rw_hashset<T,H,EQ>& s);
Destroys all elements of self and replaces them by copying all elements of s.
bool operator==(const RWTValHashSet<T,H,EQ>& s) const; bool operator==(const rw_hashset<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.
void apply(void (*fn)(const_reference,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. Each item will have its destructor called.
bool contains(const_reference 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_reference,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_reference a, void* d);
Client data may be passed through parameter d.
void difference(const RWTValHashSet<T,H,EQ>& s); void difference(const rw_hashset<T,H,EQ>& s);
Sets self to the set-theoretic difference given by (self - s).
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().
bool find(const_reference a, value_type& k) const;
If there exists an element t in self that compares equal to a, assigns t to k and returns true. Otherwise, returns false and leaves the value of k unchanged.
bool find(bool (*fn)(const_reference,void*), void* d, value_type& k) const;
If there exists an element t in self such that the expression ((*fn)(t,d)) is true, assigns t to k and returns true. Otherwise, returns false and leaves the value of k unchanged. fn points to a user-defined tester function which must have prototype:
bool yourTester(const_reference a, void* d);
Client data may be passed through parameter d.
bool insert(const_reference 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 the equivalent key.
void intersection(const RWTValHashSet<T,H,EQ>& rhs); void intersection(const rw_hashset<T,H,EQ>& rhs);
Destructively performs a set theoretic intersection of self and rhs, 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 RWTValHashSet<T,H,EQ>& s) const;
Returns true if there is set equivalence between self and s, and returns false otherwise.
bool isProperSubsetOf(const RWTValHashSet<T,H,EQ>& s) const;
Returns true if self is a proper subset of s, and returns false otherwise.
bool isSubsetOf(const RWTValHashSet<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_reference a) const;
Returns the number of elements t in self that compare equal to a.
size_type occurrencesOf (bool (*fn)(const_reference,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_reference a, void* d);
Client data may be passed through parameter d.
bool remove(const_reference a);
Removes the first element t in self that compares equal to a. Returns false if there is no such element.
bool remove(bool (*fn)(const_reference,void*), void* d);
Removes the first element t in self such that the expression ((*fn)(t,d)) is true and returns true. Returns false if there is no such element. fn points to a user-defined tester function which must have prototype:
bool yourTester(const_reference a, void* d);
Client data may be passed through parameter d.
size_type removeAll(const_reference a);
Removes all elements t in self that compare equal to a. Returns the number of items removed.
size_type removeAll(bool (*fn)(const_reference,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_reference 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,H,EQ>& std(); const rw_hashset<T,H,EQ>& std() const;
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 collections.
void symmetricDifference(const RWTValHashSet<T,H,EQ>& s); void symmetricDifference(const rw_hashset<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 RWTValHashSet<T,H,EQ>& s); void Union(const rw_hashsett<T,H,EQ>& s);
Destructively performs a set theoretic union operation on self and s. Self is replaced by the result. Note the use of the uppercase "U"in Union to avoid conflict with the C++ reserved word.
RWvostream& operator<<(RWvostream& strm, const RWTValHashSet<T,H,EQ>& coll); RWFile& operator<<(RWFile& strm, const RWTValHashSet<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, RWTValHashSet<T,H,EQ>& coll); RWFile& operator>>(RWFile& strm, RWTValHashSet<T,H,EQ>& coll);
Restores the contents of the collection coll from the input stream strm.
RWvistream& operator>>(RWvistream& strm, RWTValHashSet<T,H,EQ>*& p); RWFile& operator>>(RWFile& strm, RWTValHashSet<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.