Chapter 8. RWTQueue<T,C> - RWTValHashTable

RWTQueue<T,C>

Synopsis

#include <rw/tqueue.h>
 
RWTQueue<T, C> queue;

Description

This class represents a parameterized queue. Not only can the type of object inserted into the queue be parameterized, but also the implementation.

Parameter T represents the type of object in the queue, either a class or built in type. The class T must have:

  • well-defined copy semantics (T::T(const T&) or equiv.);

  • well-defined assignment semantics (T::operator=(const T&) or equiv.);

  • any other semantics required by class C.

Parameter C represents the class used for implementation. Useful choices are RWTValSlist<T> or RWTValDlist<T>. Vectors, such as RWTValOrderedVector<T>, can also be used, but tend to be less efficient at removing an object from the front of the list.

Persistence

None

Example

In this example a queue of RWCStrings, implemented as a singly-linked list, is exercised.

#include <rw/tqueue.h>
#include <rw/cstring.h>
#include <rw/tvslist.h>
#include <rw/rstream.h>
 
main() {
  RWTQueue<RWCString, RWTValSlist<RWCString> > queue;
 
  queue.insert("one");   // Type conversion occurs
  queue.insert("two");
  queue.insert("three");
 
  while (!queue.isEmpty())
    cout << queue.get() << endl;

  return 0;
}

Program Output:

one
two
three

Public Member Functions

void clear();

Removes all items from the queue.

size_t entries() const;

Returns the number of items in the queue.

T first() const;

Returns, but does not remove, the first item in the queue (the item least recently inserted into the queue).

T get();

Returns and removes the first item in the queue (the item least recently inserted into the queue).

RWBoolean isEmpty() const;

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

void insert(T a);

Inserts the item a at the end of the queue.

T last() const;

Returns, but does not remove, the last item in the queue (the item most recently inserted into the queue).

RWTStack<T,C>

Synopsis

#include <rw/tstack.h>
 
RWTStack<T, C> stack;

Description

This class maintains a stack of values. Not only can the type of object inserted onto the stack be parameterized, but also the implementation of the stack.

Parameter T represents the type of object in the stack, either a class or built in type. The class T must have:

  • well-defined copy semantics (T::T(const T&) or equiv.);

  • well-defined assignment semantics (T::operator=(const T&) or equiv.);

  • any other semantics required by class C.

Parameter C represents the class used for implementation. Useful choices are RWTValOrderedVector<T> or RWTValDlist<T>. Class RWTValSlist<T> can also be used, but note that singly-linked lists are less efficient at removing the last item of a list (function pop()), because of the necessity of searching the list for the next-to-the-last item.

Persistence

None

Example

In this example a stack of ints, implemented as an ordered vector, is exercised.

#include <rw/tstack.h>
#include <rw/tvordvec.h>
#include <rw/rstream.h>
 
main() {
  RWTStack<int, RWTValOrderedVector<int> > stack;
 
  stack.push(1);
  stack.push(5);
  stack.push(6);
 
  while (!stack.isEmpty())
    cout << stack.pop() << endl;
  return 0;
}

Program Output:

6
5
1

Public Member Functions

void clear();

Removes all items from the stack.

size_t entries() const;

Returns the number of items currently on the stack.

RWBoolean isEmpty() const;

Returns TRUE if there are currently no items on the stack, FALSE otherwise.

void push(T a);

Push the item a onto the top of the stack.

T pop();

Pop (remove and return) the item at the top of the stack. If there are no items on the stack then an exception of type TOOL_INDEX will occur.

T top() const;

Returns (but does not remove) the item at the top of the stack.

RWTValDeque<T>

Synopsis

#include <rw/tvdeque.h> 
RWTValDeque<T> deq;

Standard C++ Library Dependent!


Note: RWTValDeque requires the Standard C++ Library.


Description

This class maintains a collection of values implemented as a double-ended queue, or deque. Order is determined externally and elements are accessible by index. Use this class when insertions and deletions usually occur at either the beginning or the end of the collection.

Persistence

Isomorphic

Example

In this example, a double-ended queue of ints is exercised.

//
 
// tvdqint.cpp
//
#include <rw/tvdeque.h>
#include <iostream.h>
 
/* 
 * This program partitions integers into even and odd numbers
 */
 
