Chapter 4. RWFactory - RWOrderedIterator

RWFactory

Synopsis

typedef unsigned short  RWClassID;
 
typedef RWCollectable*  (*RWuserCreator)();
#include <rw/factory.h>
 
RWFactory* theFactory;
 

Description

Class RWFactory can create an instance of an RWCollectable object, given a class ID. It does this by maintaining a table of class IDs and associated "creator functions." A creator function has prototype:

RWCollectable*  aCreatorFunction(); 

This function should create an instance of a particular class. For a given RWClassID tag, the appropriate function is selected, invoked and the resultant pointer returned. Because any object created this way is created off the heap, you are responsible for deleting it when done.

There is a one-of-a-kind global RWFactory which can be accessed using getRWFactory. It is guaranteed to have creator functions in it for all of the classes referenced by your program. See also the section in the User's Guide about RWFactory.

Persistence

None

Example

#include <rw/factory.h>
#include <rw/rwbag.h>
#include <rw/colldate.h>
#include <rw/rstream.h>
 
main(){
 // Create new RWBag off the heap, using Class ID __RWBAG.
 
 RWBag* b = (RWBag*)getRWFactory ()->create(__RWBAG);
 
 b->insert( new RWCollectableDate ); // Insert today's date
 // ...
 b->clearAndDestroy();     // Cleanup: first delete members,
 delete b;                 // then the bag itself
}

Public Constructors

RWFactory();

Construct an RWFactory.

Public Operator

RWBoolean operator<=(const RWFactory& h);

Returns TRUE if self is a subset of h, that is, every element of self has a counterpart in h which isEqual. This operator is included to fix an inconsistency in the C++ language. It is not explicitly present unless you are compiling with an implementation of the Standard C++ Library. It would normally be inherited from RWSet.


Note: If you inherit from RWFactory in the presence of the Standard C++ Library, we recommend that you override this operator and explicitly forward the call. Overload resolution in C++ will choose the Standard Library provided global operators over inherited class members. These global definitions are not appropriate for set-like partial orderings.


Public Member Functions

void addFunction(RWuserCreator uc, RWClassID id);

Adds to the RWFactory the global function pointed to by uc, which creates an instance of an object with RWClassID id.

void addFunction(RWuserCreator uc, RWClassID id, RWStringID sid);

Adds to the RWFactory the global function pointed to by uc, which creates an instance of an object with RWClassID id and RWStringID sid.

RWCollectable*

create(RWClassID id) const;

Allocates a new instance of the class with RWClassIDid off the heap and returns a pointer to it. Returns nil if id does not exist. Because this instance is allocated off the heap, you are responsible for deleting it when done.

RWCollectable* create(RWString sid) const;

Allocates a new instance of the class with RWStringIDsid off the heap and returns a pointer to it. Returns nil if sid does not exist. Because this instance is allocated off the heap, you are responsible for deleting it when done.

RWuserCreator getFunction(RWClassID id) const;

Returns from the RWFactory a pointer to the global function associated with RWClassID id. Returns nil if id does not exist.

RWuserCreator getFunction(RWStringID sid) const;

Returns from the RWFactory a pointer to the global function associated with RWStringID sid. Returns nil if sid does not exist.

void removeFunction(RWClassID id);

Removes from the RWFactory the global function associated with RWClassID id. If id does not exist in the factory, no action is taken.

void removeFunction(RWStringID sid);

Removes from the RWFactory the global function associated with RWStringID sid. If sid does not exist in the factory, no action is taken.

RWStringID stringID(RWClassID id) const;

Looks up the RWStringID associated with id and returns it. If there is no such association, returns RWStringID("NoID").

RWClassID

classID(RWStringID) const;

Looks up the RWClassID associated with sid and returns it. If there is no such association, returns __RWUNKNOWN.

RWFile

Synopsis

#include <rw/rwfile.h>
 
RWFile f("filename");

Description

Class RWFile encapsulates binary file operations using the Standard C stream library (functions fopen(), fread(), fwrite(), etc.). This class is based on class PFile of the Interviews Class Library (1987, Stanford University). The member function names begin with upper case letters in order to maintain compatibility with class PFile .

Because this class is intended to encapsulate binary operations, it is important that it be opened using a binary mode. This is particularly important under MS-DOS — otherwise bytes that happen to match a newline will be expanded to (carriage return, line feed).

Persistence

None

Public Constructors

RWFile(const char* filename, const char* mode = 0);

Construct an RWFile to be used with the file of name filename and with mode mode. The mode is as given by the Standard C library function fopen(). If mode is zero (the default) then the constructor will attempt to open an existing file with the given filename for update (mode "rb+"). If this is not possible, then it will attempt to create a new file with the given filename (mode "wb+"). The resultant object should be checked for validity using function isValid().

~RWFile();

Performs any pending I/O operations and closes the file.

Public Member Functions

const char* Access();

Returns the access mode with which the underlying FILE* was opened.

void ClearErr();

Reset error state so that neither Eof() nor Error() returns TRUE. Calls C library function clearerr().

RWoffset CurOffset();

Returns the current position, in bytes from the start of the file, of the file pointer.

RWBoolean Eof();

Returns TRUE if an end-of-file has been encountered.

RWBoolean Erase();

Erases the contents but does not close the file. Returns TRUE if the operation was successful.

RWBoolean Error();

Returns TRUE if a file I/O error has occurred as determined by a call to the C library function ferror().

RWBoolean Exists();

Returns TRUE if the file exists.

RWBoolean Flush();

Perform any pending I/O operations. Returns TRUE if successful.

const char* GetName();

Returns the file name.

FILE* GetStream();

Returns the FILE* that underlies the RWFile interface. Provided for users who need to "get under the hood" for system-dependent inquiries, etc. Do not use to alter the state of the file!

RWBoolean IsEmpty();

Returns TRUE if the file contains no data, FALSE otherwise.

RWBoolean isValid() const;

Returns TRUE if the file was successfully opened, FALSE otherwise.

RWBoolean Read(char& c); RWBoolean Read(wchar_t& wc); RWBoolean Read(short& i); RWBoolean Read(int& i); RWBoolean Read(long& i); RWBoolean Read(unsigned char& c); RWBoolean Read(unsigned short& i); RWBoolean Read(unsigned int& i); RWBoolean Read(unsigned long& i); RWBoolean Read(float& f); RWBoolean Read(double& d);

Reads the indicated built-in type. Returns TRUE if the read is successful.

RWBoolean Read(char* i, size_t count); RWBoolean Read(wchar_t* i, size_t count); RWBoolean Read(short* i, size_t count); RWBoolean Read(int* i, size_t count); RWBoolean Read(long* i, size_t count); RWBoolean Read(unsigned char* i, size_t count); RWBoolean Read(unsigned short* i,size_t count); RWBoolean Read(unsigned int* i, size_t count); RWBoolean Read(unsigned long* i, size_t count); RWBoolean Read(float* i, size_t count); RWBoolean Read(double* i, size_t count);

Reads count instances of the indicated built-in type into a block pointed to by i. Returns TRUE if the read is successful. Note that you are responsible for declaring i and for allocating the necessary storage before calling this function.

RWBoolean Read(char* string);

Reads a character string, including the terminating null character, into a block pointed to by string. Returns TRUE if the read is successful. Note that you are responsible for declaring string and for allocating the necessary storage before calling this function. Beware of overflow when using this function.

RWBoolean SeekTo(RWoffset offset);

Repositions the file pointer to offset bytes from the start of the file. Returns TRUE if the operation is successful.

RWBoolean SeekToBegin();

Repositions the file pointer to the start of the file. Returns TRUE if the operation is successful.

RWBoolean SeekToEnd();

Repositions the file pointer to the end of the file. Returns TRUE if the operation is successful.

RWBoolean Write(char i); RWBoolean Write(wchar_t i); RWBoolean Write(short i); RWBoolean Write(int i); RWBoolean Write(long i); RWBoolean Write(unsigned char i); RWBoolean Write(unsigned short i); RWBoolean Write(unsigned int i); RWBoolean Write(unsigned long i); RWBoolean Write(float f); RWBoolean Write(double d);

Writes the appropriate built-in type. Returns TRUE if the write is successful.

RWBoolean Write(const char* i, size_t count); RWBoolean Write(const wchar_t* i, size_t count); RWBoolean Write(const short* i, size_t count); RWBoolean Write(const int* i, size_t count); RWBoolean Write(const long* i, size_t count); RWBoolean Write(const unsigned char* i, size_t count); RWBoolean Write(const unsigned short* i,size_t count); RWBoolean Write(const unsigned int* i, size_t count); RWBoolean Write(const unsigned long* i, size_t count); RWBoolean Write(const float* i, size_t count); RWBoolean Write(const double* i, size_t count);

Writes count instances of the indicated built-in type from a block pointed to by i. Returns TRUE if the write is successful.

RWBoolean Write(const char* string);

Writes a character string, including the terminating null character, from a block pointed to by string. Returns TRUE if the write is successful. Beware of non-terminated strings when using this function.

Static Public Member Functions

static RWBoolean Exists(const char* filename, int mode = F_OK);

Returns TRUE if a file with name filename exists and may be accessed according to the mode specified. The mode may be ORed together from one or more of:

F_OK: "Exists" (Implied by any of the others)

X_OK: "Executable or searchable"

W_OK: "Writable"

R_OK: "Readable"

If your compiler or operating system does not support the POSIX access() function, then mode X_OK will always return FALSE.

RWFileManager

RWFileManager ->- RWFile 

Synopsis

typedef long     RWoffset ;
 
typedef unsigned long   RWspace;  // (typically)
#include <rw/filemgr.h>
RWFileManager f("file.dat");

Description

Class RWFileManager allocates and deallocates storage in a disk file, much like a "freestore" manager. It does this by maintaining a linked list of free space within the file.


Note: Class RWFileManager inherits class RWFile as a public base class; hence all the public member functions of RWFile are visible to RWFileManager. They are not listed here.

If a file is managed by an RWFileManager then reading or writing to unallocated space in the file will have undefined results. In particular, overwriting the end of allocated space is a common problem which usually results in corrupted data. One way to encounter this problem is to use binaryStoreSize() to discover the amount of space needed to store an RWCollection. For most purposes, the storage size of an RWCollection is found using the RWCollectable method recursiveStoreSize().

Persistence

None

Public Constructor

RWFileManager(const char* filename, const char* mode = 0);

Constructs an RWFileManager for the file with path name filename using mode mode. The mode is as given by the Standard C library function fopen(). If mode is zero (the default) then the constructor will attempt to open an existing file with the given filename for update (mode "rb+"). If this is not possible, then it will attempt to create a new file with the given filename (mode "wb+"). If the file exists and is not empty, then the constructor assumes it contains an existing file manager; other contents will cause an exception of type RWExternalErr to be thrown. If no file exists or if an existing file is empty, then the constructor will attempt to create the file (if necessary) and initialize it with a new file manager. The resultant object should be checked for validity using function isValid(). A possible exception that could occur is RWFileErr.

Public Member Functions

RWoffset allocate(RWspace s);

Allocates s bytes of storage in the file. Returns the offset to the start of the storage location. The very first allocation for the file is considered "special" and can be returned at any later time by the function start(). A possible exception that could occur is RWFileErr.

void deallocate(RWoffset t);

Deallocates (frees) the storage space starting at offset t. This space must have been previously allocated by a call to allocate(). The very first allocation ever made in the file is considered "special" and cannot be deallocated. A possible exception that could occur is RWFileErr.

RWoffset endData();

Returns an offset just past the end of the file.

RWoffset start();

Returns the offset of the first space ever allocated for data in this file. If no space has ever been allocated, returns RWNIL. This is typically used to "get started" and find the rest of the data in the file.

RWGBitVec(size)

Synopsis

#include <rw/gbitvec.h>
 
declare(RWGBitVec,size)
RWGBitVec(size) a;

Description

RWGBitVec(size) is a bit vector of fixed length size. The length cannot be changed dynamically (see class RWBitVec if you need a bit vector whose length can be changed at run time). Objects of type RWGBitVec(size) are declared with macros defined in the standard C++ header file <generic.h>.

Bits are numbered from 0 through size-1, inclusive.

Persistence

None

Example

In this example, a bit vector 24 bits long is declared and exercised:

#include "rw/gbitvec.h"
#include <iostream.h>
 
const int VECSIZE = 8;
 
declare(RWGBitVec, VECSIZE)   // declare a 24 bit long vector
implement(RWGBitVec, VECSIZE) // implement the vector
 
main()
{ 
  RWGBitVec(VECSIZE) a, b;   // Allocate two vectors.
 
  a(2) = TRUE;               // Set bit 2 (the third bit) of a on.
  b(3) = TRUE;               // Set bit 3 (the fourth bit) of b on.
 
  RWGBitVec(VECSIZE) c = a ^ b;  // Set c to the XOR of a and b.
 
  cout << "Vector 1" << "\t" << "Vector 2" << "\t"
       << "Vector 1 xor Vector 2" << endl;
 
  for(int i = 0; i < VECSIZE; i++)
    cout << a[i] << "\t\t" << b[i] << "\t\t" << c[i] << endl;
 
  return 0;
}

Public Constructors

RWGBitVec(size)();

Construct a bit vector size elements long, with all bits initialized to FALSE.

RWGBitVec(size)(RWBoolean f);

Construct a bit vector size elements long, with all bits initialized to f.

Assignment Operators

RWGBitVec(sz)& operator=(const RWGBitVec(sz)& v);

Set each element of self to the corresponding bit value of v. Return a reference to self.

RWGBitVec(sz)& operator=(RWBoolean f);

Set all elements of self to the boolean value f.

RWGBitVec(sz)& operator&=(const RWGBitVec(sz)& v); RWGBitVec(sz)& operator^=(const RWGBitVec(sz)& v); RWGBitVec(sz)& operator|=(const RWGBitVec(sz)& v);

Logical assignments. Set each element of self to the logical AND, XOR, or OR, respectively, of self and the corresponding bit in v.

Indexing Operators

RWBitRef operator[](size_t i);

Returns a reference to the ith bit of self. This reference can be used as an lvalue. The index i must be between 0 and size-1, inclusive. Bounds checking will occur.

