Chapter 11. Appendix A: Alternate Template Class Interfaces

If you do not have the Standard C++ Library, use the template class interfaces described in this Appendix. If you do have the Standard C++ Library use the interfaces described in the "Class Hierarchy" section of Class Reference.

RWTPtrDlist<T>

Synopsis

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

Please Note!

If you do not have the Standard C++ Library, use the interface described here. Otherwise, use the interface described in the "Class Hierarchy" section of Class Reference.

Description

This class maintains a collection of pointers to type T, implemented as a doubly linked list. This is a pointer based list: pointers to objects are copied in and out of the links that make up the list.

Parameter T represents the type of object to be inserted into the list, either a class or fundamental type. The class T must have:

  • well-defined equality semantics (T::operator==(const T&)).

Persistence

Isomorphic

Example

In this example, a doubly-linked list of pointers to the user type Dog is exercised. Contrast this approach with the example given under RWTValDlist<T>.

#include <rw/tpdlist.h>
#include <rw/rstream.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; }
 
  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

Public Constructors

RWTPtrDlist<T>();

Constructs an empty list.

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

Constructs a new doubly-linked list as a shallow copy of c. After construction, pointers will be shared between the two collections.

Public Operators

RWTPtrDlist& operator=(const RWTPtrDlist<T>& c);

Sets self to a shallow copy of c. Afterwards, pointers will be shared between the two collections.

T*& operator[](size_t i); T* const& operator[](size_t i) const;

Returns a pointer to the ith value in the list. The first variant can be used as an lvalue, the second cannot. The index i must be between zero and the number of items in the collection less one, or an exception of type RWBoundsError will be thrown.

Public Member Functions

void append(T* a);

Appends the item pointed to by a to the end of the list.

void apply(void (*applyFun)(T*, void*), void* d);

Applies the user-defined function pointed to by applyFun to every item in the list. This function must have the prototype:

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

This function will be called for each item in the list, with a pointer to the item as the first argument. Client data may be passed through as parameter d.

T*& at(size_t i); T* const& at(size_t i) const;

Returns a pointer to the ith value in the list. The first variant can be used as an lvalue, the second cannot. The index i must be between zero and the number of items in the collection less one, or an exception of type RWBoundsError will be thrown.

void clear();

Removes all items from the collection.

void clearAndDestroy();

Removes all items from the collection and deletes them.

RWBoolean contains(const T* a) const;

Returns TRUE if the list contains an object that is equal to the object pointed to by a, FALSE otherwise. Equality is measured by the class-defined equality operator for type T.

RWBoolean contains(RWBoolean (*testFun)(T*, void*),void* d) const;

Returns TRUE if the list contains an item for which the user-defined "tester" function pointed to by testFun returns TRUE . Returns FALSE otherwise. The tester function must have the prototype:

RWBoolean yourTester(T*, void* d);

This function will be called for each item in the list, with a pointer to the item as the first argument. Client data may be passed through as parameter d.

size_t entries() const;

Returns the number of items that are currently in the collection.

T* find(const T* target) const;

Returns a pointer to the first object encountered which is equal to the object pointed to by target, or nil if no such object can be found. Equality is measured by the class-defined equality operator for type T.

T* find(RWBoolean (*testFun)(T*, void*),void* d,) const;

Returns a pointer to the first object encountered for which the user-defined tester function pointed to by testFun returns TRUE, or nil if no such object can be found. The tester function must have the prototype:

RWBoolean yourTester(T*, void* d);

This function will be called for each item in the list, with a pointer to the item as the first argument. Client data may be passed through as parameter d.

T*& first(); T* const& first() const;

Returns a pointer to the first item in the list. The behavior is undefined if the list is empty.

T* get();

Returns a pointer to the first item in the list and removes the item. The behavior is undefined if the list is empty.

size_t index(const T* a);

Returns the index of the first object that is equal to the object pointed to by a, or RW_NPOS if there is no such object. Equality is measured by the class-defined equality operator for type T.

size_t index(RWBoolean (*testFun)(T*, void*),void* d) const;

Returns the index of the first object for which the user-defined tester function pointed to by testFun returns TRUE, or RW_NPOS if there is no such object. The tester function must have the prototype:

RWBoolean yourTester(T*, void* d);

This function will be called for each item in the list, with a pointer to the item as the first argument. Client data may be passed through as parameter d.

void insert(T* a);

Adds the object pointed to by a to the end of the list.

void insertAt(size_t i, T* a);

Adds the object pointed to by a at the index position i. This position must be between zero and the number of items in the list, or an exception of type RWBoundsError will be thrown.

RWBoolean isEmpty() const;

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

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

Returns a pointer to the last item in the list. The behavior is undefined if the list is empty.

size_t occurrencesOf(const T* a) const;

Returns the number of objects in the list that are equal to the object pointed to by a. Equality is measured by the class-defined equality operator for type T.

size_t occurrencesOf(RWBoolean (*testFun)(T*, void*),void* d)const;

Returns the number of objects in the list for which the user-defined "tester" function pointed to by testFun returns TRUE . The tester function must have the prototype:

RWBoolean yourTester(T*, void* d);

This function will be called for each item in the list, with a pointer to the item as the first argument. Client data may be passed through as parameter d.

void prepend(T* a);

Adds the item pointed to by a to the beginning of the list.

T* remove(const T* a);

Removes the first object which is equal to the object pointed to by a and returns a pointer to it, or nil if no such object could be found. Equality is measured by the class-defined equality operator for type T.

T* remove(RWBoolean (*testFun)(T*, void*),void* d);

Removes the first object for which the user-defined tester function pointed to by testFun returns TRUE and returns a pointer to it, or nil if there is no such object. The tester function must have the prototype:

RWBoolean yourTester(T*, void* d);

This function will be called for each item in the list, with a pointer to the item as the first argument. Client data may be passed through as parameter d.

size_t removeAll(const T* a);

Removes all objects which are equal to the object pointed to by a. Returns the number of objects removed. Equality is measured by the class-defined equality operator for type T.

size_t removeAll(RWBoolean (*testFun)(T*, void*),void* d);

Removes all objects for which the user-defined tester function pointed to by testFun returns TRUE. Returns the number of objects removed. The tester function must have the prototype:

RWBoolean yourTester(T*, void* d);

This function will be called for each item in the list, with a pointer to the item as the first argument. Client data may be passed through as parameter d.

T* removeAt(size_t i);

Removes the object at index i and returns a pointer to it. An exception of type RWBoundsError will be thrown if i is not a valid index. Valid indices are from zero to the number of items in the list less one.

T* removeFirst();

Removes the first item in the list and returns a pointer to it. The behavior is undefined if the list is empty.

T* removeLast();

Removes the last item in the list and returns a pointer to it. The behavior is undefined if the list is empty.

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> list;
RWTPtrDlistIterator<T> iterator(list);

Please Note!

If you do not have the Standard C++ Library, use the interface described here. Otherwise, use the interface described in the "Class Hierarchy" section of Class Reference.

Description

Iterator for class RWTPtrDlist<T>, allowing sequential access to all the elements of a doubly-linked parameterized list. Elements are accessed in order, in either direction.

Like all Rogue Wave iterators, the "current item" is undefined immediately after construction — you must define it by using operator() or some other (valid) operation.

Once the iterator has advanced beyond the end of the collection it is no longer valid — continuing to use it will bring undefined results.

Persistence

None

Public Constructor

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

Constructs an iterator to be used with the list c.

Public Member Operators

RWBoolean operator++();

Advances the iterator to the next item and returns TRUE. When the end of the collection is reached, returns FALSE and the position of the iterator will be undefined.

RWBoolean operator--();

Retreats the iterator to the previous item and returns TRUE. When the beginning of the collection is reached, returns FALSE and the position of the iterator will be undefined.

RWBoolean operator+=(size_t n);

Advances the iterator n positions and returns TRUE. When the end of the collection is reached, returns FALSE and the position of the iterator will be undefined.

RWBoolean operator-=(size_t n);

Retreats the iterator n positions and returns TRUE. When the beginning of the collection is reached, returns FALSE and the position of the iterator will be undefined.

T* operator()();

Advances the iterator to the next item and returns a pointer to it. When the end of the collection is reached, returns nil and the position of the iterator will be undefined.

Public Member Functions

RWTPtrDlist<T>* container() const;

Returns a pointer to the collection over which this iterator is iterating.

T* findNext(const T* a);

Advances the iterator to the first element that is equal to the object pointed to by a and returns a pointer to it. If no item is found, returns nil and the position of the iterator will be undefined. Equality is measured by the class-defined equality operator for type T.

T* findNext(RWBoolean (*testFun)(T*, void*), void*);

Advances the iterator to the first element for which the tester function pointed to by testFun returns TRUE and returns a pointer to it. If no item is found, returns nil and the position of the iterator will be undefined.

void insertAfterPoint(T* a);

Inserts the object pointed to by a into the iterator's associated collection in the position immediately after the iterator's current position which remains unchanged.

T* key() const;

Returns a pointer to the object at the iterator's current position. The results are undefined if the iterator is no longer valid.

T* remove();

Removes and returns the object at the iterator's current position from the iterator's associated collection. Afterwards, the iterator will be positioned at the element immediately before the removed element. Returns nil if unsuccessful in which case the position of the iterator is undefined. If the first element of the iterator's associated collection is removed, then the position of the iterator will be undefined.

T* removeNext(const T* a);

Advances the iterator to the first element that is equal to the object pointed to by a, then removes and returns it. Afterwards, the iterator will be positioned at the element immediately before the removed element. Returns nil if unsuccessful in which case the position of the iterator is undefined. Equality is measured by the class-defined equality operator for type T.

T* removeNext(RWBoolean (*testFun)(T*, void*), void*);

Advances the iterator to the first element for which the tester function pointed to by testFun returns TRUE, then removes and returns it. Afterwards, the iterator will be positioned at the element immediately before the removed element. Returns nil if unsuccessful in which case the position of the iterator is undefined.

void reset();

Resets the iterator to the state it had immediately after construction.

void reset(RWTPtrDlist<T>& c);

Resets the iterator to iterate over the collection c.

RWTPtrHashDictionary<K,V>

Synopsis

#include <rw/tphdict.h>
unsigned hashFun(const K&);
RWTPtrHashDictionary<K,V> dictionary(hashFun);

Please Note!

If you do not have the Standard C++ Library, use the interface described here. Otherwise, use the interface described in the "Class Hierarchy" section of Class Reference.

Description

RWTPtrHashDictionary<K,V> is a dictionary of keys of type K and values of type V, implemented using a hash table. While duplicates of values are allowed, duplicates of keys are not.

It is a pointer based collection: pointers to the keys and values are copied in and out of the hash buckets.

Parameters K and V represent the type of the key and the type of the value, respectively, to be inserted into the table. These can be either classes or fundamental types. Class K must have

  • well-defined equality semantics (K::operator==(const K&)).

Class V can be of any type.

A user-supplied hashing function for type K must be supplied to the constructor when creating a new table. If K is a Rogue Wave class, then this requirement is usually trivial because most Rogue Wave objects know how to return a hashing value. In fact, classes RWCString, RWDate, RWTime, and RWWString contain static member functions called hash that can be supplied to the constructor as is. The function must have prototype:

unsigned hFun(const K& a);

and should return a suitable hash value for the object a.

To find a value, the key is first hashed to determine in which bucket the key and value can be found. The bucket is then searched for an object that is equal (as determined by the equality operator) to the key.

The initial number of buckets in the table is set by the constructor. There is a default value. If the number of (key/value) pairs in the collection greatly exceeds the number of buckets then efficiency will sag because each bucket must be searched linearly. The number of buckets can be changed by calling member function resize(). This is relatively expensive because all of the keys must be rehashed.

If you wish for this to be done automatically, then you can subclass from this class and implement your own special insert() and remove() functions which perform a resize() as necessary.

Persistence

None

Example

#include <rw/tphdict.h>
#include <rw/cstring.h>
#include <rw/rwdate.h>
#include <rw/rstream.h>
 
main()  { 
  RWTPtrHashDictionary<RWCString, RWDate>
    birthdays(RWCString::hash);
  birthdays.insertKeyAndValue
    (new RWCString("John"),
     new RWDate(12, "April", 1975)
    );
  birthdays.insertKeyAndValue
    (new RWCString("Ivan"),
     new RWDate(2, "Nov", 1980)
    );
 
  // Alternative syntax:
  birthdays[new RWCString("Susan")] = 
    new RWDate(30, "June", 1955);
  birthdays[new RWCString("Gene")] =
    new RWDate(5, "Jan", 1981);
 
  // Print a birthday:
  RWCString key("John");
  cout << *birthdays[&key] << endl;
 
  birthdays.clearAndDestroy();
  return 0;
}

