Chapter 6. RWTPtrDlist<T> - RWTPtrHashTableIterator

RWTPtrDlist<T>

Synopsis

#include <rw/tpdlist.h> 
RWTPtrDlist<T> dlist;


Note: If you have the Standard C++ Library, use the interface described here. Otherwise, use the restricted interface to RWTPtrDlist described in Appendix A: Alternate Template Class Interfaces.


Description

This class maintains a pointer-based collection of values, implemented as a doubly-linked list. Class T is the type pointed to by the items in the collection.

Persistence

Isomorphic

Example

In this example, a pointer-based doubly-linked list of user type Dog is exercised.

//
 
// tpdlist.cpp
//
#include <rw/tpdlist.h>
#include <iostream.h>
#include <string.h>
 
class Dog {
  char* name;
public:
  Dog( const char* c) {
   name = new char[strlen(c)+1];
   strcpy(name, c); }
 
  ~Dog() { delete name; }
 
  // Define a copy constructor:
  Dog(const Dog& dog) {
   name = new char[strlen(dog.name)+1];
   strcpy(name, dog.name); }
 
  // Define an assignment operator:
  void operator=(const Dog& dog) {
   if (this!=&dog) {
     delete name;
     name = new char[strlen(dog.name)+1];
     strcpy(name, dog.name);
  }
  }
 
  // Define an equality test operator:
  int operator==(const Dog& dog) const {
  return strcmp(name, dog.name)==0; }
 
  // Order alphabetically by name:
  int operator<(const Dog& dog) const {
  return strcmp(name, dog.name)<0; }
 
  friend ostream& operator<<(ostream& str, const Dog& dog){
    str << dog.name;
    return str;}
};
 
main(){
  RWTPtrDlist<Dog> terriers;
  terriers.insert(new Dog("Cairn Terrier"));
  terriers.insert(new Dog("Irish Terrier"));
  terriers.insert(new Dog("Schnauzer"));
 
  Dog key1("Schnauzer");
  cout << "The list " <<
    (terriers.contains(&key1) ? "does " : "does not ") <<
    "contain a Schnauzer\n";
 
  Dog key2("Irish Terrier");
  terriers.insertAt(
      terriers.index(&key2),
      new Dog("Fox Terrier")
    );
 
  Dog* d;
  while (!terriers.isEmpty()) {
    d = terriers.get();
    cout << *d << endl;
    delete d;
  }
 
  return 0;
}

Program Output:

The list does contain a Schnauzer
Cairn Terrier
Fox Terrier
Irish Terrier
Schnauzer

Related Classes

Classes RWTPtrDeque<T>, RWTPtrSlist<T>, and RWTPtrOrderedVector<T> also provide a Rogue Wave pointer-based interface to C++-standard sequence collections.

Class list<T*, allocator> is the C++-standard collection that serves as the underlying implementation for this class.

Public Typedefs

typedef list<T*, allocator>                    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
typedef T*                                     reference;
typedef T* const&                              const_reference;

Public Constructors

RWTPtrDlist<T>();

Constructs an empty, doubly-linked list.

RWTPtrDlist<T>(const RWTPtrDlist<T>& rwlst);

Copy constructor.

RWTPtrDlist<T>(const list<T*, allocator>& lst);

Constructs a pointer based doubly linked list by copying all elements of lst.

RWTPtrDlist<T>(size_type n, T* a=0);

Constructs a doubly-linked list with n elements, each initialized to a.

RWTPtrDlist<T>(T*const* first, T*const* last);

Constructs a doubly-linked list by copying elements from the array of T*s pointed to by first, up to, but not including, the element pointed to by last.

Public Member Operators

RWTPtrDlist<T>& operator=(const list<T*, allocator>& lst); RWTPtrDlist<T>& operator=(const RWTPtrDlist<T>& lst);

Clears all elements of self and replaces them by copying all elements of lst.

bool operator<(const RWTPtrDlist<T>& lst);

Returns true if self compares lexicographically less than lst, otherwise returns false. Items in each collection are dereferenced before being compared. Assumes that type T has well-defined less-than semantics.

bool operator==(const RWTPtrDlist<T>& lst);

Returns true if self compares equal to lst, 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.

reference operator()(size_type i); const_reference operator()(size_type i) const;

Returns a reference to the ith element of self. Index i must be between 0 and one less then the number of entries, otherwise the results are undefined—no bounds checking is performed.

reference operator[](size_type i); const_reference operator[](size_type i) const;

