Chapter 1. RWAuditStreamBuffer - RWbostream

RWAuditStreamBuffer

RWAuditStreamBuffer - > - streambuf 

Synopsis

#include <rw/auditbuf.h>
 
#include <iostream.h>
RWAuditStreamBuffer buf(arguments)
ostream os(&buf); // may be used for ostreams
istream is(&buf); // or istreams of any kind

Description

Class RWAuditStreamBuffer is used to construct a stream, after which the RWAuditStreamBuffer instance will count all the bytes that pass through the stream. If constructed with a function pointer, RWAuditStreamBuffer will call that function with each byte that passes through the stream. The counting capacity provides for streams the equivalent of the RWCollectable method recursiveStoreSize() which is only available for RWFile.

Persistence

None

Short Example

#include <rw/auditbuf.h>
#include <rw/bstream.h>
#include <rw/pstream.h>
#include <iostream.h>
int main() {
  RWCollectable ct;
  fillCollectable();  // make a collection, somehow
  RWAuditStreamBuffer bcounter, pcounter;
  RWbostream bcount(&bcounter); //ctor takes streambuf pointer
  RWpostream pcount(&pcounter);
//
  bcount << ct;
  pcount << ct;
cout  << "We just counted " << bcounter 
      << " bytes from an RWbostream." << endl;
cout  << "We just counted " << pcounter 
      << " bytes from an RWpostream." << endl;
return 0;
}

Related Classes

RWAuditStreamBuffer may be used as the streambuf for any stream, including those derived from RWvostream or RWvistream, strstream, ifstream, ofstream, etc.

Global Typedef

typedef void (*RWauditFunction)(unsigned char, void*);

If you wish to do more than count each character handled by the buffer, you may provide an RWauditFunction to the constructor. The first parameter to this function is a byte provided by the stream. The second parameter is the address of the conter to be manipulated by RWAuditFunction.

Public Constructors

RWAuditStreamBuffer(RWauditFunction=0, void*=0);

Constructs a new RWAuditStreamBuffer that may be used only to examine and count every byte that passes into an ostream that has the RWAuditStreamBuffer instance as its streambuf. It will not forward the bytes to any stream, nor accept bytes from a stream. The second argument to the constructor allows you to supply storage for the byte count. It is optional.

RWAuditStreamBuffer(istream&, RWauditFunction=0, void*=0);

Constructs a new RWAuditStreamBuffer that passes bytes from the istream on which it is constructed to the istream that has the RWAuditStreamBuffer instance as its streambuf. A typical use would be to count or examine the bytes being input from a file through a stream derived from RWvistream. The second argument to the constructor allows you to supply storage for the byte count. It is optional.

RWAuditStreamBuffer(iostream&, RWauditFunction=0, void*=0);

Constructs a new RWAuditStreamBuffer that passes bytes to and from the iostream on which it is constructed to and from the istream that has the RWAuditStreamBuffer instance as its streambuf. A typical use would be to count or examine the bytes being transferred to and from a file used to store and retrieve changing data. The second argument to the constructor allows you to supply storage for the byte count. It is optional.

RWAuditStreamBuffer(ostream&, RWauditFunction=0, void*=0);

Constructs a new RWAuditStreamBuffer that passes bytes into the ostream on which it is constructed from the ostream that has the RWAuditStreamBuffer instance as its streambuf. A typical use would be to count or examine the bytes being output to a file through a stream derived from RWvostream. The second argument to the constructor allows you to supply storage for the byte count. It is optional.

RWAuditStreamBuffer(streambuf*, RWauditFunction=0, void*=0);

Constructs a new RWAuditStreamBuffer that passes bytes into the ostream on which it is constructed from the ostream that has the RWAuditStreamBuffer instance as its streambuf. A typical use would be to count or examine the bytes being output to a file through a stream derived from RWvostream. The second argument to the constructor allows you to supply storage for the byte count. It is optional.

Public Destructor

virtual ~RWAuditStreamBuffer();

We have provided an empty destructor since some compilers complain if there is no virtual destructor for a class that has virtual methods.

Public Member Operator

operator unsigned long();

Provides the count of bytes seen so far.

Public Member Function

unsigned long reset(unsigned long value = 0);

Resets the count of bytes seen so far. Returns the current count.

Extended Example

#include <iostream.h>
 
#include <fstream.h>
#include <rw/auditbuf.h>
#include <rw/pstream.h>
#include <rw/cstring.h>
void doCrc (unsigned char c, void* x) {
  *(unsigned char*)x ^= c;
}
 