RWBitRef operator()(size_t i);

Returns a reference to the ith bit of self. This reference can be used as an lvalue. The index i must be between 0 and size-1, inclusive. No bounds checking is done.

Public Member Functions

void clearBit(size_t i);

Clears (i.e., sets to FALSE) the bit with index i. The index i must be between 0 and size-1. No bounds checking is performed. The following are equivalent, although clearBit(size_t) is slightly smaller and faster than using operator()(size_t):

a(i) = FALSE;

a.clearBit(i);

const RWByte* data() const;

Returns a const pointer to the raw data of self. Should be used with care.

void setBit(size_t i);

Sets (i.e., sets to TRUE) the bit with index i. The index i must be between 0 and size-1. No bounds checking is performed. The following are equivalent, although setBit(size_t) is slightly smaller and faster than using operator()(size_t):

a(i) = TRUE;

a.setBit(i);

RWBoolean testBit(size_t i) const;

Tests the bit with index i. The index i must be between 0 and size-1. No bounds checking is performed. The following are equivalent, although testBit(size_t) is slightly smaller and faster than using operator()(size_t):

if( a(i) ) doSomething();

if( a.testBit(i) ) doSomething();

Related Global Functions

RWGBitVec(sz) operator&(const RWGBitVec(sz)& v1, const RWGBitVec(sz)& v2); RWGBitVec(sz) operator^(const RWGBitVec(sz)& v1, const RWGBitVec(sz)& v2); RWGBitVec(sz) operator|(const RWGBitVec(sz)& v1, const RWGBitVec(sz)& v2);

Return the logical AND, XOR, and OR, respectively, of vectors v1 and v2.

RWBoolean operator==(const RWGBitVec(sz)& v1, const RWGBitVec(sz)& v2) const;

Returns TRUE if each bit of v1 is set to the same value as the corresponding bit in v2. Otherwise, returns FALSE.

RWBoolean operator!=(const RWGBitVec(sz)& v1, const RWGBitVec(sz)& v2) const;

Returns FALSE if each bit of v1 is set to the same value as the corresponding bit in v2. Otherwise, returns TRUE.

RWGDlist(type)

Synopsis

#include <rw/gdlist.h>
 
declare(RWGDlist, type)
 
RWGDlist(type) a;

Description

Class RWGDlist(type) represents a group of ordered elements of type type, not accessible by an external key. Duplicates are allowed. This class is implemented as a doubly-linked list. Objects of type RWGDlist(type) are declared with macros defined in the standard C++ header file <generic.h>.

In order to find a particular item within the collection, a user-provided global "tester" function is required to test for a "match," definable in any consistent way. This function should have prototype:

RWBoolean yourTesterFunction(const type* c, const void* d);

The argument c is a candidate within the collection to be tested for a match. The argument d is for your convenience and will be passed to yourTesterFunction(). The function should return TRUE if a "match" is found between c and d.

In order to simplify the documentation below, an imaginary typedef

typedef RWBoolean (*yourTester)(const type*, const void*);

has been used for this tester function.

Persistence

None

Example

#include <rw/gdlist.h>
#include <rw/rstream.h>
 
declare(RWGDlist,int)    /* Declare a list of ints */
 
main()  {
  RWGDlist(int) list;    // Define a list of ints
  int *ip;
 
  list.insert(new int(5));   // Insert some ints
  list.insert(new int(7));
  list.insert(new int(1));
  list.prepend(new int(11));
 
  RWGDlistIterator(int) next(list);
 
  while(ip = next() )
    cout << *ip << endl;   // Print out the members
 
  while(!list.isEmpty())
    delete list.get();     // Remove & delete list items
 
  return 0;
}

Program Output:

11
5
7
1

Public Constructors

RWGDlist(type)();

Construct an empty collection.

RWGDlist(type)(type* a);

Construct a collection with one entry a.

RWGDlist(type)(const RWGDlist(type)& a);

Copy constructor. A shallow copy of a is made.

Assignment Operator

void operator=(const RWGDlist(type)& a);

Assignment operator. A shallow copy of a is made.

Public Member Functions

type* append(type* a);

Adds an item to the end of the collection. Returns nil if the insertion was unsuccessful.

void apply(void (*ap)(type*, void*), void* );

Visits all the items in the collection in order, from first to last, calling the user-provided function pointed to by ap for each item. This function should have prototype:

void yourApplyFunction(type* c, void*);

and can perform any operation on the object at address c. The last argument is useful for passing data to the apply function.

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

Returns a pointer to the ith item in the collection. 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 TOOL_INDEX will be thrown.

void clear();

Removes all items in the collection.

RWBoolean contains(yourTester t, const void* d) const;

Returns TRUE if the collection contains an item for which the user-defined function pointed to by t finds a match with d.

RWBoolean containsReference(const type* e) const;

Returns TRUE if the collection contains an item with the address e.

size_t entries() const;

Returns the number of items in the collection.

type*find(yourTester t, const void* d) const;

Returns the first item in the collection for which the user-provided function pointed to by t finds a match with d, or nil if no item is found.

type* findReference(const type* e) const;

Returns the first item in the collection with the address e, or nil if no item is found.

type* first() const;

Returns the first item of the collection.

type*get();

Returns and removes the first item of the collection.

type* insert(type* e);

Adds an item to the end of the collection and returns it. Returns nil if the insertion was unsuccessful.

void insertAt(size_t indx, type* e);

Adds a new item to the collection at position indx. The item previously at position i is moved to i+1, etc. The index indx must be between 0 and the number of items in the collection, or an exception of type TOOL_INDEX will be thrown.

RWBoolean isEmpty() const;

Returns TRUE if the collection is empty, otherwise FALSE.

type* last() const;

Returns the last item of the collection.

size_t occurrencesOf(yourTester t, const void* d) const;

Returns the number of occurrences in the collection for which the user-provided function pointed to by t finds a match with d.

size_t occurrencesOfReference(const type* e) const;

Returns the number of items in the collection with the address e.

type* prepend(type* a);

Adds an item to the beginning of the collection. Returns nil if the insertion was unsuccessful.

type* remove(yourTester t, const void* d);

Removes and returns the first item from the collection for which the user-provided function pointed to by t finds a match with d, or returns nil if no item is found.

type* removeReference(const type* e);

Removes and returns the first item from the collection with the address e, or returns nil if no item is found.

RWGDlistIterator(type)

Synopsis

#include <rw/gdlist.h>
 
declare(RWGDlist, type)
 
RWGDlist(type) a;
RWGDlistIterator(type) I(a) ;

Description

Iterator for class RWGDlist(type), which allows sequential access to all the elements of a doubly-linked list. Elements are accessed in order, in either direction.

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

In order to simplify the documentation below, an imaginary typedef

typedef RWBoolean (*yourTester)(const type*, const void*);

has been used. See the documentation for class RWGDlist(type) for an explanation of this function.

Persistence

None

Example

See class RWGDlist(type)

Public Constructor

RWGDlistIterator(type)( RWGDlist(type)& list);

Construct an iterator for the RWGDlist(type)list. Immediately after construction, the position of the iterator is undefined.

Public Member Operators

type*operator()();

Advances the iterator to the next item and returns it. Returns nil if at the end of the collection.

void operator++();

Advances the iterator one item.

void operator--();

Moves the iterator back one item.

void operator+=(size_t n);

Advances the iterator n items.

void operator-=(size_t n);

Moves the iterator back n items.

Public Member Functions

RWBoolean atFirst() const;

Returns TRUE if the iterator is at the start of the list, FALSE otherwise;

RWBoolean atLast() const;

Returns TRUE if the iterator is at the end of the list, FALSE otherwise;

type*findNext(yourTester t,const type* d);

Moves the iterator to the next item for which the function pointed to by t finds a match with d and returns it. Returns nil if no match is found, in which case the position of the iterator will be undefined.

type* findNextReference(const type* e);

Moves the iterator to the next item with the address e and returns it. Returns nil if no match is found, in which case the position of the iterator will be undefined.

type* insertAfterPoint(type* a);

Adds item a after the current iterator position and return the item. The position of the iterator is left unchanged.

type* key() const;

Returns the item at the current iterator position.

type*remove();

Removes and returns the item at the current cursor position. Afterwards, the iterator will be positioned at the previous item in the list.

type*removeNext(yourTester t, const type* d);

Moves the iterator to the next item for which the function pointed to by t finds a "match" with d and removes and returns it. Returns nil if no match is found, in which case the position of the iterator will be undefined.

type* removeNextReference(const type* a);

Moves the iterator to the next item with the address e and removes and returns it. Returns nil if no match is found, in which case the position of the iterator will be undefined.

void reset();

Resets the iterator to its initial state.

void toFirst();

Moves the iterator to the first item in the list.

void toLast();

Moves the iterator to the last item in the list.

RWGOrderedVector(val)

Synopsis

#include <rw/gordvec.h>
 
declare(RWGVector,val)
declare(RWGOrderedVector,val)
implement(RWGVector,val)
implement(RWGOrderedVector,val)
 
RWGOrderedVector(val) v;// Ordered vector of objects of val val.

Description

Class RWGOrderedVector(val) represents an ordered collection of objects of val val. Objects are ordered by the order of insertion and are accessible by index. Duplicates are allowed. RWGOrderedVector(val) is implemented as a vector, using macros defined in the standard C++ header file <generic.h>.

Note that it is a value-based collection: items are copied in and out of the collection.

The class val must have:

  • a default constructor;

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

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

  • well-defined equality semantics (val::operator==(const val&) or equiv.).

To use this class you must declare and implement its base class as well as the class itself. For example, here is how you declare and implement an ordered collection of doubles:

declare(RWGVector,double)                   // Declare base class
declare(RWGOrderedVector,double)  // Declare ordered vector
 
// In one and only one .cpp file you must put the following:
implement(RWGVector,double)             // Implement base class
implement(RWGOrderedVector,double)      // Implement ordered vector
 

For each val of RWGOrderedVector you must include one (and only one) call to the macro implement somewhere in your code for both the RWGOrderedVector itself and for its base class RWGVector.

Persistence

None

Example

Here's an example that uses an ordered vector of RWCStrings.

#include <rw/gordvec.h>
#include <rw/cstring.h>
#include <rw/rstream.h>
 
declare(RWGVector,RWCString)
declare(RWGOrderedVector,RWCString)
implement(RWGVector,RWCString)
implement(RWGOrderedVector,RWCString)
 
main()  {
  RWGOrderedVector(RWCString) vec;
 
  RWCString one("First");
  vec.insert(one);
 
  vec.insert("Second");   // Automatic val conversion occurs
  vec.insert("Last");     // Automatic val conversion occurs
 
  for(size_t i=0; i<vec.entries(); i++)  cout << vec[i] << endl;
 
  return 0;
}

Program Output:

First
Second
Last

Public Constructors

RWGOrderedVector(val)(size_t capac=RWDEFAULT_CAPACITY);

Construct an ordered vector of elements of val val. The initial capacity of the vector will be capac whose default value is RWDEFAULT_CAPACITY. The capacity will be automatically increased as necessary should too many items be inserted, a relatively expensive process because each item must be copied into the new storage.

Public Member Functions

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

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

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

Return the ith value in the vector. The index i must be between 0 and one less than the number of items in the vector. Bounds checking will be performed. The second variant can be used as an lvalue, the first cannot.

void clear();

Remove all items from the collection.

const val* data() const;

Returns a pointer to the raw data of self. Should be used with care.

size_t entries() const;

Return the number of items currently in the collection.

size_t index(val item) const;

Perform a linear search of the collection returning the index of the first item that isEqual to the argument item. If no item is found, then it returns RW_NPOS.

void insert(val item);

Add the new value item to the end of the collection.

void insertAt(size_t indx, val item);

Add the new value item to the collection at position indx. The value of indx must be between zero and the length of the collection. No bounds checking is performed. Old items from index indx upwards will be shifted to higher indices.

RWBoolean isEmpty() const;

Returns TRUE if the collection has no entries. FALSE otherwise.

void size_tlength() const;

Synonym for entries().

valpop();

Removes and returns the last item in the vector.

voidpush(val);

Synonym for insert().

removeAt(size_t indx);

Removes the item at position indx from the collection. The value of indx must be between zero and one less than the length of the collection. No bounds checking is performed. Old items from index indx+1 will be shifted to lower indices. E.g., the item at index indx+1 will be moved to position indx, etc.

void resize(size_t newCapacity);

Change the capacity of the collection to newCapacity, which must be at least as large as the present number of items in the collection. Note that the actual number of items in the collection does not change, just the capacity.

RWGQueue(type)

Synopsis

#include <rw/gqueue.h>
 
declare(RWGQueue, type)
 
RWGQueue(type) a ;

Description

Class RWGQueue(type) represents a group of ordered elements, not accessible by an external key. A RWGQueue(type) is a first in, first out (FIFO) sequential list for which insertions are made at one end (the "tail"), but all removals are made at the other (the "head"). Hence, the ordering is determined externally by the ordering of the insertions. Duplicates are allowed. This class is implemented as a singly-linked list. Objects of type RWGQueue(type) are declared with macros defined in the standard C++ header file <generic.h>.

In order to find a particular item within the collection, a user-provided global "tester" function is required to test for a "match", definable in any consistent way. This function should have prototype:

RWBoolean yourTesterFunction(const type* c, const void* d);

The argument c is a candidate within the collection to be tested for a match. The argument d is for your convenience and will be passed to yourTesterFunction(). The function should return TRUE if a "match" is found between c and d.

In order to simplify the documentation below, an imaginary typedef

typedef RWBoolean (*yourTester)(const type*, const void*);

has been used for this tester function.

Persistence

None

Public Constructors

RWGQueue(type)();

Construct an empty queue.

RWGQueue(type)(type* a);

Construct a queue with one entry a.

RWGQueue(type)(const RWGQueue(type)& q);

Copy constructor. A shallow copy of q is made.

Assignment Operator

void operator=(const RWGQueue(type)& q);

Assignment operator. A shallow copy of q is made.

Public Member Functions

type* append(type* a);

Adds a to the end of the queue and returns it. Returns nil if the insertion was unsuccessful.

void clear();