Returns a reference to the ith element of self. Index i must be between 0 and one less then the number of entries in self, otherwise the function throws an exception of type RWBoundsErr.

Public Member Functions

void append(T* a);

Adds the item a to the end of the collection.

void apply(void (*fn)(T*,void*), void* d); void apply(void (*fn)(T*&,void*), void* d); 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. self function must have one of the prototypes:

void yourfun(T* a, void* d);

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

void yourfun(reference a, void* d);

Client data may be passed through parameter d.

const const_reference at (size_type i); reference at(size_type i);

Returns a reference to the ith element of self. Index i must be between 0 and one less then the number of entries in self, otherwise the function throws an exception of type RWBoundsErr.

iterator begin(); const_iterator begin() const;

Returns an iterator positioned at the first element of self.

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 self 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 such that the expression(*t == *a) is true, otherwise returns false.

bool contains(bool (*fn)(T*,void*), void* d) const; 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 one of the prototypes:

bool yourTester(T* a, void* d);

bool yourTester(const T* a, void* d);

for the const version. Client data may be passed through parameter d.

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.

T* find(const T* a) const;

If there exists an element t in self such that the expression (*t == *a) is true, returns t. Otherwise, returns rwnil.

T* find(bool (*fn)(T*,void*), void* d) 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 one of the prototypes:

bool yourTester(T* a, void* d);

bool yourTester(const T* a, void* d);

for the const version. Client data may be passed through parameter d.

reference first(); const_reference first() const;

Returns a reference to the first element of self.

T* get();

Removes and returns the first element in the collection.

size_type index(const T* a) const;

Returns the position of the first item t in self such that (*t == *a), or returns the static member npos if no such item exists.

size_type index(bool (*fn)(T*,void*), void* d) const; size_type index(bool (*fn)(const T*,void*), void* d) const;

Returns the position of the first item t in self such that((*fn)(t,d)) is true, or returns the static member npos if no such item exists. fn points to a user-defined tester function which must have one of the prototypes:

bool yourTester(T* a, void* d);

bool yourTester(const T* a, void* d);

for the const version. Client data may be passed through parameter d.

bool insert(T* a);

Adds the item a to the end of the collection. Returns true.

void insertAt(size_type i, T* a);

Inserts the item a in front of the item at position i in self. self position must be between zero and the number of entries in the collection, otherwise the function throws an exception of type RWBoundsErr.

bool isEmpty() const;

Returns true if there are no items in the collection, false otherwise.

T*& last(); T*const& last() const;

Returns a reference to the last item in the collection.

reference maxElement(); const_reference maxElement() const; reference minElement(); const_reference minElement() const;

Returns a reference to the maximum or minimum element in self.

size_type occurrencesOf(const T* a) const;

Returns the number of elements t in self such that the expression (*t == *a) is true.

size_type occurrencesOf(bool (*fn)( T*,void*), void* d) const; 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 one of the prototypes:

bool yourTester(T* a, void* d);

bool yourTester(const T* a, void* d);

for the const version. Client data may be passed through parameter d.

void prepend(T* a);

Adds the item a to the beginning of the collection.

T* remove(const T* a);

Removes and returns the first element t in self such that the expression (*t == *a) is true. Returns rwnil if there is no such element.

T* remove(bool (*fn)( T*,void*), void* d); 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 one of the prototypes:

bool yourTester(T* a, void* d);

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 such that the expression (*t == *a) is true. Returns the number of items removed.

size_type removeAll(bool (*fn)( T*,void*), void* d); 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 one of the prototypes:

bool yourTester(T* a, void* d);

bool yourTester(const T* a, void* d);

for the const version. Client data may be passed through parameter d.

T* removeAt(size_type i);

Removes and returns the item at position i in self. self position must be between zero and one less then the number of entries in the collection, otherwise the function throws an exception of type RWBoundsErr.

T* removeFirst();

Removes and returns the first item in the collection.

T* removeLast();

Removes and returns the first item in the collection.

size_type replaceAll(const T* oldVal,T* newVal);

Replaces with newVal all elements t in self such that the expression (*t == *oldVal) is true. Returns the number of items replaced.

size_type replaceAll(bool (*fn)(T*, void*),void* d,T* newVal); size_type replaceAll(bool (*fn)(const T*, void*),void* d,T* newVal);

Replaces with newVal all elements t in self such that the expression ((*fn)(t,d))is true. Returns the number of items replaced. fn points to a user-defined tester function which must have prototype:

bool yourTester(T* a, void* d);

bool yourTester(const T* a, void* d);