int main() {
if(1) { // just a block to control variable lifetime
    unsigned char check = '\0';
 
     // create an output stream
     ofstream                            op("crc.pst");
 
     // create an RWAuditStreamBuffer that will do CRC
     RWAuditStreamBuffer               crcb(op,doCrc,&check);
 
     // create an RWpostream to put the data through.
RWpostream                           p(&crcb);
 
     // now send some random stuff to the stream
     p << RWCString("The value of Tools.h++ is at least ");
     p << (int)4;
     p << RWCString(" times that of the next best library!\n") ;
     p << RWCString("Pi is about ") << (double)3.14159 << '.';
 
     // finally, save the sum on the stream itself.
p << (unsigned int)check; // alters check, _after_ saving it...
 
    // just for fun, print out some statistics:
    cout << "We just saved " << crcb 
         << " bytes of data to the file." << endl;
    cout << "The checksum for those bytes was " <<check << endl;
} // end of block
 
    // now read the data back in, checking to see if it survived.
    unsigned char check = '\0';
 
    // create an instream
    ifstream                            ip("crc.pst");
 
    // create an RWAuditStreamBuffer that will do CRC
    RWAuditStreamBuffer               crcb(ip,doCrc,&check);
 
    // create an RWpistream to interpret the bytes
    RWpistream                           p(&crcb);
 
    RWCString first, mid1, mid2;
    int value;
    double pi;
    char pnc;
    unsigned int savedCRC;
    unsigned char matchCRC;
    // read in the data. Don\'t read the checksum yet!
    p >> first >> value >> mid1 >> mid2 >> pi >> pnc;
    // save the checksum
        matchCRC = check;
    // Now it is safe to alter the running checksum by reading in
    // the one saved in the file.
p >> savedCRC;
 
    if(savedCRC != matchCRC) {
      cout << "Checksum error. Saved CRC: " << savedCRC
    << " built CRC: " << matchCRC << dec << endl;
    }
    else {
        cout << "The message was: " << endl;
        cout << first << value << mid1 << mid2 << pi << pnc << endl;
    }
    // just for fun, print out some statistics:
    cout  << "We just read " << crcb 
        << " bytes of data from the file." << endl;
    cout  << "The checksum was " << matchCRC << flush;
    cout  << " and the saved checksum was " << savedCRC << endl;
return 0;
}

RWBag

RWBag - > - RWCollection - > - RWCollectable 

Synopsis

typedef RWBag Bag;     // Smalltalk typedef .
 
#include <rw/rwbag.h>
RWBag h;

Description

Class RWBag corresponds to the Smalltalk class Bag. It represents a group of unordered elements, not accessible by an external key. Duplicates are allowed.

An object stored by RWBag must inherit abstract base class RWCollectable, with suitable definition for virtual functions hash() and isEqual() (see class RWCollectable). The function hash() is used to find objects with the same hash value, then isEqual() is used to confirm the match.

Class RWBag is implemented by using an internal hashed dictionary (RWHashDictionary) which keeps track of the number of occurrences of an item. If an item is added to the collection that compares equal (isEqual) to an existing item in the collection, then the count is incremented. Note that this means that only the first instance of a value is actually inserted: subsequent instances cause the occurrence count to be incremented. This behavior parallels the Smalltalk implementation of Bag.

Member function apply() and the iterator are called repeatedly according to the count for an item.

See class RWHashTable if you want duplicates to be stored, rather than merely counted.

Persistence

Polymorphic

Public Constructors

RWBag(size_t n = RWDEFAULT_CAPACITY);

Construct an empty bag with n buckets.

RWBag(const RWBag& b);

Copy constructor. A shallow copy of b will be made.

Public Member Operators

void operator=(const RWBag& b);

Assignment operator. A shallow copy of b will be made.

RWBoolean operator==(const RWBag& b) const;

Returns TRUE if self and bag b have the same number of total entries and if for every key in self there is a corresponding key in b which isEqual and which has the same number of entries.

Public Member Functions

virtual void apply(RWapplyCollectable ap, void*);

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 a generally unpredictable order. If an item has been inserted more than once (i.e., more than one item isEqual), then apply() will be called that many times. The user-supplied function should not do anything that could change the hash value or the meaning of "isEqual" of the items.

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. The first item that was inserted into the Bag and which equals target is returned or nil if no item is found. Hashing is used to narrow the search.

virtual unsigned hash() const;

Inherited from class RWCollectable.

virtual RWCollectable* insert(RWCollectable* c);

Redefined from class RWCollection. Inserts the item c into the collection and returns it, or if an item was already in the collection that isEqual to c, then returns the old item and increments its count.

RWCollectable* insertWithOccurrences(RWCollectable* c,size_t n);

Inserts the item c into the collection with count n and returns it, or if an item was already in the collection that isEqual to c, then returns the old item and increments its count by n.

virtual RWClassID isA() const;

Redefined from class RWCollectable to return __RWBAG.

virtual RWBoolean isEmpty() const;

Redefined from class RWCollection.

virtual RWBoolean isEqual(const RWCollectable* a) const;

Inherited from class RWCollectable.

virtual size_t occurrencesOf(const RWCollectable* target) const;

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

virtual RWCollectable* remove(const RWCollectable* target);

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

virtual void removeAndDestroy(const RWCollectable* target);

Redefined from class RWCollection. Removes the item that isEqual to the item pointed to by target. Destroys the item as well if it is the last occurrence in the collection.

void resize(size_t n = 0);

Resizes the internal hash table to have n buckets. The overhead for this function is the hashing of every element in 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.

RWBagIterator

RWBagIterator - > - RWIterator 

Synopsis

#include <rw/rwbag.h>
 
RWBag b;
RWBagIterator it(b);

Description

Iterator for class RWBag, which allows sequential access to all the elements of RWBag. Note that because an RWBag is unordered, elements are not accessed in any particular order. If an item was inserted N times into the collection, then it will be visited N consecutive times.

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

RWBagIterator(const RWBag&);

Construct an iterator for an RWBag. 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 has been reached.

Public Member Functions

virtual RWCollectable* findNext(const RWCollectable* target);