Removes all items from the queue.

RWBoolean contains(yourTester t, const void* d) const;

Returns TRUE if the queue contains an item for which the user-defined function pointed to by t finds a match with d.

RWBoolean containsReference(const type* e) const;

Returns TRUE if the queue contains an item with the address e.

size_t entries() const;

Returns the number of items in the queue.

type* first() const;

Returns the first item in the queue, or nil if the queue is empty.

type* get();

Returns and removes the first item in the queue. Returns nil if the queue is empty.

RWBoolean isEmpty() const;

Returns TRUE if the queue is empty, otherwise FALSE.

type* insert(type* a);

Calls append(type*) with a as the argument.

type* last();

Returns the last (most recently inserted) item in the queue, or nil if the queue is empty.

size_t occurrencesOf(yourTester t, const void* d) const;

Returns the number of items in the queue for which the user-provided function pointed to by t finds a match with d.

size_t occurrencesOfReference(const type* e) const;

Returns the number of items in the queue with the address e.

RWGSlist(type)

Synopsis

#include <rw/gslist.h>
 
declare(RWGSlist, type)
 
RWGSlist(type) a ;
 

Description

Class RWGSlist(type) represents a group of ordered elements of type type, not accessible by an external key. Duplicates are allowed. This class is implemented as a singly-linked list. Objects of type RWGSlist(type) are declared with macros defined in the standard C++ header file <generic.h>.

In order to find a particular item within the collection, a user-provided global "tester" function is required to test for a "match," definable in any consistent way. This function should have prototype:

RWBoolean yourTesterFunction(const type* c, const void* d);

The argument c is a candidate within the collection to be tested for a match. The argument d is for your convenience and will be passed to yourTesterFunction(). The function should return TRUE if a "match" is found between c and d.

In order to simplify the documentation below, an imaginary typedef

typedef RWBoolean (*yourTester)(const type*, const void*);

has been used for this tester function.

Persistence

None

Public Constructors

RWGSlist(type)();

Construct an empty collection.

RWGSlist(type)(type* a);

Construct a collection with one entry a.

RWGSlist(type)(const RWGSlist(type)& a);

Copy constructor. A shallow copy of a is made.

Assignment Operator

void operator=(const RWGSlist(type)&);

Assignment operator. A shallow copy of a is made.

Public Member Functions

type* append(type* a);

Adds an item to the end of the collection and returns it. Returns nil if the insertion was unsuccessful.

void apply(void (*ap)(type*, void*), void* );

Visits all the items in the collection in order, from first to last, calling the user-provided function pointed to by ap for each item. This function should have prototype:

void yourApplyFunction(type* c, void*);

and can perform any operation on the object at address c. The last argument is useful for passing data to the apply function.

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

Returns a pointer to the ith item in the collection. 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 TOOL_INDEX will be thrown.

void clear();

Removes all items in the collection.

RWBoolean contains(yourTester t, const void* d) const;

Returns TRUE if the collection contains an item for which the user-defined function pointed to by t finds a match with d.

RWBoolean containsReference(const type* e) const;

Returns TRUE if the collection contains an item with the address e.

size_t entries() const;

Returns the number of items in the collection.

type* find(yourTester t, const void* d) const;

Returns the first item in the collection for which the user-provided function pointed to by t finds a match with d, or nil if no item is found.

type* findReference(const type* e) const;

Returns the first item in the collection with the address e, or nil if no item is found.

type* first() const;

Returns the first item of the collection.

type*get();

Returns and removes the first item of the collection.

type* insert(type* e);

Adds an item to the end of the collection and returns it. Returns nil if the insertion was unsuccessful.

void insertAt(size_t indx, type* e);

Adds a new item to the collection at position indx. The item previously at position i is moved to i+1, etc. The index indx must be between 0 and the number of items in the collection, or an exception of type TOOL_INDEX will be thrown.

RWBoolean isEmpty() const;

Returns TRUE if the collection is empty, otherwise FALSE.

type* last() const;

Returns the last item of the collection.

size_t occurrencesOf(yourTester t, const void* d) const;

Returns the number of occurrences in the collection for which the user-provided function pointed to by t finds a match with d.

size_t occurrencesOfReference(const type* e) const;

Returns the number of items in the collection with the address e.

type* prepend(const type* a);

Adds an item to the beginning of the collection and returns it. Returns nil if the insertion was unsuccessful.

type* remove(yourTester t, const void* d);

Removes and returns the first item from the collection for which the user-provided function pointed to by t finds a match with d, or returns nil if no item is found.

type* removeReference(const type* e);

Removes and returns the first item from the collection with the address e, or returns nil if no item is found.

RWGSlistIterator(type)

Synopsis

#include <rw/gslist.h>
 
declare(RWGSlist, type)
 
RWGSlist(type) a ;
RWGSlistIterator(type) I(a);

Description

Iterator for class RWGSlist(type), which allows sequential access to all the elements of a singly-linked list. Elements are accessed in order, first to last.

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

In order to simplify the documentation below, an imaginary typedef

typedef RWBoolean (*yourTester)(const type*, const void*);

has been used. See the documentation for class RWGSlist(type) for an explanation of this function.

Persistence

None

Public Constructor

RWGSlistIterator(type)( RWGSlist(type)& list);

Constructs an iterator for the RWGSlist(type)list. Immediately after construction, the position of the iterator is undefined.

Public Member Operators

type* operator()();

Advances the iterator to the next item and returns it. Returns nil if it is at the end of the collection.

void operator++();

Advances the iterator one item.

void operator+=(size_t n);

Advances the iterator n items.

Public Member Functions

RWBoolean atFirst() const;

Returns TRUE if the iterator is at the start of the list, FALSE otherwise;

RWBoolean atLast() const;

Returns TRUE if the iterator is at the end of the list, FALSE otherwise;

type* findNext(yourTester t,const type* d);

Moves the iterator to the next item for which the function pointed to by t finds a match with d and returns it. Returns nil if no match is found, in which case the position of the iterator will be undefined.

type* findNextReference(const type* e);

Moves the iterator to the next item with the address e and returns it. Returns nil if no match is found, in which case the position of the iterator will be undefined.

type* insertAfterPoint(type* a);

Adds item a after the current iterator position and return the item. The position of the iterator is left unchanged.

type* key() const;

Returns the item at the current iterator position.

type* remove();

Removes and returns the item at the current cursor position. Afterwards, the iterator will be positioned at the previous item in the list. In a singly-linked list, this function is an inefficient operation because the entire list must be traversed, looking for the link before the link to be removed.

type* removeNext(yourTester t, const type* d);

Moves the iterator to the next item for which the function pointed to by t finds a "match" with d and removes and returns it. Returns nil if no match is found, in which case the position of the iterator will be undefined.

type* removeNextReference(const type* e);

Moves the iterator to the next item with the address e and removes and returns it. Returns nil if no match is found, in which case the position of the iterator will be undefined.

void reset();

Resets the iterator to its initial state.

void toFirst();

Moves the iterator to the start of the list.

void toLast();

Moves the iterator to the end of the list.

RWGSortedVector(val)

RWGSortedVector(val) ->- RWGVector(val) 

Synopsis

#include <rw/gsortvec.h>
 
declare(RWGSortedVector,val)
implement(RWGSortedVector, val)
RWGSortedVector(val) v;    // A sorted vector of vals .

Description

Class RWGSortedVector(val) represents a vector of elements of val val, sorted using an insertion sort. The elements can be retrieved using an index or a search. Duplicates are allowed. Objects of val RWGSortedVector(val) are declared with macros defined in the standard C++ header file <generic.h>.

Note that it is a value-based collection: items are copied in and out of the collection.

The class val must have:

  • a default constructor;

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

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

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

  • well-defined less-than semantics (val::operator<(const val&) or equiv.).

To use this class you must declare and implement its base class as well as the class itself. For example, here is how you declare and implement a sorted collection of doubles:

declare(RWGVector,double)                    // Declare base class
 
declare(RWGSortedVector,double)   // Declare sorted vector
 
// In one and only one .cpp file you must put the following:
implement(RWGVector,double)         // Implement base class
implement(RWGSortedVector,double)   // Implement sorted vector

For each val of RWGSortedVector you must include one (and only one) call to the macro implement somewhere in your code for both the RWGSortedVector itself and for its base class RWGVector.

Insertions and retrievals are done using a binary search. Note that the constructor of an RWGSortedVector(val) requires a pointer to a "comparison function." This function should have protoval:

int comparisonFunction(const val* a, const val* b);

and should return an int less than, greater than, or equal to zero, depending on whether the item pointed to by a is less than, greater than, or equal to the item pointed to by b. Candidates from the collection will appear as a, the key as b.

Persistence

None

Example

Here's an example of a sorted vector of ints:

#include <rw/gsortvec.h>
#include <rw/rstream.h>
 
declare(RWGVector,int)
declare(RWGSortedVector,int)
implement(RWGVector,int)
implement(RWGSortedVector,int)
 
// Declare and define the "comparison function":
int compFun(const int* a, const int* b)  {
  return *a - *b;
}
 
main()  {
  // Declare and define an instance,
  // using the comparison function 'compFun':
  RWGSortedVector(int) avec(compFun);
 
  // Do some insertions:
  avec.insert(3);          // 3
  avec.insert(17);         // 3 17
  avec.insert(5);          // 3  5 17
 
  cout << avec(1);         // Prints '5'
  cout << avec.index(17);  // Prints '2'
}

Public Constructors

RWGSortedVector(val)( int (*f)(const val*, const val*) );

Construct a sorted vector of elements of val val, using the comparison function pointed to by f. The initial capacity of the vector will be set by the value RWDEFAULT_CAPACITY. The capacity will automatically be increased should too many items be inserted.

RWGSortedVector(val)(int (*f)(const val*, const val*), size_t N);

Construct a sorted vector of elements of val val, using the comparison function pointed to by f. The initial capacity of the vector will be N. The capacity will automatically be increased should too many items be inserted.

Public Member Functions

val operator()(size_t i) const;

Return the ith value in the vector. The index i must be between 0 and the length of the vector less one. No bounds checking is performed.

val operator[](size_t i) const;

Return the ith value in the vector. The index i must be between 0 and the length of the vector less one. Bounds checking is performed.

size_t entries() const;

Returns the number of items currently in the collection.

size_t index(val v);

Return the index of the item with value v. The value "RW_NPOS" is returned if the value does not occur in the vector. A binary search, using the comparison function, is done to find the value. If duplicates are present, the index of the first instance is returned.

RWBoolean insert(val v);

Insert the new value v into the vector. A binary search, using the comparison function, is performed to determine where to insert the value. The item will be inserted after any duplicates. If the insertion causes the vector to exceed its capacity, it will automatically be resized by an amount given by RWDEFAULT_RESIZE.

void removeAt(size_t indx);

Remove the item at position indx from the collection. The value of indx must be between zero and the length of the collection less one. No bounds checking is performed. Old items from index indx+1 will be shifted to lower indices. E.g., the item at index indx+1 will be moved to position indx, etc.

void resize(size_t newCapacity);

Change the capacity of the collection to newCapacity, which must be at least as large as the present number of items in the collection. Note that the actual number of items in the collection does not change, just the capacity.

RWGStack(type)

Synopsis

#include <rw/gstack.h>
 
declare(RWGStack,type)
 
RWGStack(type) a ;

Description

Class RWGStack(type) represents a group of ordered elements, not accessible by an external key. A RWGStack(type) is a last in, first out (LIFO) sequential list for which insertions and removals are made at the beginning of the list. Hence, the ordering is determined externally by the ordering of the insertions. Duplicates are allowed. This class is implemented as a singly-linked list. Objects of type RWGStack(type) are declared with macros defined in the standard C++ header file <generic.h>.

In order to find a particular item within the collection, a user-provided global "tester" function is required to test for a "match," definable in any consistent way. This function should have prototype:

RWBoolean yourTesterFunction(const type* c, const void* d);

The argument c is a candidate within the collection to be tested for a match. The argument d is for your convenience and will be passed to yourTesterFunction(). The function should return TRUE if a "match" is found between c and d.

In order to simplify the documentation below, an imaginary typedef

typedef RWBoolean (*yourTester)(const type*, const void*);

has been used for this tester function.

Persistence

None

Public Constructors

RWGStack(type)();

Constructs an empty stack.

RWGStack(type)(type* a);

Constructs a stack with one entry a.

RWGStack(type)(const RWGStack(type)& a);

Copy constructor. A shallow copy of a is made.

Assignment Operator

void operator=(const RWGStack(type)& a);

Assignment operator. A shallow copy of a is made.

Public Member Functions

void clear();

Removes all items from the stack.

RWBoolean contains(yourTester t, const void* d) const;

Returns TRUE if the stack contains an item for which the user-defined function pointed to by t finds a match with d.

RWBoolean containsReference(const type* e) const;

Returns TRUE if the stack contains an item with the address e.

size_t entries() const;

Returns the number of items in the stack.

RWBoolean isEmpty() const;

Returns TRUE if the stack is empty, otherwise FALSE.

size_t occurrencesOf(yourTester t, const void* d) const;

Returns the number of items in the stack for which the user-provided function pointed to by t finds a match with d.

size_t occurrencesOfReference(const type* e) const;

Returns the number of items in the stack with the address e.

type* pop();

Removes and returns the item at the top of the stack, or returns nil if the stack is empty.

void push(type* a);

Adds an item to the top of the stack.

type*top() const;

Returns the item at the top of the stack or nil if the stack is empty.

RWGVector(val)

Synopsis

#include <rw/gvector.h>
 
declare(RWGVector,val)
implement(RWGVector,val)
 
RWGVector(val) a;   // A Vector of val's.

Description

Class RWGVector(val) represents a group of ordered elements, accessible by an index. Duplicates are allowed. This class is implemented as an array. Objects of type RWGVector(val) are declared with macros defined in the standard C++ header file <generic.h>.

Note that it is a value-based collection: items are copied in and out of the collection.

The class val must have:

  • a default constructor;

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

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

For each type of RWGVector, you must include one (and only one) call to the macro implement, somewhere in your code.

Persistence

None

Example

#include <rw/gvector.h>
 