Client data may be passed through parameter d.

void sort();

Sorts the collection using the less-than operator to compare elements. Elements are dereferenced before being compared.

list<T*, allocator>& std(); const list<T*, allocator>& std() const;

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

Static Public Data Member

const size_type npos;

This is the value returned by member functions such as index to indicate a non-position. The value is equal to ~(size_type)0.

Related Global Operators

RWvostream& operator<<(RWvostream& strm, const RWTPtrDlist<T>& coll); RWFile& operator<<(RWFile& strm, const RWTPtrDlist<T>& 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, RWTPtrDlist<T>& coll); RWFile& operator>>(RWFile& strm, RWTPtrDlist<T>& coll);

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

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

RWTPtrDlistIterator<T>

Synopsis

#include<rw/tpdlist.h>
 
RWTPtrDlist<T> dl;
RWTPtrDlistIterator<T> itr(dl);


Note: If you have the Standard C++ Library, use the interface described here. Otherwise, use the restricted interface to RWTPtrDlistIterator described in Appendix A: Alternate Template Class Interfaces.


Description

RWTPtrDlistIterator provides an iterator interface to the Tools 7 Standard C++ Library-based collections which is compatible with the iterator interface provided for the Tools.h++ 6.xcontainers.

The order of iteration over an RWTPtrDlist is dependent on the order of the values in the container.

The current item referenced by this iterator is undefined after construction or after a call to reset(). The iterator becomes valid after being advanced with either preincrement or operator().

For both operator++ and operator(), iterating past the last element will return a value equivalent to boolean false. Continued increments will return a value equivalent to false until reset() is called. For operator--, decrementing past the first element will return a value equivalent to false.

Persistence

None

Examples

#include<rw/tpdlist.h>
 
#include<iostream.h>
#include<rw/cstring.h>
 
int main(){
   RWTPtrDlist<RWCString> a;
   RWTPtrDlistIterator<RWCString> itr(a);
   a.insert(new RWCString("John"));
   a.insert(new RWCString("Steve"));
   a.insert(new RWCString("Mark"));
   a.insert(new RWCString("Steve"));
 
   for(;itr();)
 
     cout << *itr.key() <<endl;
 
   return 0;
 
}

Program Output:

John
Steve
Mark
Steve

Public Constructors

RWTPtrDlistIterator<T>(RWTPtrDlist<T>& l);

Creates an iterator for the list l. The iterator begins in an undefined state and must be advanced before the first element will be accessible

Public Member Operators

T* operator()();

Advances self to the next element, dereferences the resulting iterator and returns its value. If the iterator has advanced past the last item in the container, the element returned will be a nil pointer equivalent to boolean false.

RWBoolean operator++();

Advances self to the next element. If the iterator has been reset or just created, self will reference the first element. If, before iteration, self referenced the last value in the list, self will now referece an undefined value distinct from the reset value and a value equivalent to false will be returned. Otherwise, a value equivalent to true is returned.


Note: No post-increment operator is provided.


RWBoolean operator+=(size_type n);

Behaves as if operator++() had been applied n times

RWBoolean operator--();

Moves self back to the immediately previous element. If the iterator has been reset or just created, self operator will returna value equivalent to false, otherwise it will return a value equivalent to true. If self references the the first element, it will now be in the reset state. If self has been iterated past the last value in the list, it will now reference the last item in the list.


Note: No post-decrement operator is provided.


RWBoolean operator-=(size_type n);

Behaves as if operator—() had been applied n times

Public Member Functions

RWTPtrDlist<T>* container() const;

Returns a pointer to the collection being iterated over.

T* findNext(const T* a);

Returns the first element t encountered while iterating self forward, such that the expression (*t == *a) is true. If no such element exists, returns a nil pointer equivalent to false. Leaves self referencing the found item, or "past the end."

T* findNext(RWBoolean(*fn)(T*, void*), void* d);

Returns the first element t encountered by iterating self forward 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. If no such element exists, returns a nil pointer equivalent to false. Leaves self referencing the found item, or "past the end."

void insertAfterPoint(T* p);

Inserts the pointer p into the container directly after the element referenced by self.

T* key();

Returns the stored value referenced by self. Undefined if self is not referencing a value within the list.

T* remove();

Returns the stored value referenced by self and removes it from the collection. Undefined if self is not referencing a value within the list.

T* removeNext(const T*);

Returns and removes the first element t, encountered by iterating self forward, such that the expression (*t == *a) is true. If no such element exists, returns nil.

T* removeNext(RWBoolean(*fn)(T*, void*), void* d);