Redefined from class RWIterator. Moves iterator to the next item which isEqual to the object pointed to by target and returns it. Hashing is used to find the target. 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.

RWBench

Synopsis

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

Description

This is an abstract class that can automate the process of benchmarking a piece of code. To use it, derive a class from RWBench, including a definition for the virtual function doLoop(unsigned long N). This function should perform N operations of the type that you are trying to benchmark. RWBench will call doLoop() over and over again until a preset amount of time has elapsed. It will then sum the total number of operations performed.

To run, construct an instance of your derived class and then call go(). Then call report() to get a standard summary. For many compilers, this summary will automatically include the compiler type and memory model. You can call ops(), outerLoops(), etc. for more detail.

If you wish to correct for overhead, then provide an idleLoop() function which should do all non-benchmark-related calculations.

Persistence

None

Example

This example benchmarks the time required to return a hash value for a Rogue Wave string versus a Borland string.

#include <rw/bench.h>                 /* Benchmark software */
 
#include <rw/cstring.h>               /* Rogue Wave string class */
#include <stdlib.h>
#include <iostream.h>
#include <rw/ctoken.h>
#include <rw/regexp.h>
 
// The string to be hashed:
const char* cs = "A multi-character string with lots of words in it to be parsed out and searched for.";
 
class TestBrute : public RWBench {
public:
TestBrute() { }
  virtual void       doLoop(unsigned long n);
  virtual void       idleLoop(unsigned long n);
  virtual void       what(ostream& s) const
    { s << "Brute force string search: \n"; }
};
 
class TestRW : public RWBench {
public:
TestRW() { }
  virtual void     doLoop(unsigned long n);
  virtual void       idleLoop(unsigned long n);
  virtual void       what(ostream& s) const
     { s << "Rogue Wave search: \n"; }
};
 
main(int argc, char* argv[]){
  cout << "Testing string \n\"" << cs << "\"\n";
 
  // Test brute force string search algorithm:
  TestBrute other;
  other.parse(argc, argv);
  other.go();
  other.report(cout);
 
  // Test RW searching w/regular expressions:
  TestRW rw;
  rw.parse(argc, argv);
  rw.go();
  rw.report(cout);
 
  return 0;
}
 
void TestBrute::doLoop(unsigned long n){
  RWCString string(cs);
  RWCTokenizer *tokener;
  RWCString token;
 
  tokener = new RWCTokenizer(string);
 
  while(n--){
 
if((token = (*tokener)()).isNull())
{
        delete tokener;
        tokener = new RWCTokenizer(string);
        token = (*tokener)();
}
 
size_t j = 0;
 
for(size_t i = 0; i < string.length() && j != token.length(); 
    i++)
{
    j = 0;
    while((j < token.length()) && (string[i+j]==token[j]))
       j++;
    }
 
  }
 delete tokener;
}
 
void TestRW::doLoop(unsigned long n){
  RWCString string(cs);
  RWCTokenizer *tokener;
  RWCString token, result;
  RWCRegexp re("");
 
  tokener = new RWCTokenizer(string);
   
  while(n--){
  
  if((token = (*tokener)()).isNull())
   {
        delete tokener;
        tokener = new RWCTokenizer(string);
        token = (*tokener)();
   }  
 
  re = RWCRegexp(token); 
  result = string(re);       //Do the search! 
 
   }
  delete tokener;
}
 
void TestBrute::idleLoop(unsigned long n){
  RWCString string(cs);           // Subtract out the overhead
  RWCTokenizer *tokener;
  RWCString token;
 
  tokener = new RWCTokenizer(string);
   
  while(n--){
   
   if((token = (*tokener)()).isNull())
    {
        delete tokener;
        tokener = new RWCTokenizer(string);
        token = (*tokener)();
    }  
 
    }
 delete tokener;
  }
 
void TestRW::idleLoop(unsigned long n){
  RWCString string(cs);                //Subtract out the overhead
  RWCTokenizer *tokener;
  RWCString token, result;
  RWCRegexp re("");
 
  tokener = new RWCTokenizer(string);
 
  while(n--){
 
  if((token = (*tokener)()).isNull())
    {
        delete tokener;
        tokener = new RWCTokenizer(string);
        token = (*tokener)();
    }  
 
re = RWCRegexp(token); 
 
    }
  delete tokener;
}
 

Program Output:

Testing string 
"A multi-character string with lots of words in it to be parsed out and searched for."
Borland C++ V4.0 
 
Brute force string search: 
 
Iterations:                 35
Inner loop operations:      1000
Total operations:           35000
Elapsed (user) time:        4.596
Kilo-operations per second: 7.61532
 
Borland C++ V4.0 
 
Rogue Wave search: 
 
Iterations:                 53
Inner loop operations:      1000
Total operations:           53000
Elapsed (user) time:        2.824
Kilo-operations per second: 18.7677

Public Constructors

RWBench(double duration = 5, unsigned long ILO=1000, const char* machine = 0);

The parameter duration is the nominal amount of time that the benchmark should take in seconds. The virtual function doLoop(unsigned long) will be called over and over again until at least this amount of time has elapsed. The parameter ILO is the number of "inner loop operations" that should be performed. This parameter will be passed in as parameter N to doLoop(N). Parameter machine is an optional null terminated string that should describe the test environment (perhaps the hardware the benchmark is being run on ).

Public Member Functions

virtual void doLoop(unsigned long N)=0;