int main(){
    RWTValDeque<int> numbers;
 
    int n;
   
    cout << "Input an assortment of integers (EOF to end):" 
       << endl;
   
  while (cin >> n) {
    if (n % 2 == 0) 
      numbers.pushFront(n);
    else
      numbers.pushBack(n);
  }
 
  while (numbers.entries()) {
    cout << numbers.popFront() << endl;
  }
      
  return 0;
}

Program Input:

1 2 3 4 5

Program Output:

4
2
1
3
5

Related Classes

Classes RWTValSlist<T>, RWTValDlist<T>, RWTValSortedDlist<T>, and RWTValOrderedVector<T> also provide a Rogue Wave interface to C++-standard sequence collections. The list classes should be considered for frequent insertions (or removals) in the interior of the collection. The vector may be more efficient if most insertions and removals occur at the end of the collection.

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

Public Typedefs

typedef deque<T,allocator>                  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;

Public Constructors

RWTValDeque<T>();

Constructs an empty, double-ended queue.

RWTValDeque<T>(const deque<T,allocator>& deq);

Constructs a double-ended queue by copying all elements of deq.

RWTValDeque<T>(const RWTValDeque<T>& rwdeq);

Copy constructor.

RWTValDeque<T>(size_type n, const T& val = T());

Constructs a double-ended queue with n elements, each initialized to val.

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

Constructs a double-ended queue by copying elements from the array of Ts pointed to by first, up to, but not including, the element pointed to by last.

Public Member Operators

RWTValDeque<T>& operator=(const RWTValDeque<T,allocator>& deq); RWTValDeque<T>& operator=(const deque<T>& deq);

Calls the destructor on all elements of self and replaces them by copying all elements of deq.

bool operator<(const RWTValDeque<T>& deq) const; bool operator<(const deque<T,allocator>& deq) const;

Returns true if self compares lexicographically less than deq, otherwise returns false. Type T must have well-defined less-than semantics (T::operator<(const T&) or equivalent).

bool operator==(const RWTValDeque<T>& deq) const; bool operator==(const deque<T,allocator>& deq) const;

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

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

Returns a reference to the ith element of self. Index i should 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(const_reference a);

Adds the item a to the end of the collection.

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

void yourfun(const_reference a, void* d);

void yourfun(reference a, void* d);

Client data may be passed through parameter d.