Returns and removes the first element t, encountered by iterating self forward, 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. If no such element exists, returns nil.

void reset(); void reset(RWTPtrDlist<T>& l*);

Resets the iterator so that after being advanced it will reference the first element of the collection. Using reset with no argument will reset the iterator on the current container. Supplying RWTPtrDlist<T> to reset() will reset the iterator on the new container.

RWTPtrHashDictionary

Synopsis

#define RWTPtrHashDictionary RWTPtrHashMap


Note: If you have the Standard C++ Library, refer to the reference for this class under its new name: RWTPtrHashMap. Although the old name (RWTPtrHashDictionary) is still supported, we recommend that you use the new name when coding your applications. If you do not have the Standard C++ Library, refer to the description of RWTPtrHashDictionary in Appendix A: Alternate Template Class Interfaces.


RWTPtrHashDictionaryIterator

Synopsis

#define RWTPtrHashDictionaryIterator RWTPtrHashMapIterator


Note: If you have the Standard C++ Library, refer to the reference for this class under its new name: RWTPtrHashMapIterator. Although the old name (RWTPtrHashDictionaryIterator) is still supported, we recommend that you use the new name when coding your applications. If you do not have the Standard C++ Library, refer to the description of RWTPtrHashDictionaryIterator in Appendix A: Alternate Template Class Interfaces.


RWTPtrHashMap<K,T,H,EQ>

Synopsis

#include <rw/tphdict.h> 
RWTPtrHashMap<K,T,H,EQ> m;


Note: If you have the Standard C++ Library, use the interface described here. Otherwise, use the interface for RWTPtrHashDictionary described in Appendix A: Alternate Template Class Interfaces.


Description

This class maintains a pointer-based collection of associations of type pair<K* const, T*>. These pairs are stored according to a hash object of type H. H must provide a hash function on elements of type K via a public member

     unsigned long operator()(const K& x)

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)

which should return true if x and y are equivalent.

RWTPtrHashMap<K,T,H,EQ> will not accept a key that compares equal to any key already in the collection. (RWTPtrHashMultiMap<K,T,H,EQ> may contain multiple keys that compare equal to each other.) Equality is based on the comparison object and not on the == operator.

Persistence

Isomorphic

Examples

//
 
// tphmap.cpp
//
#include<rw/tphdict.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";
   RWTPtrHashMap<RWCString,int,silly_hash,equal_to<RWCString> >
       contest;
   contest.insert(new RWCString("First"), new int(7));
   contest.insert(&snd,new int(3));
 
   //duplicate insertion rejected
   contest.insert(&snd,new int(6));
   contest.insert(new RWCString("Third"), new int(2));
   cout << "There was " 
        << contest.occurrencesOf(new RWCString("Second"))
        << " second place winner." << endl;
   return 0;
}

Program Output:

There was 1 second place winner.

Related Classes

Class RWTPtrHashMultiMap<K,T,H,EQ> offers the same interface to a pointer-based collection that accepts multiple keys that compare equal to each other.

Class rw_hashmap<K*,T*,rw_deref_hash<H,K>,rw_deref_compare<C,K> > is the C++-standard library style collection that serves as the underlying implementation for this collection.

Public Typedefs

typedef rw_deref_hash<H,K>                container_hash;
typedef rw_deref_compare<EQ,K>            container_eq;
typedef rw_hashmap<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;

Public Constructors

RWTPtrHashMap<K,T,H,EQ>();

Constructs an empty map.

RWTPtrHashMap<K,T,H,EQ>(const RWTPtrHashMap<K,T,H,EQ>& rwm);

Copy constructor.

RWTPtrHashMap<K,T,H,EQ>(const container_type & m);

Constructs a pointer based hash map by copying all elements from m.

RWTPtrHashMap<K,T,H,EQ>(const H& h, size_type sz = RWDEFAULT_CAPACITY);

This Tools.h++ 6.x style constructor creates an empty hashed map which uses the hash object h and has an initial capacity of sz.