A pure virtual function whose actual definition should be supplied by the specializing class. This function will be repeatedly called until a time duration has elapsed. It should perform the operation to be benchmarked N times. See the example.

double duration() const;

Return the current setting for the benchmark test duration. This should not be confused with function time() which returns the actual test time.

virtual void go();

Call this function to run the benchmark.

virtual void idleLoop(unsigned long N);

This function can help to correct the benchmark for overhead. The default definition merely executes a "for()" loop N times. See the example.

const char * machine();

This function accesses the name of the machine which is passed into the benchmark object through parse().

virtual void parse(int argc, char* argv[]);

This function allows an easy way to change the test duration, number of inner loops and machine description from the command line:

Table 1-1. virtual void command line options

Argument

Type

Description

argv[1]

double

Duration (sec.)

argv[2]

unsigned long

No. of inner loops

argv[3]

const char*

Machine


void parse(const char *);

This is a non-virtual function which provides the same service as parse(int argc, char * argv[]), but is designed for Windows users. It extracts tokens from the null-terminated command argument provided by Windows, then calls the virtual parse for ANSI C command arguments.

virtual void report(ostream&) const;

Calling this function provides an easy and convenient way of getting an overall summary of the results of a benchmark.

double setDuration(double t);

Change the test duration to time t.

unsigned long setInnerLoops(unsigned long N);

Change the number of "inner loop operations" to N.

virtual void what(ostream&) const;

You can supply a specializing version of this virtual function that provides some detail of what is being benchmarked. It is called by report() when generating a standard report.

void where(ostream&) const;

This function will print information to the stream about the compiler and memory model that the code was compiled under.

unsigned long innerLoops() const;

Returns the current setting for the number of inner loop operations that will be passed into function doLoop(unsigned long N) as parameter N.

double time() const;

Returns the amount of time the benchmark took, corrected for overhead.

unsigned long outerLoops() const;

Returns the number of times the function doLoop() was called.

double ops() const;

Returns the total number of inner loop operations that were performed (the product of the number of times outerLoop() was called times the number of inner loop operations performed per call).

double opsRate() const;

Returns the number of inner loop operations per second.

RWBinaryTree

RWBinaryTree - > - RWCollection - > - RWCollectable 

Synopsis

typedef RWBinaryTree SortedCollection;   // Smalltalk typedef.
 
#include <rw/bintree.h>
RWBinaryTree bt;

Description

Class RWBinaryTree represents a group of ordered elements, internally sorted by the compareTo() function. Duplicates are allowed. An object stored by an RWBinaryTree must inherit abstract base class RWCollectable.

Persistence

Polymorphic

Public Constructors

RWBinaryTree();

Construct an empty sorted collection.

RWBinaryTree(const RWBinaryTree& t);

Copy constructor. Constructs a shallow copy from t. Member function balance() (see below) is called before returning.

virtual ~RWBinaryTree();

Redefined from RWCollection. Calls clear().

Public Member Operators

void operator=(const RWBinaryTree& bt);

Sets self to a shallow copy of bt.

void operator+=(const RWCollection ct);

Inserts each element of .ct into self. Note that using this operator to insert an already-sorted collection will result in creating a very unbalanced tree, possibly to the point of stack overflow.

RWBoolean operator<=(const RWBinaryTree& bt) const;

Returns TRUE if self is a subset of the collection bt. That is, every item in self must compare equal to a unique item in bt.

RWBoolean operator==(const RWBinaryTree& bt) const;

Returns TRUE if self and bt are equivalent. That is, they must have the same number of items and every item in self must compare equal to a unique item in bt.

Public Member Functions

virtual void apply(RWapplyCollectable ap, void*);

Redefined from class RWCollection to apply the user-supplied function pointed to by ap to each member of the collection, in order, from smallest to largest. This supplied function should not do anything to the items that could change the ordering of the collection.

void balance();

Special function to balance the tree. In a perfectly balanced binary tree with no duplicate elements, the number of nodes from the root to any external (leaf) node differs by at most one node. Since this collection allows duplicate elements, a perfectly balanced tree is not always possible. Preserves the order of duplicate elements.

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 compares equal to the item pointed to by target, or nil if no item was found.

virtual unsigned hash() const;

Inherited from class RWCollectable.

unsigned height() const;

Returns the number of nodes between the root node and the farthest leaf. A RWBinaryTree with one entry will have a height of 1. Note that the entire tree is traversed to discover this value.

virtual RWCollectable* insert(RWCollectable* c);

Redefined from class RWCollection. Inserts the item c into the collection and returns it. Returns nil if the insertion was unsuccessful. The item c is inserted according to the value returned by compareTo(). insert() does not automatically balance the RWBinaryTree. Be careful not to insert() a long sequence of sorted items without calling balance() since the result will be very unbalanced (and therefore inefficient).

virtual RWClassID isA() const;

Redefined from class RWCollectable to return __RWBINARYTREE.

virtual RWBoolean isEmpty() const;

Redefined from class RWCollection.

virtual RWBoolean isEqual(const RWCollectable* a) const;

Inherited from class RWCollectable.

virtual size_t occurrencesOf(const RWCollectable* target) const;

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

virtual RWCollectable* remove(const RWCollectable* target);

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

virtual void removeAndDestroy(const RWCollectable* target);

Inherited from class RWCollection.

