**Module:** Essential Tools Module **Group:** STL Extension-based Collection

Does not inherit

- Local Index
- Synopsis
- Description
- Persistence
- Example
- Related Classes
- Public Typedefs
- Public Constructors
- Public Member Operators
- Public Member Functions
- Related Global Operators

#include <rw/tphset.h> RWTPtrHashSet<T,H,EQ,A> s;

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.

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,A>** 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

// // 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); } }; int main(){ RWTPtrHashSet<RWCString,silly_hash,std::equal_to<RWCString> > set1; RWTPtrHashSet<RWCString,silly_hash,std::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,A>** 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>,A>** is the C++-standard collection that serves as the underlying implementation for

typedef rw_deref_compare<EQ,T>container_eq; typedef rw_deref_hash<H,T>container_hash; typedef rw_hashset<T*,container_hash, container_eq,A>container_type; typedef container_type::size_typesize_type; typedef container_type::difference_typedifference_type; typedef container_type::iteratoriterator; typedef container_type::const_iteratorconst_iterator; typedef T*value_type; typedef T* const&reference; typedef T* const&const_reference;

RWTPtrHashSet<T,H,EQ,A>(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,A>(const RWTPtrHashSet<T,H,EQ,A>& rws);

Copy constructor.

RWTPtrHashSet<T,H,EQ,A>(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`.

RWTPtrHashSet<T,H,EQ,A>(const rw_hashset<T*,container_hash,container_eq,A>& s);

Constructs a pointer based hash set by copying all elements from `s`.

RWTPtrHashSet<T,H,EQ,A>(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,A> &operator=(const RWTPtrHashSet<T,H,EQ,A> & s);

Clears all elements of self and replaces them by copying all elements of `s`.

booloperator==(const RWTPtrHashSet<T,H,EQ,A> & 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.

voidapply(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:

voidyourfun(const T* a, void* d);

Client data may be passed through parameter `d`.

iteratorbegin(); const_iteratorbegin() const;

Returns an iterator positioned at the first element of self.

size_typecapacity() const;

Returns the number of buckets (slots) available in the underlying hash representation. See **resize** below.

voidclear();

Clears the collection by removing all items from self.

voidclearAndDestroy();

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.)

boolcontains(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`.

boolcontains(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:

boolyourTester(const T* a, void* d);

Client data may be passed through parameter `d`.

voiddifference(const RWTPtrHashSet<T,H,EQ,A> & s);

Sets self to the set-theoretic difference given by `(self - s)`. Elements from each set are dereferenced before being compared.

iteratorend(); const_iteratorend() const;

Returns an iterator positioned "just past" the last element in self.

size_typeentries() const;

Returns the number of items in self.

floatfillRatio() 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:

boolyourTester(const T* a, void* d);

Client data may be passed through parameter `d`.

boolinsert(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.

voidintersection(const RWTPtrHashSet<T,H,EQ,A> & s);

Destructively performs a set theoretic intersection of self and `s`, replacing the contents of self with the result.

boolisEmpty() const;

Returns `TRUE` if there are no items in the collection, `FALSE` otherwise.

boolisEquivalent(const RWTPtrHashSet<T,H,EQ,A> & s) const;

Returns `TRUE` if there is set equivalence between self and `s`, and returns `FALSE` otherwise.

boolisProperSubsetOf(const RWTPtrHashSet<T,H,EQ,A> & s) const;

Returns `TRUE` if self is a proper subset of `s`, and returns `FALSE` otherwise.

boolisSubsetOf(const RWTPtrHashSet<T,H,EQ,A> & s) const;

Returns `TRUE` if self is a subset of `s` or if self is set equivalent to `s`, `FALSE` otherwise.

size_typeoccurrencesOf(const T* a) const;

Returns the number of elements `t` that compare equal to `*a`

size_typeoccurrencesOf(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:

boolyourTester(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:

boolyourTester(const T* a, void* d);

Client data may be passed through parameter `d`.

size_typeremoveAll(const T* a);

Removes all elements `t` in self that compare equal to `*a`. Returns the number of items removed.

size_typeremoveAll(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:

boolyourTester(const T* a, void* d);

Client data may be passed through parameter `d`.

voidresize(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,A>&std(); const rw_hashset<T*,container_hash, container_eq,A>&std() const;

Returns a reference to the underlying C++-standard collection that serves as the implementation for self.

voidsymmetricDifference(const RWTPtrHashSet<T,H,EQ,A> & 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 .

voidUnion(const RWTPtrHashSet<T,H,EQ,A> & 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,A> & coll); RWFile&operator<<(RWFile& strm, const RWTPtrHashSet<T,H,EQ,A> & 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,A> & coll); RWFile&operator>>(RWFile& strm, RWTPtrHashSet<T,H,EQ,A> & coll);

Restores the contents of the collection `coll` from the input stream `strm`.

RWvistream&operator>>(RWvistream& strm, RWTPtrHashSet<T,H,EQ,A> *& p); RWFile&operator>>(RWFile& strm, RWTPtrHashSet<T,H,EQ,A> *& 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.

©2004 Copyright Quovadx, Inc. All Rights Reserved.

Rogue Wave and SourcePro are registered trademarks of Quovadx, 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.