reference at(size_type i); const_reference at(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.

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. Each item will have its destructor called.

bool contains(const_reference 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_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.

iterator end(); const_iterator end() const;

Returns a past-the-end valued iterator of self.

size_type entries() const;

Returns the number of elements in self.

bool find(const_reference a,T& k) const;

If there exists an element t in self such that the expression (t == a) is true, 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, T& 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.

reference first(); const_reference first() const;

Returns a reference to the first element of self.

size_type index(const_reference 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)(const_reference,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 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 end of the collection. Returns true.

void insertAt(size_type i, const_reference a);

Inserts the item a in front of the item at position i in self. This position must be between 0 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.

reference last(); const_reference 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 minimum or maximum element in the collection. Type T must have well-defined less-than semantics (T::operator<(const T&) or equivalent).

size_type occurrencesOf(const_reference a) const;

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

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.

void prepend(const_reference a);

Adds the item a to the beginning of the collection.

T popBack();

Removes and returns the last item in the collection.

T popFront();

Removes and returns the first item in the collection.

void pushBack(const_reference a);

Adds the item a to the end of the collection.

void pushFront(const_reference a);

Adds the item a to the beginning of the collection.

bool remove(const_reference a);

Removes the first element t in self such that the expression (t == a) is true and returns true. 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 such that the expression (t == a) is true. 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.

T removeAt(size_type i);

Removes and returns the item at position i in self. This position must be between 0 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, const T& newVal);

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

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

Replaces all elements t in self such that the expression ((*fn)(t,d))is true.with newVal Returns the number of items replaced. 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 sort();

Sorts the collection using the less-than operator (<) to compare elements.

deque<T,allocator>& std(); const deque<T,allocator>& std() const;

Returns a reference to the underlying C++-standard 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 C++-standard collections.

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

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

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

RWTValDlist<T>

Synopsis

#include <rw/tvdlist.h> 
RWTValDlist<T> dlist;


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


Description

This class maintains a collection of values, implemented as a doubly-linked list.

Persistence

Isomorphic

Example

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

//
 
// tvdldog.cpp
//
#include <rw/tvdlist.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:
  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(){
  RWTValDlist<Dog> terriers;
  terriers.insert("Cairn Terrier");  // NB: type conversion occurs
  terriers.insert("Irish Terrier");
  terriers.insert("Schnauzer");
 
  cout << "The list " <<
    (terriers.contains("Schnauzer") ? "does " : "does not ") <<
    "contain a Schnauzer\n";
 
  terriers.insertAt(
      terriers.index("Irish Terrier"),
      "Fox Terrier"
    );
 
  while (!terriers.isEmpty())
    cout << terriers.get() << endl;
 
  return 0;
}

Program Output:

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

Related Classes

Classes RWTValDeque<T>, RWTValSlist<T>, and RWTValOrderedVector<T> also provide a Rogue Wave 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::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;

Public Constructors

RWTValDlist<T>();

Constructs an empty, doubly-linked list.

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

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

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

Copy constructor.

RWTValDlist<T>(size_type n, const T& val = T());

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

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

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

Public Member Operators

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

Calls the destructor on all elements of self and replaces them by copying all elements of lst.

bool operator<(const RWTValDlist<T>& lst) const; bool operator<(const list<T,allocator>& lst) const;

Returns true if self compares lexicographically less than lst, otherwise returns false. Type T must have well-defined less-than semantics (T::operator<(const T&) or equivalent).

bool operator==(const RWTValDlist<T>& lst) const; bool operator==(const list<T,allocator>& lst) const;

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.

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

Returns a reference to the ith element of self. Index i should 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(const_reference a);

Adds the item a to the end of the collection.

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

void yourfun(const_reference a, void* d);

void yourfun(reference a, void* d);

Client data may be passed through parameter d.

reference at(size_type i); const_reference at(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.

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. Each item will have its destructor called.

bool contains(const_reference 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_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.

iterator end(); const_iterator end() const;

Returns a past-the-end valued iterator of self.

size_type entries() const;

Returns the number of elements in self.

bool find(const_reference a, T& k) const;

If there exists an element t in self such that the expression (t == a) is true, 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, T& 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.

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. If the collection is empty, the function throws an exception of type RWBoundsErr. This method is identical to removeFirst and is included for compatibility with previous versions.

size_type index(const_reference 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)(const_reference,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 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 end of the collection. Returns true.

void insertAt(size_type i,const_reference a);

Inserts the item a in front of the item at position i in self. This position must be between 0 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.

reference last(); const_reference 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 minimum or maximum element in the collection. Type T must have well-defined less-than semantics (T::operator<(const T&) or equivalent).

size_type occurrencesOf(const_reference a) const;

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

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.

void prepend(const_reference a);

Adds the item a to the beginning of the collection.

bool remove(const_reference a);

Removes the first element t in self such that the expression (t == a) is true and returns true. 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 such that the expression (t == a) is true. 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.

T removeAt(size_type i);

Removes and returns the item at position i in self. This position must be between 0 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_reference oldVal, const_reference newVal);

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

size_type replaceAll(bool (*fn)(const_reference,void*), void* d,

const value_type& newval);

Replaces 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(const_reference a, void* d);

Client data may be passed through parameter d.

void sort();

Sorts the collection using the less-than operator to compare elements.

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

Returns a reference to the underlying C++-standard 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.

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

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

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

RWTValDlistIterator<T>

Synopsis

#include<rw/tvdlist.h>
 
RWTValDlist<T> dl;
RWTValDlistIterator<T> itr(dl);


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


Description

RWTValDlistIterator provides an iterator interface to the Tools.h++ 7 Standard Library based collections which is compatible with the iterator interface provided for the Tools.h++ 6.x containers.

The order of iteration over an RWTValDlist is dependent on the order of insertion of the values into 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 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 equal to false until reset() is called.

Persistence

None

Examples

#include<rw/tvdlist.h>
 
#include<iostream.h>
#include<rw/cstring.h>
 
int main(){
 
   RWTValDlist<RWCString> a;
   RWTValDlistIterator<RWCString> itr(a);
 
   a.insert("John");
 
   a.insert("Steve");
   a.insert("Mark");
   a.insert("Steve");
 
   for(;itr();)
 
     cout << itr.key() << endl;
 
   return 0;
}

Program Output:

John
Steve
Mark
Steve

Public Constructors

RWTValDlistIterator<T>(RWTValDlist<T>& s);

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

Public Member Operators

RWBoolean operator()();

Advances self to the next element 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 reference an undefined value distinct from the reset value and false will be returned. Otherwise, true is returned. Note: no postincrement operator is provided.

RWBoolean operator+=(size_type n);

Behaves as if the operator++ member function had been applied n times

RWBoolean operator--();

Moves self back to the immediately previous element. If the iterator has been reset or just created, this operator will return false, otherwise it will return 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 postdecrement operator is provided.

RWBoolean operator-=(size_type n);

Behaves as if the operator-- member function had been applied n times

Public Member Functions

RWTValDlist<T>* container() const;

Returns a pointer to the collection being iterated over.

RWBoolean findNext(const T& a);

Advances self to the first element t encountered by iterating forward, such that the expression (t == a) is true. Returns true if an element was found, returns false otherwise.

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

Advances self to the first element t encountered by iterating 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. Returns true if an element was found, returns false otherwise.

T key();

Returns the stored value referenced by self.

RWBoolean remove();

Removes the value referenced by self from the collection. true is returned if the removal is successful, false is returned otherwise.

RWBoolean removeNext(const T);

Removes the first element t, encountered by iterating self forward, such that the expression (t == a) is true. Returns true if an element was found and removed, returns false otherwise.

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

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. Returns true if an element was found and removed, returns false otherwise.

void reset(); void reset(RWTValDlist<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 a RWTValDlist to reset() will reset the iterator on the new container.

RWTValHashDictionary

Synopsis

#define RWTValHashDictionary RWTValHashMap


Note: If you have the Standard C++ Library, refer to the reference for this class under its new name: RWTValHashMap. Although the old name (RWTValHashDictionary) 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 RWTValHashDictionary in Appendix A: Alternate Template Class Interfaces.


RWTValHashDictionaryIterator

Synopsis

#define RWTValHashDictionaryIterator RWTValHashMapIterator


Note: If you have the Standard C++ Library, refer to the reference for this class under its new name: RWTValHashMapIterator. Although the old name (RWTValHashDictionaryIterator) 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 RWTValHashDictionaryIterator in Appendix A: Alternate Template Class Interfaces.


RWTValHashMap<K,T,H,EQ>

Synopsis

#include <rw/tvhdict.h> 
RWTValHashMap<K,T,H,EQ> m;


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


Description

This class maintains a collection of keys, each with an associated item of type 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.

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

Persistence

Isomorphic

Related Classes

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

Class rw_hashmap<K,T,H,EQ> is the C++-standard compliant collection that serves as the underlying implementation for this collection.

Public Typedefs

typedef rw_hashmap<K,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 pair <const K,T>                       value_type;
typedef K                                      key_type;
typedef T                                      data_type;
typedef pair <const K,T>&                      reference;
typedef pair <const K,T>&                      const_reference;

Public Constructors

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

Constructs an empty map.

RWTValHashMap<K,T,H,EQ>(const rw_hashmap<K,T,H,EQ>& m);

Constructs a map by copying all elements of m.

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

Creates an empty hashed map which uses the hash object h and has an initial capacity of sz.

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

Copy constructor.

RWTValHashMap<K,T,H,EQ>(const value_type* first, const value_type* last);

Constructs a map by copying elements from the array of value_type pairs pointed to by first, up to, but not including, the pair pointed to by last.

Public Member Operators

RWTValHashMap<K,T,H,EQ>& operator=(const RWTValHashMap<K,T,H,EQ>& m); RWTValHashMap<K,T,H,EQ>& operator=(const rw_hashmap<K,T,H,EQ>& m);

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

bool operator==(const RWTValHashMap<K,T,H,EQ>& m) const; bool operator==(const rw_hashmap<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 pairs that compare equal to each other.

T& operator[](const 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 item provided by the default constructor for type T.

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. This 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. Each key and its associated item will have its destructor called.

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)(const_reference,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(const_reference 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().

bool find(const K& key, K& r) const;

If there exists a key j in self that compares equal to key, assigns j to r and returns true. Otherwise, returns false and leaves the value of r unchanged.

bool find(bool (*fn)(const_reference,void*),void* d, pair<K,T>& r) const;

If there exists an association a in self such that the expression ((*fn)(a,d)) is true, assigns a to r 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 K& a, void* d);

Client data may be passed through parameter d.

bool findValue(const K& key, T& r) const;

If there exists a key j in self that compares equal to key, assigns the item associated with j to r and returns true. Otherwise, returns false and leaves the value of r unchanged.

bool findKeyValue(const K& key, K& kr, T& tr) const;

If there exists a key j in self that compares equal to key, assigns j to kr, assigns the item associated with j to tr, and returns true. Otherwise, returns false and leaves the values of kr and tr unchanged.

bool insert(const K& key, const 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(const K& key,const 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)(const_reference,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(const_reference a, void* d);

Client data may be passed through parameter d.

bool remove(const K& key);

Removes the first association with key j in self such that the expression (j == key) is true and returns true. Returns false if there is no such association.

bool remove(bool (*fn)(const_reference,void*), void* d);

Removes the first association a in self such that the expression ((*fn)(a,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 K& key);

Removes all elements j in self that compare equal to key. Returns the number of items removed.

size_type removeAll(bool (*fn)(const_reference,void*), void* d);

Removes all associations a in self such that the expression ((*fn)(a,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 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,H,EQ>& std(); const rw_hashmap<K,T,H,EQ>& std() const;

Returns a reference to the underlying C++-standard 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 compliant collections.

Related Global Operators

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

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

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

RWTValHashMapIterator<K,T,H,EQ>

Synopsis

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


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


Description

RWTValHashMapIterator is supplied with Tools 7 to provide an iterator interface to RWTValHashMapIterator that has backward compatibility with the container iterators provided in Tools 6.

Iteration over an RWTValHashMap 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 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

Example

#include<rw/tvhdict.h>
 
#include<iostream.h>
#include<rw/cstring.h>
 
struct silly_h{
 
   unsigned long operator()(const RWCString& x) const
   { return x.length() * (long)x(0); }
};
 
int main(){
 
   RWTValHashMap
     <RWCString,int,silly_h,equal_to<RWCString> > age;
   RWTValHashMapIterator
     <RWCString, int, silly_h, equal_to<RWCString > > itr(age);
 
   age.insert(RWCString("John"), 30);
 
   age.insert(RWCString("Steve"),17);
   age.insert(RWCString("Mark"),24);
 
//Duplicate insertion rejected
 
   age.insert(RWCString("Steve"),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
Steve's age is 17
Mark's age is 24

Public Constructors

RWTValHashMapIterator<K,T,H,EQ>(RWTValHashMap<K,T,H,EQ>&h);

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

Public Member Operators

RWBoolean operator()();

Advances self to the next element. Returns false if the iterator has advanced past the last item in the container and true otherwise.

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 multimap, self will now reference an undefined value and false will be returned. Otherwise, true is returned.


Note: No postincrement operator is provided.


Public Member Functions

RWTValHashMap<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 pointed to by self.

void reset(); void reset(RWTValHashMap<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 RWTValHashMap to reset() will reset the iterator on that container.

T value();

Returns the value portion of the association referenced by self.

RWTValHashMultiMap<K,T,H,EQ>

Synopsis

#include <rw/tvhmmap.h> 
RWTValHashMultiMap<K,T,H,EQ> m;

Standard C++ Library Dependent!


Note: RWTValHashMultiMap requires the Standard C++ Library


Description

This class maintains a collection of keys, each with an associated item of type 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) const

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

which should return true if x and y are equivalent.

RWTValHashMultiMap<K,T,H,EQ> may contain multiple keys that compare equal to each other. (RWTValHashMap<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

//
 
// tvhmmrat.cpp
//
#include<rw/tvhmmap.h>
#include<iostream.h>
#include<rw/cstring.h>
 
struct silly_hash{
   unsigned long operator()(RWCString x) const
   { return x.length() * (long)x[0]; }
};
int main(){
  RWCString trd = "Third";
  RWTValHashMultiMap<RWCString,int,silly_hash,equal_to<RWCString> >
       contest;
  contest.insert("First", 7);
  contest.insert(trd,3);
 
  contest.insert(trd,6);      // self contains two distinct values
                              //equivalent to trd 
  contest.insert("Second",2);
 
  contest.resize(8);
  cout << "The table is " << contest.fillRatio() * 100.0 
       << "% full<< endl;
  return 0;
}

Program Output:

The table is 50% full

Related Classes

Class RWTValHashMap<K,T,H,EQ> offers the same interface to a collection that will not accept multiple keys that compare equal to each other.

Class rw_hashmultimap<K,T,H,EQ> is the C++-standard collection that serves as the underlying implementation for this collection.

Public Typedefs

typedef rw_hashmultimap<K,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 pair <const K,T>                       value_type;
typedef pair <const K,T>&                      reference; 
typedef const pair<const K,T>&                 const_reference;

Public Constructors

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

Constructs an empty map.

RWTValHashMultiMap<K,T,H,EQ>(const rw_hashmultimap<K,T,H,EQ>& m);

Constructs a map by copying all elements of m.

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

Copy constructor.

RWTValHashMultiMap<K,T,H,EQ>(const value_type* first, const value_type* last);

Constructs a map by copying elements from the array of association pairs pointed to by first, up to, but not including, the association pointed to by last.

Public Member Operators

RWTValHashMultiMap<K,T,H,EQ>& operator=(const RWTValHashMultiMap<K,T,H,EQ>& m); RWTValHashMultiMap<K,T,H,EQ>& operator=(const rw_hashmultimap<K,T,H,EQ>& m);

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

bool operator==(const RWTValHashMultiMap<K,T,H,EQ>& m) const; bool operator==(const rw_hashmultimap<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.

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. This function must have one of the prototypes:

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

void yourfun(const K&, 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. Each key and its associated item will have its destructor called.

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)(const_reference,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(const_reference 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().

bool find(const K& key, Key& r) const;

If there exists a key j in self that compares equal to key, assigns j to r and returns true. Otherwise, returns false and leaves the value of r unchanged.

bool find (bool (*fn)(const_reference,void*), void* d,pair<K,T>& r) const;

If there exists an association a in self such that the expression ((*fn)(a,d)) is true, assigns a to r 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 findValue(const K& key, T& r) const;

If there exists a key j in self that compares equal to key, assigns the item associated with j to r and returns true. Otherwise, returns false and leaves the value of r unchanged.

bool findKeyValue(const K& key, K& kr, T& tr) const;

If there exists a key j in self that compares equal to key, assigns j to kr, assigns the item associated with j to tr, and returns true. Otherwise, returns false and leaves the values of kr and tr unchanged.

bool insert(const K& key, const T& a);

Adds key with associated item a to the collection. Returns true.

bool insertKeyAndValue(const K& key, const 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 compares equal to key.

size_type occurrencesOf (bool (*fn)(const_reference,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(const_reference a, void* d);

Client data may be passed through parameter d.

bool remove(const K& key);

Removes the first association with key j in self such that j compares equal to key and returns true. Returns false if there is no such association.

bool remove(bool (*fn)(const_reference,void*), void* d);

Removes the first association a in self such that the expression ((*fn)(a,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 K& key);

Removes all associations with key j in self where j compares equal to key. Returns the number of items removed.

size_type removeAll(bool (*fn)(const_reference,void*), void* d);

Removes all associations a in self such that the expression ((*fn)(a,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 multimap 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.

rw_hashmultimap<K,T,H,EQ>& std(); const rw_hashmultimap<K,T,H,EQ>& std() const;

Returns a reference to the underlying C++-standard collection that serves as the implementation for self. This reference may be used freely, providing accessibility to the C++-standard interface and interoperability with other software components that make use of the C++-standard collections.

Related Global Operators

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

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

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

RWTValHashMultiMapIterator<K,T,H,EQ>

Synopsis

#include<rw/tvhmmap.h>
 
RWTValHashMultiMap<K,T,H,EQ> m;
RWTValHashMultiMapIterator<K,T,H,EQ> itr(m);

Standard C++ Library Dependent!


Note: RWTValHashMultiMapIterator requires the Standard C++ Library.


Description

RWTValHashMultiMapIterator is supplied with Tools.h++ 7 to provide an iterator interface to RWTValHashMultiMapIterator that is backward compatible with the container iterators provided in Tools.h++ 6.x.

Iteration over an RWTValHashMultiMap 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

Example

#include<rw/tvhmmap.h>
 
#include<rw/cstring.h>
#include<iostream.h>
 
struct silly_h{
 
   unsigned long operator()(const RWCString& x) const
   { return x.length() * (long)x(0); }
};
int main(){
 
   RWTValHashMultiMap
     <RWCString,int,silly_h,equal_to<RWCString> > age;
   RWTValHashMultiMapIterator
     <RWCString, int, silly_h, equal_to<RWCString > > itr(age);
 
   age.insert(RWCString("John"), 30);
 
   age.insert(RWCString("Steve"),17);
   age.insert(RWCString("Mark"),24);
   age.insert(RWCString("Steve"),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
Steve's age is 24
Steve's age is 17
Mark's age is 24

Public Constructors

RWTValHashMultiMapIterator<K,T,H,EQ>(RWTValHashMultiMap<K,T,H,EQ>&h);

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

Public Member Operators

RWBoolean operator()();

Advances self to the next element, dereferences the resulting iterator and returns false if the iterator has advanced past the last item in the container and true otherwise.

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 multimap, self will now reference an undefined value and false will be returned. Otherwise, true is returned. Note: no postincrement operator is provided.

Public Member Functions

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

void reset(); void reset(RWTValHashMultiMap<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 RWTValHashMultiMap with reset() will reset the iterator on that container.

T value();

Returns the value portion of the association referenced by self.

RWTValHashMultiSet<T,H,EQ>

Synopsis

#include <rw/tvhasht.h>
 
RWTValHashMultiSet<T,H,EQ>


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


Description

This class maintains a collection of values, which are stored according to a hash object of type H. H must offer a hash function for elements of type T via a 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.

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

Persistence

Isomorphic

Example

//
 
// tvhmsstr.cpp
//
#include <rw/tvhasht.h>
#include <rw/cstring.h>
#include <iostream.h>
 
struct silly_hash{
   unsigned long operator()(RWCString x) const
   { return x.length() * (long)x[0]; }
};
 
main(){
RWTValHashMultiSet<RWCString,silly_hash,equal_to<RWCString> > set1;
RWTValHashMultiSet<RWCString,silly_hash,equal_to<RWCString> > set2;
 
  set1.insert("one");
  set1.insert("two");
  set1.insert("three");
  set1.insert("one");     // OK: duplicates allowed
  set1.insert("one");
 
  cout << set1.entries() << endl;  // Prints "5"
 
  set2.insert("one");
  set2.insert("five");
  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 "2"
 
  return 0;
}

Related Classes

Class RWTValHashSet<T,H,EQ> offers the same interface to a collection that will not accept multiple items that compare equal to each other.

Class rw_hashmultiset<T,H,EQ> is the C++-standard compliant collection that serves as the underlying implementation for RWTValHashMultiSet<T,H,EQ>.

Public Typedefs

typedef rw_hashmultiset<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;

Public Constructors

RWTValHashMultiSet<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 as its hashing function and will use eq to determine equivalence between elements.

RWTValHashMultiSet<T,H,EQ>(const rw_hashmultiset<T,H,EQ>& s);

Constructs a set by copying all elements of s.

RWTValHashMultiSet<T,H,EQ>(const RWTValHashMultiSet<T,H,EQ>&);

Copy constructor.

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

Creates an empty hashed multi-set which uses the hash object h and has an initial hash table capacity of sz.

RWTValHashMultiSet<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 as its hashing function and will use eq to determine equivalence between elements.

Public Member Operators

RWTValHashMultiSet<T,H,EQ>& operator=(const RWTValHashMultiSet<T,H,EQ>& s); RWTValHashMultiSet<T,H,EQ>& operator=(const rw_hashmultiset<T,H,EQ>& s);

Destroys all elements of self and replaces them by copying all elements of s.

bool operator==(const RWTValHashMultiSet<T,H,EQ>& s) const; bool operator==(const rw_hashmultiset<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.

Public Member Functions

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_reference 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 RWTValHashMultiSet<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,T& k) const;

If there exists an element t in self such that the expression (t == a) is true, 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,T& 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. 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.

void intersection(const RWTValHashMultiSet<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 RWTValHashMultiSet<T,H,EQ>& s) const;

Returns true if there is set equivalence between self and s, and returns false otherwise.

bool isProperSubsetOf(const RWTValHashMultiSet<T,H,EQ>& s) const;

Returns true if self is a proper subset of s, and returns false otherwise.

bool isSubsetOf(const RWTValHashMultiSet<T,H,EQ>& s) const;

Returns true if self is a subset of s, and returns false otherwise.

size_type occurrencesOf(const_reference a) const;

Returns the number of elements t in self that compares 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 and returns true. 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 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_hashmultiset<T,H,EQ>& std(); const rw_hashmultiset<T,H,EQ>& std() const;

Returns a reference to the underlying C++-standard 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 RWTValHashMultiSet<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 informally defined as (A(B)-(A(B).

void Union(const RWTValHashMultiSet<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 RWTValHashMultiSet<T,H,EQ>& coll); RWFile& operator<<(RWFile& strm, const RWTValHashMultiSet<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, RWTValHashMultiSet<T,H,EQ>& coll); RWFile& operator>>(RWFile& strm, RWTValHashMultiSet<T,H,EQ>& coll);

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

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

RWTValHashMultiSetIterator<T,H,EQ>

Synopsis

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


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


Description

RWTValHashMultiSetIterator is supplied with Tools.h++ 7 to provide an iterator interface to RWTValHashMultiSetIterator that is backward compatible with the container iterators provided in Tools.h++ 6.x.

Iteration over an RWTValHashMultiSet 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

Example

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

Program Output (not necessarily in this order):

John
Steve
Mark
Steve

Public Constructors

RWTValHashMultiSetIterator<T,H,EQ> (RWTValHashMultiSet<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

RWBoolean operator()();

Advances self to the next element. Returns false if the iterator has advanced past the last item in the container and true otherwise.

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 value in the multi-set, self will now reference an undefined value and false will be returned. Otherwise, true is returned. Note: no postincrement operator is provided.

Public Member Functions

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

Returns a pointer to the collection being iterated over.

T key() const;

Returns the value currently referenced by self.

void reset(); void reset(RWTValHashMultiSet<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 RWTValHashMultiSet to reset() will reset the iterator on that container.

RWTValHashSet<T,H,EQ>

Synopsis

#include <rw/tvhset.h> 
RWTValHashSet<T,H,EQ> s;


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


Description

This class maintains a collection of values, which are stored according to a hash object of type H. H must offer a hash function for elements of type T via a 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.

RWTValHashSet<T,H,EQ> will not accept an item that compares equal to an item already in the collection. (RWTValHashMultiSet<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

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

Related Classes

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

Public Typedefs

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;

Public Constructors

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.

Public Member Operators

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.

Public Member Functions

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.

Related Global Operators

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.

RWTValHashSetIterator<T,H,EQ>

Synopsis

#include<rw/tvhset.h>
 
RWTValHashSet<T,H,EQ> m;
RWTValHashSetIterator<T,H,EQ> itr(m);


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


Description

RWTValHashSetIterator is supplied with Tools.h++ 7 to provide an iterator interface to RWTValHashSetIterator that is backward compatible with the container iterators provided in Tools.h++ 6.x.

Iteration over an RWTValHashSet 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 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

Example

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

Program Output (not necessarily in this order):

John
Steve
Mark

Public Constructors

RWTValHashSetIterator<T,H,EQ> (RWTValHashSet<T,H,EQ>&h);

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

Public Member Operators

RWBoolean operator()();

Advances self to the next element. Returns false if the iterator has advanced past the last item in the container and true otherwise.

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 value in the multi-set, self will now reference an undefined value and false will be returned. Otherwise, true is returned. Note: no postincrement operator is provided.

Public Member Functions

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

Returns a pointer to the collection being iterated over.

T key() const;

Returns the value currently pointed to by self.

void reset(); void reset(RWTValHashSet<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 RWTValHashSet to reset() will reset the iterator on that container.

RWTValHashTable

Synopsis

#define RWTValHashTable RWTValHashMultiSet


Note: If you have the Standard C++ Library, refer to the reference for this class under its new name: RWTValHashMultiSet. Although the old name (RWTValHashTable) 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 RWTValHashTable in Appendix A: Alternate Template Class Interfaces.