virtual void restoreGuts(RWvistream&); virtual void restoreGuts(RWFile&);

Inherited from class RWCollection.

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

Redefined from class RWCollection to store objects by level, rather than in order. This results in the tree maintaining its morphology.

RWStringID stringID();

(acts virtual) Inherited from class RWCollectable.

RWBinaryTreeIterator

RWBinaryTreeIterator - > - RWIterator 

Synopsis

// Smalltalk typedef:
 
typedef RWBinaryTreeIterator SortedCollectionIterator; 
#include <rw/bintree.h>
RWBinaryTree bt;
RWBinaryTreeIterator iterate(bt);

Description

Iterator for class RWBinaryTree. Traverses the tree from the "smallest" to "largest" element, where "smallest" and "largest" are defined by the virtual function compareTo(). Note that this approach is generally less efficient than using the member function RWBinaryTree::apply().

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

RWBinaryTreeIterator(const RWBinaryTree&);

Constructs an iterator for an RWBinaryTree. Immediately after construction, the position of the iterator is undefined until positioned.

Public Member Operator

virtual RWCollectable* operator()();

Redefined from class RWIterator. Advances iterator to the next "largest" element and returns a pointer to 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 compares equal to the object pointed to by target 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 iterator to its state at construction.

virtual RWCollectable* key() const;

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

RWbistream

            - > - RWvistream - > - RWios - > - RWvios 
RWbistream 
            - > - ios 

Synopsis

#include <rw/bstream.h>
 
RWbistream bstr(cin);        // Construct an RWbistream,
                             // using cin's streambuf

Description

Class RWbistream specializes the abstract base class RWvistream to restore variables stored in binary format by RWbostream.

You can think of it as a binary veneer over an associated streambuf. Because the RWbistream retains no information about the state of its associated streambuf, its use can be freely exchanged with other users of the streambuf (such as an istream or ifstream).

RWbistream can be interrogated as to the stream state using member functions good(), bad(), eof(), etc.

Persistence

None

Example

See RWbostream for an example of how the file "data.dat" might be created.

#include <rw/bstream.h>
#include <fstream.h>
 
main(){
   ifstream fstr("data.dat");    // Open an input file
  RWbistream bstr(fstr);     // Construct RWbistream from it
 
   int i;
   float f;
   double d;
 
   bstr >> i;         // Restore an int that was stored in binary
   bstr >> f >> d;    // Restore a float & double
}

Public Constructors

RWbistream(streambuf* s);

Construct an RWbistream from the streambufs. For DOS, this streambuf must have been opened in binary mode.

RWbistream(istream& str);

Construct an RWbistream using the streambuf associated with the istreamstr. For DOS, the streambuf must have been opened in binary mode. This can be done by specifying ios::binary as part of the second argument to the constructor for an ifstream. Using the example above, the line to create the ifstream would read, ifstream fstr("data.dat", ios::in | ios::binary); where the "|" is the binary OR operator.

Public Operators

virtual RWvistream& operator>>(char& c);

Redefined from class RWvistream. Get the next char from the input stream and store it in c.

virtual RWvistream& operator>>(wchar_t& wc);

Redefined from class RWvistream. Get the next wide char from the input stream and store it in wc.

virtual RWvistream& operator>>(double& d);

Redefined from class RWvistream. Get the next double from the input stream and store it in d.

virtual RWvistream& operator>>(float& f);

Redefined from class RWvistream. Get the next float from the input stream and store it in f.

virtual RWvistream& operator>>(int& i);

Redefined from class RWvistream. Get the next int from the input stream and store it in i.

virtual RWvistream& operator>>(long& l);

Redefined from class RWvistream. Get the next long from the input stream and store it in l.

virtual RWvistream& operator>>(short& s);

Redefined from class RWvistream. Get the next short from the input stream and store it in s.

virtual RWvistream& operator>>(unsigned char& c);

Redefined from class RWvistream. Get the next unsigned char from the input stream and store it in c.

virtual RWvistream& operator>>(unsigned short& s);

Redefined from class RWvistream. Get the next unsigned short from the input stream and store it in s.

virtual RWvistream& operator>>(unsigned int& i);

Redefined from class RWvistream. Get the next unsigned int from the input stream and store it in i.

virtual RWvistream& operator>>(unsigned long& l);

Redefined from class RWvistream. Get the next unsigned long from the input stream and store it in l.

operator void*();

Inherited via RWvistream from RWvios.

Public Member Functions

virtual int get();

Redefined from class RWvistream. Get and return the next char from the input stream. Returns EOF if end of file is encountered.

virtual RWvistream& get(char& c);

Redefined from class RWvistream. Get the next char and store it in c.

virtual RWvistream& get(wchar_t& wc);

Redefined from class RWvistream. Get the next wide char and store it in wc.

virtual RWvistream& get(unsigned char& c);

Redefined from class RWvistream. Get the next unsigned char and store it in c.

virtual RWvistream& get(char* v, size_t N);

Redefined from class RWvistream. Get a vector of chars and store them in the array beginning at v. If the restore operation stops prematurely, because there are no more data available on the stream, because an exception is thrown, or for some other reason; get stores what has already been retrieved from the stream into v, and sets the failbit.

virtual RWvistream& get(wchar_t* v, size_t N);