RWTPtrHashMap<K,T,H,EQ>(const 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.

Public Member Operators

RWTPtrHashMap<K,T,H,EQ>& operator=(const container_type& m); RWTPtrHashMap<K,T,H,EQ>& operator=(const RWTPtrHashMap<K,T,H,EQ>& m);

Destroys all associations in self and replaces them by copying all associations from m.

bool operator==(const RWTPtrHashMap<K,T,H,EQ>& m) const;

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.

T*& operator[](K* key);

Looks up key and returns a reference to its associated item. If the key is not in the dictionary, then it will be added with an associated uninitialized pointer of type T*. Because of this, if there is a possibility that a key will not be in the dictionary, then this operator should only be used as an lvalue.

Public Member Functions

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. self 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 self 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 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 if the insertion is successful, otherwise returns false. The function will return true unless the collection already holds an association with the equivalent key.

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 and returns j. 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 map 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_hashmap<K*,T*,rw_deref_hash<H,K>,deref_compare<EQ,K>>& std(); const rw_hashmap<K*,T*,rw_deref_hash<H,K>,deref_compare<EQ,K>>& std() const;

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

Related Global Operators

RWvostream& operator<<(RWvostream& strm, const RWTPtrHashMap<K,T,H,EQ>& coll); RWFile& operator<<(RWFile& strm, const RWTPtrHashMap<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, RWTPtrHashMap<K,T,H,EQ>& coll); RWFile& operator>>(RWFile& strm, RWTPtrHashMap<K,T,H,EQ>& coll);

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

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

RWTPtrHashMapIterator<K,T,H,EQ>

Synopsis

#include<rw/tphdict.h>
 
RWTPtrHashMap<K,T,H,EQ> m;
RWTPtrHashMap<K,T,H,EQ> itr(m); 


Note: If you have the Standard C++ Library, use the interface described here. Otherwise, use the interface for RWTPtrHashDictionaryIterator described in Appendix A: Alternate Template Class Interfaces.


Description

RWTPtrHashMapIterator is supplied with Tools.h++ 7.x to provide an iterator interface to the Standard Library based collections that has backward compatibility with the container iterators provided in Tools.h++ 6.x.

Iteration over an RWTPtrHashMap is pseudorandom and dependent on the capacity of the underlying hash table and the hash function being used.

The current item referenced by this iterator is undefined after construction or after a call to reset(). The iterator becomes valid after being advanced with either a preincrement or operator().

For both operator++ and operator(), iterating past the last element will return a value equivalent to boolean false. Once this state is reached, continued increments will return a value equivalent to false until reset() is called.

Persistence

None

Examples

#include<rw/tphdict.h>
 
#include<iostream.h>
#include<rw/cstring.h>
 
struct silly_h{
   unsigned long operator()(RWCString x) const
     { return x.length() * (long)x(0); }
};
 
int main(){
   RWTPtrHashMap
     <RWCString,int,silly_h,equal_to<RWCString> > age;
 
   RWTPtrHashMapIterator
     <RWCString,int,silly_h,equal_to<RWCString> > itr(age);
 
   age.insert(new RWCString("John"),new int(30));
   age.insert(new RWCString("Steve"),new int(17));
   age.insert(new RWCString("Mark"),new int(24));
 
//Duplicate insertion is rejected
   age.insert(new RWCString("Steve"),new int(24));
 
   for(;++itr;)
     cout << *itr.key() << "\'s age is " << *itr.value() << endl;
 
   return 0;
}

Program Output (not necessarily in this order):

John's age is 30
Mark's age is 24
Steve's age is 17

Public Constructors

RWTPtrHashMapIterator<K,T,H,EQ>(RWTPtrHashMap<K,T,H,EQ>&h);

Creates an iterator for the hashed map h. The iterator begins in an undefined state and must be advanced before the first element will be accessible.

Public Member Operators

K* operator()();

Advances self to the next element, dereferences the resulting iterator and returns its key. If the iterator has advanced past the last item in the container, the element returned will be a nil pointer equivalent to boolean false.

RWBoolean operator++();

Advances self to the next element. If the iterator has been reset or just created self will now reference the first element. If, before iteration, self referenced the last association in the multi-map, self will now reference an undefined value and a value equivalent to false will be returned. Otherwise, a value equivalent to true is returned.


Note: No post-increment operator is provided.


Public Member Functions

RWTPtrHashMap<K,T,H,EQ>* container() const;

Returns a pointer to the collection being iterated over.

K* key() const;

Returns the key portion of the association currently referenced by self. Undefined if self is not referencing a value within the map.

void reset(); void reset(RWTPtrHashMap<K,T,H,EQ>& h);

Resets the iterator so that after being advanced it will reference the first element of the collection. Using reset() with no argument will reset the iterator on the current container. Supplying a hashed map with reset() will reset the iterator on that container.

T* value();

Returns the value portion of the association pointed to by self. The behavior is undefined if the map is empty.

RWTPtrHashMultiMap<K,T,H,EQ>

Synopsis

#include <rw/tphmmap.h> 
RWTPtrHashMultiMap<K,T,H,EQ> m;

Standard C++ Library Dependent!


Note: RWTPtrHashMultiMap requires the Standard C++ Library.


Description

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 hash function on elements of type K via a public member

   unsigned long operator()(const K& x)

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)