#include <rw/rwdate.h>
#include <rw/rstream.h>
 
declare(RWGVector, RWDate)   /* Declare a vector of dates */
 
implement(RWGVector, RWDate) /* Implement a vector of dates */
main()  {
  RWGVector(RWDate) oneWeek(7);
  for (int i=1; i<7; i++)
    oneWeek(i) = oneWeek(0) + i;
for (i=0; i<7; i++)
    cout << oneWeek(i) << endl;
return 0;
}

Program Output:

04/12/93
04/13/93
04/14/93
04/15/93
04/16/93
04/17/93
04/18/93

Public Constructors

RWGVector(val)();

Construct an empty vector.

RWGVector(val)(size_t n);

Construct a vector with length n. The initial values of the elements can (and probably will) be garbage.

RWGVector(val)(size_t n, val v);

Construct a vector with length n. Each element is assigned the value v.

RWGVector(val)(RWGVector(val)& s);

Copy constructor. The entire vector is copied, including all embedded values.

Public Member Operators

RWGVector(val)& operator=(RWGVector(val)& s);

Assignment operator. The entire vector is copied.

RWGVector(val)& operator=(val v);

Sets all elements of self to the value v.

valoperator()(size_t i) const; val&operator()(size_t i);

Return the i'th element in the vector. The index i must be between zero and the length of the vector less one. No bounds checking is performed. The second variant can be used as an lvalue.

valoperator[](size_t i) const; val&operator[](size_t i);

Return the ith element in the vector. The index i must be between zero and the length of the vector less one. Bounds checking is performed.

Public Member Functions

const val* data() const;

Returns a pointer to the raw data of self. Should be used with care.

size_t length() const;

Returns the length of the vector.

void reshape(size_t n);

Resize the vector. If the vector shrinks, it will be truncated. If the vector grows, then the value of the additional elements will be undefined.

RWHashDictionary

RWHashDictionary ->- RWSet ->- RWHashTable ->- RWCollection ->-... 
                 ... ->- RWCollectable 

Synopsis

typedef RWHashDictionary Dictionary;  // Smalltalk typedef.
 
#include <rw/hashdict.h>
RWHashDictionary  a ;

Description

An RWHashDictionary represents a group of unordered values, accessible by external keys. Duplicate keys are not allowed. RWHashDictionary is implemented as a hash table of associations of keys and values. Both the key and the value must inherit from the abstract base class RWCollectable, with a suitable definition of the virtual function hash() and isEqual() for the key.

This class corresponds to the Smalltalk class Dictionary.

Persistence

None

Public Constructors

RWHashDictionary(size_t n = RWDEFAULT_CAPACITY);

Construct an empty hashed dictionary using n hashing buckets.

RWHashDictionary(const RWHashDictionary& hd);

Copy constructor. A shallow copy of the collection hd is made.

Public Member Operators

void operator=(const RWHashDictionary& hd);

Assignment operator. A shallow copy of the collection hd is made.

RWBoolean operator<=(const RWHashDictionary& hd) const;

Returns TRUE if for every key-value pair in self, there is a corresponding key in hd that isEqual. Their corresponding values must also be equal.


Note: If you inherit from RWHashDictionary in the presence of the Standard C++ Library, we recommend that you override this operator and explicitly forward the call. Overload resolution in C++ will choose the Standard Library provided global operators over inherited class members. These global definitions are not appropriate for set-like partial orderings.


RWBoolean operator==(const RWHashDictionary& hd) const;

Returns TRUE if self and hd have the same number of entries and if for every key-value pair in self, there is a corresponding key in hd that isEqual. Their corresponding values must also be equal.

Public Member Functions

void applyToKeyAndValue(RWapplyKeyAndValue ap, void* x);

Applies the user-supplied function pointed to by ap to each key-value pair of the collection. Items are not visited in any particular order. An untyped argument may be passed to the ap function through x.

RWBinaryTree asBinaryTree(); RWBag asBag() const; RWSet asOrderedCollection() const; asSet() const; RWOrdered RWBinaryTree asSortedCollection() const;

Converts the RWHashDictionary to an RWBag, RWSet, RWOrdered, or an RWBinaryTree. Note that since a dictionary contains pairs of keys and values, the result of this call will be a container holding RWCollectableAssociations. Note also that the return value is a copy of the data. This can be very expensive for large collections. Consider using operator+=() to insert each RWCollectableAssociation from this dictionary into a collection of your choice.

virtual RWspace binaryStoreSize() const;

Inherited from class RWCollection.

virtual void clear();

Redefined from class RWCollection. Removes all key-value pairs in the collection.

virtual void clearAndDestroy();

Redefined from class RWCollection. Removes all key-value pairs in the collection, and deletes the key and the value.

virtual int compareTo(const RWCollectable* a) const;

Inherited from class RWCollectable.

virtual RWBoolean contains(const RWCollectable* target) const;

Inherited from class RWCollection.

virtual size_t entries() const;

Inherited from class RWSet.

virtual RWCollectable* find(const RWCollectable* target) const;

Redefined from class RWCollection. Returns the key which isEqual to the object pointed to by target, or nil if no key was found.

RWCollectable* findKeyAndValue(const RWCollectable* target, RWCollectable*& v) const;

Returns the key which isEqual to the item pointed to by target, or nil if no key was found. The value is put in v. You are responsible for defining v before calling this function.

RWCollectable* findValue(const RWCollectable* target) const;

Returns the value associated with the key which isEqual to the item pointed to by target, or nil if no key was found.

RWCollectable* findValue(const RWCollectable* target, RWCollectable* newValue);

Returns the value associated with the key which isEqual to the item pointed to by target, or nil if no key was found. Replaces the value with newValue (if a key was found).

virtual unsigned hash() const;

Inherited from class RWCollectable.

RWCollectable* insertKeyAndValue(RWCollectable* key,RWCollectable* value);

Adds a key-value pair to the collection and returns the key if successful, nil if the key is already in the collection.

virtual RWClassID isA() const;

Redefined from class RWCollectable to return __RWHASHDICTIONARY.

virtual RWBoolean isEmpty() const;

Inherited from class RWSet.

virtual RWBoolean isEqual(const RWCollectable* a) const;

Inherited from class RWCollectable.

virtual size_t occurrencesOf(const RWCollectable* target) const;

Inherited from class RWSet. Returns the number of keys which isEqual to the item pointed to by target. Because duplicates are not allowed, this function can only return 0 or 1.

virtual RWCollectable* remove(const RWCollectable* target);

Redefined from class RWCollection. Removes the key and value pair where the key isEqual to the item pointed to by target. Returns the key, or nil if no match was found.

virtual void removeAndDestroy(const RWCollectable* target);

Redefined from class RWCollection. Removes and deletes the key and value pair where the key isEqual to the item pointed to by target. Note that both the key and the value are deleted. Does nothing if the key is not found.

RWCollectable* removeKeyAndValue(const RWCollectable* target, RWCollectable*& v);

Removes the key and value pair where the key isEqual to the item pointed to by target. Returns the key, or nil if no match was found. The value part of the removed pair is put in v. You are responsible for defining v before calling this function.

void resize(size_t n = 0);

Inherited from class RWSet.

virtual void restoreGuts(RWvistream&); virtual void restoreGuts(RWFile&); virtual void saveGuts(RWvostream&) const; virtual void saveGuts(RWFile&) const;

Inherited from class RWCollection.

virtual RWCollection* select(RWtestCollectable testfunc, void* x) const;

Evaluates the function pointed to by tst for the key of each item in the RWHashDictionary. It inserts keys and values for which the function returns TRUE into a new RWHashDictionary allocated off the heap and returns a pointer to this new collection. Because the new dictionary is allocated off the heap, you are responsible for deleting it when done. This is a virtual function which hides the non-virtual function inherited from RWCollection.

virtual RWCollection* select(RWtestCollectablePair testfunc, void* x) const;

Evaluates the function pointed to by tst for both the key and the value of each item in the RWHashDictionary. It inserts keys and values for which the function returns TRUE into a new RWHashDictionary allocated off the heap and returns a pointer to this new collection. Because the new dictionary is allocated off the heap, you are responsible for deleting it when done. This is a virtual function which hides the non-virtual function inherited from RWCollection.

RWStringID stringID();

(acts virtual) Inherited from class RWCollectable.

RWHashDictionaryIterator

RWHashDictionaryIterator ->- RWIterator 

Synopsis

#include <rw/hashdict.h>
 
RWHashDictionary hd;
RWHashDictionaryIterator  iter(hd);

Description

Iterator for class RWHashDictionary, allowing sequential access to all the elements of RWHashDictionary. Since RWHashDictionary is unordered, 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

RWHashDictionaryIterator(RWHashDictionary&);

Construct an iterator for an RWHashDictionary collection. Immediately after construction, the position of the iterator is undefined until positioned.

Public Member Operator

virtual RWCollectable* operator()();

Redefined from class RWIterator. Advances the iterator to the next key-value pair and returns the key. Returns nil if the cursor is at the end of the collection. Use member function value() to recover the value.

Public Member Functions

virtual RWCollectable* findNext(const RWCollectable* target);

Redefined from class RWIterator. Moves the iterator to the next key-value pair where the key isEqual to the object pointed to by target. Returns the key or nil if no key was found.

virtual RWCollectable* key() const;

Redefined from class RWIterator. Returns the key at the current iterator position.

RWCollectable* remove();

Removes the key-value pair at the current iterator position. Returns the key, or nil if there was no key-value pair.

RWCollectable* removeNext(const RWCollectable* target);

Moves the iterator to the next key-value pair where the key isEqual to the object pointed to by target. Removes the key-value pair, returning the key or nil if there was no match.

virtual void reset();

Redefined from class RWIterator. Inherited from class RWSetIterator. Resets the iterator to its initial state.

RWCollectable* value() const;

Returns the value at the current iterator position.

RWCollectable* value(RWCollectable* newValue) const;

Replaces the value at the current iterator position and returns the old value.

rw_hashmap

Synopsis

#include <rw/rwstl/hashmap.h>
rw_hashmap<K,V,Hash,EQ> map;

Description

Class rw_hashmap<K,V,Hash,EQ> maintains a collection of mappings between K and V, implemented as a hash table of pair<const K,V> . Pairs with duplicate keys are not allowed. Two pairs having duplicate keys is the result of the EQ comparison, applied to the first element of each, is TRUE. Since this is a value based collection, objects are copied into and out of the collection. As with all classes that meet the ANSI associative container specification, rw_hashmap provides for iterators that reference its elements. Operations that alter the contents of rw_hashmap may invalidate other iterators that reference the container. Since the contents of rw_hashmap are in pseudo-random order, the only iterator ranges that will usually make sense are the results of calling equal_range(key), and the entire range from begin() to end().

Persistence

None

Public Typedefs

typedef K key_type; typedef Hash key_hash; typedef EQ key_equal; typedef pair<K,V> value_type; // or ... "const K" typedef (unsigned) size_type; //from rw_slist typedef (int) difference_type; // from rw_slist typedef (value_type&) reference; typedef (const value_type&) const_reference; //from rw_slist

Iterators over rw_hashmap<K,V,Hash,EQ> are forward iterators.

typedef (scoped Iterator) iterator; typedef (scoped ConstIterator) const_iterator;

Public Constructors

rw_hashmap<K,V,Hash,EQ>(size_type sz = 1024, const Hash& h = Hash(), const EQ& eq = EQ());

Construct an empty rw_hashmap<K,V,Hash,EQ> with sz slots, using h as the hash object, and eq as the equality comparator.

rw_hashmap<K,V,Hash,EQ>(const rw_hashmap<K,V,Hash,EQ>& map);

Construct an rw_hashmap<K,V,Hash,EQ> which is a copy of map. Each element from map will be copied into self.

rw_hashmap<K,V,Hash,EQ>(const_iterator first, const_iterator bound size_type sz=1024, const Hash& h = Hash(), const EQ& eq = EQ());

Construct an rw_hashmap<K,V,Hash,EQ> with sz slots, using h as the hash object, and eq as the equality comparator, containing a copy of each pair referenced by the range starting with first and bounded by bound.

rw_hashmap<K,V,Hash,EQ>(const value_type* first, const value_type* bound size_type sz=1024, const Hash& h = Hash(), const EQ& eq = EQ());

Construct an rw_hashmap<K,V,Hash,EQ> with sz slots, using h as the hash object, and eq as the equality comparator, containing a copy of each pair referenced by the range starting with first and bounded by bound. If there are items in the range for which the K parts of the pairs match EQ, then only the first such item will be inserted into self.

Public Destructor

~rw_hashmap<K,V,Hash,EQ>();

The destructor releases the memory used by the container's implementation.

Public Operators

rw_hashmap<K,V,Hash,EQ>& operator=(const rw_hashmap<K,V,Hash,EQ>& rhs);

Sets self to have the same capacity, Hash and EQ as rhs, removes all self's current contents, and replaces them with copies of the elements in rhs.

bool operator==(const rw_hashmap<K,V,Hash,EQ> & rhs) const;

Returns true if self and rhs have the same number of elements, and for each value_type in self, there is a value_type in rhs that has a first part for which the EQ object in self returns true, and a second part for which operator==() returns true. The need to test both parts means that this operator is slightly slower than the method equal_by_keys() described below.

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

Returns a reference to the V part of a pair held in self which has a part EQ to key, either by finding such a pair, or inserting one (in which case the reference is to an instance of V created by its default constructor).

Accessors

iterator begin();

The iterator returned references the first item in self. If self is empty, the iterator is equal to end(). Note that because items are stored in pseudo-random order, this iterator might reference any item that has been stored in self.

const_iterator begin() const;

The iterator returned references the first item in self. If self is empty, the iterator is equal to end(). Note that because items are stored in pseudo-random order, this iterator might reference any item that has been stored in self.

iterator end();

The iterator returned marks the location "off the end" of self. It may not be dereferenced.

const_iterator end() const;

The iterator returned marks the location "off the end" of self. It may not be dereferenced.

pair<const_iterator, const_iterator> equal_range(const key_type key) const;

Returns pair<const_iterator, const_iterator>(lower_bound(key), upper_bound(key)). Upper and lower bound have special meaning for hash-based collections. See discussion elsewhere.