Program Output:

April 12, 1975

Public Constructors

RWTPtrHashDictionary<K,V>(unsigned (*hashKey)(const K&), size_t buckets = RWDEFAULT_CAPACITY);

Constructs an empty hash dictionary. The first argument is a pointer to a user-defined hashing function for items of type K (the key). The table will initally have buckets buckets although this can be changed with member function resize().

RWTPtrHashDictionary<K,V>(const RWTPtrHashDictionary<K,V>& c);

Constructs a new hash dictionary as a shallow copy of c. After construction, pointers will be shared between the two collections. The new object will use the same hashing function and have the same number of buckets as c. Hence, the keys will not be rehashed.

Public Operators

RWTPtrHashDictionary<K,V>& operator=(const RWTPtrHashDictionary<K,V>& c);

Sets self to a shallow copy of c. Afterwards, pointers will be shared between the two collections. Self will use the same hashing function and have the number of buckets as c. Hence, the keys will not be rehashed.

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

Look up the key key and return a reference to the pointer of its associated value. If the key is not in the dictionary, then it is added to the dictionary. In this case, the pointer to the value will be undefined. Because of this, if there is a possibility that a key will not be in the dictionary, then this operator can only be used as an lvalue.

Public Member Functions

void applyToKeyAndValue( void (*applyFun)(K*,V*&,void*),void* d);

Applies the user-defined function pointed to by applyFun to every key-value pair in the dictionary. This function must have prototype:

void yourFun(K* key, V*& value, void* d);

This function will be called for each key value pair in the dictionary, with a pointer to the key as the first argument and a reference to a pointer to the value as the second argument. The key should not be changed or touched. A new value can be substituted, or the old value can be changed. Client data may be passed through as parameter d.

void clear();

Removes all key value pairs from the collection.

void clearAndDestroy();

Removes all key value pairs from the collection and deletes both the keys and the values.

RWBoolean contains(const K* key) const;

Returns TRUE if the dictionary contains a key which is equal to the key pointed to by key. Returns FALSE otherwise. Equality is measured by the class-defined equality operator for type K.

size_t entries() const;

Returns the number of key-value pairs currently in the dictionary.

K* find(const K* key) const;

Returns a pointer to the key which is equal to the key pointed to by key, or nil if no such item could be found. Equality is measured by the class-defined equality operator for type K.

V* findValue(const K* key) const;

Returns a pointer to the value associated with the key pointed to by key, or nil if no such item could be found. Equality is measured by the class-defined equality operator for type K.

K* findKeyAndValue(const K* key, V*& retVal) const;

Returns a pointer to the key associated with the key pointed to by key, or nil if no such item could be found. If a key is found, the pointer to its associated value is put in retVal. Equality is measured by the class-defined equality operator for type K.

void insertKeyAndValue(K* key, V* value);

If the key pointed to by key is in the dictionary, then its associated value is changed to value. Otherwise, a new key value pair is inserted into the dictionary.

RWBoolean isEmpty() const;

Returns TRUE if the dictionary has no items in it, FALSE otherwise.

K* remove(const K* key);

Removes the key and value pair where the key is equal to the key pointed to by key. Returns the key or nil if no match was found. Equality is measured by the class-defined equality operator for type K.

void resize(size_t N);

Changes the number of buckets to N. This will result in all of the keys being rehashed.

RWTPtrHashDictionaryIterator<K,V>

Synopsis

#include <rw/tphdict.h>
 
unsigned hashFun(const K&);
RWTPtrHashDictionary<K,V> dictionary(hashFun);
RWTPtrHashDictionaryIterator<K,V> iterator(dictionary);

Please Note!

If you do not have the Standard C++ Library, use the interface described here. Otherwise, use the interface described in the "Class Hierarchy" section of Class Reference.

Description

Iterator for class RWTPtrHashDictionary<K,V>, allowing sequential access to all keys and values of a parameterized hash dictionary. Elements are not accessed in any particular order.

Like all Rogue Wave iterators, the "current item" is undefined immediately after construction — you must define it by using operator() or some other (valid) operation.

Once the iterator has advanced beyond the end of the collection it is no longer valid — continuing to use it will bring undefined results.

Persistence

None

Public Constructor

RWTPtrHashDictionaryIterator(RWTPtrHashDictionary& c);

Constructs an iterator to be used with the dictionary c.

Public Operators

RWBoolean operator++();

Advances the iterator to the next key-value pair and returns TRUE. When the end of the collection is reached, returns FALSE and the position of the iterator will be undefined.

K* operator()();

Advances the iterator to the next key-value pair and returns a pointer to the key. When the end of the collection is reached, returns nil and the position of the iterator will be undefined. Use member function value() to recover the dictionary value.

Public Member Functions

RWTPtrHashDictionary* container() const;

Returns a pointer to the collection over which this iterator is iterating.

K* key() const;

Returns a pointer to the key at the iterator's current position. The results are undefined if the iterator is no longer valid.

void reset();

Resets the iterator to the state it had immediately after construction.

void reset(RWTPtrHashDictionary& c);

Resets the iterator to iterate over the collection c.

V* value() const;

Returns a pointer to the value at the iterator's current position. The results are undefined if the iterator is no longer valid.

RWTPtrHashSet<T>

RWTPtrHashSet<T> ->- RWTPtrHashTable<T> 

Synopsis

#include <rw/tphset.h>
 
unsigned hashFun(const T&);
RWTPtrHashSet(hashFun) set;

Please Note!

If you do not have the Standard C++ Library, use the interface described here. Otherwise, use the interface described in the "Class Hierarchy" section of Class Reference.

Description

RWTPtrHashSet<T> is a derived class of RWTPtrHashTable<T> where the insert() function has been overridden to accept only one item of a given value. Hence, each item in the collection will have a unique value.

As with class RWTPtrHashTable<T>, you must supply a hashing function to the constructor.

The class T must have:

  • well-defined equality semantics (T::operator==(const T&)).

Persistence

None

Example

This examples exercises a set of RWCStrings.

#include <rw/tphset.h>
#include <rw/cstring.h>
#include <rw/rstream.h>
 
main()  { 
  RWTPtrHashSet<RWCString> set(RWCString::hash);
 
  set.insert(new RWCString("one"));
  set.insert(new RWCString("two"));
  set.insert(new RWCString("three"));
  set.insert(new RWCString("one"));
 
  cout << set.entries() << endl;  // Prints "3"
 
  set.clearAndDestroy();
  return 0;
}

Program Output:

3

Public Constructor

RWTPtrHashSet<T>(unsigned (*hashFun)(const T&), size_t buckets = RWDEFAULT_CAPACITY);

Constructs an empty hashing set. The first argument is a pointer to a user-defined hashing function for items of type T. The table will initally have buckets buckets although this can be changed with member function resize().

Public Member Functions

RWTPtrHashSet<T>& Union(const RWTPtrHashSet<T>& h);

Computes the union of self and h, modifying self and returning self.

RWTPtrHashSet<T>& difference(const RWTPtrHashSet<T>& h);

Computes the disjunction of (const RWTPtrHashSet<T>& h);self and h, modifying self and returning self.

RWTPtrHashSet<T>& intersection(const RWTPtrHashSet<T>& h);

Computes the intersection of self and h, modifying self and returning self.

RWTPtrHashSet<T>& symmetricDifference(const RWTPtrHashSet<T>& h);

Computes the symmetric difference between self and h, modifying self and returning self.

RWBoolean isSubsetOf(const RWTPtrHashSet<T>& h) const;

Returns TRUE if self is a subset of h.

RWBoolean isProperSubsetOf(const RWTPtrHashSet<T>& h) const;

Returns TRUE if self is a proper subset of h.

RWBoolean isEquivalent(const RWTPtrHashSet<T>& h) const;

Returns TRUE if self and h are identical.

RWBoolean operator!=(const RWTPtrHashSet<T>& h) const;

Returns FALSE if self and h are identical.

void apply(void (*applyFun)(T*, void*), void* d);

Inherited from class RWTPtrHashTable<T>.

void clear();

Inherited from class RWTPtrHashTable<T>.

void clearAndDestroy();

Inherited from class RWTPtrHashTable<T>.

RWBoolean contains(const T* a) const;

Inherited from class RWTPtrHashTable<T>.

size_t entries() const;

Inherited from class RWTPtrHashTable<T>.

T* find(const T* target) const;

Inherited from class RWTPtrHashTable<T>.

void insert(T* a);

Redefined from class RWTPtrHashTable<T> to allow an object of a given value to be inserted only once.

RWBoolean isEmpty() const;

Inherited from class RWTPtrHashTable<T>.

size_t occurrencesOf(const T* a) const;

Inherited from class RWTPtrHashTable<T>.

T* remove(const T* a);

Inherited from class RWTPtrHashTable<T>.

size_t removeAll(const T* a);

Inherited from class RWTPtrHashTable<T>.

void resize(size_t N);

Inherited from class RWTPtrHashTable<T>.

RWTPtrHashTable<T>

Synopsis

#include <rw/tphasht.h> unsigned hashFun(const T&);

RWTPtrHashTable<T> table(hashFun);

Please Note!

If you do not have the Standard C++ Library, use the interface described here. Otherwise, use the interface described in the "Class Hierarchy" section of Class Reference.

Description

This class implements a parameterized hash table of types T. It uses chaining to resolve hash collisions. Duplicates are allowed.

It is a pointer based collection: pointers to objects are copied in and out of the hash buckets.

Parameter T represents the type of object to be inserted into the table, either a class or fundamental type. The class T must have:

  • well-defined equality semantics (T::operator==(const T&)).

A user-supplied hashing function for type T must be supplied to the constructor when creating a new table. If T is a Rogue Wave class, then this requirement is usually trivial because most Rogue Wave objects know how to return a hashing value. In fact, classes RWCString, RWDate, RWTime, and RWWString contain static member functions called hash that can be supplied to the constructor as is. The function must have prototype:

unsigned hFun(const T& a);

and should return a suitable hash value for the object a.

To find an object, it is first hashed to determine in which bucket it occurs. The bucket is then searched for an object that is equal (as determined by the equality operator) to the candidate.

The initial number of buckets in the table is set by the constructor. There is a default value. If the number of items in the collection greatly exceeds the number of buckets then efficiency will sag because each bucket must be searched linearly. The number of buckets can be changed by calling member function resize(). This is relatively expensive because all of the keys must be rehashed.

If you wish for this to be done automatically, then you can subclass from this class and implement your own special insert() and remove() functions which perform a resize() as necessary.

Persistence

None

Example

#include <rw/tphasht.h>
#include <rw/cstring.h>
#include <rw/rstream.h>
 
main()  { 
  RWTPtrHashTable<RWCString> table(RWCString::hash);
  RWCString *states[4] = { new RWCString("Alabama"),
                           new RWCString("Pennsylvania"), 
                           new RWCString("Oregon"),
                           new RWCString("Montana") };
 
  table.insert(states[0]);
  table.insert(states[1]);
  table.insert(states[2]);
  table.insert(states[3]);
 
  RWCString key("Oregon");
  cout << "The table " <<
    (table.contains(&key) ? "does " : "does not ") <<
    "contain Oregon\n";
 
  table.removeAll(&key);
 
  cout << "Now the table " <<
    (table.contains(&key) ? "does " : "does not ") <<
    "contain Oregon";
 
  delete states[0];
  delete states[1];
  delete states[2];
  delete states[3];
  return 0;
}

Program Output:

The table does contain Oregon
Now the table does not contain Oregon

Public Constructors

RWTPtrHashTable<T>(unsigned (*hashFun)(const T&), size_t buckets = RWDEFAULT_CAPACITY);

Constructs an empty hash table. The first argument is a pointer to a user-defined hashing function for items of type T. The table will initally have buckets buckets although this can be changed with member function resize().

RWTPtrHashTable<T>(const RWTPtrHashTable<T>& c);

Constructs a new hash table as a shallow copy of c. After construction, pointers will be shared between the two collections. The new object will have the same number of buckets as c. Hence, the keys will not be rehashed.

Public Operators

RWTPtrHashTable& operator=(const RWTPtrHashTable<T>& c);

Sets self to a shallow copy of c. Afterwards, pointers will be shared between the two collections and self will have the same number of buckets as c. Hence, the keys will not be rehashed.