which should return true if x and y are equivalent.

RWTPtrHashMultiMap<K,T,H,EQ> may contain multiple keys that compare equal to each other. (RWTPtrHashMap<K,T,H,EQ> will not accept a key that compares equal to any key already in the collection.) Equality is based on the comparison object and not on the == operator.

Persistence

Isomorphic

Examples

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

Related Classes

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.

Public Typedefs

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;

Public Constructors

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.

Public Member Operators

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.

Public Member Functions

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

Related Global Operators

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.

RWTPtrHashMultiMapIterator<K,T,H,EQ>

Synopsis

#include<rw/tphmmap.h>
 
RWTPtrHashMultiMap<K,T,H,EQ> m;
RWTPtrHashMultiMap<K,T,H,EQ> itr(m);

Standard C++ Library Dependent!


Note: RWTPtrHashMultiMapIterator requires the Standard C++ Library.


Description

RWTPtrHashMultiMapIterator is supplied with Tools 7 to provide an iterator interface to the new Standard Library based collections that has backward compatibility with the container iterators provided in Tools 6.

Iteration over an RWTPtrHashMultiMap is pseudorandom and dependent on the capacity of the underlying hash table and the hash function being used. The only useable relationship between consecutive elements is that elements which are defined to be equivalent by the equivalence object, EQ, will remain adjacent.

The current item referenced by this iterator is undefined after construction or after a call to reset(). The iterator becomes valid after being advanced with either a preincrement or operator().

For both operator++ and operator(), iterating past the last element will return a value equivalent to boolean false. Continued increments will return a value equivalent to false until reset() is called.

Persistence

None

Examples

#include<rw/tphmmap.h>
 
#include<iostream.h>
#include<rw/cstring.h>
 
struct silly_h{
   unsigned long operator()(RWCString x) const
     { return x.length() * (long)x(0); }
};
 
int main(){
   RWTPtrHashMultiMap
     <RWCString,int,silly_h,equal_to<RWCString> > age;
 
   RWTPtrHashMultiMapIterator
     <RWCString,int,silly_h,equal_to<RWCString> > itr(age);
 
   age.insert(new RWCString("John"),new int(30));
   age.insert(new RWCString("Steve"),new int(17));
   age.insert(new RWCString("Mark"),new int(24));
   age.insert(new RWCString("Steve"),new int(24));
 
   for(;++itr;)
     cout << *itr.key() << "\'s age is " << *itr.value() << endl;
 
   return 0;
}

Program Output (not necessarily in this order):

John's age is 30
Mark's age is 24
Steve's age is 24
Steve's age is 17

Public Constructors

RWTPtrHashMultiMapIterator<K,T,H,EQ>(RWTPtrHashMultiMap<K,T,H,EQ>&h);

Creates an iterator for the hashed multi-map h. The iterator begins in an undefined state and must be advanced before the first element will be accessible.

Public Member Operators

K* operator()();

Advances self to the next element, dereferences the resulting iterator and returns its key. If the iterator has advanced past the last item in the container, the element returned will be a nil pointer equivalent to boolean false.

RWBoolean operator++();

Advances self to the next element. If the iterator has been reset or just created self will now reference the first element. If, before iteration, self referenced the last association in the multi-map, self will now reference an undefined value and a value equivalent to false will be returned. Otherwise, a value equivalent to true is returned.


Note: No post-increment operator is provided.


Public Member Functions

RWTPtrHashMultiMap<K,T,H,EQ>* container() const;

Returns a pointer to the collection being iterated over.

K* key() const;

Returns the key portion of the association currently referenced by self. Undefined if self is not referencing a value within the multimap.

void reset(); void reset(RWTPtrHashMultiMap<K,T,H,EQ>& h);

Resets the iterator so that after being advanced it will reference the first element of the collection. Using reset() with no argument will reset the iterator on the current container. Supplying a RWTPtrHashMultiMap to reset() will reset the iterator on that container.

T* value();

Returns the value portion of the association referenced by self. Undefined if self is not valid.

RWTPtrHashMultiSet<T,H,EQ>

Synopsis

#include <rw/tphasht.h>
 
RWTPtrHashMultiSet<T,H,EQ> hmset;