Redefined from class RWvistream. Get a vector of wide chars and store them in the array beginning at v. If the restore operation stops prematurely, because there are no more data available on the stream, because an exception is thrown, or for some other reason; get stores what has already been retrieved from the stream into v, and sets the failbit.

virtual RWvistream& get(double* v, size_t N);

Redefined from class RWvistream. Get a vector of doubles and store them in the array beginning at v. If the restore operation stops prematurely, because there are no more data available on the stream, because an exception is thrown, or for some other reason; get stores what has already been retrieved from the stream into v, and sets the failbit.

virtual RWvistream& get(float* v, size_t N);

Redefined from class RWvistream. Get a vector of floats and store them in the array beginning at v. If the restore operation stops prematurely, because there are no more data available on the stream, because an exception is thrown, or for some other reason; get stores what has already been retrieved from the stream into v, and sets the failbit.

virtual RWvistream& get(int* v, size_t N);

Redefined from class RWvistream. Get a vector of ints and store them in the array beginning at v. If the restore operation stops prematurely, because there are no more data available on the stream, because an exception is thrown, or for some other reason; get stores what has already been retrieved from the stream into v, and sets the failbit.

virtual RWvistream& get(long* v, size_t N);

Redefined from class RWvistream. Get a vector of longs and store them in the array beginning at v. If the restore operation stops prematurely, because there are no more data available on the stream, because an exception is thrown, or for some other reason; get stores what has already been retrieved from the stream into v, and sets the failbit.

virtual RWvistream& get(short* v, size_t N);

Redefined from class RWvistream. Get a vector of shorts and store them in the array beginning at v. If the restore operation stops prematurely, because there are no more data available on the stream, because an exception is thrown, or for some other reason; get stores what has already been retrieved from the stream into v, and sets the failbit.

virtual RWvistream& get(unsigned char* v, size_t N);

Redefined from class RWvistream. Get a vector of unsigned chars and store them in the array beginning at v. If the restore operation stops prematurely, because there are no more data available on the stream, because an exception is thrown, or for some other reason; get stores what has already been retrieved from the stream into v, and sets the failbit.

virtual RWvistream& get(unsigned short* v, size_t N);

Redefined from class RWvistream. Get a vector of unsigned shorts and store them in the array beginning at v. If the restore operation stops prematurely, because there are no more data available on the stream, because an exception is thrown, or for some other reason; get stores what has already been retrieved from the stream into v, and sets the failbit..

virtual RWvistream& get(unsigned int* v, size_t N);

Redefined from class RWvistream. Get a vector of unsigned ints and store them in the array beginning at v. If the restore operation stops prematurely, because there are no more data available on the stream, because an exception is thrown, or for some other reason; get stores what has already been retrieved from the stream into v, and sets the failbit.

virtual RWvistream& get(unsigned long* v, size_t N);

Redefined from class RWvistream. Get a vector of unsigned longs and store them in the array beginning at v. If the restore operation stops prematurely, because there are no more data available on the stream, because an exception is thrown, or for some other reason; get stores what has already been retrieved from the stream into v, and sets the failbit.

virtual RWvistream& getString(char* s, size_t N);

Redefined from class RWvistream. Restores a character string from the input stream and stores it in the array beginning at s. The function stops reading at the end of the string or after N-1 characters, whichever comes first. If N-1 characters have been read and the Nth character is not the string terminator, then the failbit of the stream will be set. In either case, the string will be terminated with a null byte.

virtual RWvistream& getString(wchar_t* ws, size_t N);

Redefined from class RWvistream. Restores a wide character string from the input stream and stores it in the array beginning at ws. The function stops reading at the end of the string or after N-1 characters, whichever comes first. If N-1 characters have been read and the Nth character is not the string terminator, then the failbit of the stream will be set. In either case, the string will be terminated with a null byte.

RWBitVec

Synopsis

#include <rw/bitvec.h>
 
RWBitVec v;

Description

Class RWBitVec is a bitvector whose length can be changed at run time. Because this requires an extra level of indirection, this makes it slightly less efficient than classes RWGBitVec(size) or RWTBitVec<size> whose lengths are fixed at compile time.

Persistence

Simple

Example

#include <rw/bitvec.h>
#include <rw/rstream.h>
 
main(){
   // Allocate a vector with 20 bits, set to TRUE:
   RWBitVec av(20, TRUE);
 
   av(2) = FALSE;     // Turn bit 2 off
   av.clearBit(7);    // Turn bit 7 off
   av.setBit(2);      // Turn bit 2 back on
 
   for(int i=11; i<=14; i++) av(i) = FALSE;
 
   cout << av << endl;    // Print the vector out
}

Program Output:

   [
   1 1 1 1 1 1 1 0 1 1 1 0 0 0 0 1 1 1 1 1
   ]

Public Constructors

RWBitVec();

Construct a zero lengthed (null) vector.

RWBitVec(size_t N);

Construct a vector with N bits. The initial value of the bits is undefined.

RWBitVec(size_t N, RWBoolean initVal);

Construct a vector with N bits, each set to the Boolean value initVal.

RWBitVec(const RWByte* bp, size_t N);

Construct a vector with N bits, initialized to the data in the array of bytes pointed to by bp. This array must be at least long enough to contain N bits. The identifier RWByte is a typedef for an unsigned char.

RWBitVec(const RWBitVec& v);

Copy constructor. Uses value semantics — the constructed vector will be a copy of v.

~RWBitVec();