void

Public Member Functions

apply(void (*applyFun)(T*, void*), void* d);

Applies the user-defined function pointed to by applyFun to every item in the table. This function must have prototype:

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

Client data may be passed through as parameter d. The items should not be changed in any way that could change their hash value.

void clear();

Removes all items from the collection.

void clearAndDestroy();

Removes all items from the collection and deletes them.

RWBoolean contains(const T* p) const;

Returns TRUE if the collection contains an item which is equal to the item pointed to by p. Returns FALSE otherwise. Equality is measured by the class-defined equality operator for type T.

size_t entries() const;

Returns the number of items currently in the collection.

T* find(const T* target) const;

Returns a pointer to the object which is equal to the object pointed to by target, or nil if no such object can be found. Equality is measured by the class-defined equality operator for type T.

void insert(T* a);

Adds the object pointed to by a to the collection.

RWBoolean isEmpty() const;

Returns TRUE if the collection has no items in it, FALSE otherwise.

size_t occurrencesOf(const T* a) const;

Returns the number of objects in the collection which are equal to the object pointed to by a. Equality is measured by the class-defined equality operator for type T.

T* remove(const T* a);

Removes the object which is equal to the object pointed to by a and returns a pointer to it, or nil if no such object could be found. Equality is measured by the class-defined equality operator for type T.

size_t removeAll(const T* a);

Removes all objects which are equal to the object pointed to by a. Returns the number of objects removed. Equality is measured by the class-defined equality operator for type T.

void resize(size_t N);

Changes the number of buckets to N. This will result in all of the objects in the collection being rehashed.

RWTPtrHashTableIterator<T>

Synopsis

#include <rw/tphasht.h>
 
RWTPtrHashTable<T> table;
RWTPtrHashTableIterator<T> iterator(table);

Please Note!

If you do not have the Standard C++ Library, use the interface described here. Otherwise, use the interface described in the "Class Hierarchy" section of Class Reference.

Description

Iterator for class RWTPtrHashTable<T>, allowing sequential access to all the elements of a hash table. Elements are not accessed in any particular order.

Like all Rogue Wave iterators, the "current item" is undefined immediately after construction — you must define it by using operator() or some other (valid) operation.

Once the iterator has advanced beyond the end of the collection it is no longer valid — continuing to use it will bring undefined results.

Persistence

None

Public Constructor

RWTPtrHashTableIterator(RWTPtrHashTable<T>& c);

Constructs an iterator to be used with the table c.

Public Operators

RWBoolean operator++();

Advances the iterator to the next item and returns TRUE. When the end of the collection is reached, returns FALSE and the position of the iterator will be undefined.

T* operator()();

Advances the iterator to the next item and returns a pointer to it. When the end of the collection is reached, returns nil and the position of the iterator will be undefined.

Public Member Functions

RWTPtrHashTable<T>* container() const;

Returns a pointer to the collection over which this iterator is iterating.

T* key() const;

Returns a pointer to the item at the iterator's current position. The results are undefined if the iterator is no longer valid.

void reset();

Resets the iterator to the state it had immediately after construction.

void reset(RWTPtrHashTable<T>& c);

Resets the iterator to iterate over the collection c.

RWTPtrOrderedVector<T>

Synopsis

#include <rw/tpordvec.h>
 
RWTPtrOrderedVector<T> ordvec;

Please Note!

If you do not have the Standard C++ Library, use the interface described here. Otherwise, use the interface described in the "Class Hierarchy" section of Class Reference.

Description

RWTPtrOrderedVector<T> is a pointer-based ordered collection. That is, the items in the collection have a meaningful ordered relationship with respect to one another and can be accessed by an index number. The order is set by the order of insertion. Duplicates are allowed. The class is implemented as a vector, allowing efficient insertion and retrieval from the end of the collection, but somewhat slower from the beginning of the collection.

The class T must have:

  • well-defined equality semantics (T::operator==(const T&)).

Persistence

Isomorphic

Example

#include <rw/tpordvec.h>
#include <rw/rstream.h>
 
main() {
  RWTPtrOrderedVector<double> vec;
 
  vec.insert(new double(22.0));
  vec.insert(new double(5.3));
  vec.insert(new double(-102.5));
  vec.insert(new double(15.0));
  vec.insert(new double(5.3));
 
  cout << vec.entries() << " entries\n" << endl;  // Prints "5"
  for (int i=0; i<vec.length(); i++)
    cout << *vec[i] << endl;
 
  vec.clearAndDestroy();
  return 0;
}

Program Output:

5 entries
22
5.3
-102.5
15
5.3

Public Constructors

RWTPtrOrderedVector<T>(size_t capac=RWDEFAULT_CAPACITY);

Creates an empty ordered vector with capacity capac. Should the number of items exceed this value, the vector will be resized automatically.

RWTPtrOrderedVector<T>(const RWTPtrOrderedVector<T>& c);

Constructs a new ordered vector as a shallow copy of c. After construction, pointers will be shared between the two collections.

Public Operators

RWTPtrOrderedVector<T>& operator=(const RWTPtrOrderedVector& c);

Sets self to a shallow copy of c. Afterwards, pointers will be shared between the two collections.

T*& operator()(size_t i); T* const& operator()(size_t i) const;

Returns a pointer to the ith value in the vector. The first variant can be used as an lvalue, the second cannot. The index i must be between zero and the number of items in the collection less one. No bounds checking is performed.

T*& operator[](size_t i); T* const& operator[](size_t i) const;

Returns a pointer to the ith value in the vector. The first variant can be used as an lvalue, the second cannot. The index i must be between zero and the number of items in the collection less one, or an exception of type RWBoundsError will be thrown.

Public Member Functions

void append(T* a);

Appends the item pointed to by a to the end of the vector. The collection will automatically be resized if this causes the number of items in the collection to exceed the capacity.

T*& at(size_t i); T* const& at(size_t i) const;

Returns a pointer to the ith value in the vector. The first variant can be used as an lvalue, the second cannot. The index i must be between zero and the number of items in the collection less one, or an exception of type RWBoundsError will be thrown.

void clear();

Removes all items from the collection.

void clearAndDestroy();

Removes all items from the collection and deletes them.

RWBoolean contains(const T* a) const;

Returns TRUE if the collection contains an item that is equal to the object pointed to by a, FALSE otherwise. A linear search is done. Equality is measured by the class-defined equality operator for type T.

T* const * data() const;

Returns a pointer to the raw data of the vector. The contents should not be changed. Should be used with care.

size_t entries() const;

Returns the number of items currently in the collection.

T* find(const T* target) const;

Returns a pointer to the first object encountered which is equal to the object pointed to by target, or nil if no such object can be found. Equality is measured by the class-defined equality operator for type T.

T*& first(); T* const& first() const;

Returns a pointer to the first item in the vector. An exception of type RWBoundsError will occur if the vector is empty.

size_t index(const T* a) const;

Performs a linear search, returning the index of the first object that is equal to the object pointed to by a, or RW_NPOS if there is no such object. Equality is measured by the class-defined equality operator for type T.

void insert(T* a);

Adds the object pointed to by a to the end of the vector. The collection will be resized automatically if this causes the number of items to exceed the capacity.

void insertAt(size_t i, T* a);

Adds the object pointed to by a at the index position i. The item previously at position i is moved to i+1, etc. The collection will be resized automatically if this causes the number of items to exceed the capacity. The index i must be between 0 and the number of items in the vector or an exception of type RWBoundsError will occur.

RWBoolean isEmpty() const;

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

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

Returns a pointer to the last item in the collection. If there are no items in the collection then an exception of type RWBoundsError will occur.

size_t length() const;

Returns the number of items currently in the collection.

size_t occurrencesOf(const T* a) const;

Performs a linear search, returning the number of objects in the collection that are equal to the object pointed to by a. Equality is measured by the class-defined equality operator for type T.

void prepend(T* a);

Adds the item pointed to by a to the beginning of the collection. The collection will be resized automatically if this causes the number of items to exceed the capacity.

T* remove(const T* a);

Performs a linear search, removing the first object which is equal to the object pointed to by a and returns a pointer to it, or nil if no such object could be found. Equality is measured by the class-defined equality operator for type T.

size_t removeAll(const T* a);

Performs a linear search, removing all objects which are equal to the object pointed to by a. Returns the number of objects removed. Equality is measured by the class-defined equality operator for type T.

T* removeAt(size_t i);

Removes the object at index i and returns a pointer to it. An exception of type RWBoundsError will be thrown if i is not a valid index. Valid indices are from zero to the number of items in the list less one.

T* removeFirst();

Removes the first item in the collection and returns a pointer to it. An exception of type RWBoundsError will be thrown if the list is empty.

T* removeLast();

Removes the last item in the collection and returns a pointer to it. An exception of type RWBoundsError will be thrown if the list is empty.

void resize(size_t N);

Changes the capacity of the collection to N. Note that the number of objects in the collection does not change, just the capacity.

Related Global Operators

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

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

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

RWTPtrSlist<T>

Synopsis

#include <rw/tpslist.h>
 
RWTPtrSlist<T> list;

Please Note!

If you do not have the Standard C++ Library, use the interface described here. Otherwise, use the interface described in the "Class Hierarchy" section of Class Reference.

Description

This class maintains a collection of pointers to type T, implemented as a singly-linked list. This is a pointer based list: pointers to objects are copied in and out of the links that make up the list.

Parameter T represents the type of object to be inserted into the list, either a class or fundamental type. The class T must have:

  • well-defined equality semantics (T::operator==(const T&)).

Persistence

Isomorphic

Example

In this example, a singly-linked list of RWDates is exercised.

#include <rw/tpslist.h>
#include <rw/rwdate.h>
#include <rw/rstream.h>
 
main()  {
  RWTPtrSlist<RWDate> dates;
  dates.insert(new RWDate(2, "June", 52));        // 6/2/52
  dates.insert(new RWDate(30, "March", 46));      // 3/30/46
  dates.insert(new RWDate(1, "April", 90));       // 4/1/90
 
  // Now look for one of the dates:
  RWDate key(2, "June", 52);
  RWDate* d = dates.find(&key);
  if (d){
    cout << "Found date " << *d << endl;
  }
 
  // Remove in reverse order:
  while (!dates.isEmpty()){
    d = dates.removeLast();
    cout << *d << endl;
    delete d;
  }
 
  return 0;
}

Program Output:

Found date June 2, 1952
April 1, 1990
March 30, 1946
June 2, 1952

Public Constructors

RWTPtrSlist<T>();

Construct an empty list.

RWTPtrSlist<T>(const RWTPtrSlist<T>& c);

Constructs a new singly-linked list as a shallow copy of c. After construction, pointers will be shared between the two collections.

Public Operators

RWTPtrSlist& operator=(const RWTPtrSlist<T>& c);

Sets self to a shallow copy of c. Afterwards, pointers will be shared between the two collections.

T*& operator[](size_t i); T* const& operator[](size_t i) const;

Returns a pointer to the ith value in the list. The first variant can be used as an lvalue, the second cannot. The index i must be between zero and the number of items in the collection less one, or an exception of type RWBoundsError will be thrown.

Public Member Functions

void append(T* a);

Appends the item pointed to by a to the end of the list.

void apply(void (*applyFun)(T*, void*), void* d);

Applies the user-defined function pointed to by applyFun to every item in the list. This function must have the prototype:

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

This function will be called for each item in the list, with a pointer to the item as the first argument. Client data may be passed through as parameter d.

T*& at(size_t i); T* const; at(size_t i) const;

Returns a pointer to the ith value in the list. The first variant can be used as an lvalue, the second cannot. The index i must be between zero and the number of items in the collection less one, or an exception of type RWBoundsError will be thrown.

void clear();

Removes all items from the collection.

void clearAndDestroy();

Removes all items from the collection and deletes them.

RWBoolean contains(const T* a) const;

Returns TRUE if the list contains an object that is equal to the object pointed to by a, FALSE otherwise. Equality is measured by the class-defined equality operator for type T.

RWBoolean contains(RWBoolean (*testFun)(T*, void*),void* d) const;

Returns TRUE if the list contains an item for which the user-defined "tester" function pointed to by testFun returns TRUE . Returns FALSE otherwise. The tester function must have the prototype:

RWBoolean yourTester(T*, void* d);

This function will be called for each item in the list, with a pointer to the item as the first argument. Client data may be passed through as parameter d.

size_t entries() const;