Note: If you have the Standard C++ Library, use the interface described here. Otherwise, use the interface for RWTPtrHashTable described in Appendix A: Alternate Template Class Interfaces.


Description

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 hash function on elements of type T via a public member

unsigned long operator()(const T& x)

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)

which should return true if x and y are equivalent, false otherwise.

RWTPtrHashMultiSet<T,H,EQ> may contain multiple items that compare equal to each other. (RWTPtrHashSet<T,H,EQ> will not accept an item that compares equal to an item already in the collection.)

Persistence

Isomorphic

Examples

//
// 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;
}

Related Classes

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

Public Typedefs

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;

Public Constructors

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 self 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 self multi-set will have sz buckets, use h as a hashing function and eq to test for equality between stored elements.

Public Member Operators

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.

Public Member Functions

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

Related Global Operators

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.

RWTPtrHashMultiSetIterator<T,H,EQ>

Synopsis

#include<rw/tphasht.h>
 
RWTPtrHashMultiSet<T,H,EQ> m;
RWTPtrHashMultiSet<T,H,EQ> itr(m); 


Note: If you have the Standard C++ Library, use the interface described here. Otherwise, use the interface for RWTPtrHashTableIterator described in Appendix A: Alternate Template Class Interfaces.


Description

RWTPtrHashMultiSetIterator is supplied with Tools.h++ 7.x to provide an iterator interface to the Standard Library based collections that has backward compatibility with the container iterators provided in Tools.h++ 6.x.

Iteration over an RWTPtrHashMultiSet is pseudorandom and dependent on the capacity of the underlying hash table and the hash function being used. The only useable relationship between consecutive elements is that all elements which are defined to be equivalent by the equivalence object, EQ, will remain adjacent.

The current item referenced by this iterator is undefined after construction or after a call to reset() operation. The iterator becomes valid after being advanced with either a preincrement or operator().

For both operator++ and operator(), iterating past the last element will return a value equivalent to boolean false. Continued increments will return a value equivalent to false until reset() is called.

Persistence

None

Examples

#include<rw/tphasht.h>
 
#include<iostream.h>
#include<rw/cstring.h>
 
struct silly_h{
   unsigned long operator()(RWCString x) const
     { return x.length() * (long)x(0); }
};
 
int main(){
   RWTPtrHashMultiSet<RWCString,silly_h,equal_to<RWCString> > age;
 
   RWTPtrHashMultiSetIterator
   <RWCString,silly_h,equal_to<RWCString> > itr(age);
 
   age.insert(new RWCString("John"));
   age.insert(new RWCString("Steve"));
   age.insert(new RWCString("Mark"));
   age.insert(new RWCString("Steve"));
 
   for(;++itr;)
     cout << *itr.key() << endl;
 
   return 0;
}

Program Output (not necessarily in this order):

John
Mark
Steve
Steve

Public Constructors

RWTPtrHashMultiSetIterator<T,H,EQ>(RWTPtrHashMultiSet<T,H,EQ>&h);

Creates an iterator for the hashed multi-set h. The iterator begins in an undefined state and must be advanced before the first element will be accessible.

Public Member Operators

T* operator()();

Advances self to the next element, dereferences the resulting iterator and returns its value. If the iterator has advanced past the last item in the container, the element returned will be a nil pointer equivalent to boolean false.

RWBoolean operator++();

Advances self to the next element. If the iterator has been reset or just created self will now reference the first element. If, before iteration, self referenced the last association in the multiset, self will now reference an undefined value and a value equivalent to false will be returned. Otherwise, a value equivalent to true is returned.


Note: No post-increment operator is provided.


Public Member Functions

RWTPtrHashMultiSet<T,H,EQ>* container() const;

Returns a pointer to the collection being iterated over.

T* key() const;

Returns the value currently referenced by self. Undefined if self is not referencing a value within the multiset.

void reset(); void reset(RWTPtrHashMultiSet<T,H,EQ>& h);

Resets the iterator so that after being advanced it will reference the first element of the collection. Using reset() with no argument will reset the iterator on the current container. Supplying a RWTPtrHashMultiSet to reset() will reset the iterator on that container.

RWTPtrHashSet<T,H,EQ>

Synopsis

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


Note: If you have the Standard C++ Library, use the interface described here. Otherwise, use the restricted interface to RWTPtrHashSet described in Appendix A: Alternate Template Class Interfaces.


Description

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 hash function on elements of type T via a public member

unsigned long operator()(const T& x)

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)

which should return true if x and y are equivalent, false otherwise.