pair<iterator, iterator> equal_range(const key_type key);

Returns pair<iterator, iterator>(lower_bound(key), upper_bound(key)). Upper and lower bound have special meaning for hash-based collections. See discussion elsewhere.

const_iterator lower_bound(const key_type& key) const;

Returns the lower bound of key in self. This has a special meaning for hash-based collections. See discussion elsewhere.

iterator lower_bound(const key_type& key);

Returns the lower bound of key in self. This has a special meaning for hash-based collections. See discussion elsewhere.

const_iterator upper_bound(const key_type& key) const;

Returns the upper bound of key in self. This has a special meaning for hash-based collections. See discussion elsewhere.

iterator upper_bound(const key_type& key);

Returns the upper bound of key in self. This has a special meaning for hash-based collections. See discussion elsewhere.

Const Public Member Functions

size_type capacity() const;

Returns the number of slots in the hash table that self uses.

bool empty() const;

Returns true if self is empty.

float fill_ratio() const;

Returns the result of calculating size()/capacity().

size_type size() const;

Returns the number of pairs currently held in self.

Mutators

void clear();

A synonym for erase(begin(),end());

size_type erase(const key_type& key);

If there is a pair in self for which the first part is EQ to key, that pair is removed, and 1 is returned. Otherwise, 0 is returned.

iterator erase(iterator iter);

Removes the element referenced by iter and returns an iterator referencing the "next" element. If iter does not reference an item in self, the result is undefined.

iterator erase(iterator first, iterator bound);

Removes each element in the range which begins with first and is bound by bound. Returns an iterator referencing bound. If first does not reference an item in self (and if first and bound are not equal), the effect is undefined.

pair<iterator,bool> insert(const value_type& val);

If there is no pair in self with first part EQ to the first part of val then inserts val, returning a pair with an iterator referencing the new element and true. Otherwise, returns a pair with an iterator referencing the matching value_type and false.

size_type insert(iterator ignore, const value_type& val);

If there is no pair in self with first part EQ to the first part of val then inserts val, returning 1. Otherwise, does nothing and returns 0. Note that the first argument is provided only for conformance with the ANSI associative container specification, and is ignored by the method, since hash table look up can be done in constant time.

size_type insert(const value_type* first, const value_type* bound);

For each element in the range beginning with first and bounded by bound, if there is no pair in self with first part EQ to the first part of that element, the element is copied into self, or if there is such a pair, the element is skipped. Returns the number of elements inserted.

size_type insert(const_iterator first, const_iterator bound);

For each element in the range beginning with first and bounded by bound, if there is no pair in self with first part EQ to the first part of that element, the element is copied into self, or if there is such a pair, the element is skipped. Returns the number of elements inserted.

void swap(rw_hashmap<K,V,Hash,EQ>& other);

Exchanges the contents of self with other including the Hash and EQ objects. This method does not copy or destroy any of the items exchanged but exchanges the underlying hash tables.

Special Methods for Maps

size_type count(const key_type& key) const;

Returns 1 if self contains a pair with its first element EQ to key, else 0.

bool equal_by_keys(const rw_hashmap<K,V,Hash,EQ>& rhs) const;

Returns true if self and rhs have the same size, and if for each value_type in self, there is a value_type in rhs such that the EQ object in self returns true when called for the first parts of those pairs. Note that this method does not compare the V (second) part of the pair of the items, so it will run slightly faster than operator==().

const_iterator find(const key_type& key) const;

Returns a const_iterator referencing the pair with key as its first element if such a pair is contained in self, else returns end().

iterator find(const key_type& key);

Returns an iterator referencing the pair with key as its first element, if such a pair is contained in self, else returns end().

void resize(size_type sz);

Resizes self's hash table to have sz slots; and re-hashes all self's elements into the new table. Can be very expensive if self holds many elements.

rw_hashmultimap

Synopsis

#include <rw/rwstl/hashmmap.h>
rw_hashmultimap<K,V,Hash,EQ> mmap;

Description

Class rw_hashmultimap<K,V,Hash,EQ> maintains a collection of mappings between K and V, implemented as a hash table of pair<const K,V> in which there may be many pairs with the same K instance. Since this is a value based collection, objects are copied into and out of the collection. As with all classes that meet the ANSI associative container specification, rw_hashmap provides for iterators that reference its elements. Operations that alter the contents of rw_hashmap may invalidate other iterators that reference the container. Since the contents of rw_hashmap are in pseudo-random order, the only iterator ranges that will usually make sense are the results of calling equal_range(key), and the entire range from begin() to end().

Persistence

None

Public Typedefs

typedef K key_type; typedef Hash key_hash; typedef EQ key_equal; typedef pair<K,V> value_type; // or ... "const K" typedef (unsigned) size_type; //from rw_slist typedef (int) difference_type; // from rw_slist typedef (value_type&) reference; typedef (const value_type&) const_reference; //from rw_slist

Iterators over rw_hashmultimap<K,V,Hash,EQ> are forward iterators.

typedef (scoped Iterator) iterator; typedef (scoped ConsIterator) const_iterator;

Public Constructors

rw_hashmultimap<K,V,Hash,EQ>(size_type sz = 1024, const Hash& h = Hash(), const EQ& eq = EQ());

Construct an empty rw_hashmultimap<K,V,Hash,EQ> with sz slots, using h as the hash object, and eq as the equality comparator.

rw_hashmultimap<K,V,Hash,EQ>(const rw_hashmultimap<K,V,Hash,EQ>& mmap);

Construct an rw_hashmultimap<K,V,Hash,EQ> which is a copy of mmap. Each element from mmap will be copied into self.

rw_hashmultimap<K,V,Hash,EQ>(const_iterator first, const_iterator bound size_type sz=1024, const Hash& h = Hash(), const EQ& eq = EQ());

Construct an rw_hashmultimap<K,V,Hash,EQ> with sz slots, using h as the hash object, and eq as the equality comparator, containing a copy of each pair referenced by the range starting with first and bounded by bound.

rw_hashmultimap<K,V,Hash,EQ>(const value_type* first, const value_type* bound size_type sz=1024, const Hash& h = Hash(), const EQ& eq = EQ());

Construct an rw_hashmultimap<K,V,Hash,EQ> with sz slots, using h as the hash object, and eq as the equality comparator, containing a copy of each pair referenced by the range starting with first and bounded by bound.

Public Destructor

~rw_hashmultimap<K,V,Hash,EQ>();

The destructor releases the memory used by the container's implementation.

Public Operators

rw_hashmultimap<K,V,Hash,EQ>& operator=(const rw_hashmultimap<K,V,Hash,EQ>& rhs);

Sets self to have the same capacity, Hash and EQ as rhs, removes all self's current contents, and replaces them with copies of the elements in rhs.

bool operator==(const rw_hashmultimap<K,V,Hash,EQ> & rhs) const;

Returns true if self and rhs have the same number of elements, and for each value_type in self, there is exactly one corresponding value_type in rhs that has a first part for which the EQ object in self returns true, and a second part for which operator==() returns true. The need to test both parts, and ensure that the matches are one-to-one means that this operator may be significantly slower than the method equal_by_keys() described below.

Accessors

iterator begin();

The iterator returned references the first item in self. If self is empty, the iterator is equal to end(). Note that because items are stored in pseudo-random order, this iterator might reference any item that has been stored in self.

const_iterator begin() const;

The iterator returned references the first item in self. If self is empty, the iterator is equal to end(). Note that because items are stored in pseudo-random order, this iterator might reference any item that has been stored in self.

iterator end();

The iterator returned marks the location "off the end" of self. It may not be dereferenced.

const_iterator end() const;

The iterator returned marks the location "off the end" of self. It may not be dereferenced.

pair<const_iterator, const_iterator> equal_range(const key_type key) const;

Returns pair<const_iterator,const_iterator>(lower_bound(key), upper_bound(key)). Upper and lower bound have special meaning for hash-based collections. See discussion elsewhere.

pair<iterator, iterator> equal_range(const key_type key);

Returns pair<iterator,iterator>(lower_bound(key), upper_bound(key)). Upper and lower bound have special meaning for hash-based collections. See discussion elsewhere.

const_iterator lower_bound(const key_type& key) const;

Returns the lower bound of key in self. This has a special meaning for hash-based collections. See discussion elsewhere.

iterator lower_bound(const key_type& key);

Returns the lower bound of key in self. This has a special meaning for hash-based collections. See discussion elsewhere.

const_iterator upper_bound(const key_type& key) const;

Returns the upper bound of key in self. This has a special meaning for hash-based collections. See discussion elsewhere.

iterator upper_bound(const key_type& key);

Returns the upper bound of key in self. This has a special meaning for hash-based collections. See discussion elsewhere.

Const Public Member Functions

size_type capacity() const;

Returns the number of slots in the hash table that self uses.

bool empty() const;

Returns true if self is empty.

float fill_ratio() const;

Returns the result of calculating size()/capacity().

size_type size() const;

Returns the number of items currently held in self.

Mutators

void clear();

A synonym for erase(begin(),end());

size_type erase(const key_type& key);

Removes all pairs in self for which the first part is EQ to key, and returns the number of removed elements.

iterator erase(iterator iter);

Removes the element referenced by iter and returns an iterator referencing the "next" element. If iter does not reference an item in self, the result is undefined.

iterator erase(iterator first, iterator bound);

Removes each element in the range which begins with first and is bound by bound. Returns an iterator referencing bound. If first does not reference an item in self (and if first and bound are not equal), the effect is undefined.

pair<iterator,bool> insert(const value_type& val);

Inserts the pair, val, and returns a pair with an iterator referencing the new element and true.

size_type insert(iterator ignore, const value_type& val);

Inserts the pair, val, returning 1. Note that the first argument is provided only for conformance with the ANSI associative container specification, and is ignored by the method, since hash table look up can be done in constant time.

size_type insert(const value_type* first, const value_type* bound);

For each element in the range beginning with first and bounded by bound, the element is copied into self. Returns the number of elements inserted.

size_type insert(const_iterator first, const_iterator bound);

For each element in the range beginning with first and bounded by bound, the element is copied into self. Returns the number of elements inserted.

void swap(rw_hashmultimap<K,V,Hash,EQ>& other);

Exchanges the contents of self with other including the Hash and EQ objects. This method does not copy or destroy any of the items exchanged but exchanges the underlying hash tables.

Special Methods for Multimaps

size_type count(const key_type& key) const;

Returns the number of pairs in self which have keyEQ to their first element.

bool equal_by_keys(const rw_hashmultimap<K,V,Hash,EQ>& rhs) const;

Returns true if self and rhs have the same size, and if for each distinct key_type in self, self and rhs have the same number of pairs with first parts that test EQ to that instance. Note that this method does not compare the V (second) part of the pair of the items, so it will run slightly faster than operator==().

const_iterator find(const key_type& key) const;

Returns a const_iterator referencing some pair with key as its first element, if such a pair is contained in self, else returns end().

iterator find(const key_type& key);

Returns an iterator referencing some pair with key as its first element, if such a pair is contained in self, else returns end().

void resize(size_type sz);

Resizes self's hash table to have sz slots; and re-hashes all self's elements into the new table. Can be very expensive if self holds many elements.

rw_hashmultiset

Synopsis

#include <rw/rwstl/hashmset.h>
rw_hashmultiset<T,Hash,EQ> mset;

Description

Class rw_hashmultiset<T,Hash,EQ> maintains a collection of T, implemented as a hash table in which there may be many EQ instances of T. Since this is a value based collection, objects are copied into and out of the collection. As with all classes that meet the ANSI associative container specification, rw_hashmap provides for iterators that reference its elements. Operations that alter the contents of rw_hashmap may invalidate other iterators that reference the container. Since the contents of rw_hashmap are in pseudo-random order, the only iterator ranges that will usually make sense are the results of calling equal_range(key), and the entire range from begin() to end().

Persistence

None

Public Typedefs

typedef T key_type; typedef T value_type; // or ... "const K" typedef Hash key_hash; typedef EQ key_equal; typedef (unsigned) size_type; //from rw_slist typedef (int) difference_type; // from rw_slist typedef (value_type&) reference; typedef (const value_type&) const_reference; //from rw_slist

Iterators over rw_hashmultiset<T,Hash,EQ> are forward iterators.

typedef (scoped Iterator) iterator; typedef (scoped ConsIterator) const_iterator;

Public Constructors

rw_hashmultiset<T,Hash,EQ>(size_type sz = 1024, const Hash& h = Hash(), const EQ& eq = EQ());

Construct an empty rw_hashmultiset<T,Hash,EQ> with sz slots, using h as the hash object, and eq as the equality comparator.

rw_hashmultiset<T,Hash,EQ>(const rw_hashmultiset<T,Hash,EQ>& mset);

Construct an rw_hashmultiset<T,Hash,EQ> which is a copy of mset. Each element from mset will be copied into self.

rw_hashmultiset<T,Hash,EQ>(const_iterator first, const_iterator bound size_type sz=1024, const Hash& h = Hash(), const EQ& eq = EQ());

Construct an rw_hashmultiset<T,Hash,EQ> with sz slots, using h as the hash object, and eq as the equality comparator, containing a copy of each item referenced by the range starting with first and bounded by bound.

rw_hashmultiset<T,Hash,EQ>(const value_type* first, const value_type* bound size_type sz=1024, const Hash& h = Hash(), const EQ& eq = EQ());

Construct an rw_hashmultiset<T,Hash,EQ> with sz slots, using h as the hash object, and eq as the equals object, containing a copy of each item referenced by the range including first and bounded by bound.

Public Destructor

~rw_hashmultiset<T,Hash,EQ>();

The destructor releases the memory used by the container's implementation.

Public Operators

rw_hashmultiset<T,Hash,EQ>& operator=(const rw_hashmultiset<T,Hash,EQ>& rhs);

Sets self to have the same capacity, Hash and EQ as rhs, removes all self's current contents, and replaces them with copies of the elements in rhs.

bool operator==(const rw_hashmultiset<T,Hash,EQ> & rhs) const;

Returns true if self and rhs have the same number of elements, and for each distinct instance of T in self, both self and rhs have the same count of instances.

Accessors