Returns the number of items that are currently in the collection.

T* find(const T* target) const;

Returns a pointer to the first object encountered which is equal to the object pointed to by target, or nil if no such object can be found. Equality is measured by the class-defined equality operator for type T.

T* find(RWBoolean (*testFun)(T*, void*),void* d,) const;

Returns a pointer to the first object encountered for which the user-defined tester function pointed to by testFun returns TRUE, or nil if no such object can be found. The tester function must have the prototype:

RWBoolean yourTester(T*, void* d);

This function will be called for each item in the list, with a pointer to the item as the first argument. Client data may be passed through as parameter d.

T*& first(); T* const& first() const;

Returns a pointer to the first item in the list. The behavior is undefined if the list is empty.

T* get();

Returns a pointer to the first item in the list and removes the item. The behavior is undefined if the list is empty.

size_t index(const T* a);

Returns the index of the first object that is equal to the object pointed to by a, or RW_NPOS if there is no such object. Equality is measured by the class-defined equality operator for type T.

size_t index(RWBoolean (*testFun)(T*, void*),void* d) const;

Returns the index of the first object for which the user-defined tester function pointed to by testFun returns TRUE, or RW_NPOS if there is no such object. The tester function must have the prototype:

RWBoolean yourTester(T*, void* d);

This function will be called for each item in the list, with a pointer to the item as the first argument. Client data may be passed through as parameter d.

void insert(T* a);

Adds the object pointed to by a to the end of the list.

void insertAt(size_t i, T* a);

Adds the object pointed to by a at the index position i. This position must be between zero and the number of items in the list, or an exception of type RWBoundsError will be thrown.

RWBoolean isEmpty() const;

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

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

Returns a pointer to the last item in the list. The behavior is undefined if the list is empty.

size_t occurrencesOf(const T* a) const;

Returns the number of objects in the list that are equal to the object pointed to by a. Equality is measured by the class-defined equality operator for type T.

size_t occurrencesOf(RWBoolean (*testFun)(T*, void*),void* d) const;

Returns the number of objects in the list for which the user-defined "tester" function pointed to by testFun returns TRUE . The tester function must have the prototype:

RWBoolean yourTester(T*, void* d);

This function will be called for each item in the list, with a pointer to the item as the first argument. Client data may be passed through as parameter d.

void prepend(T* a);

Adds the item pointed to by a to the beginning of the list.

T* remove(const T* a);

Removes the first object which is equal to the object pointed to by a and returns a pointer to it, or nil if no such object could be found. Equality is measured by the class-defined equality operator for type T.

T* remove(RWBoolean (*testFun)(T*, void*),void* d);

Removes the first object for which the user-defined tester function pointed to by testFun returns TRUE and returns a pointer to it, or nil if there is no such object. The tester function must have the prototype:

RWBoolean yourTester(T*, void* d);

This function will be called for each item in the list, with a pointer to the item as the first argument. Client data may be passed through as parameter d.

size_t removeAll(const T* a);

Removes all objects which are equal to the object pointed to by a. Returns the number of objects removed. Equality is measured by the class-defined equality operator for type T.

size_t removeAll(RWBoolean (*testFun)(T*, void*),void* d);

Removes all objects for which the user-defined tester function pointed to by testFun returns TRUE. Returns the number of objects removed. The tester function must have the prototype:

RWBoolean yourTester(T*, void* d);

This function will be called for each item in the list, with a pointer to the item as the first argument. Client data may be passed through as parameter d.

T* removeAt(size_t i);

Removes the object at index i and returns a pointer to it. An exception of type RWBoundsError will be thrown if i is not a valid index. Valid indices are from zero to the number of items in the list less one.

T* removeFirst();

Removes the first item in the list and returns a pointer to it. The behavior is undefined if the list is empty.

T* removeLast();

Removes the last item in the list and returns a pointer to it. The behavior is undefined if the list is empty. This function is relatively slow because removing the last link in a singly-linked list necessitates access to the next-to-the-last link, requiring that the whole list be searched.

Related Global Operators

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

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

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

RWTPtrSlistIterator<T>

Synopsis

#include <rw/tpslist.h>
RWTPtrSlist<T> list;
RWTPtrSlistIterator<T> iterator(list);

Please Note!

If you do not have the Standard C++ Library, use the interface described here. Otherwise, use the interface described in the "Class Hierarchy" section of Class Reference.

Description

Iterator for class RWTPtrSlist<T>, allowing sequential access to all the elements of a singly-linked parameterized list. Elements are accessed in order, from first to last.

Like all Rogue Wave iterators, the "current item" is undefined immediately after construction — you must define it by using operator() or some other (valid) operation.

Once the iterator has advanced beyond the end of the collection it is no longer valid — continuing to use it will bring undefined results.

Persistence

None

Public Constructor

RWTPtrSlistIterator<T>(RWTPtrSlist<T>& c);

Constructs an iterator to be used with the list c.

Public Member Operators

RWBoolean operator++();

Advances the iterator to the next item and returns TRUE. When the end of the collection is reached, returns FALSE and the position of the iterator will be undefined.

RWBoolean operator+=(size_t n);

Advances the iterator n positions and returns TRUE. When the end of the collection is reached, returns FALSE and the position of the iterator will be undefined.

T* operator()();

Advances the iterator to the next item and returns a pointer to it. When the end of the collection is reached, returns nil and the position of the iterator will be undefined.

Public Member Functions

RWTPtrSlist<T>* container() const;

Returns a pointer to the collection over which this iterator is iterating.

T* findNext(const T* a);

Advances the iterator to the first element that is equal to the object pointed to by a and returns a pointer to it. If no item is found, returns nil and the position of the iterator will be undefined. Equality is measured by the class-defined equality operator for type T.

T* findNext(RWBoolean (*testFun)(T*, void*), void*);

Advances the iterator to the first element for which the tester function pointed to by testFun returns TRUE and returns a pointer to it. If no item is found, returns nil and the position of the iterator will be undefined.

void insertAfterPoint(T* a);

Inserts the object pointed to by a into the iterator's associated collection in the position immediately after the iterator's current position which remains unchanged.

T* key() const;

Returns a pointer to the object at the iterator's current position. The results are undefined if the iterator is no longer valid.

T* remove();

Removes and returns the object at the iterator's current position from the iterator's associated collection. Afterwards, the iterator will be positioned at the element immediately before the removed element. Returns nil if unsuccessful in which case the position of the iterator is undefined. This function is relatively inefficient for a singly-linked list.

T* removeNext(const T* a);

Advances the iterator to the first element that is equal to the object pointed to by a, then removes and returns it. Afterwards, the iterator will be positioned at the element immediately before the removed element. Returns nil if unsuccessful in which case the position of the iterator is undefined. Equality is measured by the class-defined equality operator for type T.

T* removeNext(RWBoolean (*testFun)(T*, void*), void*);

Advances the iterator to the first element for which the tester function pointed to by testFun returns TRUE, then removes and returns it. Afterwards, the iterator will be positioned at the element immediately before the removed element. Returns nil if unsuccessful in which case the position of the iterator is undefined.

void reset();

Resets the iterator to the state it had immediately after construction.

void reset(RWTPtrSlist<T>& c);

Resets the iterator to iterate over the collection c.

RWTPtrSortedVector<T>

Synopsis

#include <rw/tpsrtvec.h>
 
RWTPtrSortedVector<T> sortvec;

Please Note!

If you do not have the Standard C++ Library, use the interface described here. Otherwise, use the interface described in the "Class Hierarchy" section of Class Reference.

Description

RWTPtrSortedVector<T> is a pointer-based sorted collection. That is, the items in the collection have a meaningful ordered relationship with respect to each other and can be accessed by an index number. In the case of RWTPtrSortedVector<T>, objects are inserted such that objects "less than" themselves are before the object, objects "greater than" themselves after the object. An insertion sort is used. Duplicates are allowed.

Stores a pointer to the inserted item into the collection according to an ordering determined by the less-than (<) operator.

The class T must have:

  • well-defined equality semantics (T::operator==(const T&));

  • well-defined less-than semantics (T::operator<(const T&));

Although it is possible to alter objects that are referenced by pointers within a RWTPtrSortedVector<T>, it is dangerous since the changes may affect the way that operator<() and operator==() behave, causing the RWTPtrSortedVector<T> to become unsorted.

Persistence

Isomorphic

Example

This example inserts a set of dates into a sorted vector in no particular order, then prints them out in order.

#include <rw/tpsrtvec.h>
#include <rw/rwdate.h>
#include <rw/rstream.h>
 
main()  {
  RWTPtrSortedVector<RWDate> vec;
 
  vec.insert(new RWDate(10, "Aug", 1991));
  vec.insert(new RWDate(9, "Aug", 1991));
  vec.insert(new RWDate(1, "Sep", 1991));
  vec.insert(new RWDate(14, "May", 1990));
  vec.insert(new RWDate(1, "Sep", 1991));  // Add a duplicate
  vec.insert(new RWDate(2, "June", 1991));
 
  for (int i=0; i<vec.length(); i++)
    cout << *vec[i] << endl;
 
  vec.clearAndDestroy();
 
  return 0;
}

Program Output:

May 14, 1990
June 2, 1991
August 9, 1991
August 10, 1991
September 1, 1991
September 1, 1991

Public Constructor

RWTPtrSortedVector(size_t capac = RWDEFAULT_CAPACITY);

Create an empty sorted vector with an initial capacity equal to capac. The vector will be automatically resized should the number of items exceed this amount.

RWTPtrSortedVector<T>(const RWTPtrSortedVector<T>& c);

Constructs a new ordered vector as a shallow copy of c. After construction, pointers will be shared between the two collections.

Public Operators

RWTPtrSortedVector<T>& operator=(const RWTPtrSortedVector& c);

Sets self to a shallow copy of c. Afterwards, pointers will be shared between the two collections.

T*& operator()(size_t i); T* const& operator()(size_t i) const;

Returns a pointer to the ith value in the vector. The first variant can be used as an lvalue, the second cannot. The index i must be between zero and the number of items in the collection less one. No bounds checking is performed. When used as an lvalue, care must be taken so as not to disturb the sortedness of the collection.

T*& operator[](size_t i); T* const& operator[](size_t i) const;

Returns a pointer to the ith value in the vector. The first variant can be used as an lvalue, the second cannot. The index i must be between zero and the number of items in the collection less one, or an exception of type RWBoundsError will be thrown. When used as an lvalue, care must be taken so as not to disturb the sortedness of the collection.

Public Member Functions

T*& at(size_t i); T* const& at(size_t i) const;

Returns a pointer to the ith value in the vector. The first variant can be used as an lvalue, the second cannot. The index i must be between zero and the number of items in the collection less one, or an exception of type RWBoundsError will be thrown. When used as an lvalue, care must be taken so as not to disturb the sortedness of the collection.

void clear();

Removes all items from the collection.

void clearAndDestroy();

Removes all items from the collection and deletes them.

RWBoolean contains(const T* a) const;

Returns TRUE if the collection contains an item that is equal to the object pointed to by a, FALSE otherwise. A binary search is done. Equality is measured by the class-defined equality operator for type T.

T* const * data() const;

Returns a pointer to the raw data of the vector. The contents should not be changed. Should be used with care.

size_t entries() const;

Returns the number of items currently in the collection.

T* find(const T* target) const;

Returns a pointer to the first object encountered which is equal to the object pointed to by target, or nil if no such object can be found. A binary search is used. Equality is measured by the class-defined equality operator for type T.

T* const& first() const;

Returns a pointer to the first item in the vector. An exception of type RWBoundsError will occur if the vector is empty.

size_t index(const T* a) const;

Performs a binary search, returning the index of the first object that is equal to the object pointed to by a, or RW_NPOS if there is no such object. Equality is measured by the class-defined equality operator for type T.

void insert(T* a);

Performs a binary search, inserting the object pointed to by a after all items that compare less than or equal to it, but before all items that do not. "Less than" is measured by the class-defined '<' operator for type T. The collection will be resized automatically if this causes the number of items to exceed the capacity.

RWBoolean isEmpty() const;

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

T* const& last() const;

Returns a pointer to the last item in the collection. If there are no items in the collection then an exception of type RWBoundsError will occur.

size_t length() const;

Returns the number of items currently in the collection.

size_t occurrencesOf(const T* a) const;

Performs a binary search, returning the number of items that are equal to the object pointed to by a. Equality is measured by the class-defined equality operator for type T.

T* remove(const T* a);