RWTPtrHashSet<T,H,EQ> will not accept an item that compares equal to an item already in the collection. (RWTPtrHashMultiSet<T,H,EQ> may contain multiple items that compare equal to each other.) Equality is based on the equality object and not on the == operator.

Persistence

Isomorphic

Example

//
 
// 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;
}

Related Classes

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

Public Typedefs

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;

Public Constructors

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.

Public Member Operators

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.

Public Member Functions

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

Related Global Operators

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.

RWTPtrHashSetIterator<T,H,EQ>

Synopsis

#include<rw/tphset.h>
 
RWTPtrHashSet<T,H,EQ> m;
RWTPtrHashSet<T,H,EQ> itr(m); 


Note: If you have the Standard C++ Library, use the interface described here. Otherwise, use the restricted interface to RWTPtrHashSetIterator described in Appendix A: Alternate Template Class Interfaces.


Description

RWTPtrHashSetIterator is supplied with Tools.h++ 7.x to provide an iterator interface to the Standard Library based collections that has backward compatibility with the container iterators provided in Tools.h++ 6.x.

Iteration over an RWTPtrHashSet is pseudorandom and dependent on the capacity of the underlying hash table and the hash function being used.

The current item referenced by this iterator is undefined after construction or after a call to reset(). The iterator becomes valid after being advanced with either a pre-increment or an operator().

For both operator++ and operator(), iterating past the last element will return a value equivalent to boolean false. Continued increments will return a value equivalent to false until reset() is called.

Persistence

None

Examples

#include<rw/tphset.h>
 
#include<iostream.h>
#include<rw/cstring.h>
 
struct silly_h{
   unsigned long operator()(RWCString x) const
     { return x.length() * (long)x(0); }
};
 
int main(){
   RWTPtrHashSet <RWCString,silly_h,equal_to<RWCString> > age;
 
   RWTPtrHashSetIterator
     <RWCString,silly_h,equal_to<RWCString> > itr(age);
 
   age.insert(new RWCString("John"));
   age.insert(new RWCString("Steve"));
   age.insert(new RWCString("Mark"));
 
//Duplicate insertion is rejected
   age.insert(new RWCString("Steve"));
 
   for(;++itr;) cout << *itr.key() << endl;
 
   return 0;
}

Program Output (not necessarily in this order):

John
Mark
Steve

Public Constructors

RWTPtrHashSetIterator<T,H,EQ>(RWTPtrHashSet<T,H,EQ>&h);

Creates an iterator for the hashed set h. The iterator begins in an undefined state and must be advanced before the first element will be accessible.

Public Member Operators

T* operator()();

Advances self to the next element, dereferences the resulting iterator and returns its value. If the iterator has advanced past the last item in the container, the element returned will be a nil pointer equivalent to boolean false.

RWBoolean operator++();

Advances self to the next element. If the iterator has been reset or just created self will now reference the first element. If, before iteration, self referenced the last association in the multi-map, self will now point to an undefined value and a value equivalent to false will be returned. Otherwise, a value equivalent to true is returned.


Note: No post-increment operator is provided.


Public Member Functions

RWTPtrHashSet<T,H,EQ>* container() const;

Returns a pointer to the collection being iterated over.

T* key() const;

Returns the element referenced by self. Undefined if self is not referencing a value within the set.

void reset(); void reset(RWTPtrHashSet<T,H,EQ>& h);

Resets the iterator so that after being advanced it will reference the first element of the collection. Using reset() with no argument will reset the iterator on the current container. Supplying a RWTPtrHashSet to reset() will reset the iterator on that container.

RWTPtrHashTable

Synopsis

#define RWTPtrHashTable RWTPtrHashMultiSet


Note: If you have the Standard C++ Library, refer to the reference for this class under its new name: RWTPtrHashMultiSet. Although the old name (RWTPtrHashTable) is still supported, we recommend that you use the new name when coding your applications. If you do not have the Standard C++ Library, refer to the description of RWTPtrHashTable in Appendix A: Alternate Template Class Interfaces.


RWTPtrHashTableIterator

Synopsis

#define RWTPtrHashTableIterator RWTPtrHashMultiSetIterator


Note: If you have the Standard C++ Library, refer to the reference for this class under its new name: RWTPtrHashMultiSetIterator. Although the old name (RWTPtrHashTableIterator) is still supported, we recommend that you use the new name when coding your applications. If you do not have the Standard C++ Library, refer to the description of RWTPtrHashTableIterator in Appendix A: Alternate Template Class Interfaces.