iterator begin();

The iterator returned references the first item in self. If self is empty, the iterator is equal to end(). Note that because items are stored in pseudo-random order, this iterator might reference any item that has been stored in self.

const_iterator begin() const;

The iterator returned references the first item in self. If self is empty, the iterator is equal to end(). Note that because items are stored in pseudo-random order, this iterator might reference any item that has been stored in self.

iterator end();

The iterator returned marks the location "off the end" of self. It may not be dereferenced.

const_iterator end() const;

The iterator returned marks the location "off the end" of self. It may not be dereferenced.

pair<const_iterator, const_iterator> equal_range(const key_type key) const;

Returns pair<const_iterator, const_iterator>(lower_bound(key), upper_bound(key)). Upper and lower bound have special meaning for hash-based collections. See discussion elsewhere.

pair<iterator, iterator> equal_range(const key_type key);

Returns pair<iterator, iterator>(lower_bound(key), upper_bound(key)). Upper and lower bound have special meaning for hash-based collections. See discussion elsewhere.

const_iterator lower_bound(const key_type& key) const;

Returns the lower bound of key in self. This has a special meaning for hash-based collections. See discussion elsewhere.

iterator lower_bound(const key_type& key);

Returns the lower bound of key in self. This has a special meaning for hash-based collections. See discussion elsewhere.

const_iterator upper_bound(const key_type& key) const;

Returns the upper bound of key in self. This has a special meaning for hash-based collections. See discussion elsewhere.

iterator upper_bound(const key_type& key);

Returns the upper bound of key in self. This has a special meaning for hash-based collections. See discussion elsewhere.

Const Public Member Functions

size_type capacity() const;

Returns the number of slots in the hash table that self uses.

bool empty() const;

Returns true if self is empty.

float fill_ratio() const;

Returns the result of calculating size()/capacity().

size_type size() const;

Returns the number of items currently held in self.

Mutators

void clear();

A synonym for erase(begin(),end());

size_type erase(const key_type& key);

Removes all items in self which are EQ to key, and returns the number of removed elements.

iterator erase(iterator iter);

Removes the element referenced by iter and returns an iterator referencing the "next" element. If iter does not reference an item in self, the result is undefined.

iterator erase(iterator first, iterator bound);

Removes each element in the range which begins with first and is bound by bound. Returns an iterator referencing bound. If first does not reference an item in self (and if first and bound are not equal), the effect is undefined.

pair<iterator,bool> insert(const value_type& val);

Inserts val, returning a pair with an iterator referencing the new element and true.

size_type insert(iterator ignore, const value_type& val);

Inserts val, returning 1. Note that the first argument is provided only for conformance with the ANSI associative container specification, and is ignored by the method, since hash table look up can be done in constant time.

size_type insert(const value_type* first, const value_type* bound);

For each element in the range beginning with first and bounded by bound, the element is copied into self. Returns the number of elements inserted.

size_type insert(const_iterator first, const_iterator bound);

For each element in the range beginning with first and bounded by bound, the element is copied into self. Returns the number of elements inserted.

void swap(rw_hashmultiset<T,Hash,EQ>& other);

Exchanges the contents of self with other including the Hash and EQ objects. This method does not copy or destroy any of the items exchanged but exchanges the underlying hash tables.

Special Methods for Multisets

size_type count(const key_type& key) const;

Returns the number of items in self which are EQ to key.

const_iterator find(const key_type& key) const;

Returns a const_iterator referencing some item EQ to key if such an item is contained in self, else returns end().

iterator find(const key_type& key);

Returns an iterator referencing some item EQ to key if such a item is contained in self, else returns end().

void resize(size_type sz);

Resizes self's hash table to have sz slots; and re-hashes all self's elements into the new table. Can be very expensive if self holds many elements.

rw_hashset

Synopsis

#include <rw/rwstl/hashset.h>
rw_hashset<T,Hash,EQ> set;

Description

Class rw_hashset<T,Hash,EQ> maintains a collection of T, implemented as a hash table in which there may not be more than one instance of any given T. Since this is a value based collection, objects are copied into and out of the collection. As with all classes that meet the ANSI associative container specification, rw_hashset provides for iterators that reference its elements. Operations that alter the contents of rw_hashset may invalidate other iterators that reference the container. Since the contents of rw_hashset are in pseudo-random order, the only iterator ranges that will usually make sense are the results of calling equal_range(key), and the entire range from begin() to end().

Persistence

None

Public Typedefs

typedef T key_type; typedef T value_type; // or ... "const K" typedef Hash key_hash; typedef EQ key_equal; typedef (unsigned) size_type; //from rw_slist typedef (int) difference_type; // from rw_slist typedef (value_type&) reference; typedef (const value_type&) const_reference; //from rw_slist

Iterators over rw_hashset<T,Hash,EQ> are forward iterators.

typedef (scoped Iterator) iterator; typedef (scoped ConsIterator) const_iterator;

Public Constructors

rw_hashset<T,Hash,EQ>(size_type sz = 1024, const Hash& h = Hash(), const EQ& eq = EQ());

Construct an empty rw_hashset<T,Hash,EQ> with sz slots, using h as the hash object, and eq as the equality comparator.

rw_hashset<T,Hash,EQ>(const rw_hashset<T,Hash,EQ>& set);

Construct an rw_hashset<T,Hash,EQ> which is a copy of set. Each element from set will be copied into self.

rw_hashset<T,Hash,EQ>(const_iterator first, const_iterator bound size_type sz=1024, const Hash& h = Hash(), const EQ& eq = EQ());

Construct an rw_hashset<T,Hash,EQ> with sz slots, using h as the hash object, and eq as the equality comparator, containing a copy of each item referenced by the range starting with first and bounded by bound.

rw_hashset<T,Hash,EQ>(const value_type* first, const value_type* bound size_type sz=1024, const Hash& h = Hash(), const EQ& eq = EQ());

Construct an rw_hashset<T,Hash,EQ> with sz slots, using h as the hash object, and eq as the equality comparator, containing a copy of each item referenced by the range starting with first and bounded by bound. If there are items in the range which test EQ, then only the first such item will be inserted into self.

Public Destructor

rw_hashset<T,Hash,EQ>();

The destructor releases the memory used by the container's implementation.

Public Operators

rw_hashset<T,Hash,EQ>& operator=(const rw_hashset<T,Hash,EQ>& rhs);

Sets self to have the same capacity, Hash and EQ as rhs, removes all self's current contents, and replaces them with copies of the elements in rhs.

bool operator==(const rw_hashset<T,Hash,EQ> & rhs) const;

Returns true if self and rhs have the same number of elements, and for each item in self there is an item in rhs which tests EQ.

Accessors

iterator begin();

The iterator returned references the first item in self. If self is empty, the iterator is equal to end(). Note that because items are stored in pseudo-random order, this iterator might reference any item that has been stored in self.

const_iterator begin() const;

The iterator returned references the first item in self. If self is empty, the iterator is equal to end(). Note that because items are stored in pseudo-random order, this iterator might reference any item that has been stored in self.

iterator end();

The iterator returned marks the location "off the end" of self. It may not be dereferenced.

const_iterator end() const;

The iterator returned marks the location "off the end" of self. It may not be dereferenced.

pair<const_iterator, const_iterator> equal_range(const key_type key) const;

Returns pair<const_iterator, const_iterator>(lower_bound(key), upper_bound(key)). Upper and lower bound have special meaning for hash-based collections. See discussion elsewhere.

pair<iterator, iterator> equal_range(const key_type key);

Returns pair<iterator,iterator>(lower_bound(key), upper_bound(key)). Upper and lower bound have special meaning for hash-based collections. See discussion elsewhere.

onst_iterator lower_bound(const key_type& key) const;

Returns the lower bound of key in self. This has a special meaning for hash-based collections. See discussion elsewhere.

iterator lower_bound(const key_type& key);

Returns the lower bound of key in self. This has a special meaning for hash-based collections. See discussion elsewhere.

const_iterator upper_bound(const key_type& key) const;

Returns the upper bound of key in self. This has a special meaning for hash-based collections. See discussion elsewhere.

iterator upper_bound(const key_type& key);

Returns the upper bound of key in self. This has a special meaning for hash-based collections. See discussion elsewhere.

Const Public Member Functions

size_type capacity() const;

Returns the number of slots in the hash table that self uses.

bool empty() const;

Returns true if self is empty.

float fill_ratio() const;

Returns the result of calculating size()/capacity().

size_type size() const;

Returns the number of items currently held in self.

Mutators

void clear();

A synonym for erase(begin(),end());

size_type erase(const key_type& key);

If there is an item EQ to key, it is removed, and 1 is returned. Otherwise, 0 is returned.

iterator erase(iterator iter);

Removes the element referenced by iter and returns an iterator referencing the "next" element. If iter does not reference an item in self, the result is undefined.

iterator erase(iterator first, iterator bound);

Removes each element in the range which begins with first and is bounded by bound. Returns an iterator referencing bound. If first does not reference an item in self (and if first and bound are not equal), the effect is undefined.

pair<iterator,bool> insert(const value_type& val);

If there is no item in self EQ to val then inserts val, returning a pair with an iterator referencing the new element and true. Otherwise, returns a pair with an iterator referencing the matching value_type and false.

size_type insert(iterator ignore, const value_type& val);

If there is no item in self EQ to val then inserts val, returning 1. Otherwise, does nothing and returns 0. Note that the first argument is provided only for conformance with the ANSI associative container specification, and is ignored by the method, since hash table look up can be done in constant time.

size_type insert(const value_type* first, const value_type* bound);

For each element in the range beginning with first and bounded by bound, if there is no item in self EQ to that element, the element is copied into self, or if there is such an element, it is skipped. Returns the number of elements inserted.

size_type insert(const_iterator first, const_iterator bound);

For each element in the range beginning with first and bounded by bound, if there is no item in self EQ to that element, the element is copied into self, or if there is such an element, it is skipped. Returns the number of elements inserted.

void swap(rw_hashset<T,Hash,EQ>& other);

Exchanges the contents of self with other including the Hash and EQ objects. This method does not copy or destroy any of the items exchanged but exchanges the underlying hash tables.

Special Methods for Sets

size_type count(const key_type& key) const;

Returns 1 if self contains key, else 0.

const_iterator find(const key_type& key) const;

Returns a const_iterator referencing key, if it is contained in self, else returns end().

iterator find(const key_type& key);

Returns an iterator referencing key, if it is contained in self, else returns end().

void resize(size_type sz);

Resizes self's hash table to have sz slots; and re-hashes all self's elements into the new table. Can be very expensive if self holds many elements.

RWHashTable

RWHashTable ->- RWCollection ->- RWCollectable 

Synopsis

#include <rw/hashtab.h>
 
RWHashTable h ;

Description

This class is a simple hash table for objects inheriting from RWCollectable. It uses chaining (as implemented by class RWSlistCollectables) to resolve hash collisions. Duplicate objects are allowed.

An object stored by RWHashTable must inherit from the abstract base class RWCollectable, with suitable definition for virtual functions hash() and isEqual() (see class RWCollectable).

To find an object that matches a key, the key's virtual function hash() is first called to determine in which bucket the object occurs. The bucket is then searched linearly by calling the virtual function isEqual() for each candidate, with the key as the argument. The first object to return TRUE is the returned object.

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 will require that all objects be rehashed.

The iterator for this class is RWHashTableIterator.

Persistence

None

Example

#include <rw/hashtab.h>
#include <rw/colldate.h>
#include <rw/rstream.h>
 
main(){
 RWHashTable table;
 RWCollectableDate *july 
      = new RWCollectableDate(7, "July", 1990);
 RWCollectableDate *may
      = new RWCollectableDate (1, "May", 1977);
 RWCollectableDate *feb 
      = new RWCollectableDate (22, "Feb", 1983);
 RWCollectableDate *aug
      = new RWCollectableDate (2, "Aug", 1966);
 
 table.insert(july);
 table.insert(may);
 table.insert(feb);
 table.insert(aug);
 
 cout << "Table contains " << table.entries() << " entries.\n";
 RWCollectableDate key(22, "Feb", 1983);
 cout << "It does ";
 if (!table.contains(&key)) cout << "not ";
 cout << "contain the key " << key << endl;
  
 delete july;
 delete may;
 delete feb;
 delete aug;
 return 0;
}

Program Output:

Table contains 4 entries.
It does contain the key February 22, 1983

Public Constructors

RWHashTable(size_t N = RWCollection::DEFAULT_CAPACITY);

Construct an empty hash table with N buckets.

RWHashTable(const RWHashTable& t);

Copy constructor. Create a new hash table as a shallow copy of the table t. The new table will have the same number of buckets as the old table. Hence, the members need not be and will not be rehashed.

Public Operators

void operator=(const RWHashTable& t);

Assignment operator. Sets self as a shallow copy of t. Afterwards, the two tables will have the same number of buckets. Hence, the members need not be and will not be rehashed.

RWBoolean operator==(const RWHashTable& t) const;

Returns TRUE if self and t have the same number of elements and if for every key in self there is a corresponding key in t which isEqual.

RWBoolean operator<=(const RWHashTable& t) const;

Returns TRUE if self is a subset of t, that is, every element of self has a counterpart in t which isEqual.


Note: If you inherit from RWHashTable in the presence of the Standard C++ Library, we recommend that you override this operator and explicitly forward the call. Overload resolution in C++ will choose the Standard Library provided global operators over inherited class members. These global definitions are not appropriate for set-like partial orderings.


RWBoolean operator!=(const RWHashTable&) const;

Returns the negation of operator==(), above.

Member Functions

virtual void apply(RWapplyCollectable ap, void*);

Redefined from RWCollection. The function pointed to by ap will be called for each member in the collection. Because of the nature of hashing collections, this will not be done in any particular order. The function should not do anything that could change the hash value or equality properties of the objects.

virtual RWspace binaryStoreSize() const;

Inherited from RWCollection.

virtual void clear();

Redefined from RWCollection.

virtual void clearAndDestroy();

Inherited from RWCollection.

virtual int compareTo(const RWCollectable*) const;

Inherited from RWCollection.

virtual RWBoolean contains(const RWCollectable*) const;

Inherited from RWCollection.