Performs a binary search, removing the first object which is equal to the object pointed to by a and returns a pointer to it, or nil if no such object could be found. Equality is measured by the class-defined equality operator for type T.

size_t removeAll(const T* a);

Performs a binary search, removing all objects which are equal to the object pointed to by a. Returns the number of objects removed. Equality is measured by the class-defined equality operator for type T.

T* removeAt(size_t i);

Removes the object at index i and returns a pointer to it. An exception of type RWBoundsError will be thrown if i is not a valid index. Valid indices are from zero to the number of items in the list less one.

T* removeFirst();

Removes the first item in the collection and returns a pointer to it. An exception of type RWBoundsError will be thrown if the list is empty.

T* removeLast();

Removes the last item in the collection and returns a pointer to it. An exception of type RWBoundsError will be thrown if the list is empty.

void resize(size_t N);

Changes the capacity of the collection to N. Note that the number of objects in the collection does not change, just the capacity.

Related Global Operators

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

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

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

Please Note!

If you do not have the Standard C++ Library, use the interface described here. Otherwise, use the interface described in the "Class Hierarchy" section of Class Reference.

Description

This class maintains a collection of values, implemented as a doubly linked list. This is a value based list: objects are copied in and out of the links that make up the list. Unlike intrusive lists (see class RWTIsvDlist<T>), the objects need not inherit from a link class. However, this makes the class slightly less efficient than the intrusive lists because of the need to allocate a new link off the heap with every insertion and to make a copy of the object in the newly allocated link.

Parameter T represents the type of object to be inserted into the list, either a class or fundamental type. The class T must have:

  • A default constructor;

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

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

  • well-defined equality semantics (T::operator==(const T&)).

Persistence

Isomorphic

Example

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

#include <rw/tvdlist.h>
#include <rw/rstream.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; 
  }
 
 
  friend ostream& operator<<(ostream& str, const Dog& dog){
    str << dog.name;
    return str;}
};
 