The destructor. Releases any allocated memory.

Assignment Operators

RWBitVec& operator=(const RWBitVec& v);

Assignment operator. Value semantics are used — self will be a copy of v.

RWBitVec& operator=(RWBoolean b);

Assignment operator. Sets every bit in self to the boolean value b.

RWBitVec& operator&=(const RWBitVec& v); RWBitVec& operator^=(const RWBitVec& v); RWBitVec& operator|=(const RWBitVec& v);

Logical assignments. Set each element of self to the logical AND, XOR, or OR, respectively, of self and the corresponding bit in v. Self and v must have the same number of elements (i.e., be conformal) or an exception of type RWInternalErr will occur.

Indexing Operators

RWBitRef operator[](size_t i);

Returns a reference to bit i of self. A helper class, RWBitRef, is used. The result can be used as an lvalue. The index i must be between 0 and the length of the vector less one. Bounds checking is performed. If the index is out of range, then an exception of type RWBoundsErr will occur.

RWBitRef operator()(size_t i);

Returns a reference to bit i of self. A helper class, RWBitRef, is used. The result can be used as an lvalue. The index i must be between 0 and the length of the vector less one. Bounds checking is performed only if the preprocessor macro RWBOUNDS_CHECK has been defined before including the header file <rw/bitvec.h>. If so, and if the index is out of range, then an exception of type RWBoundsErr will occur.

RWBoolean operator[](size_t i) const;

Returns the boolean value of bit i. The result cannot be used as an lvalue. The index i must be between 0 and the length of the vector less one. Bounds checking is performed. If the index is out of range, then an exception of type RWBoundsErr will occur.

RWBoolean operator()(size_t i) const;

Returns the boolean value of bit i. The result cannot be used as an lvalue. The index i must be between 0 and the length of the vector less one. Bounds checking is performed only if the preprocessor macro RWBOUNDS_CHECK has been defined before including the header file <rw/bitvec.h>. If so, and if the index is out of range, then an exception of type RWBoundsErr will occur.

Logical Operators

RWBoolean operator==(const RWBitVec& u) const;

Returns TRUE if self and v have the same length and if each bit of self is set to the same value as the corresponding bit in v. Otherwise, returns FALSE.

RWBoolean operator!=(const RWBitVec& u) const;

Returns FALSE if self and v have the same length and if each bit of self is set to the same value as the corresponding bit in v. Otherwise, returns TRUE.

RWBoolean operator==(RWBoolean b) const;

Returns TRUE if every bit of self is set to the boolean value b. Otherwise FALSE.

RWBoolean operator!=(RWBoolean b) const;

Returns FALSE if every bit of self is set to the boolean value b. Otherwise TRUE.

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 the length of the vector less one. 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.

size_t firstFalse() const;

Returns the index of the first FALSE bit in self. Returns RW_NPOS if there is no FALSE bit.

size_t firstTrue() const;

Returns the index of the first TRUE bit in self. Returns RW_NPOS if there is no TRUE bit.

unsigned hash() const;

Returns a value suitable for hashing.

RWBoolean isEqual(const RWBitVec& v) const;

Returns TRUE if self and v have the same length and if each bit of self is set to the same value as the corresponding bit in v. Otherwise, returns FALSE.

size_t length() const;

Returns the number of bits in the vector.

ostream& printOn(ostream& s) const;

Print the vector v on the output stream s. See the example above for a sample of the format.

void resize(size_t N);

Resizes the vector to have length N. If this results in a lengthening of the vector, the additional bits will be set to FALSE.

istream& scanFrom(istream&);

Read the bit vector from the input stream s. The vector will dynamically be resized as necessary. The vector should be in the same format printed by member function printOn(ostream&).

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

RWBitVec operator!(const RWBitVec& v);

Unary operator that returns the logical negation of vector v.

RWBitVec operator&(const RWBitVec&,const RWBitVec&); RWBitVec operator^(const RWBitVec&,const RWBitVec&); RWBitVec operator|(const RWBitVec&,const RWBitVec&);

Returns a vector that is the logical AND, XOR, or OR of the vectors v1 and v2. The two vectors must have the same length or an exception of type RWInternalErr will occur.

ostream& operator<<(ostream& s, const RWBitVec& v);

Calls v.printOn(s).

istream& operator>>(istream& s, RWBitVec& v);

Calls v.scanFrom(s).

RWvostream& operator<<(RWvostream&, const RWBitVec& vec); RWFile& operator<<(RWFile&, const RWBitVec& vec);

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

RWvistream& operator>>(RWvistream&, RWBitVec& vec);

RWFile& operator>>(RWFile&, RWBitVec& vec);

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

size_t sum(const RWBitVec& v);

Returns the total number of bits set in the vector v.

RWbostream

            - > - RWvostream - > - RWios - > - RWvios 
RWbostream 
           - > - ios 

Synopsis

#include <rw/bstream.h>
 
// Construct an RWbostream, using cout's streambuf:
RWbostream bstr(cout);

Description

Class RWbostream specializes the abstract base class RWvostream to store variables in binary format. The results can be restored by using its counterpart RWbistream.

You can think of it as a binary veneer over an associated streambuf. Because the RWbostream retains no information about the state of its associated streambuf, its use can be freely exchanged with other users of the streambuf (such as ostream or ofstream). Note that variables should not be separated with white space. Such white space would be interpreted literally and would have to be read back in as a character string.