virtual size_t entries() const;

Redefined from RWCollection.

virtual RWCollectable* find(const RWCollectable*) const;

Redefined from RWCollection.

virtual unsigned hash() const;

Inherited from RWCollection.

virtual RWCollectable* insert(RWCollectable* a);

Redefined from RWCollection. Returns a if successful, nil otherwise.

virtual RWClassID isA() const;

Redefined from RWCollection to return __RWHASHTABLE.

virtual RWBoolean isEmpty() const;

Redefined from RWCollection.

virtual RWBoolean isEqual(const RWCollectable*) const;

Redefined from RWCollection.

virtual RWCollectable* newSpecies() const;

Redefined from RWCollection.

virtual size_t occurrencesOf(const RWCollectable*) const;

Redefined from RWCollection.

virtual RWCollectable* remove(const RWCollectable*);

Redefined from RWCollection.

virtual void removeAndDestroy(const RWCollectable*);

Inherited from RWCollection.

virtual void resize(size_t n = 0);

Resizes the internal hash table to have n buckets. This causes rehashing all the members of the collection. If n is zero, then an appropriate size will be picked automatically.

virtual void restoreGuts(RWvistream&); virtual void restoreGuts(RWFile&); virtual void saveGuts(RWvostream&) const; virtual void saveGuts(RWFile&) const;

Inherited from class RWCollection.

RWStringID stringID();

(acts virtual) Inherited from class RWCollectable.

RWHashTableIterator

RWHashTableIterator ->- RWIterator 

Synopsis

#include <rw/hashtab.h>
 
RWHashTable h;
RWHashTableIterator it(h);

Description

Iterator for class RWHashTable, which allows sequential access to all the elements of RWHashTable. Note that because an RWHashTable is unordered, elements are not accessed in any particular order.

As with 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

RWHashTableIterator(RWHashTable&);

Construct an iterator for an RWHashTable. After construction, the position of the iterator is undefined.

Public Member Operator

virtual RWCollectable* operator()();

Redefined from class RWIterator. Advances the iterator to the next item and returns it. Returns nil when the end of the collection is reached.

Public Member Functions

virtual RWCollectable* findNext(const RWCollectable* target);

Redefined from class RWIterator. Moves iterator to the next item which isEqual to the item pointed to by target and returns it.

virtual RWCollectable* key() const;

Redefined from class RWIterator. Returns the item at the current iterator position.

RWCollectable* remove();

Remove the item at the current iterator position from the collection.

RWCollectable* removeNext(const RWCollectable*);

Moves the iterator to the next item which isEqual to the item pointed to by target, removes it from the collection and returns it. If no item is found, returns nil and the position of the iterator will be undefined.

virtual void reset();

Redefined from class RWIterator. Resets the iterator to its starting state.

RWIdentityDictionary

RWIdentityDictionary ->- RWHashDictionary ->- RWSet ->-...
...->- RWHashTable ->- RWCollection ->- RWCollectable 

Synopsis

#include <rw/idendict.h>
 
// Smalltalk typedef:
typedef RWIdentityDictionary IdentityDictionary;
RWIdentityDictionary a;

Description

The class RWIdentityDictionary is implemented as a hash table, for the storage and retrieval of key-value pairs. Class RWIdentityDictionary is similar to class RWHashDictionary except that items are found by requiring that they be identical (i.e., have the same address) as the key, rather than being equal (i.e., test true for isEqual()).

Both keys and values must inherit from the abstract base class RWCollectable.

The iterator for this class is RWHashDictionaryIterator.

Persistence

None

Public Constructor

RWIdentityDictionary(size_t n = RWDEFAULT_CAPACITY);

Construct an empty identity dictionary with n hashing buckets.

Public Operator

RWBoolean operator<=(const RWIdentityDictionary& t) const;

Returns TRUE if self is a subset of t, that is, every element of self has a counterpart in t which isEqual. This operator is not explicitly present unless you are compiling with an implementation of the Standard C++ Library. It is normally inherited from RWHashDictionary.


Note: If you inherit from RWIdentityDictionary in the presence of the Standard C++ Library, we recommend that you override t#his operator and explicitly forward the call. Overload resolution in C++ will choose the Standard Library provided global operators over inherited class members. These global definitions are not appropriate for set-like partial orderings.


Public Member Functions

The user interface to this class is identical to class RWHashDictionary and is not reproduced here. The only difference between the classes is that keys are found on the basis of identity rather than equality, and that the virtual function isA() returns __RWIDENTITYDICTIONARY, the ClassId for RWIdentityDictionary.

RWIdentitySet

RWIdentitySet ->- RWSet ->- RWHashTable ->- RWCollection ->-...   
     ...->- RWCollectable 

Synopsis

#include <rw/idenset.h>
 
typedef RWIdentitySet IdentitySet; // Smalltalk typedef
RWIdentitySet a;

Description

The class RWIdentitySet is similar to class RWSet except that items are found by requiring that they be identical (i.e., have the same address) as the key, rather than being equal (i.e., test true for isEqual()).

The iterator for this class is RWSetIterator.

Persistence

Polymorphic

Public Constructor

RWIdentitySet(size_t n = RWDEFAULT_CAPACITY);

Construct an empty identity set with n hashing buckets.

Public Member Functions

The user interface to this class is identical to class RWSet and is not reproduced here. The only difference between the classes is that keys are found on the basis of identity rather than equality, and that the virtual function isA() returns __RWIDENTITYSET, the ClassId for RWIdentitySet.

RWInteger

Synopsis

#include <rw/rwint.h>
 
RWInteger i;

Description

Integer class. This class is useful as a base class for classes that use integers as keys in dictionaries, etc.

Persistence

Isomorphic

Public Constructors

RWInteger();

Construct an RWInteger with value zero (0).

RWInteger(int i);

Construct an RWInteger with value i. Serves as a type conversion from int.

Type Conversion

operator int();

Type conversion to int.

Public Member Functions

RWspace binaryStoreSize() const;

Returns the number of bytes necessary to store the object using the global function:

RWFile& operator<<(RWFile&, const RWInteger&);

int value() const;

Returns the value of the RWInteger.

int value(int newval);

Changes the value of the RWInteger to newval and returns the old value.

Related Global Operators

ostream& operator<<(ostream& o, const RWInteger& x);

Output x to ostream o.

istream& operator>>(istream& i, RWInteger& x);

Input x from istream i.

RWvostream& operator<<(RWvostream&, const RWInteger& x); RWFile& operator<<(RWFile&, const RWInteger& x);

Saves the RWIntegerx to a virtual stream or RWFile, respectively.

RWvistream& operator>>(RWvistream&, RWInteger& x); RWFile& operator>>(RWFile&, RWInteger& x);

Restores an RWInteger into x from a virtual stream or RWFile, respectively, replacing the previous contents of x.

RWIterator

Synopsis

#include <rw/iterator.h>
 
typedef RWIterator Iterator;  // "Smalltalk" typedef

Description

Class RWIterator is an abstract base class for iterators used by the Smalltalk-like collection classes. The class contains virtual functions for positioning and resetting the iterator. They are all pure virtual functions, meaning that deriving classes must supply a definition. The descriptions below are intended to be generic — all inheriting iterators generally follow the described pattern.

Persistence

None

Public Virtual Functions

virtual RWCollectable* findNext(const RWCollectable* target) = 0;

Moves the iterator forward to the next item which "matches" the object pointed to by target and returns it or nil if no item was found. For most collections, an item "matches" the target if either isEqual() or compareTo() indicate equivalence, whichever is appropriate for the actual collection type. However, when an iterator is used with an "identity collection" (i.e., RWIdentitySet and RWIdentityDictionary), it looks for an item with the same address (i.e., "is identical to").

virtual RWCollectable* key() const = 0;

Returns the item at the current iterator position.

virtual RWCollectable* operator()() = 0;

Advances the iterator and returns the next item, or nil if the end of the collection has been reached.

virtual void reset() = 0;

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

RWLocale

Synopsis

#include <locale.h>
#include <rw/locale.h>
 
(Abstract base class) 

Description

RWLocale is an abstract base class. It defines an interface for formatting dates (including day and month names), times, numbers (including digit grouping), and currency, to and from strings.

Note that because it is an abstract base class, there is no way to actually enforce these goals — the description here is merely the model of how a class derived from RWLocale should act.

There are three ways to use an RWLocale object:

  • By passing the object to functions which expect one, such as RWDate::asString().

  • By specifying a "global" locale using the static member function RWLocale::global(RWLocale*). This locale is passed as the default argument to functions that use a locale.

  • By "imbuing" a stream with the object, so that when an RWDate or RWTime is written to a stream using operator<<(), the appropriate formatting will be used automatically.

Two implementations of RWLocale are provided with the library:

  • Class RWLocaleSnapshot encapsulates the Standard C library locale facility, with two additional advantages: more than one locale can be active at the same time; and it supports conversions from strings to other types.

  • There is also an internal class that mimics RWLocaleSnapshot("C"). If your compiler does not have built-in support for locales, one is constructed automatically at program startup to be used as the default value of RWLocale::global(). If your compiler does support locales, RWLocale::global() returns a const reference to an instance of RWLocaleSnapshot("C").

Persistence

None

Enumeration

enum CurrSymbol { NONE, LOCAL, INTL };

Controls whether no currency symbol, the local currency symbol, or the international currency symbol should be used to format currency.

Public Member Functions

virtual RWCString asString(long) const = 0; virtual RWCString asString(unsigned long) const = 0;

Converts the number to a string (e.g., "3,456").

virtual RWCString asString(double f, int precision = 6, RWBoolean showpoint = 0) const = 0;

Converts the double f to a string. The variable precision is the number of digits to place after the decimal separator. If showpoint is TRUE, the decimal separator will appear regardless of the precision.

virtual RWCString asString(const struct tm* tmbuf,char format, const RWZone& zone) const = 0;

Converts components of the struct tm object to a string, according to the format character. The meanings assigned to the format character are identical to those used in the Standard C Library function strftime(). The members of struct tm are assumed to be set consistently. See Table 1 for a summary of strftime() formatting characters.

RWCString asString(const struct tm* tmbuf,const char* format, const RWZone& zone) const;

Converts components of the struct tm object to a string, according to the format string. Each format character in the format string must be preceded by %. Any characters not preceded by % are treated as ordinary characters which are returned unchanged. You may represent the special character % with "%%". The meanings assigned to the format character are identical to those used in the Standard C Library function strftime(). The members of struct tm are assumed to be set consistently. See Table 1 for a summary of strftime() formatting characters. This function is not virtual in order to maintain link-compatibility with the previous version of the library.

virtual RWCString moneyAsString(double value,enum CurrSymbol = LOCAL) const = 0;

Returns a string containing the value argument formatted according to monetary conventions for the locale. The value argument is assumed to contain an integer representing the number of units of currency (e.g., moneyAsString(1000., RWLocale::LOCAL) in a US locale would yield "$10.00"). The CurrSymbol argument determines whether the local (e.g., "$") or international (e.g., "USD ") currency symbol is applied, or none.

virtual int monthIndex(const RWCString&) const = 0;

Interprets its argument as a full or abbreviated month name, returning values 1 through 12 to represent (respectively) January through December, or 0 for an error. Leading white space is ignored.

virtual RWBoolean stringToNum(const RWCString&, double* fp) const = 0;

Interprets the RWCString argument as a floating point number. Spaces are allowed before and after the (optional) sign, and at the end. Digit group separators are allowed in the integer portion. Returns TRUE for a valid number, FALSE for an error. If it returns FALSE, the double* argument is untouched. All valid numeric strings are accepted; all others are rejected. The following are examples of valid numeric strings in an English-speaking locale:

"1" " -02. " ".3"

"1234.56" "1e10" "+ 19,876.2E+20"

virtual RWBoolean stringToNum(const RWCString&, long* ip) const = 0;

Interprets the RWCString argument as an integer. Spaces are allowed before and after the (optional) sign, and at the end. Digit group separators are allowed. Returns TRUE for a valid integer, FALSE for an error. If it returns FALSE, the long* argument is untouched. All valid numeric strings are accepted; all others are rejected. The following are examples of valid integral strings in an English-speaking locale:

"1" " -02. " "+ 1,234"

"1234545" "1,234,567"

Examples are given (in parenthesis). For those formats that do not use all members of the struct tm, only those members that are actually used are noted [in brackets].

Table 4-1. Formatting characters used by strftime().

Format character

Meaning

Example

a

Abbreviated weekday name [from tm::tm_wday]

Sun

A

Full weekday name [from tm::tm_wday]

Sunday

b

Abbreviated month name

Feb

B

Full month name

February

c

Date and time [may use all members]

Feb 29 14:34:56 1984

d

Day of the month

29

H

Hour of the 24-hour day

14

I

Hour of the 12-hour day

02

j

Day of the year, from 001 [from tm::tm_yday]

60

m

Month of the year, from 01

02

M

Minutes after the hour

34

p

AM/PM indicator, if any

AM

S

Seconds after the minute

56

U

Sunday week of the year, from 00 [from tm::tm_yday and tm::tm_wday]

 

w

Day of the week, with 0 for Sunday

0

W

Monday week of the year, from 00 [from tm::tm_yday and tm::tm_wday]

 

x

Date [uses tm::tm_yday in some locales]

Feb 29 1984

X

Time

14:34:56

y

Year of the century, from 00 (deprecated)

84

Y

Year

1984

Z

Time zone name [from tm::tm_isdst]

PST or PDT


virtual RWBoolean stringToDate(const RWCString&, struct tm*) const = 0;

Interprets the RWCString as a date, and extracts the month, day, and year components to the tm argument. It returns TRUE for a valid date, FALSE otherwise. If it returns FALSE, the struct tm argument is untouched; otherwise it sets the tm_mday, tm_mon, and tm_year members. If the date is entered as three numbers, the order expected is the same as that produced by strftime(). Note that this function cannot reject all invalid date strings.

The following are examples of valid date strings in an English-speaking locale:

"Jan 9, 62" "1/9/62" "January 9 1962"

"09Jan62" "010962"

virtual RWBoolean stringToTime(const RWCString&, struct tm*) const = 0;