main()  { 
  RWTValDlist<Dog> terriers;
  terriers.insert("Cairn Terrier");   // automatic type conversion 
  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

Public Constructors

RWTValDlist<T>();

Construct an empty list.

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

Construct a copy of the list list. Depending on the nature of the copy constructor of T, this could be relatively expensive because every item in the list must be copied.

Public Operators

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

Sets self to a copy of the list list. Depending on the nature of the copy constructor of T, this could be relatively expensive because every item in the list must be copied.

T& operator[](size_t i);

Returns a reference to the item at index i. The results can be used as an lvalue. An exception of type RWBoundsError will be thrown if i is not a valid index. Valid indices are from zero to the number of items in the list less one.

const T& operator[](size_t i) const;

Returns a copy of the item at index i. The results cannot be used as an lvalue. An exception of type RWBoundsError will be thrown if i is not a valid index. Valid indices are from zero to the number of items in the list less one.

Public Member Functions

void append(const T& a);

Adds the item a to the end of the list.

void apply(void (*applyFun)(T&, void*), void* d);

Applies the user-defined function pointed to by applyFun to every item in the list. This function must have prototype:

void yourFun(T& a, void* d);

Client data may be passed through as parameter d.

T& at(size_t i);

Returns a reference to the item at index i. The results can be used as an lvalue. An exception of type RWBoundsError will be thrown if i is not a valid index. Valid indices are from zero to the number of items in the list less one.

const T& at(size_t i) const;

Returns a copy of the item at index i. The results cannot be used as an lvalue. An exception of type RWBoundsError will be thrown if i is not a valid index. Valid indices are from zero to the number of items in the list less one.

void clear();

Removes all items from the list. Their destructors (if any) will be called.

RWBoolean contains(const T& a) const;

Returns TRUE if the list contains an object that is equal to the object a. Returns FALSE otherwise. Equality is measured by the class-defined equality operator.

RWBoolean contains(RWBoolean (*testFun)(const T&, void*),void* d) const;

Returns TRUE if the list contains an item for which the user-defined "tester" function pointed to by testFun returns TRUE . Returns FALSE otherwise. The tester function must have the prototype:

RWBoolean yourTester(const T&, void* d);

For each item in the list this function will be called with the item as the first argument. Client data may be passed through as parameter d.

size_t entries() const;

Returns the number of items that are currently in the collection.

RWBoolean find(const T& target, T& k) const;

Returns TRUE if the list contains an object that is equal to the object target and puts a copy of the matching object into k. Returns FALSE otherwise and does not touch k. Equality is measured by the class-defined equality operator. If you do not need a copy of the found object, use contains() instead.

RWBoolean find(RWBoolean (*testFun)(const T&, void*), void* d,T& k) const;

Returns TRUE if the list contains an object for which the user-defined tester function pointed to by testFun returns TRUE and puts a copy of the matching object into k. Returns FALSE otherwise and does not touch k. The tester function must have the prototype:

RWBoolean yourTester(const T&, void* d);

For each item in the list this function will be called with the item as the first argument. Client data may be passed through as parameter d. If you do not need a copy of the found object, use contains() instead.

T& first(); const T& first() const;

Returns (but does not remove) the first item in the list. The behavior is undefined if the list is empty.

T get();

Returns and removes the first item in the list. The behavior is undefined if the list is empty.

size_t index(const T& a);

Returns the index of the first object that is equal to the object a, or RW_NPOS if there is no such object. Equality is measured by the class-defined equality operator.

size_t index(RWBoolean (*testFun)(const T&, void*), void* d) const;

Returns the index of the first object for which the user-defined tester function pointed to by testFun returns TRUE, or RW_NPOS if there is no such object. The tester function must have the prototype:

RWBoolean yourTester(const T&, void* d);

For each item in the list this function will be called with the item as the first argument. Client data may be passed through as parameter d.

void insert(const T& a);

Adds the item a to the end of the list.

void insertAt(size_t i, const T& a);

Insert the item a at the index position i. This position must be between zero and the number of items in the list, or an exception of type RWBoundsError will be thrown.

RWBoolean isEmpty() const;

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

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

Returns (but does not remove) the last item in the list. The behavior is undefined if the list is empty.

size_t occurrencesOf(const T& a) const;

Returns the number of objects in the list that are equal to the object a. Equality is measured by the class-defined equality operator.

size_t occurrencesOf(RWBoolean (*testFun)(const T&, void*), void* d) const;

Returns the number of objects in the list for which the user-defined "tester" function pointed to by testFun returns TRUE . The tester function must have the prototype:

RWBoolean yourTester(const T&, void* d);

For each item in the list this function will be called with the item as the first argument. Client data may be passed through as parameter d.

void prepend(const T& a);

Adds the item a to the beginning of the list.

RWBoolean remove(const T& a);

Removes the first object which is equal to the object a and returns TRUE. Returns FALSE if there is no such object. Equality is measured by the class-defined equality operator.

RWBoolean remove(RWBoolean (*testFun)(const T&, void*),void* d);

Removes the first object for which the user-defined tester function pointed to by testFun returns TRUE, and returns TRUE. Returns FALSE if there is no such object. The tester function must have the prototype:

RWBoolean yourTester(const T&, void* d);

For each item in the list this function will be called with the item as the first argument. Client data may be passed through as parameter d.

size_t removeAll(const T& a);

Removes all objects which are equal to the object a. Returns the number of objects removed. Equality is measured by the class-defined equality operator.

size_t removeAll(RWBoolean (*testFun)(const T&, void*),void* d);

Removes all objects for which the user-defined tester function pointed to by testFun returns TRUE. Returns the number of objects removed. The tester function must have the prototype:

RWBoolean yourTester(const T&, void* d);

For each item in the list this function will be called with the item as the first argument. Client data may be passed through as parameter d.

T removeAt(size_t i);

Removes and returns the object at index i. An exception of type RWBoundsError will be thrown if i is not a valid index. Valid indices are from zero to the number of items in the list less one.

T removeFirst();

Removes and returns the first item in the list. The behavior is undefined if the list is empty.

T removeLast();

Removes and returns the last item in the list. The behavior is undefined if the list is empty.

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> list;
RWTValDlistIterator<T> iterator(list);

Please Note!

If you do not have the Standard C++ Library, use the interface described here. Otherwise, use the interface described in the "Class Hierarchy" section of Class Reference.

Description

Iterator for class RWTValDlist<T>, allowing sequential access to all the elements of a doubly-linked parameterized list. Elements are accessed in order, in either direction.

Like all Rogue Wave iterators, the "current item" is undefined immediately after construction — you must define it by using operator() or some other (valid) operation.

Once the iterator has advanced beyond the end of the collection it is no longer valid — continuing to use it will bring undefined results.

Persistence

Isomorphic

Public Constructor

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

Constructs an iterator to be used with the list c.

Public Member Operators

RWBoolean operator++();

Advances the iterator to the next item and returns TRUE. When the end of the collection is reached, returns FALSE and the position of the iterator will be undefined.

RWBoolean operator--();

Retreats the iterator to the previous item and returns TRUE. When the beginning of the collection is reached, returns FALSE and the position of the iterator will be undefined.

RWBoolean operator+=(size_t n);

Advances the iterator n positions and returns TRUE. When the end of the collection is reached, returns FALSE and the position of the iterator will be undefined.

RWBoolean operator-=(size_t n);

Retreats the iterator n positions and returns TRUE. When the beginning of the collection is reached, returns FALSE and the position of the iterator will be undefined.

RWBoolean operator()();

Advances the iterator to the next item. Returns TRUE if the new position is valid, FALSE otherwise.

Public Member Functions

RWTValDlist<T>* container() const;

Returns a pointer to the collection over which this iterator is iterating.

RWBoolean findNext(const T& a);

Advances the iterator to the first element that is equal to a and returns TRUE, or FALSE if there is no such element. Equality is measured by the class-defined equality operator for type T.

RWBoolean findNext(RWBoolean (*testFun)(const T&, void*), void*);

Advances the iterator to the first element for which the tester function pointed to by testFun returns TRUE and returns TRUE, or FALSE if there is no such element.

void insertAfterPoint(const T& a);

Inserts the value a into the iterator's associated collection in the position immediately after the iterator's current position.

T key() const;

Returns the value at the iterator's current position. The results are undefined if the iterator is no longer valid.

RWBoolean remove();

Removes the value from the iterator's associated collection at the current position of the iterator. Returns TRUE if successful, FALSE otherwise. Afterwards, if successful, the iterator will be positioned at the element immediately before the removed element.

RWBoolean removeNext(const T& a);

Advances the iterator to the first element that is equal to a and removes it. Returns TRUE if successful, FALSE otherwise. Equality is measured by the class-defined equality operator for type T. Afterwards, if successful, the iterator will be positioned at the element immediately before the removed element.

RWBoolean removeNext(RWBoolean (*testFun)(const T&, void*), void*);

Advances the iterator to the first element for which the tester function pointed to by testFun returns TRUE and removes it. Returns TRUE if successful, FALSE otherwise. Afterwards, if successful, the iterator will be positioned at the element immediately before the removed element.

void reset();

Resets the iterator to the state it had immediately after construction.

void reset(RWTValDlist<T>& c);

Resets the iterator to iterate over the collection c.

RWTValHashDictionary<K,V>

Synopsis

#include <rw/tvhdict.h>
 
unsigned hashFun(const K&);
RWTValHashDictionary<K,V> dictionary(hashFun);

Please Note!

If you do not have the Standard C++ Library, use the interface described here. Otherwise, use the interface described in the "Class Hierarchy" section of Class Reference.

Description

RWTValHashDictionary<K,V> is a dictionary of keys of type K and values of type V, implemented using a hash table. While duplicates of values are allowed, duplicates of keys are not.

It is a value based collection: keys and values are copied in and out of the hash buckets.

Parameters K and V represent the type of the key and the type of the value, respectively, to be inserted into the table. These can be either classes or fundamental types. Classes K and V must have:

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

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

In addition, class K must have

  • well-defined equality semantics (K::operator==(const K&)).

A user-supplied hashing function for type K must be supplied to the constructor when creating a new table. If K is a Rogue Wave class, then this requirement is usually trivial because most Rogue Wave objects know how to return a hashing value. In fact, classes RWCString, RWDate, RWTime, and RWWString contain static member functions called hash that can be supplied to the constructor as is. The function must have prototype:

unsigned hFun(const K& a);

and should return a suitable hash value for the object a.

To find a value, the key is first hashed to determine in which bucket the key and value can be found. The bucket is then searched for an object that is equal (as determined by the equality operator) to the key.

The initial number of buckets in the table is set by the constructor. There is a default value. If the number of (key/value) pairs in the collection greatly exceeds the number of buckets then efficiency will sag because each bucket must be searched linearly. The number of buckets can be changed by calling member function resize(). This is an expensive proposition because not only must all the items be copied into the new buckets, but all of the keys must be rehashed.

If you wish this to be done automatically, then you can subclass from this class and implement your own special insert() and remove() functions which perform a resize() as necessary.

Persistence

None

Example

#include <rw/tvhdict.h>
#include <rw/cstring.h>
#include <rw/rwdate.h>
#include <rw/rstream.h>
 
main()  { 
  RWTValHashDictionary<RWCString, RWDate>     birthdays(RWCString::hash);
 
  birthdays.insertKeyAndValue(
    "John",
    RWDate(12, "April", 1975)
  );
  birthdays.insertKeyAndValue("Ivan", RWDate(2, "Nov", 1980));
 
  // Alternative syntax:
  birthdays["Susan"] = RWDate(30, "June", 1955);
  birthdays["Gene"] = RWDate(5, "Jan", 1981);
 
  // Print a birthday:
  cout << birthdays["John"] << endl;
  return 0;
}

Program Output:

April 12, 1975

Public Constructors

RWTValHashDictionary<K,V>(unsigned (*hashKey)(const K&), size_t buckets = RWDEFAULT_CAPACITY);

Constructs a new hash dictionary. The first argument is a pointer to a user-defined hashing function for items of type K (the key). The table will initally have buckets buckets although this can be changed with member function resize().

RWTValHashDictionary<K,V>(const RWTValHashDictionary<K,V>& dict);

Copy constructor. Constructs a new hash dictionary as a copy of dict. The new dictionary will have the same number of buckets as the old table. Hence, although the keys and values must be copied into the new table, the keys will not be rehashed.

Public Operators

RWTValHashDictionary<K,V>& operator=(const RWTValHashDictionary<K,V>& dict);

Sets self to a copy of dict. Afterwards, the new table will have the same number of buckets as the old table. Hence, although the keys and values must be copied into the new table, the keys will not be rehashed.

V& operator[](const K& key);

Look up the key key and return its associated value as an lvalue reference. If the key is not in the dictionary, then it is added to the dictionary. In this case, the value associated with the key will be provided by the default constructor for objects of type V.

Public Member Functions

void applyToKeyAndValue(void (*applyFun)(const K&, V&,void*), void* d);

Applies the user-defined function pointed to by applyFun to every key-value pair in the dictionary. This function must have prototype:

void yourFun(const K& key, V& value, void* d);

The key will be passed by constant reference and hence cannot be changed. The value will be passed by reference and can be modified. Client data may be passed through as parameter d.

void clear();

Removes all items from the collection.

RWBoolean contains(const K& key) const;

Returns TRUE if the dictionary contains a key which is equal to key. Returns FALSE otherwise. Equality is measured by the class-defined equality operator for class K.

size_t entries() const;

Returns the number of key-value pairs currently in the dictionary.

RWBoolean find(const K& target, K& retKey) const;

Returns TRUE if the dictionary contains a key which is equal to target and puts the matching key into retKey. Returns FALSE otherwise and leaves retKey untouched. Equality is measured by the class-defined equality operator for class K.

RWBoolean findValue(const K& key, V& retVal) const;

Returns TRUE if the dictionary contains a key which is equal to key and puts the associated value into retVal. Returns FALSE otherwise and leaves retVal untouched. Equality is measured by the class-defined equality operator for class K.

RWBoolean findKeyAndValue(const K& key, K& retKey,V& retVal) const;

Returns TRUE if the dictionary contains a key which is equal to key and puts the matching key into retKey and the associated value into retVal. Returns FALSE otherwise and leaves retKey and retVal untouched. Equality is measured by the class-defined equality operator for class K.

void insertKeyAndValue(const K& key, const V& value);

Inserts the key key and value value into the dictionary.

RWBoolean isEmpty() const;

Returns TRUE if the dictionary has no items in it, FALSE otherwise.

RWBoolean remove(const K& key);

Returns TRUE and removes the (key/value) pair where the key is equal to the key. Returns FALSE if there is no such key. Equality is measured by the class-defined equality operator for class K.

void resize(size_t N);

Changes the number of buckets to N, a relatively expensive operation if there are many items in the collection.

RWTValHashDictionaryIterator<K,V>

Synopsis

#include <rw/tvhdict.h>
 
unsigned hashFun(const K&);
RWTValHashDictionary<K,V> dictionary(hashFun);
RWTValHashDictonaryIterator<K,V> iterator(dictionary);

Please Note!

If you do not have the Standard C++ Library, use the interface described here. Otherwise, use the interface described in the "Class Hierarchy" section of Class Reference.

Description

Iterator for class RWTValHashDictionary<K,V>, allowing sequential access to all keys and values of a parameterized hash dictionary. Elements are not accessed in any particular order.

Like all Rogue Wave iterators, the "current item" is undefined immediately after construction — you must define it by using operator() or some other (valid) operation.

Once the iterator has advanced beyond the end of the collection it is no longer valid — continuing to use it will bring undefined results.

Persistence

None

Public Constructor

RWTValHashDictionaryIterator(RWTValHashDictionary& c);

Constructs an iterator to be used with the dictionary c.

Public Operators

RWBoolean operator++();

Advances the iterator one position. Returns TRUE if the new position is valid, FALSE otherwise.

RWBoolean operator()();

Advances the iterator one position. Returns TRUE if the new position is valid, FALSE otherwise.

Public Member Functions

RWTValHashDictionary* container() const;

Returns a pointer to the collection over which this iterator is iterating.

K key() const;

Returns the key at the iterator's current position. The results are undefined if the iterator is no longer valid.

void reset();

Resets the iterator to the state it had immediately after construction.

void reset(RWTValHashDictionary& c);

Resets the iterator to iterate over the collection c.

V value() const;

Returns the value at the iterator's current position. The results are undefined if the iterator is no longer valid.

RWTValHashSet<T>

RWTValHashSet<T> ->- RWTValHashTable<T> 

Synopsis

#include <rw/tvhset.h>
 
unsigned hashFun(const T&);
RWTValHashSet(hashFun) set;

Please Note!

If you do not have the Standard C++ Library, use the interface described here. Otherwise, use the interface described in the "Class Hierarchy" section of Class Reference.

Description

RWTValHashSet<T> is a derived class of RWTValHashTable<T> where the insert() function has been overridden to accept only one item of a given value. Hence, each item in the collection will be unique.

As with class RWTValHashTable<T>, you must supply a hashing function to the constructor.

The class T must have:

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

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

  • well-defined equality semantics (T::operator==(const T&)).

Persistence

None

Example

This examples exercises a set of RWCStrings.

#include <rw/tvhset.h>
#include <rw/cstring.h>
#include <rw/rstream.h>
 
main(){ 
  RWTValHashSet<RWCString> set(RWCString::hash);
 
  set.insert("one");
  set.insert("two");
  set.insert("three");
  set.insert("one");  // Rejected: already in collection
 
  cout << set.entries() << endl;  // Prints "3"
  return 0;
}

Program Output:

3

Public Member Functions

RWTValHashSet<T>& Union(const RWTValHashSet<T>& h);

Computes the union of self and h, modifying self and returning self.

RWTValHashSet<T>& difference(const RWTValHashSet<T>& h);

Computes the disjunction of self and h, modifying self and returning self.

RWTValHashSet<T>& intersection(const RWTValHashSet<T>& h);

Computes the intersection of self and h, modifying self and returning self.

RWTValHashSet<T>& symmetricDifference(const RWTValHashSet<T>& h);

Computes the symmetric difference between self and h, modifying self and returning self.

RWBoolean isSubsetOf(const RWTValHashSet<T>& h) const;

Returns TRUE if self is a subset of h.

RWBoolean isProperSubsetOf(const RWTValHashSet<T>& h) const;

Returns TRUE if self is a proper subset of h.

RWBoolean isEquivalent(const RWTValHashSet<T>& h) const;

Returns TRUE if self and h are identical.

void apply(void (*applyFun)(T&, void*), void* d);

Inherited from class RWTValHashTable<T>.

void clear();

Inherited from class RWTValHashTable<T>.

RWBoolean contains(const T& val) const;

Inherited from class RWTValHashTable<T>.

size_t entries() const;

Inherited from class RWTValHashTable<T>.

RWBoolean find(const T& target, T& k) const;

Inherited from class RWTValHashTable<T>.

void insert(const T& val);

Redefined from class RWTValHashTable<T> to allow an object of a given value to be inserted only once.

RWBoolean isEmpty() const;

Inherited from class RWTValHashTable<T>.

size_t occurrencesOf(const T& val) const;

Inherited from class RWTValHashTable<T>.

RWBoolean remove(const T& val);

Inherited from class RWTValHashTable<T>.

size_t removeAll(const T& val);

Inherited from class RWTValHashTable<T>.

void resize(size_t N);

Inherited from class RWTValHashTable<T>.

RWTValHashTable<T>

Synopsis

#include <rw/tvhasht.h>
 
unsigned hashFun(const T&);
RWTValHashTable<T> table(hashFun);

Please Note!

If you do not have the Standard C++ Library, use the interface described here. Otherwise, use the interface described in the "Class Hierarchy" section of Class Reference.

Description

This class implements a parameterized hash table of types T. It uses chaining to resolve hash collisions. Duplicates are allowed.

It is a value based collection: objects are copied in and out of the hash buckets.

Parameter T represents the type of object to be inserted into the table, either a class or fundamental type. The class T must have:

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

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

  • well-defined equality semantics (T::operator==(const T&)).

A user-supplied hashing function for type T must be supplied to the constructor when creating a new table. If T is a Rogue Wave class, then this requirement is usually trivial because most Rogue Wave objects know how to return a hashing value. In fact, classes RWCString, RWDate, RWTime, and RWWString contain static member functions called hash that can be supplied to the constructor as is. The function must have prototype:

unsigned hFun(const T& a);

and should return a suitable hash value for the object a.

To find an object, it is first hashed to determine in which bucket it occurs. The bucket is then searched for an object that is equal (as determined by the equality operator) to the candidate.

The initial number of buckets in the table is set by the constructor. There is a default value. If the number of items in the collection greatly exceeds the number of buckets then efficiency will sag because each bucket must be searched linearly. The number of buckets can be changed by calling member function resize(). This is an expensive proposition because not only must all items be copied into the new buckets, but they must also be rehashed.

If you wish this to be automatically done, then you can subclass from this class and implement your own special insert() and remove() functions which perform a resize() as necessary.

Persistence

None

Example

#include <rw/tvhasht.h>
#include <rw/cstring.h>
#include <rw/rstream.h>
 
main()  { 
  RWTValHashTable<RWCString> table(RWCString::hash);
 
  table.insert("Alabama");   // NB: Type conversion occurs
  table.insert("Pennsylvania");
  table.insert("Oregon");
  table.insert("Montana");
 
  cout << "The table " <<
    (table.contains("Oregon") ? "does " : "does not ") <<
    "contain Oregon\n";
 
  table.removeAll("Oregon");
 
  cout << "Now the table " 
       << (table.contains("Oregon") ? "does " : "does not ") 
       << "contain Oregon";
  return 0;
}

Program Output:

The table does contain Oregon
Now the table does not contain Oregon

Public Constructors

RWTValHashTable<T>(unsigned (*hashFun)(const T&), size_t buckets = RWDEFAULT_CAPACITY);

Constructs a new hash table. The first argument is a pointer to a user-defined hashing function for items of type T. The table will initally have buckets buckets although this can be changed with member function resize().

RWTValHashTable<T>(const RWTValHashTable<T>& table);

Constructs a new hash table as a copy of table. The new table will have the same number of buckets as the old table. Hence, although objects must be copied into the new table, they will not be hashed.

Public Operators

RWTValHashTable& operator=(const RWTValHashTable<T>&);

Sets self to a copy of table. Afterwards, the new table will have the same number of buckets as the old table. Hence, although objects must be copied into the new table, they will not be hashed.

Public Member Functions

void apply(void (*applyFun)(T&, void*), void* d);

Applies the user-defined function pointed to by applyFun to every item in the table. This function must have prototype:

void yourFun(T& a, void* d);

Client data may be passed through as parameter d.

void clear();

Removes all items from the collection.

RWBoolean contains(const T& val) const;

Returns TRUE if the collection contains an item which is equal to val. Returns FALSE otherwise. Equality is measured by the class-defined equality operator.

size_t entries() const;

Returns the number of items currently in the collection.

RWBoolean find(const T& target, T& k) const;

Returns TRUE if the collection contains an item which is equal to target and puts the matching object into k. Returns FALSE otherwise and leaves k untouched. Equality is measured by the class-defined equality operator.

void insert(const T& val);

Inserts the value val into the collection.

RWBoolean isEmpty() const;

Returns TRUE if the collection has no items in it, FALSE otherwise.

size_t occurrencesOf(const T& val) const;

Returns the number of items in the collection which are equal to val. Equality is measured by the class-defined equality operator.

RWBoolean remove(const T& val);

Removes the first object which is equal to the object a and returns TRUE. Returns FALSE if there is no such object. Equality is measured by the class-defined equality operator.

size_t removeAll(const T& val);

Removes all objects which are equal to the object a. Returns the number of objects removed. Equality is measured by the class-defined equality operator.

void resize(size_t N);

Changes the number of buckets to N, a relatively expensive operation if there are many items in the collection.

RWTValHashTableIterator<T>

Synopsis

#include <rw/tvhasht.h>
 
RWTValHashTable<T> table;
RWTValHashTableIterator<T> iterator(table);

Please Note!

If you do not have the Standard C++ Library, use the interface described here. Otherwise, use the interface described in the "Class Hierarchy" section of Class Reference.

Description

Iterator for class RWTValHashTable<T>, allowing sequential access to all the elements of a hash table. Elements are not accessed in any particular order.

Like all Rogue Wave iterators, the "current item" is undefined immediately after construction — you must define it by using operator() or some other (valid) operation.

Once the iterator has advanced beyond the end of the collection it is no longer valid — continuing to use it will bring undefined results.

Persistence

None

Public Constructor

RWTValHashTableIterator(RWTValHashTable<T>& c);

Constructs an iterator to be used with the table c.

Public Operators

RWBoolean operator++();

Advances the iterator one position. Returns TRUE if the new position is valid, FALSE otherwise.

RWBoolean operator()();

Advances the iterator one position. Returns TRUE if the new position is valid, FALSE otherwise.

Public Member Functions

RWTValHashTable<T>* container() const;

Returns a pointer to the collection over which this iterator is iterating.

T key() const;

Returns the value at the iterator's current position. The results are undefined if the iterator is no longer valid.

void reset();

Resets the iterator to the state it had immediately after construction.

void reset(RWTValHashTable<T>& c);

Resets the iterator to iterate over the collection c.

RWTValOrderedVector<T>

Synopsis

#include <rw/tvordvec.h>
 
RWTValOrderedVector<T> ordvec;

Please Note!

If you do not have the Standard C++ Library, use the interface described here. Otherwise, use the interface described in the "Class Hierarchy" section of Class Reference.

Description

RWTValOrderedVector<T> is an ordered collection. That is, the items in the collection have a meaningful ordered relationship with respect to one another and can be accessed by an index number. The order is set by the order of insertion. Duplicates are allowed. The class is implemented as a vector, allowing efficient insertion and retrieval from the end of the collection, but somewhat slower from the beginning of the collection.

The class T must have:

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

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

  • well-defined equality semantics (T::operator==(const T&));

  • a default constructor.

Note that an ordered vector has a length (the number of items returned by length() or entries()) and a capacity. Necessarily, the capacity is always greater than or equal to the length. Although elements beyond the collection's length are not used, nevertheless, in a value-based collection, they are occupied. If each instance of class T requires considerable resources, then you should ensure that the collection's capacity is not much greater than its length, otherwise unnecessary resources will be tied up.

Persistence

Isomorphic

Example

#include <rw/tvordvec.h>
#include <rw/rstream.h>
 
main()  {
  RWTValOrderedVector<double> vec;
 
  vec.insert(22.0);
  vec.insert(5.3);
  vec.insert(-102.5);
  vec.insert(15.0);
  vec.insert(5.3);
 
  cout << vec.entries() << " entries\n" << endl;  // Prints "5"
  for (int i=0; i<vec.length(); i++)
    cout << vec[i] << endl;
 
  return 0;
}

Program Output:

5 entries
22
5.3
-102.5
15
5.3

Public Constructor

RWTValOrderedVector<T>(size_t capac=RWDEFAULT_CAPACITY);

Create an empty ordered vector with capacity capac. Should the number of items exceed this value, the vector will be resized automatically.

RWTValOrderedVector<T>(const RWTValOrderedVector<T>& c);

Constructs a new ordered vector as a copy of c. The copy constructor of all elements in the vector will be called. The new vector will have the same capacity and number of members as the old vector.

Public Operators

RWTValOrderedVector<T>& operator=(const RWTValOrderedVector& c);

Sets self to a copy of c. The copy constructor of all elements in the vector will be called. Self will have the same capacity and number of members as the old vector.

T& operator()(size_t i); const T& operator()(size_t i) const;

Returns the ith value in the vector. The first variant can be used as an lvalue, the second cannot. The index i must be between zero and the number of items in the collection less one. No bounds checking is performed.

T& operator[](size_t i); const T& operator[](size_t i) const;

Returns the ith value in the vector. The first variant can be used as an lvalue, the second cannot. The index i must be between zero and the number of items in the collection less one, or an exception of type RWBoundsError will be thrown.

Public Member Functions

void append(const T& a);

Appends the value a to the end of the vector. The collection will automatically be resized if this causes the number of items in the collection to exceed the capacity.

T& at(size_t i); const T& at(size_t i) const;

Return the ith value in the vector. The first variant can be used as an lvalue, the second cannot. The index i must be between 0 and the length of the vector less one or an exception of type RWBoundsError will be thrown.

void clear();

Removes all items from the collection.

RWBoolean contains(const T& a) const;

Returns TRUE if the collection contains an item that is equal to a. A linear search is done. Equality is measured by the class-defined equality operator.

const T* data() const;

Returns a pointer to the raw data of the vector. The contents should not be changed. Should be used with care.

size_t entries() const;

Returns the number of items currently in the collection.

RWBoolean find(const T& target, T& ret) const;

Performs a linear search and returns TRUE if the vector contains an object that is equal to the object target and puts a copy of the matching object into ret. Returns FALSE otherwise and does not touch ret. Equality is measured by the class-defined equality operator.

T& first(); const T& first() const;

Returns the first item in the collection. An exception of type RWBoundsError will occur if the vector is empty.

size_t index(const T& a) const;

Performs a linear search, returning the index of the first item that is equal to a. Returns RW_NPOS if there is no such item. Equality is measured by the class-defined equality operator.

void insert(const T& a);

Appends the value a to the end of the vector. The collection will automatically be resized if this causes the number of items in the collection to exceed the capacity.

void insertAt(size_t i, const T& a);

Inserts the value a into the vector at index i. The item previously at position i is moved to i+1, etc. The collection will automatically be resized if this causes the number of items in the collection to exceed the capacity. The index i must be between 0 and the number of items in the vector or an exception of type RWBoundsError will occur.

RWBoolean isEmpty() const;

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

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

Returns the last item in the collection. If there are no items in the collection then an exception of type RWBoundsError will occur.

size_t length() const;

Returns the number of items currently in the collection.

size_t occurrencesOf(const T& a) const;

Performs a linear search, returning the number of items that are equal to a. Equality is measured by the class-defined equality operator.

void prepend(const T& a);

Prepends the value a to the beginning of the vector. The collection will automatically be resized if this causes the number of items in the collection to exceed the capacity.

RWBoolean remove(const T& a);

Performs a linear search, removing the first object which is equal to the object a and returns TRUE. Returns FALSE if there is no such object. Equality is measured by the class-defined equality operator.

size_t removeAll(const T& a);

Removes all items which are equal to a, returning the number removed. Equality is measured by the class-defined equality operator.

T removeAt(size_t i);

Removes and returns the object at index i. An exception of type RWBoundsError will be thrown if i is not a valid index. Valid indices are from zero to the number of items in the list less one.

T removeFirst();

Removes and returns the first object in the collection. An exception of type RWBoundsError will be thrown if the list is empty.

T removeLast();

Removes and returns the last object in the collection. An exception of type RWBoundsError will be thrown if the list is empty.

void resize(size_t N);

Changes the capacity of the collection to N. Note that the number of objects in the collection does not change, just the capacity.

Related Global Operators

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

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

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

RWTValSlist<T>

Synopsis

#include <rw/tvslist.h>
 
RWTValSlist<T> list;

Please Note!

If you do not have the Standard C++ Library, use the interface described here. Otherwise, use the interface described in the "Class Hierarchy" section of Class Reference.

Description

This class maintains a collection of values, implemented as a singly-linked list. This is a value based list: objects are copied in and out of the links that make up the list. Unlike intrusive lists (see class RWTIsvSlist<T>) the objects need not inherit from a link class. However, this makes the class slightly less efficient than the intrusive lists because of the need to allocate a new link off the heap with every insertion and to make a copy of the object in the newly allocated link.

Parameter T represents the type of object to be inserted into the list, either a class or fundamental type. The class T must have:

  • A default constructor;

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

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

  • well-defined equality semantics (T::operator==(const T&)).

Persistence

Isomorphic

Example

In this example, a singly-linked list of RWDates is exercised.

#include <rw/tvslist.h>
#include <rw/rwdate.h>
#include <rw/rstream.h>
 
main()  {
  RWTValSlist<RWDate> dates;
  dates.insert(RWDate(2, "June", 52));     // 6/2/52
  dates.insert(RWDate(30, "March", 46));   // 3/30/46
  dates.insert(RWDate(1, "April", 90));    // 4/1/90
 
  // Now look for one of the dates:
  RWDate ret;
  if (dates.find(RWDate(2, "June", 52), ret)){
    cout << "Found date " << ret << endl;
  }
 
  // Remove in reverse order:
  while (!dates.isEmpty())
    cout << dates.removeLast() << endl;
 
  return 0;
}

Program Output:

Found date June 2, 1952
April 1, 1990
March 30, 1946
June 2, 1952

Public Constructors

RWTValSlist<T>();

Construct an empty list.

RWTValSlist<T>(const RWTValSlist<T>& list);

Construct a copy of the list list. Depending on the nature of the copy constructor of T, this could be relatively expensive because every item in the list must be copied.

Public Operators

RWTValSlist& operator=(const RWTValSlist<T>& list);

Sets self to a copy of the list list. Depending on the nature of the copy constructor of T, this could be relatively expensive because every item in the list must be copied.

T& operator[](size_t i);

Returns a reference to the item at index i. The results can be used as an lvalue. An exception of type RWBoundsError will be thrown if i is not a valid index. Valid indices are from zero to the number of items in the list less one.

const T& operator[](size_t i) const;

Returns a copy of the item at index i. The results cannot be used as an lvalue. An exception of type RWBoundsError will be thrown if i is not a valid index. Valid indices are from zero to the number of items in the list less one.

Public Member Functions

void append(const T& a);

Adds the item a to the end of the list.

void apply(void (*applyFun)(T&, void*), void* d);

Applies the user-defined function pointed to by applyFun to every item in the list. This function must have prototype:

void yourFun(T& a, void* d);

Client data may be passed through as parameter d.

T& at(size_t i);

Returns a reference to the item at index i. The results can be used as an lvalue. An exception of type RWBoundsError will be thrown if i is not a valid index. Valid indices are from zero to the number of items in the list less one.

const T& at(size_t i) const;

Returns a copy of the item at index i. The results cannot be used as an lvalue. An exception of type RWBoundsError will be thrown if i is not a valid index. Valid indices are from zero to the number of items in the list less one.

void clear();

Removes all items from the list. Their destructors, if any, will be called.

RWBoolean contains(const T& a) const;

Returns TRUE if the list contains an object that is equal to the object a. Returns FALSE otherwise. Equality is measured by the class-defined equality operator.

RWBoolean contains(RWBoolean (*testFun)(const T&, void*), void* d) const;

Returns TRUE if the list contains an item for which the user-defined "tester" function pointed to by testFun returns TRUE . Returns FALSE otherwise. The tester function must have the prototype:

RWBoolean yourTester(const T&, void* d);

For each item in the list this function will be called with the item as the first argument. Client data may be passed through as parameter d.

size_t entries() const;

Returns the number of items that are currently in the collection.

RWBoolean find(const T& target, T& k) const;

Returns TRUE if the list contains an object that is equal to the object target and puts a copy of the matching object into k. Returns FALSE otherwise and does not touch k. Equality is measured by the class-defined equality operator. If you do not need a copy of the found object, use contains() instead.

RWBoolean find(RWBoolean (*testFun)(const T&, void*),void* d, T& k) const;

Returns TRUE if the list contains an object for which the user-defined tester function pointed to by testFun returns TRUE and puts a copy of the matching object into k. Returns FALSE otherwise and does not touch k. The tester function must have the prototype:

RWBoolean yourTester(const T&, void* d);

For each item in the list this function will be called with the item as the first argument. Client data may be passed through as parameter d. If you do not need a copy of the found object, use contains() instead.

T& first(); const T& first() const;

Returns but does ot remove the first item in the list. The behavior is undefined if the list is empty.

T get();

Returns and removes the first item in the list. The behavior is undefined if the list is empty.

size_t index(const T& a);

Returns the index of the first object that is equal to the object a, or RW_NPOS if there is no such object. Equality is measured by the class-defined equality operator.

size_t index(RWBoolean (*testFun)(const T&, void*),void* d) const;

Returns the index of the first object for which the user-defined tester function pointed to by testFun returns TRUE, or RW_NPOS if there is no such object. The tester function must have the prototype:

RWBoolean yourTester(const T&, void* d);

For each item in the list this function will be called with the item as the first argument. Client data may be passed through as parameter d.

void insert(const T& a);

Adds the item a to the end of the list.

void insertAt(size_t i, const T& a);

Insert the item a at the index position i. This position must be between zero and the number of items in the list, or an exception of type RWBoundsError will be thrown.

RWBoolean isEmpty() const;

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

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

Returns but does not remove the last item in the list. The behavior is undefined if the list is empty.

size_t occurrencesOf(const T& a) const;

Returns the number of objects in the list that are equal to the object a. Equality is measured by the class-defined equality operator.

size_t occurrencesOf(RWBoolean (*testFun)(const T&, void*),void* d) const;

Returns the number of objects in the list for which the user-defined "tester" function pointed to by testFun returns TRUE . The tester function must have the prototype:

RWBoolean yourTester(const T&, void* d);

For each item in the list this function will be called with the item as the first argument. Client data may be passed through as parameter d.

void prepend(const T& a);

Adds the item a to the beginning of the list.

RWBoolean remove(const T& a);

Removes the first object which is equal to the object a and returns TRUE. Returns FALSE if there is no such object. Equality is measured by the class-defined equality operator.

RWBoolean remove(RWBoolean (*testFun)(const T&, void*), void* d);

Removes the first object for which the user-defined tester function pointed to by testFun returns TRUE, and returns TRUE. Returns FALSE if there is no such object. The tester function must have the prototype:

RWBoolean yourTester(const T&, void* d);

For each item in the list this function will be called with the item as the first argument. Client data may be passed through as parameter d.

size_t removeAll(const T& a);

Removes all objects which are equal to the object a. Returns the number of objects removed. Equality is measured by the class-defined equality operator.

size_t removeAll(RWBoolean (*testFun)(const T&, void*),void* d);

Removes all objects for which the user-defined tester function pointed to by testFun returns TRUE. Returns the number of objects removed. The tester function must have the prototype:

RWBoolean yourTester(const T&, void* d);

For each item in the list this function will be called with the item as the first argument. Client data may be passed through as parameter d.

T removeAt(size_t i);

Removes and returns the object at index i. An exception of type RWBoundsError will be thrown if i is not a valid index. Valid indices are from zero to the number of items in the list less one.

T removeFirst();

Removes and returns the first item in the list. The behavior is undefined if the list is empty.

T removeLast();

Removes and returns the last item in the list. The behavior is undefined if the list is empty. This function is relatively slow because removing the last link in a singly-linked list necessitates access to the next-to-the-last link, requiring the whole list to be searched.

Related Global Operators

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

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

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

RWTValSlistIterator<T>

Synopsis

#include <rw/tvslist.h>
 
RWTValSlist<T> list;
RWTValSlistIterator<T> iterator(list);

Please Note!

If you do not have the Standard C++ Library, use the interface described here. Otherwise, use the interface described in the "Class Hierarchy" section of Class Reference.

Description

Iterator for class RWTValSlist<T>, allowing sequential access to all the elements of a singly-linked parameterized list. Elements are accessed in order, from first to last.

Like all Rogue Wave iterators, the "current item" is undefined immediately after construction — you must define it by using operator() or some other valid operation.

Once the iterator has advanced beyond the end of the collection it is no longer valid — continuing to use it will bring undefined results.

Persistence

None

Public Constructor

RWTValSlistIterator<T>(RWTValSlist<T>& c);

Constructs an iterator to be used with the list c.

Public Member Operators

RWBoolean operator++();

Advances the iterator one position. Returns TRUE if the new position is valid, FALSE otherwise.

RWBoolean operator+=(size_t n);

Advances the iterator n positions. Returns TRUE if the new position is valid, FALSE otherwise.

RWBoolean operator()();

Advances the iterator one position. Returns TRUE if the new position is valid, FALSE otherwise.

Public Member Functions

RWTValSlist<T>* container() const;

Returns a pointer to the collection over which this iterator is iterating.

RWBoolean findNext(const T& a);

Advances the iterator to the first element that is equal to a and returns TRUE, or FALSE if there is no such element. Equality is measured by the class-defined equality operator for type T.

RWBoolean findNext(RWBoolean (*testFun)(const T&, void*),void*);

Advances the iterator to the first element for which the tester function pointed to by testFun returns TRUE and then returns TRUE, or FALSE if there is no such element.

void insertAfterPoint(const T& a);

Inserts the value a into the iterator's associated collection in the position immediately after the iterator's current position.

T key() const;

Returns the value at the iterator's current position. The results are undefined if the iterator is no longer valid.

RWBoolean remove();

Removes the value from the iterator's associated collection at the current position of the iterator. Returns TRUE if successful, FALSE otherwise. Afterwards, if successful, the iterator will be positioned at the element immediately before the removed element. This function is relatively inefficient for a singly-linked list.

RWBoolean removeNext(const T& a);

Advances the iterator to the first element that is equal to a and removes it. Returns TRUE if successful, FALSE otherwise. Equality is measured by the class-defined equality operator for type T. Afterwards, if successful, the iterator will be positioned at the element immediately before the removed element.

RWBoolean removeNext(RWBoolean (*testFun)(const T&, void*),void*);

Advances the iterator to the first element for which the tester function pointed to by testFun returns TRUE and removes it. Returns TRUE if successful, FALSE otherwise. Afterwards, if successful, the iterator will be positioned at the element immediately before the removed element.

void reset();

Resets the iterator to the state it had immediately after construction.

void reset(RWTValSlist<T>& c);

Resets the iterator to iterate over the collection c.

RWTValSortedVector<T>

Synopsis

#include <rw/tvsrtvec.h> RWTValSortedVector<T> sortvec;

If you do not have the Standard C++ Library, use the interface described here. Otherwise, use the interface described in the "Class Hierarchy" section of Class Reference.

Description

RWTValSortedVector<T> is an ordered collection. That is, the items in the collection have a meaningful ordered relationship with respect to each other and can be accessed by an index number. In the case of RWTValSortedVector<T>, objects are inserted such that objects "less than" themselves are before the object, objects "greater than" themselves after the object. An insertion sort is used. Duplicates are allowed.

Stores a copy of the inserted item into the collection according to an ordering determined by the less-than (<) operator.

The class T must have:

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

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

  • well-defined equality semantics (T::operator==(const T&));

  • well-defined less-than semantics (T::operator<(const T&));

  • a default constructor.

Note that a sorted vector has a length (the number of items returned by length() or entries()) and a capacity. Necessarily, the capacity is always greater than or equal to the length. Although elements beyond the collection's length are not used, nevertheless, in a value-based collection, they are occupied. If each instance of class T requires considerable resources, then you should ensure that the collection's capacity is not much greater than its length, otherwise unnecessary resources will be tied up.

Although it is possible to alter objects that are contained in a RWTValSortedVector<T>, it is dangerous since the changes may affect the way that operator<() and operator==() behave, causing the RWTValSortedVector<T> to become unsorted.

Persistence

Isomorphic

Example

This example inserts a set of dates into a sorted vector in no particular order, then prints them out in order.

#include <rw/tvsrtvec.h>
#include <rw/rwdate.h>
#include <rw/rstream.h>
 
{
  RWTValSortedVector<RWDate> vec;
 
  vec.insert(RWDate(10, "Aug", 1999));
  vec.insert(RWDate(9, "Aug", 1999));
  vec.insert(RWDate(1, "Sept", 1999));
  vec.insert(RWDate(14, "May", 1999));
  vec.insert(RWDate(1, "Sept", 1999));     // Add a duplicate
  vec.insert(RWDate(2, "June", 1999));
 
  for (int i=0; i<vec.length(); i++)
    cout << vec[i] << endl;
  return 0;
}

Program Output:

May 14, 1999
June 2, 1999
August 9, 1999
August 10, 1999
September 1, 1999
September 1, 1999

Public Constructor

RWTValSortedVector(size_t capac = RWDEFAULT_CAPACITY);

Create an empty sorted vector with an initial capacity equal to capac. The vector will be automatically resized should the number of items exceed this amount.

Public Operators

T& operator()(size_t i); const T& operator()(size_t i) const;

Returns the ith value in the vector. The first variant can be used as an lvalue, the second cannot. The index i must be between zero and the number of items in the collection less one. No bounds checking is performed. When used as an lvalue, care must be taken so as not to disturb the sortedness of the collection.

T& operator[](size_t i); const T& operator[](size_t i) const;

Returns the ith value in the vector. The first variant can be used as an lvalue, the second cannot. The index i must be between zero and the number of items in the collection less one, or an exception of type RWBoundsError will be thrown. When used as an lvalue, care must be taken so as not to disturb the sortedness of the collection.

Public Member Functions

T& at(size_t i); const T& at(size_t i) const;

Return the ith value in the vector. The first variant can be used as an lvalue, the second cannot. The index i must be between 0 and the length of the vector less one, or an exception of type RWBoundsError will be thrown. When used as an lvalue, care must be taken so as not to disturb the sortedness of the collection.

void clear();

Removes all items from the collection.

RWBoolean contains(const T& a) const;

Returns TRUE if the collection contains an item that is equal to a. A binary search is done. Equality is measured by the class-defined equality operator.

const T* data() const;

Returns a pointer to the raw data of the vector. The contents should not be changed. Should be used with care.

size_t entries() const;

Returns the number of items currently in the collection.

RWBoolean find(const T& target, T& ret) const;

Performs a binary search and returns TRUE if the vector contains an object that is equal to the object target and puts a copy of the matching object into ret. Returns FALSE otherwise and does not touch ret. Equality is measured by the class-defined equality operator.

const T& first() const;

Returns the first item in the collection. An exception of type RWBoundsError will occur if the vector is empty.

size_t index(const T& a) const;

Performs a binary search, returning the index of the first item that is equal to a. Returns RW_NPOS if there is no such item. Equality is measured by the class-defined equality operator.

void insert(const T& a);

Performs a binary search, inserting a after all items that compare less than or equal to it, but before all items that do not. "Less Than" is measured by the class-defined '<' operator for type T. The collection will be resized automatically if this causes the number of items to exceed the capacity.

RWBoolean isEmpty() const;

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

const T& last() const;

Returns the last item in the collection. If there are no items in the collection then an exception of type RWBoundsError will occur.

size_t length() const;

Returns the number of items currently in the collection.

size_t occurrencesOf(const T& a) const;

Performs a binary search, returning the number of items that are equal to a. Equality is measured by the class-defined equality operator.

RWBoolean remove(const T& a);

Performs a binary search, removing the first object which is equal to the object a and returns TRUE. Returns FALSE if there is no such object. Equality is measured by the class-defined equality operator.

size_t removeAll(const T& a);

Removes all items which are equal to a, returning the number removed. Equality is measured by the class-defined equality operator.

T removeAt(size_t i);

Removes and returns the object at index i. An exception of type RWBoundsError will be thrown if i is not a valid index. Valid indices are from zero to the number of items in the list less one.

T removeFirst();

Removes and returns the first object in the collection. An exception of type RWBoundsError will be thrown if the list is empty.

T removeLast();

Removes and returns the last object in the collection. An exception of type RWBoundsError will be thrown if the list is empty.

void resize(size_t N);

Changes the capacity of the collection to N. Note that the number of objects in the collection does not change, just the capacity.

Related Global Operators

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

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

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