RWbostream can be interrogated as to the stream state using member functions good(), bad(), eof(), etc.

Persistence

None

Example

See RWbistream for an example of how the file "data.dat" might be read back in.

#include <rw/bstream.h>
#include <fstream.h>
 
main(){
  ofstream fstr("data.dat");   // Open an output file
   RWbostream bstr(fstr);       // Construct an RWbostream from it
 
  int i = 5;
  float f = 22.1;
  double d = -0.05;
 
  bstr << i;             // Store an int in binary
  bstr << f << d;        // Store a float & double
}

Public Constructors

RWbostream(streambuf* s);

Construct an RWbostream from the streambufs. For DOS, the streambuf must have been opened in binary mode.

RWbostream(ostream& str);

Construct an RWbostream from the streambuf associated with the output stream str. For DOS, the streambuf must have been opened in binary mode. This can be done by specifying ios::binary as part of the second argument to the constructor for an ofstream. Using the example above, the line to create the ofstream would read, ofstream fstr("data.dat", ios::out | ios::binary); where the "|" is the binary OR operator.

Public Destructor

virtual ~RWvostream();

This virtual destructor allows specializing classes to deallocate any resources that they may have allocated.

Public Operators

virtual RWvostream& operator<<(const char* s);

Redefined from class RWvostream. Store the character string starting at s to the output stream in binary. The character string is expected to be null terminated.

virtual RWvostream& operator<<(const wchar_t* ws);

Redefined from class RWvostream. Store the wide character string starting at ws to the output stream in binary. The wide character string is expected to be null terminated.

virtual RWvostream& operator<<(char c);

Redefined from class RWvostream. Store the charc to the output stream in binary.

virtual RWvostream& operator<<(wchar_t wc);

Redefined from class RWvostream. Store the wide charwc to the output stream in binary.

virtual RWvostream& operator<<(unsigned char c);

Redefined from class RWvostream. Store the unsigned charc to the output stream in binary.

virtual RWvostream& operator<<(double d);

Redefined from class RWvostream. Store the doubled to the output stream in binary.

virtual RWvostream& operator<<(float f);

Redefined from class RWvostream. Store the floatf to the output stream in binary.

virtual RWvostream& operator<<(int i);

Redefined from class RWvostream. Store the inti to the output stream in binary.

virtual RWvostream& operator<<(unsigned int i);

Redefined from class RWvostream. Store the unsigned inti to the output stream in binary.

virtual RWvostream& operator<<(long l);

Redefined from class RWvostream. Store the longl to the output stream in binary.

virtual RWvostream& operator<<(unsigned long l);

Redefined from class RWvostream. Store the unsigned longl to the output stream in binary.

virtual RWvostream& operator<<(short s);

Redefined from class RWvostream. Store the shorts to the output stream in binary.

virtual RWvostream& operator<<(unsigned short s);

Redefined from class RWvostream. Store the unsigned shorts to the output stream in binary.

operator void*();

Inherited via RWvostream from RWvios.

Public Member Functions

virtual RWvostream& flush();

Send the contents of the stream buffer to output immediately.

virtual RWvostream& put(char c);

Redefined from class RWvostream. Store the charc to the output stream.

virtual RWvostream& put(wchar_t wc);

Redefined from class RWvostream. Store the wide character wc to the output stream.

virtual RWvostream& put(unsigned char c);

Redefined from class RWvostream. Store the unsigned charc to the output stream.

virtual RWvostream& put(const char* p, size_t N);

Redefined from class RWvostream. Store the vector of chars starting at p to the output stream in binary.

virtual RWvostream& put(const wchar_t* p, size_t N);

Redefined from class RWvostream. Store the vector of wide chars starting at p to the output stream in binary.

virtual RWvostream& put(const unsigned char* p, size_t N);

Redefined from class RWvostream. Store the vector of unsigned chars starting at p to the output stream in binary.

virtual RWvostream& put(const short* p, size_t N);

Redefined from class RWvostream. Store the vector of shorts starting at p to the output stream in binary.

virtual RWvostream& put(const unsigned short* p, size_t N);

Redefined from class RWvostream. Store the vector of unsigned shorts starting at p to the output stream in binary.

virtual RWvostream& put(const int* p, size_t N);

Redefined from class RWvostream. Store the vector of ints starting at p to the output stream in binary.

virtual RWvostream& put(const unsigned int* p, size_t N);

Redefined from class RWvostream. Store the vector of unsigned ints starting at p to the output stream in binary.

virtual RWvostream& put(const long* p, size_t N);

Redefined from class RWvostream. Store the vector of longs starting at p to the output stream in binary.

virtual RWvostream& put(const unsigned long* p, size_t N);

Redefined from class RWvostream. Store the vector of unsigned longs starting at p to the output stream in binary.

virtual RWvostream& put(const float* p, size_t N);

Redefined from class RWvostream. Store the vector of floats starting at p to the output stream in binary.

virtual RWvostream& put(const double* p, size_t N);

Redefined from class RWvostream. Store the vector of doubles starting at p to the output stream in binary.

virtual RWvostream& putString(const char* p, size_t N);

Redefined from class RWvostream. Data is formatted as a string containing N characters.

virtual RWvostream& putString(const char*s, size_t N);

Store the character string, including embedded nulls, starting at s to the output string.