Interprets the RWCString argument as a time, with hour, minute, and optional second. If the hour is in the range [1..12], the local equivalent of "AM" or "PM" is allowed. Returns TRUE for a valid time string, FALSE for an error. If it returns FALSE, the tm argument is untouched; otherwise it sets the tm_hour, tm_min, and tm_sec members. Note that this function cannot reject all invalid time strings. The following are examples of valid time strings in an English-speaking locale:

"1:10 AM" "13:45:30" "12.30.45pm"

"PM 3:15" "1430"

virtual RWBoolean stringToMoney(const RWCString&, double*, RWLocale::CurrSymbol=LOCAL) const = 0;

Interprets the RWCString argument as a monetary value. The currency symbol, if any, is ignored. Negative values may be specified by the negation symbol or by enclosing parentheses. Digit group separators are optional; if present they are checked. Returns TRUE for a valid monetary value, FALSE for an error. If it returns FALSE, the double* argument is untouched; otherwise it is set to the integral number of monetary units entered (e.g. cents, in a U.S. locale).

const RWLocale* imbue(ios& stream) const;

Installs self in the stream argument, for later use by the operators << and >> (e.g. in RWDate or RWTime). The pointer may be retrieved from the stream with the static member RWLocale::of(). In this way a locale may be passed transparently through many levels of control to be available where needed, without intruding elsewhere.

virtual int weekdayIndex(const RWCString&) const = 0;

Interprets its argument as a full or abbreviated weekday name, returning values 1 through 7 to represent (respectively) Monday through Sunday, or 0 for an error.

Static Public Member Functions

static const RWLocale& of(ios&);

Returns the locale installed in the stream argument by a previous call to RWLocale::imbue() or, if no locale was installed, the result from RWLocale::global().

static const RWLocale* global(const RWLocale* loc);

Sets the global "default" locale object to loc, returning the old object. This object is used by RWDate and RWTime string conversion functions as a default locale. It is set initially to refer to an instance of a class that provides the functionality of RWLocaleSnapshot("C").

static const RWLocale& global();

Returns a reference to the present global "default" locale.

const RWLocale* defaultLocale();

Returns a pointer to a new instance of either RWLocaleSnapshot("C"); or another class that provides the same behavior for compilers that don't fully support Standard C locales.

RWLocaleSnapshot

RWLocaleSnapshot ->- RWLocale 

Synopsis

#include <locale.h>
 
#include <rw/locale.h>
 
RWLocaleSnapshot ourLocale("");  // encapsulate user's formats

Description

The class RWLocaleSnapshot implements the RWLocale interface using Standard C library facilities. To use it, the program creates an RWLocaleSnapshot instance. The constructor of the instance queries the program's environment (using standard C library functions such as localeconv(), strftime(), and, if available , vendor specific library functions) to learn everything it can about formatting conventions in effect at the moment of instantiation. When done, the locale can then be switched and another instance of RWLocaleSnapshot created. By creating multiple instances of RWLocaleSnapshot, your program can have more than one locale active at the same time, something that is difficult to do with the Standard C library facilities.


Note: RWLocaleSnapshot does not encapsulate character set, collation, or message information.

Class RWLocaleSnapshot has a set of public data members initialized by its constructor with information extracted from its execution environment.

Persistence

None

Example

Try this program with the environmental variable LANG set to various locales:

#include <rw/rwdate.h>
 
#include <rw/locale.h>
#include <iostream.h>
 
main(){
 RWLocaleSnapshot *userLocale = new RWLocaleSnapshot("");
 RWLocale::global(userLocale);
// Print a number using the global locale:
 cout << RWLocale::global().asString(1234567.6543) << endl;
 // Now get and print a date:
 cout << "enter a date: " << flush;
 RWDate date;
 cin >> date;
 if (date.isValid())
 cout << date << endl;
 else
 cout << "bad date" << endl;
 delete userLocale;
return 0;
}

Enumerations

enum RWDateOrder { DMY, MDY, YDM, YMD };

Public Constructor

RWLocaleSnapshot(const char* localeName = 0);

Constructs an RWLocale object by extracting formats from the global locale environment. It uses the Standard C Library function setlocale() to set the named locale, and then restores the previous global locale after formats have been extracted. If localeName is 0, it simply uses the current locale. The most useful locale name is the empty string, "", which is a synonym for the user's chosen locale (usually specified by the environment variable LANG).

Public Member Functions

virtual RWCString asString(long) const; virtual RWCString asString(unsigned long) const; virtual RWCString asString(double f, int precision = 6, RWBoolean showpoint = 0) const; virtual RWCString asString(struct tm* tmbuf,char format, const RWZone& zone); const; virtual RWCString asString(struct tm* tmbuf,char* format, const RWZone& zone) const; virtual RWCString moneyAsString(double value,enum CurrSymbol = LOCAL) const; virtual RWBoolean stringToNum (const RWCString&, double* fp) const; virtual RWBoolean stringToNum (const RWCString&, long* ip ) const; virtual RWBoolean stringToDate (const RWCString&, struct tm*) const; virtual RWBoolean stringToTime (const RWCString&, struct tm*) const; virtual RWBoolean stringToMoney(const RWCString&, double* , RWLocale::CurrSymbol=LOCAL) const;

Redefined from class RWLocale. These virtual functions follow the interface described under class RWLocale. They generally work by converting values to and from strings using the rules specified by the structlconv values (see <locale.h>) encapsulated in self.

Public Data Members

RWCString decimal_point_; RWCString thousands_sep_; RWCString grouping_; RWCString int_curr_symbol_; RWCString currency_symbol_; RWCString mon_decimal_point_; RWCString mon_thousands_sep_; RWCString mon_grouping_; RWCString positive_sign_; RWCString negative_sign_; char int_frac_digits_; char frac_digits_; char p_cs_precedes_; char p_sep_by_space_; char n_cs_precedes_; char n_sep_by_space_; char p_sign_posn_; char n_sign_posn_;

These are defined identically as the correspondingly-named members of the standard C library type lconv, from <locale.h>.

RWModel

Synopsis

#include <rw/model.h>
 
(abstract base class) 

Description

This abstract base class has been designed to implement the "Model" leg of a Model-View-Controller architecture. A companion class, RWModelClient, supplies the "View" leg.

It maintains a list of dependent RWModelClient objects. When member function changed(void*) is called, the list of dependents will be traversed, calling updateFrom(RWModel*, void*) for each one, with itself as the first argument. Subclasses of RWModelClient should be prepared to accept such a call.

Persistence

None

Example

This is an incomplete and somewhat contrived example in that it does not completely define the classes involved. "Dial" is assumed to be a graphical representation of the internal settings of "Thermostat." The essential point is that there is a dependency relationship between the "Thermostat" and the "Dial": when the setting of the thermostat is changed, the dial must be notified so that it can update itself to reflect the new setting of the thermostat.

#include <rw/model.h>
 
class Dial : public RWModelClient {
public:
  virtual void updateFrom(RWModel* m, void* d);
};
 
class Thermostat : public RWModel {
  double setting;
public:
  Thermostat( Dial* d )
    { addDependent(d); }
  double temperature() const
    { return setting; }
  void setTemperature(double t)
    { setting = t; changed(); }
};
 
void Dial::updateFrom(RWModel* m, void*) {
  Thermostat* t = (Thermostat*)m;
  double temp = t->temperature();
  // Redraw graphic.
}

Public Constructor

RWModel();

When called by the specializing class, sets up the internal ordered list of dependents.

Public Member Functions

void addDependent(RWModelClient* m);

Adds the object pointed to by m to the list of dependents of self.

void removeDependent(RWModelClient* m);

Removes the object pointed to by m from the list of dependents of self.

virtual void changed(void* d);

Traverse the internal list of dependents, calling member function updateFrom(RWModel*, void*) for each one, with self as the first argument and d as the second argument.

RWModelClient

Synopsis

#include <rw/model.h>
 
(abstract base class) 

Description

This abstract base class has been designed to implement the "View" leg of a Model-View-Controller architecture. Class RWModel, supplies the "Model" leg. See class RWModel for details.

Persistence

None

Public Member Function

virtual void updateFrom(RWModel* p, void* d) = 0;

Deriving classes should supply an appropriate definition for this pure virtual function. The overall semantics of the definition should be to update self from the data presented by the object pointed to by p. That is, self is considered a dependent of the object pointed to by p. The pointer d is available to pass client data.

RWOrdered

RWOrdered ->- RWSequenceable ->- RWCollection ->- RWCollectable 

Synopsis

#include <rw/ordcltn.h>
 
RWOrdered a;

Description

Class RWOrdered represents a group of ordered items, accessible by an index number, but not accessible by an external key. Duplicates are allowed. The ordering of elements is determined externally, generally by the order of insertion and removal. An object stored by RWOrdered must inherit from the abstract base class RWCollectable.

Class RWOrdered is implemented as a vector of pointers, allowing for more efficient traversing of the collection than the linked list classes. RWSlistCollectables and RWDlistCollectables, but slower insertion in the center of the collection.

Persistence

Polymorphic

Public Constructors

RWOrdered(size_t size = RWDEFAULT_CAPACITY);

Construct an RWOrdered with an initial capacity of size.

Public Member Operators

RWBoolean operator==(const RWOrdered& od) const;

Returns TRUE if for every item in self, the corresponding item in od at the same index isEqual. The two collections must also have the same number of members.

RWCollectable*& operator[](size_t i);

Returns the ith element in the collection. If i is out of range, an exception of type RWBoundsErr will occur. The results of this function can be used as an lvalue.

RWCollectable*& operator()(size_t i);

Returns the ith element in the collection. Bounds checking is enabled by defining the preprocessor directive RWBOUNDS_CHECK before including the header file ordcltn.h. In this case, if i is out of range, an exception of type RWBoundsErr will occur. The results of this function can be used as an lvalue.

Public Member Functions

virtual RWCollectable* append(RWCollectable*);

Redefined from class RWSequenceable. Adds the item to the end of the collection and returns it. Returns nil if the insertion was unsuccessful.

virtual void apply(RWapplyCollectable ap, void* x);

Redefined from class RWCollection. This function has been redefined to apply the user-supplied function pointed to by ap to each member of the collection, in order, from first to last.

virtual RWCollectable*& at(size_t i); virtual const RWCollectable* at(size_t i) const;

Redefined from class RWSequenceable.

virtual RWspace binaryStoreSize() const;

Inherited from class RWCollection.

virtual void clear();

Redefined from class RWCollection.

virtual void clearAndDestroy();

Inherited from class RWCollection.

virtual int compareTo(const RWCollectable* a) const;

Inherited from class RWCollectable.

virtual RWBoolean contains(const RWCollectable* target) const;

Inherited from class RWCollection.

virtual size_t entries() const;

Redefined from class RWCollection.

virtual RWCollectable* find(const RWCollectable* target) const;

Redefined from class RWCollection. Returns the first item that isEqual to the item pointed to by target, or nil if no item was found..

virtual RWCollectable* first() const;

Redefined from class RWSequenceable. Returns the first item in the collection.

virtual unsigned hash() const;

Inherited from class RWCollectable.

virtual size_t index(const RWCollectable*) const;

Redefined from class RWSequenceable.

virtual RWCollectable* insert(RWCollectable* c);

Redefined from class RWCollection. Adds the item to the end of the collection and returns it. Returns nil if the insertion was unsuccessful.

void insertAt(size_t indx, RWCollectable* e);

Redefined from class RWSequenceable. Adds a new item to the collection at position indx. The item previously at position i is moved to i+1, etc. The index indx must be between 0 and the number of items in the collection, or an exception of type RWBoundsErr will be thrown.

virtual RWClassID isA() const;

Redefined from class RWCollectable to return __RWORDERED.

virtual RWBoolean isEmpty() const;

Redefined from class RWCollection.

virtual RWBoolean isEqual(const RWCollectable* a) const;

Inherited from class RWCollectable.

virtual RWCollectable* last() const;

Redefined from class RWSequenceable. Returns the last item in the collection.

virtual size_t occurrencesOf(const RWCollectable* target) const;

Redefined from class RWCollection. Returns the number of items that compare isEqual to the item pointed to by target.

RWCollectable* prepend(RWCollectable*);

Redefined from class RWSequenceable. Adds the item to the beginning of the collection and returns it. Returns nil if the insertion was unsuccessful.

void push(RWCollectable* c);

This is an alternative implementation of a stack to class RWSlistCollectablesStack. The item pointed to by c is put at the end of the collection.

RWCollectable* pop();

This is an alternative implementation of a stack to class RWSlistCollectablesStack. The last item in the collection is removed and returned. If there are no items in the collection, nil is returned.

virtual RWCollectable* remove(const RWCollectable* target);

Redefined from class RWCollection. Removes the first item that isEqual to the item pointed to by target and returns it. Returns nil if no item was found.

RWCollectable* removeAt(size_t index);

Removes the item at the position index in the collection and returns it.

virtual void removeAndDestroy(const RWCollectable* target);

Inherited from class RWCollection.

RWCollectable* top() const;

This is an alternative implementation of a stack to class RWSlistCollectablesStack. The last item in the collection is returned. If there are no items in the collection, nil is returned.

RWOrderedIterator

RWOrderedIterator ->- RWIterator 

Synopsis

#include <rw/ordcltn.h>
RWOrdered a ;
RWOrderedIterator iter(a);

Description

Iterator for class RWOrdered. Traverses the collection from the first to the last item.

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 Constructors

RWOrderedIterator(const RWOrdered& a);

Construct an RWOrderedIterator from an RWOrdered. Immediately after construction the position of the iterator is undefined.

Public Member Operator

virtual RWCollectable* operator()();

Redefined from class RWIterator. Advances the iterator to the next item and returns it. Returns nil when the end of the collection is reached.

Public Member Functions

virtual RWCollectable* findNext(const RWCollectable*);

Redefined from class RWIterator. Moves iterator to the next item which isEqual to the item pointed to by target and returns it. If no item is found, returns nil and the position of the iterator will be undefined.

virtual RWCollectable* key() const;

Redefined from class RWIterator. Returns the item at the current iterator position.

virtual void reset();

Redefined from class RWIterator. Resets the iterator to its starting state.