Chapter 5. RWpistream - RWTPtrDeque<T>

RWpistream

RWpistream ->- RWvistream ->- RWvios 

Synopsis

#include <rw/pstream.h>
 
RWpistream pstr(cin); // Construct an RWpistream, using cin's
                      // streambuf

Description

Class RWpistream specializes the abstract base class RWvistream to restore variables stored in a portable ASCII format by RWpostream.

You can think of RWpistream and RWpostream as an ASCII veneer over an associated streambuf which are responsible for formatting variables and escaping characters such that the results can be interchanged between any machines. As such, they are slower than their binary counterparts RWbistream and RWbostream which are more machine dependent. Because RWpistream and RWpostream retain no information about the state of their associated streambufs, their use can be freely exchanged with other users of the streambuf (such as istream or ifstream).

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

Persistence

None

Example

See RWpostream for an example of how to create an input stream for this program.

#include <rw/pstream.h>
 
main(){
   // Construct an RWpistream to use standard input
   RWpistream pstr(cin);
 
   int i;
   float f;
   double d;
   char string[80];
 
   pstr >> i;  // Restore an int that was stored in binary
   pstr >> f >> d;  // Restore a float & double
   pstr.getString(string, 80);  // Restore a character string
}

Public Constructors

RWpistream(streambuf* s);

Initialize an RWpistream from the streambuf s.

RWpistream(istream& str);

Initialize an RWpistream using the streambuf associated with the istream str.

Public Operators

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

Redefined from class RWvistream. Get the next character from the input stream and store it in c. This member attempts to preserve the symbolic characters values transmitted over the stream.

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

Redefined from class RWvistream. Get the next widechar 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 character 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. This member only preserves ASCII numerical codes, not the coresponding character symbol.

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 unsignedchar 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. Note that this member preserves ASCII numerical codes, not their corresponding character values. If you wish to restore a character string, use the function getString(char*, size_t).

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. Note that this member preserves ASCII numerical codes, not their corresponding character values. If you wish to restore a character string, use the function getString(char*, size_t).

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. Note that this member preserves ASCII numerical codes, not their corresponding character values. If you wish to restore a character string, use the function getString(char*, size_t).

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. If the input stream has been corrupted, then an exception of type RWExternalErr will be thrown.

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

Redefined from class RWvistream. Restores a 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. If the input stream has been corrupted, then an exception of type RWExternalErr will be thrown.

RWpostream

RWpostream ->- RWvostream ->- RWvios 

Synopsis

#include <rw/pstream.h>
 
// Construct an RWpostream, using cout's streambuf:
RWpostream pstr(cout) ;

Description

Class RWpostream specializes the abstract base class RWvostream to store variables in a portable (printable) ASCII format. The results can be restored by using its counterpart RWpistream.

You can think of RWpistream and RWpostream as an ASCII veneer over an associated streambuf which are responsible for formatting variables and escaping characters such that the results can be interchanged between any machines. As such, they are slower than their binary counterparts RWbistream and RWbostream which are more machine dependent. Because RWpistream and RWpostream retain no information about the state of their associated streambufs, their use can be freely exchanged with other users of the streambuf (such as istream or ifstream).

The goal of class RWpostream and RWpistream is to store variables using nothing but printable ASCII characters. Hence, nonprintable characters must be converted into an external representation where they can be recognized. Furthermore, other characters may be merely bit values (a bit image, for example), having nothing to do with characters as symbols. For example,

RWpostream pstrm(cout);
char c = `\n';
 
pstr << c;     // Stores "newline"
pstr.put©;     // Stores the number 10.
 

The expression "pstr << c" treats c as a symbol for a newline, an unprintable character. The expression "pstr.put©" treats c as the literal number "10".


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

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

Persistence

None

Example

See RWpistream for an example of how to read back in the results of this program. The symbol "o" is intended to represent a control-G, or bell.

#include <rw/pstream.h>
 
main(){
   // Construct an RWpostream to use standard output:
   RWpostream pstr(cout);
 
  int i = 5;
   float f = 22.1;
   double d = -0.05;
   char string[]
           = "A string with\ttabs,\nnewlines and a o bell.";
 
   pstr << i;        // Store an int in binary
   pstr << f << d;   // Store a float & double
   pstr << string;   // Store a string
}

Program Output:

5
22.1
-0.05
"A string with\ttabs,\nnewlines and a \x07 bell."

Public Constructors

RWpostream(streambuf* s);

Initialize an RWpostream from the streambuf s.

RWpostream(ostream& str);

Initialize an RWpostream from the streambuf associated with the output stream str.

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 using a portable format. 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 using a portable format. The character string is expected to be null terminated.

virtual RWvostream& operator<<(char c);

Redefined from class RWvostream. Store the char c to the output stream using a portable format. Note that c is treated as a character, not a number. This member attempts to preserve the symbolic characters values transmitted over the stream

virtual RWvostream& operator<<(wchar_t wc);

Redefined from class RWvostream. Store the wide charwc to the output stream using a portable format. Note that wc is treated as a character, not a number.

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

Redefined from class RWvostream. Store the unsigned charc to the output stream using a portable format. Note that c is treated as a character, not a number.

virtual RWvostream& operator<<(double d);

Redefined from class RWvostream. Store the doubled to the output stream using a portable format.

virtual RWvostream& operator<<(float f);

Redefined from class RWvostream. Store the floatf to the output stream using a portable format.

virtual RWvostream& operator<<(int i);

Redefined from class RWvostream. Store the inti to the output stream using a portable format.

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

Redefined from class RWvostream. Store the unsigned int i to the output stream using a portable format.

virtual RWvostream& operator<<(long l);

Redefined from class RWvostream. Store the long l to the output stream using a portable format.

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

Redefined from class RWvostream. Store the unsigned long l to the output stream using a portable format.

virtual RWvostream& operator<<(short s);

Redefined from class RWvostream. Store the short s to the output stream using a portable format.

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

Redefined from class RWvostream. Store the unsigned short s to the output stream using a portable format.

operator void*();

Inherited via RWvostream from RWvios.

Public Member Functions

int precision() const;

Returns the currently set precision used for writing float and double data. At construction, the precision is set to RW_DEFAULT_PRECISION (defined in compiler.h.)

int precision(int p);

Changes the precision used for writing float and double data. Returns the previously set precision. At construction, the precision is set to RW_DEFAULT_PRECISION (defined in compiler.h.)

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, preserving its value using a portable format. This member only preserves ASCII numerical codes, not the coresponding character symbol.

virtual RWvostream& put(wchar_t wc);

Redefined from class RWvostream. Store the wide character wc to the output stream, preserving its value using a portable format.

virtual RWvostream& put(unsigned char c);

Redefined from class RWvostream. Store the unsigned charc to the output stream, preserving its value using a portable format.

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, preserving their values using a portable format. Note that the characters will be treated as literal numbers (i.e., not as a character string).

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, preserving their values using a portable format. Note that the characters will be treated as literal numbers (i.e., not as a character string).

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 using a portable format. The characters should be treated as literal numbers (i.e., not as a character string).

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 using a portable format.

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 using a portable format.

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 using a portable format.

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 using a portable format.

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 using a portable format.

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 using a portable format.

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 using a portable format.

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 using a portable format.

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

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

RWSequenceable

RWSequenceable ->- RWCollection ->- RWCollectable 

Synopsis

#include <rw/seqcltn.h>
 
typedef RWSequenceable SequenceableCollection;
              // Smalltalk typedef

Description

Class RWSequenceable is an abstract base class for collections that can be accessed by an index. It inherits class RWCollection as a public base class and adds a few extra virtual functions. This documentation only describes these extra functions.

Persistence

Polymorphic

Public Member Functions

RWCollectable* append(RWCollectable*) = 0;

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

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

Allows access to the ith element of 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 RWBoundsErr will be thrown.

virtual RWCollectable* first() const = 0;

Returns the first item in the collection.

virtual size_t index(const RWCollectable* c) const = 0;

Returns the index number of the first item that "matches" the item pointed to by c. If there is no such item, returns RW_NPOS. For most collections, an item "matches" the target if either isEqual() or compareTo() find equivalence, whichever is appropriate for the actual collection type.

void insertAt(size_t indx, RWCollectable* 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 RWBoundsErr will be thrown.

virtual RWCollectable* last() const = 0;

Returns the last item in the collection.

RWCollectable* prepend(RWCollectable*) = 0;

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

RWSet

RWSet ->- RWHashTable ->- RWCollection ->- RWCollectable 

Synopsis

typedef RWSet Set; // Smalltalk typedef.
#include <rw/rwset.h>
 
RWSet h ;

Description

Class RWSet represents a group of unordered elements, not accessible by an external key, where duplicates are not allowed. It corresponds to the Smalltalk class Set.

An object stored by RWSet 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.

An item c is considered to be "already in the collection" if there is a member of the collection with the same has value as c for which isEqual(c) returns TRUE. In this case, method insert(c) will not add it, thus insuring that there are no duplicates.

The iterator for this class is RWSetIterator.

Persistence

Polymorphic

Public Constructors

RWSet (size_t n = RWDEFAULT_CAPACITY);

Constructs an empty set with n hashing buckets.

RWSet (const RWSet & h);

Copy constructor. Makes a shallow copy of the collection h.

virtual ~RWSet();

Calls clear().

Public Member Operators

void operator=(const RWSet& h);

Assignment operator. Makes a shallow copy of the collection h.

RWBoolean operator==(const RWSet& h);

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

RWBoolean operator!=(const RWSet& h);

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

RWBoolean operator<=(const RWSet& h);

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


Note: If you inherit from RWSet in the presence of the C++ Standard 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 RWSet& h);

Returns TRUE if self is a proper subset of h, that is, every element of self has a counterpart in h which isEqual, but where the two sets are not identical.

RWSet& operator*=(const RWSet& h);

Sets self to be the intersection of self and h. Returns self.

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 a (generally) unpredictable order. This supplied function must not do anything to the items that could change the ordering of the collection.

virtual RWspace binaryStoreSize() const;

Inherited from class RWCollection.

virtual void clear();

Inherited from class RWCollection.

virtual void clearAndDestroy();

Redefined 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;

Inherited from class RWCollection.

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

Returns the item in self which isEqual to the item pointed to by target 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);

Adds c to the collection and returns it. If an item is already in the collection which isEqual to c, then the old item is returned and the new item is not inserted.

virtual RWClassID isA() const;

Redefined from class RWCollectable to return __RWSET.

virtual RWBoolean isEmpty() const;

Inherited from class RWCollectable.

virtual RWBoolean isEqual(const RWCollectable* a) const;

Redefined from class RWCollection.

void intersectWith(const RWSet& h, RWSet& ret) const;

Computes the intersection of self and h, and inserts the result into ret (which may be either empty or not, depending on the effect desired). It may be slightly more efficient than operator*=().

virtual size_t occurrencesOf(const RWCollectable* target) const;

Redefined from class RWCollection. Returns the count of entries that isEqual to the item pointed to by target. Because duplicates are not allowed for this collection, only 0 or 1 can be returned.

virtual RWCollectable* remove(const RWCollectable* target);

Redefined from class RWCollection. Returns and removes the item that isEqual to the item pointed to by target, or nil if there is no item.

virtual void removeAndDestroy(const RWCollectable* target);

Inherited from class RWCollection.

void resize(size_t n = 0);

Resizes the internal hashing table to leave n slots. If n==0, resizes to 3*entries()/2.

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.

RWSetIterator

RWSetIterator ->- RWHashTableIterator ->- RWIterator 

Synopsis

#include <rw/rwset.h>
 
RWSet h;
RWSetIterator it(h) ;

Description

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

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.

Persistence

None

Public Constructor

RWSetIterator(RWSet&);

Construct an iterator for an RWSet. After construction, the position of the iterator will be undefined.

Public Member Operator

virtual RWCollectable* operator()();

Inherited from RWHashTableIterator.

Public Member Functions

virtual RWCollectable* findNext(const RWCollectable* target);

Inherited from RWHashTableIterator.

virtual RWCollectable* key() const;

Inherited from RWHashTableIterator.

RWCollectable* remove();

Inherited from RWHashTableIterator.

RWCollectable* removeNext(const RWCollectable*);

Inherited from RWHashTableIterator.

virtual void reset();

Inherited from RWHashTableIterator.

rw_slist<T>

Synopsis

#include <rw/rwstl/slist.h>
rw_slist<T> list;

Description

Class rw_slist<T> maintains a collection of T, implemented as a singly-linked list. Since this is a value based list, objects are copied into and out of the links that make up the list. As with all classes that meet the ANSI sequence specification, rw_slist provides for iterators that reference its elements. Operations that alter the contents of rw_slist will invalidate iterators that reference items at or after the location of change.

Public Typedefs

typedef T                  value_type;
typedef T&                 reference;
typedef const T&           const_reference;
typedef (unsigned)         size_type; //from Allocator<Node>

Iterators over rw_slist<T> are forward iterators.

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

Public Constructors

rw_slist<T>();

Construct an empty rw_slist<T>.

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

Construct an rw_slist<T> which is a copy of list. Each element from list will be copied into self.

rw_slist<T>(size_type count, const T& value);

Construct an rw_slist<T> containing exactly count copies of value.

rw_slist<T>(const_iterator first, const_iterator bound);

Construct an rw_slist<T> containing a copy of each element referenced by the range starting at first and bounded by bound.

rw_slist<T>(const T* first, const T* bound);

Construct an rw_slist<T> containing a copy of each element referenced by the range starting at first and bounded by bound.

Public Destructor

~rw_slist<T>();

The destructor releases the memory used by the links.

Accessors

iterator begin();

The iterator returned references the first item in self. If self is empty, the iterator is equal to end().

const_iterator begin() const;

The iterator returned references the first item in self. If self is empty, the iterator is equal to end().

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.

T& front();

References the first item in the list as an L-value. If self is empty, the behavior is undefined.

const T& front();

References the first item in the list as an R-value. If self is empty, the behavior is undefined.

Const Public Member Functions

bool empty() const;

Returns true if self is empty.

size_type size() const;

Returns the number of items currently held in self.

Mutators

iterator erase(iterator iter);

Removes from self the element referenced by iter. If iter does not reference an actual item contained in self, the effect is undefined. Returns an iterator referencing the location just after the erased item.

iterator erase(iterator first, iterator bound);

Removes from self the elements referenced by the range beginning at first and bounded by bound. Returns an iterator referencing a position just after the last erased item. If first does not reference an item in self (and if first and bound are not equal), the effect is undefined.

iterator insert(iterator loc, const T& val);

Insert val just prior to the place referenced by loc. Returns an iterator referencing the newly inserted element.

(Note: ++(list.insert(loc,val))==loc; )

iterator insert(iterator loc, const_iterator first, const_iterator bound);

Insert a copy of each item in the range beginning at first and bounded by bound into self at a place just prior to the place referenced by loc. Returns an iterator referencing the last newly inserted element.

(Note: ++(list.insert(loc,first,bound))==loc; )

iterator insert(iterator loc, const T* first, const T* bound);

Insert a copy of each item in the range beginning at first and bounded by bound into self at a place just prior to the place referenced by loc. Returns an iterator referencing the last newly inserted element.

(Note: ++(list.insert(loc,first,bound))==loc; )

void pop_front();

Erases the first element of self. If self is empty, the effect is undefined.

void push_back(const T& item);

Inserts item as the last element of the list.

void push_front(const T& item);

Inserts item as the first element of the list.

void reverse();

Reverses the order of the nodes containing the elements in self.

void sort();

Sorts self according to T::operator<(T) or equivalent. Runs in time proportional to N log(N) where N is the number of elements.This is method does not copy or destroy any of the items exchanged during the sort, but adjusts the order of the links in the list.

void swap(rw_slist<T>& other);

Exchanges the contents of self with other retaining the ordering of each. This is method does not copy or destroy any of the items exchanged, but re-links the lists.

void unique();

Removes from self all but the first element from each equal range. A precondition is that any duplicate elements are adjacent.

Special Methods for Lists

void merge(rw_slist& donor);

Assuming both donor and self are sorted, moves every item from donor into self, leaving donor empty, and self sorted. If either list is unsorted, the move will take place, but the result may not be sorted. This method does not copy or destroy the items in donor, but re-links list nodes into self.

void splice(iterator to, rw_slist<T>& donor);

Insert the entire contents of donor into self, just before the position referenced by to, leaving donor empty. This method does not copy or destroy any of the items moved, but re-links the list nodes from donor into self.

void splice(iterator to, rw_slist<T>& donor, iterator from);

Remove from donor and insert into self, just before location to, the item referenced by from. If from does not reference an actual item contained in donor the effect is undefined. This method does not copy or destroy the item referenced by from, but re-links the node containing it from donor into self.

void splice(iterator to, rw_slist<T>& donor, iterator from_start, iterator from_bound);

Remove from donor and insert into self just before location to, the items referenced by the range beginning with from_start and bounded by from_bound. If that range does not refer to items contained by donor, the effect is undefined. This method does not copy or destroy the items referenced by the range, but re-links those list nodes from donor into self.

Related Global Operators

bool operator==(const rw_slist<T>& lhs, const rw_slist<T>& rhs);

Returns true if lhs and rhs have the same number of elements and each element of rhs tests equal (T::operator==() or equivalent) to the corresponding element of lhs.

bool operator<(const rw_slist<T>& lhs, const rw_slist<T>& rhs);

Returns the result of calling

             lexicographical_compare(lhs.begin(), lhs.end(),                  rhs.begin(), rhs.end());

RWSlistCollectables

RWSlistCollectables ->- RWSequenceable ->- RWCollection ->-...
      ...->- RWCollectable 

Synopsis

// Smalltalk typedef:
 
typedef RWSlistCollectables LinkedList ;
#include <rw/slistcol.h>
RWSlistCollectables a;

Description

Class RWSlistCollectables represents a group of ordered elements, without keyed access. Duplicates are allowed. The ordering of elements is determined externally, by the order of insertion and removal. An object stored by RWSlistCollectables must inherit abstract base class RWCollectable.

The virtual function isEqual() (see class RWCollectable) is required to find a match between a target and an item in the collection

Class RWSlistCollectables is implemented as a singly-linked list, which allows for efficient insertion and removal, but efficient movement in only one direction. This class corresponds to the Smalltalk class LinkedList.

Persistence

Polymorphic

Public Constructors

RWSlistCollectables();

Constructs an empty linked list.

RWSlistCollectables(RWCollectable* a);

Constructs a linked list with single item a.

Public Member Operators

RWBoolean operator==(const RWSlistCollectables& s) const;

Returns TRUE if self and s have the same number of members and if for every item in self, the corresponding item at the same index in sisEqual to it.

Public Member Functions

virtual RWCollectable* append(RWCollectable*);

Redefined from RWSequenceable. Inserts the item at the end of the collection and returns it. Returns nil if the insertion was unsuccessful.

virtual void apply(RWapplyCollectable ap, void*);

Redefined from class RWCollection. This function has been redefined to apply the user-defined 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. The index i must be between 0 and the number of items in the collection less one, or an exception of type RWBoundsErr will be thrown. Note that for a linked list, these functions must traverse all the links, making them not particularly efficient.

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.

RWBoolean containsReference(const RWCollectable* e) const;

Returns true if the list contains an item that is identical to the item pointed to by e (that is, that has the address e).

virtual size_t entries() const;

Redefined from class RWCollection.

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

Redefined from class RWCollection. The first item that matches target is returned, or nil if no item was found.

RWCollectable* findReference(const RWCollectable* e) const;

Returns the first item that is identical to the item pointed to by e (that is, that has the address e), or nil if none is found.

virtual RWCollectable* first() const;

Redefined from class RWSequenceable. Returns the item at the beginning of the list.

RWCollectable* get();

Returns and removes the item at the beginning of the list.

virtual unsigned hash() const;

Inherited from class RWCollectable.

virtual size_t index(const RWCollectable* c) const;

Redefined from class RWSequenceable. Returns the index of the first item that isEqual to the item pointed to by c. If there is no such item, returns RW_NPOS.

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

virtual RWBoolean isEmpty() const;

Redefined from class RWCollection.

virtual RWCollectable* last() const;

Redefined from class RWSequenceable. Returns the value at the end of the collection.

virtual size_t occurrencesOf(const RWCollectable* target) const;

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

size_t occurrencesOfReference(const RWCollectable* e) const;

Returns the number of items that are identical to the item pointed to by e (that is, that have the address e).

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

virtual RWCollectable* remove(const RWCollectable* target);

Redefined from class RWCollection. Removes and returns the first item that isEqual to the item pointed to by target. Returns nil if there is no such item.

virtual void removeAndDestroy(const RWCollectable* target);

Inherited from class RWCollection.

RWCollectable* removeReference(const RWCollectable* e);

Removes and returns the first item that is identical to the item pointed to by e (that is, that has the address e). Returns nil if there is no such item.

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.

RWSlistCollectablesIterator

RWSlistCollectablesIterator ->- RWIterator 

Synopsis

// Smalltalk typedef.
 
typedef RWSlistCollectablesIterator LinkedListIterator;
#include <rw/slistcol.h>
RWSlistCollectables sc;
RWSlistCollectablesIterator sci(sc) ;

Description

Iterator for class RWSlistCollectables. Traverses the linked-list from the first to last item.

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

RWSlistCollectablesIterator (RWSlistCollectables&);

Constructs an iterator from a singly-linked list. Immediately after construction, the position of the iterator will be undefined.

Public Member Operators

virtual RWCollectable* operator()();

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

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 beginning of the list, otherwise FALSE;

RWBoolean atLast() const;

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

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. If no item is found, returns nil and the position of the iterator will be undefined.

RWCollectable* findNextReference(const RWCollectable* e);

Moves iterator to the next item which is identical to the item pointed to by e (that is, that has address e) and returns it. If no item is found, returns nil and the position of the iterator will be undefined.

RWCollectable* insertAfterPoint(RWCollectable* a);

Insert item a after the current cursor position and return the item. The cursor's position will be unchanged.

virtual RWCollectable* key() const;

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

RWCollectable* remove();

Removes and returns the item at the current cursor position. Afterwards, the iterator will be positioned at the previous item in the list. This function is not very efficient in a singly-linked list.

RWCollectable* removeNext(const RWCollectable* target);

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

RWCollectable* removeNextReference(const RWCollectable* e);

Moves iterator to the next item in the list which is identical to the item pointed to by e (that is, that has address e), removes it from the list and returns it. Afterwards, the iterator will be positioned at the previous item in the list. 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. Afterwards, the position of the iterator will be undefined.

void toFirst();

Moves the iterator to the beginning of the list.

void toLast();

Moves the iterator to the end of the list.

RWSlistCollectablesQueue

RWSlistCollectablesQueue ->- RWSlistCollectables ->-...
...->- RWSequenceable ->- RWCollection ->- RWCollectable 

Synopsis

// Smalltalk typedef:
 
typedef RWSlistCollectablesQueue Queue ;
#include <rw/queuecol.h>
RWSlistCollectablesQueue a;

Description

Class RWSlistCollectablesQueue represents a restricted interface to class RWSlistCollectables to implement a first in first out (FIFO) queue. A queue is a sequential list for which all insertions are made at one end (the "tail"), but all removals are made at the other end (the "head"). Hence, the ordering is determined externally by the ordering of the insertions. Duplicates are allowed.

An object stored by RWSlistCollectablesQueue must inherit abstract base class RWCollectable. The virtual function isEqual() (see class RWCollectable) is required, to find a match between a target and an item in the queue.

This class corresponds to the Smalltalk class Queue

Persistence

Polymorphic

Public Constructors

RWSlistCollectablesQueue();

Construct an empty queue.

RWSlistCollectablesQueue(RWCollectable* a);

Construct an queue with single item a.

RWSlistCollectablesQueue(const RWSlistCollectablesQueue & q);

Copy constructor. A shallow copy of the queue q is made.

Public Member Operators

void operator=(const RWSlistCollectablesQueue & q);

Assignment operator. A shallow copy of the queue q is made.

Public Member Functions

virtual void apply(RWapplyCollectable ap, void*);

Inherited from class RWSlistCollectables.

virtual RWCollectable* append(RWCollectable*);

Inherited from class RWSlistCollectables. Adds an element to the end of the queue.

virtual RWspace binaryStoreSize() const;

Inherited from class RWCollection.

virtual void clear();

Inherited from class RWSlistCollectables.

virtual void clearAndDestroy(); virtual RWBoolean contains(const RWCollectable* target) const;

Inherited from class RWCollection.

RWBoolean containsReference(const RWCollectable* e) const; virtual size_t entries() const;

Inherited from class RWSlistCollectables.

virtual RWCollectable* first() const;

Inherited from class RWSlistCollectables. Returns the item at the beginning of the queue (i.e., the least recently inserted item). Returns nil if the queue is empty.

RWCollectable* get();

Inherited from class RWSlistCollectables. Returns and removes the item at the beginning of the queue (i.e., the least recently inserted item). Returns nil if the queue is empty.

virtual RWCollectable* insert(RWCollectable* c);

Redefined from class RWSlistCollectables to call append().

virtual RWClassID isA() const;

Redefined from class RWCollectable to return __RWSLISTCOLLECTABLESQUEUE.

virtual RWBoolean isEmpty() const;

Inherited from class RWSlistCollectables.

virtual RWCollectable* last() const;

Inherited from class RWSlistCollectables. Returns the last item in the queue (the most recently inserted item).

virtual size_t occurrencesOf(const RWCollectable* target) const; size_t occurrencesOfReference(const RWCollectable* e) const;

Inherited from class RWSlistCollectables.

virtual RWCollectable* remove(const RWCollectable*);

Redefined from class RWSlistCollectables. Calls get(). The argument is ignored.

RWSlistCollectablesStack

RWSlistCollectablesStack ->- RWSlistCollectables ->- RWSequenceable...
      ...->- RWCollection ->- RWCollectable  

Synopsis

// Smalltalk typedef:
 
typedef RWSlistCollectablesStack Stack; 
#include <rw/stackcol.h>
RWSlistCollectablesStack a;

Description

Class RWSlistCollectablesStack represents a restricted interface to class RWSlistCollectables to implement a last in first out (LIFO) stack. A Stack is a sequential list for which all insertions and deletions are made at one end (the beginning of the list). Hence, the ordering is determined externally by the ordering of the insertions. Duplicates are allowed.

An object stored by RWSlistCollectablesStack must inherit abstract base class RWCollectable. The virtual function isEqual() (see class RWCollectable) is required, to find a match between a target and an item in the stack.

This class corresponds to the Smalltalk class Stack.

Persistence

Polymorphic

Public Constructors

RWSlistCollectablesStack();

Construct an empty stack.

RWSlistCollectablesStack(RWCollectable* a);

Construct a stack with one entry a.

RWSlistCollectablesStack(const RWSlistCollectablesStack& s);

Copy constructor. A shallow copy of the stack s is made.

Assignment Operator

void operator=(const RWSlistCollectablesStack& s);

Assignment operator. A shallow copy of the stack s is made.

Public Member Functions

virtual void apply(RWapplyCollectable ap, void*); virtual RWspace binaryStoreSize() const; virtual void clear();

Inherited from class RWSlistCollectables.

virtual void clearAndDestroy(); virtual RWBoolean contains(const RWCollectable* target) const;

Inherited from class RWCollection.

RWBoolean containsReference(const RWCollectable* e) const; virtual size_t entries() const;

Inherited from class RWSlistCollectables.

virtual RWCollectable* first() const;

Inherited from class RWSlistCollectables. Same as top().

virtual RWCollectable* insert(RWCollectable* c);

Inherited from class RWSlistCollectables. Same as push().

virtual RWClassID isA() const;

Redefined from class RWCollectable to return __RWSLISTCOLLECTABLESSTACK.

virtual RWBoolean isEmpty()const;

Inherited from class RWSlistCollectables.

virtual RWCollectable* last() const;

Inherited from class RWSlistCollectables. Returns the item at the bottom of the stack.

virtual size_t occurrencesOf(const RWCollectable* target) const; size_t occurrencesOfReference(const RWCollectable* e) const;

Inherited from class RWSlistCollectables.

virtual RWCollectable* remove(const RWCollectable*);

Redefined from class RWSlistCollectables. Calls pop(). The argument is ignored.

RWCollectable* pop();

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

void push(RWCollectable*);

Adds an item to the top of the stack.

RWCollectable* top() const;

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

RWSortedVector

RWSortedVector ->- RWOrdered ->- RWSequenceable ->- RWCollection...
 ...->- RWCollectable 

Synopsis

#include <rw/sortvec.h
 
RWSortedVector a;

Description

Class RWSortedVector represents a group of ordered items, internally sorted by the compareTo() function and accessible by an index number. Duplicates are allowed. An object stored by RWSortedVector must inherit from the abstract base class RWCollectable. An insertion sort is used to maintain the vector in sorted order.

Because class RWSortedVector is implemented as a vector of pointers, traversing the collection is more efficient than with class RWBinaryTree. However, insertions are slower in the center of the collection.

Note that because the vector is sorted, you must not modify elements contained in the vector in such a way as to invalidate the ordering.

Persistence

Polymorphic

Example

sortvec.cpp
 
#include <rw/sortvec.h>
#include <rw/collstr.h>
#include <rw/rstream.h>
 
main(){
   RWSortedVector sv;
   sv.insert(new RWCollectableString("dog"));
   sv.insert(new RWCollectableString("cat"));
   sv.insert(new RWCollectableString("fish"));
   RWSortedVectorIterator next(sv);
   RWCollectableString* item;
   while( item = (RWCollectableString*)next() )
     cout << *item << endl;
   sv.clearAndDestroy();
}

Program Output:

cat
dog
fish

Public Constructors

RWSortedVector(size_t size = RWDEFAULT_CAPACITY);

Construct an empty RWSortedVector that has an initial capacity of size items. The capacity will be increased automatically as needed.

Public Member Operators

RWBoolean operator==(const RWSortedVector& sv) const;

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

const RWCollectable* operator[](size_t i);

Returns the ith element in the collection. If i is out of range, an exception of type RWBoundsErr will be thrown. The return value cannot be used as an lvalue.

const 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 "rwsortvec.h". In this case, if i is out of range, an exception of type RWBoundsErr will be thrown. The return value cannot be used as an lvalue.

Public Member Functions

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

Inherited from class RWOrdered.

virtual const RWCollectable* at(size_t i) const;

Inherited from class RWOrdered.

virtual RWspace binaryStoreSize() const;

Inherited from class RWCollection.

virtual void clear();

Inherited from class RWOrdered.

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;

Inherited from class RWOrdered.

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

Inherited from class RWOrdered. Note that RWOrdered::find() uses the virtual function index() to perform its search. Hence, a binary search will be used.

virtual RWCollectable* first() const;

Inherited from class RWOrdered.

virtual unsigned hash() const;

Inherited from class RWCollectable.

virtual size_t index(const RWCollectable*) const;

Redefined from class RWOrdered. Performs a binary search to return the index of the first item that compares equal to the target item, or RW_NPOS if no such item can be found.

virtual RWCollectable* insert(RWCollectable* c);

Redefined from class RWOrdered. Performs a binary search to insert the item pointed to by c after all items that compare less than or equal to it, but before all items that compare greater than it. Returns nil if the insertion was unsuccessful, c otherwise.

virtual RWClassID isA() const;

Redefined from class RWCollectable to return __RWSORTEDVECTOR.

virtual RWBoolean isEmpty() const;

Inherited from class RWOrdered.

virtual RWBoolean isEqual(const RWCollectable* a) const;

Inherited from class RWCollectable.

virtual RWCollectable* last() const;

Inherited from class RWOrdered.

virtual size_t occurrencesOf(const RWCollectable* target) const;

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

virtual RWCollectable* remove(const RWCollectable* target);

Inherited from class RWOrdered. Note that RWOrdered::remove() uses the virtual function index() to perform its search. Hence, a binary search will be used.

virtual void removeAndDestroy(const RWCollectable* target);

Inherited from class RWCollection.

RWCollectable* removeAt(size_t index);

Inherited from class RWOrdered. Removes the item at the position index in the collection and returns it.

RWTBitVec<size>

Synopsis

#include <rw/tbitvec.h>
 
RWTBitVec<22>   // A 22 bit long vector

Description

RWTBitVec<size> is a parameterized bit vector of fixed length size. Unlike class RWBitVec, its length cannot be changed at run time. The advantage of RWBitVec is its smaller size, and one less level of indirection, resulting in a slight speed advantage.

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

The copy constructor and assignment operator use copy semantics.

Persistence

None

Example

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

#include <rw/tbitvec.h>
 
main()  {
  RWTBitVec<24> 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.
  RWTBitVec<24> c = a ^ b; // Set c to the XOR of a and b.
}

Public Constructor

RWTBitVec<size>();

Constructs an instance with all bits set to FALSE.

RWTBitVec<size>(RWBoolean val);

Constructs an instance with all bits set to val.

Assignment Operators

RWTBitVec<size>& operator=(const RWTBitVec<size>& v);

Sets self to a copy of v.

RWTBitVec& operator=(RWBoolean val);

Sets all bits in self to the value val.

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

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

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.

Logical Operators

RWBoolean operator==(RWBoolean b) const;

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

RWBoolean operator!=(RWBoolean b) const;

Returns TRUE if any bit of self is not set to the value b. Otherwise, returns FALSE.

RWBoolean operator==(const RWTBitVec& v) const;

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

RWBoolean operator!=(const RWTBitVec& v) const;

Returns TRUE if any bit of self is not set to the same value as the corresponding bit in v. Otherwise, returns FALSE.

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 two lines 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 OFF (False) bit in self. Returns RW_NPOS if there is no OFF bit.

size_t firstTrue() const;

Returns the index of the first ON (True) bit in self. Returns RW_NPOS if there is no ON bit.

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 two lines 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

RWTBitVec operator&(const RWTBitVec& v1, const RWTBitVec& v2); RWTBitVec operator^(const RWTBitVec& v1, const RWTBitVec& v2); RWTBitVec operator|(const RWTBitVec& v1, const RWTBitVec& v2);

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

RWTime

Synopsis

#include <rw/rwtime.h>
 
RWTime a;   // Construct with current time

Description

Class RWTime represents a time, stored as the number of seconds since 00:00:00 January 1, 1901 UTC. See Section 8 for how to set the time zone for your compiler. Failure to do this may result in UTC (GMT) times being wrong.

Output formatting is done using an RWLocale object. The default locale formats according to U.S. conventions.

Note that because the default constructor for this class creates an instance holding the current date and time, constructing a large array of RWTime may be slow.

RWTime v[5000];     // Figures out the current time 5000 times 

Those with access to the C++ Standard Library-based versions of the Tools.h++ template collections should consider the following:

// Figures out the current time just once:
RWTValOrderedVector<RWTime> v(5000, RWTime());

Thanks to the smart allocation scheme of the standard collections, the above declaration will result in only one call to the default constructor followed by 5000 invocations of the copy constructor. In the case of RWTime, the copy constructor amounts to an assignment of one long to another, resulting in faster creation than the simple array.

Persistence

Simple

Example

This example constructs a current time, and the time when Daylight-Saving Time starts in the year 1990. It then prints them out.

#include <rw/rwtime.h>
 
#include <rw/rwdate.h>
#include <rw/rstream.h>
 
main(){
 RWTime t;   // Current time
 RWTime d(RWTime::beginDST(1990, RWZone::local()));
   cout << "Current time:         " << RWDate(t) << " " << t << 
            endl;
   cout << "Start of DST, 1990:   " << RWDate(d) << " " << d << 
           endl;
}

Program Output:

Current time:         03/22/91 15:01:40
Start of DST, 1990:   05/01/90 02:00:00

Public Constructors

RWTime();

Default constructor. Constructs a time with the present time.

RWTime(const RWTime&);

Copy constructor.

RWTime(unsigned long s);

Constructs a time with s seconds since 00:00:00 January 1, 1901 UTC. If s==0, an invalid time is constructed. Note that for small s this may be prior to January 1, 1901 in your time zone.

RWTime(unsigned hour, unsigned minute, unsigned second=0, const RWZone& zone = RWZone::local());

Constructs a time with today's date, and the specified hour, minute, and second, relative to the time zone zone, which defaults to local time.

RWTime(const RWDate& date, unsigned hour = 0, unsigned minute = 0,unsigned second = 0, const RWZone& = RWZone::local());

Constructs a time for a given date, hour, minute, and second, relative to the time zone zone, which defaults to local time. Note that the maximum RWTime is much sooner than maximum RWDate. (In fact, it is on Feb. 5, 2037 for platforms with 4-byte longs.) This is a consequence of the fact that RWTime counts seconds while RWDate only deals with full days.

RWTime(const struct tm*, const RWZone& = RWZone::local());

Constructs a time from the tm_year, tm_mon, tm_mday, tm_hour, tm_min, and tm_sec components of the struct tm argument. These components are understood to be relative to the time zone zone, which defaults to local time. Note that the numbering of months and years in a struct tm differs from that used in RWTime arguments.

RWTime(const RWDate& date, const RWCString& str, const RWZone& zone = RWZone::local(), const RWLocale& locale = RWLocale::global());

Constructs a time for the given date, extracting the time from the string str. The string str should contain only the time. The time is understood to be relative to the time zone zone, which defaults to local time. The specified locale is used for formatting information . Use function isValid() to check the results.


Note: Not all time string errors can be detected by this function.


Public Member Operators

RWTime& operator=(const RWTime&);

Assignment operator.

RWTime operator++();

Prefix increment operator. Add one second to self, then return the results.

RWTime operator--();

Prefix decrement operator. Subtract one second from self, then return the results.

RWTime operator++(int);

Postfix increment operator. Add one second to self, returning the initial value.

RWTime operator--(int);

Postfix decrement operator. Subtract one second from self, returning the initial value.

RWTime& operator+=(unsigned long s);

Add s seconds to self, returning self.

RWTime& operator-=(unsigned long s);

Subtract s seconds from self, returning self.

Public Member Functions

RWCString asString(char format = `\0',const RWZone& = RWZone::local(), const RWLocale& = RWLocale::global()) const;

Returns self as a string, formatted by the RWLocale argument, with the time zone adjusted according to the RWZone argument. Formats are as defined by the standard C library function strftime(). The default format is the date followed by the time: "%x %X". The exact format of the date and time returned is dependent upon the implementation of strftime() available. For more information, look under RWLocale.

RWCString asString(char* format,const RWZone& = RWZone::local(), const RWLocale& = RWLocale::global()) const;

Returns self as a string, formatted by the RWLocale argument, with the time zone adjusted according to the RWZone argument. Formats are as defined by the standard C library function strftime().

RWBoolean between(const RWTime& a, const RWTime& b) const;

Returns TRUE if RWTime is between a and b, inclusive.

size_t binaryStoreSize() const;

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

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

int compareTo(const RWTime* t) const;

Comparison function, useful for sorting times. Compares self to the RWTime pointed to by t and returns:

0 if self == *t;

1 if self > *t;

–1 if self < *t;

void extract(struct tm*,const RWZone& = RWZone::local()) const;

Fills all members of the struct tm argument, adjusted to the time zone specified by the RWZone argument. If the time is invalid, the struct tm members are all set to -1. Note that the encoding of struct tm members is different from that used in RWTime and RWDate functions.

unsigned hash() const;

Returns a suitable hashing value.

unsigned hour(const RWZone& zone = RWZone::local()) const;

Returns the hour, adjusted to the time zone specified.

unsigned hourGMT() const;

Returns the hour in UTC (GMT).

RWBoolean isDST(const RWZone& zone = RWZone::local()) const;

Returns TRUE if self is during Daylight-Saving Time in the time zone given by zone, FALSE otherwise.

RWBoolean isValid() const;

Returns TRUE if this is a valid time, FALSE otherwise.

RWTime max(const RWTime& t) const;

Returns the later time of self or t.

RWTime min(const RWTime& t) const;

Returns the earlier time of self or t.

unsigned minute(const RWZone& zone = RWZone::local()) const;

Returns the minute, adjusted to the time zone specified.

unsigned minuteGMT() const;

Returns the minute in UTC (GMT).

unsigned second() const;

Returns the second; local time or UTC (GMT).

unsigned long seconds() const;

Returns the number of seconds since 00:00:00 January 1, 1901 UTC.

Static Public Member Functions

static RWTime beginDST(unsigned year, const RWZone& zone = RWZone::local());

Return the start of Daylight-Saving Time (DST) for the given year, in the given time zone. Returns an "invalid time" if DST is not observed in that year and zone.

static RWTime endDST(unsigned year, const RWZone& = RWZone::local());

Return the end of Daylight-Saving Time for the given year, in the given time zone. Returns an "invalid time" if DST is not observed in that year and zone.

static unsigned hash(const RWTime& t);

Returns the hash value of t as returned by t.hash().

static RWTime now();

Returns the present time.

Related Global Operators

RWTime operator+(const RWTime& t, unsigned long s); RWTime operator+(unsigned long s, const RWTime& t);

Returns an RWTimes seconds greater than t.

RWTime operator-(const RWTime& t, unsigned long s);

Returns an RWTimes seconds less than t.

RWBoolean operator<(const RWTime& t1, const RWTime& t2);

Returns TRUE if t1 is less than t2.

RWBoolean operator<=(const RWTime& t1, const RWTime& t2);

Returns TRUE if t1 is less than or equal to t2.

RWBoolean operator>(const RWTime& t1, const RWTime& t2);

Returns TRUE if t1 is greater than t2.

RWBoolean operator>=(const RWTime& t1, const RWTime& t2);

Returns TRUE if t1 is greater than or equal to t2.

RWBoolean operator==(const RWTime& t1, const RWTime& t2);

Returns TRUE if t1 is equal to t2.

RWBoolean operator!=(const RWTime& t1, const RWTime& t2);

Returns TRUE if t1 is not equal to t2.

ostream& operator<<(ostream& s, const RWTime& t);

Outputs the time t on ostream s, according to the locale imbued in the stream (see class RWLocale), or by RWLocale::global() if none.

RWvostream& operator<<(RWvostream&, const RWTime& t); RWFile& operator<<(RWFile&, const RWTime& t);

Saves RWTimet to a virtual stream or RWFile, respectively.

RWvistream& operator>>(RWvistream&, RWTime& t); RWFile& operator>>(RWFile&, RWTime& t);

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

RWTimer

Synopsis

#include <rw/timer.h>
 
RWTimer timer;

Description

This class can measure elapsed CPU (user) time. The timer has two states: running and stopped. The timer measures the total amount of time spent in the "running" state since it was either constructed or reset.

The timer is put into the "running" state by calling member function start(). It is put into the "stopped" state by calling stop().

RWTimer uses the system-dpendent function clock() which returns the number of "ticks" since it was first called. As a result, RWTimer will not be able to measure intervals longer than some system-dependent value. (For instance, on several common UNIX systems, this value is just under 36 minutes.)

Persistence

None

Example

This example prints out the amount of CPU time used while looping for 5 seconds (as measured using class RWTime).

#include <rw/timer.h>
 
#include <rw/rwtime.h>
#include <rw/rstream.h>
 
main()
{RWTimer t;
 t.start();                 // Start the timer
 
 RWTime start;
 start.now();               // Record starting time
 
 // Loop for 5 seconds:
 for (RWTime current; current.seconds() - start.seconds() < 5;
      current = RWTime::now())
 {;}
 
 t.stop();                  // Stop the timer
 
 cout << t.elapsedTime() << endl;
 return 0;
}

Program Output (exact value may differ):

5.054945

Public Constructor

RWTimer();

Constructs a new timer. The timer will not start running until start() is called.

Public Member Functions

double elapsedTime() const;

Returns the amount of (CPU) time that has accumulated while the timer was in the running state.

void reset();

Resets (and stops) the timer.

void start();

Puts the timer in the "running" state. Time accumulates while in this state.

void stop();

Puts the timer in the "stopped" state. Time will not accumulate while in this state.

RWTIsvDlist<T>

Synopsis

#include <rw/tidlist.h>
 
RWTIsvDlist<T> list;

Descripton

Class RWTIsvDlist<T> is a class that implements intrusive doubly-linked lists.

An intrusive list is one where the member of the list must inherit from a common base class, in this case RWIsvDlink. The advantage of such a list is that memory and space requirements are kept to a minimum. The disadvantage is that the inheritance hierarchy is inflexible, making it slightly more difficult to use with an existing class. Class RWTValDlist<T> is offered as an alternative, non-intrusive, linked list.

See Stroustrup (1991; Section 8.3.1) for more information about intrusive lists.


Note: When you insert an item into an intrusive list, the actual item (not a copy) is inserted. Because each item carries only one link field, the same item cannot be inserted into more than one list, nor can it be inserted into the same list more than once.


Example

#include <rw/tidlist.h>
#include <rw/rstream.h>
#include <string.h>
 
struct Symbol : public RWIsvDlink {
  char name[10];
  Symbol( const char* cs)  {
    strncpy(name, cs, sizeof(name)); name[9] = '\0'; 
  }
};
void printem(Symbol* s, void*) { cout << s->name << endl; }
main()  {
  RWTIsvDlist<Symbol> list;
  list.insert( new Symbol("one") );
  list.insert( new Symbol("two") );
  list.prepend( new Symbol("zero") );
  list.apply(printem, 0);
  list.clearAndDestroy();  // Deletes the items inserted into 
                           // the list
  return 0;
}

Program Output:

zero
one
two

Public Constructors

RWTIsvDlist();

Constructs an empty list.

RWTIsvDlist(T* a);

Constructs a list with the single item pointed to by a in it.

Public Member Functions

void append(T* a);

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

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

Calls the function pointed to by applyFun to every item in the collection. This must have the prototype:

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

The item will be passed in as argument item. Client data may be passed through as parameter d.

T* at(size_t i) const;

Returns the item at index i. 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 from the list.

void clearAndDestroy();

Removes and calls delete for each item in the list. Note that this assumes that each item was allocated off the heap.

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

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

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

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

RWBoolean containsReference(const T* a) const;

Returns TRUE if the list contains an item with the address a.

size_t entries() const;

Returns the number of items currently in the list.

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

Returns the first item in the list for which the user-defined "tester" function pointed to by testFun returns TRUE. If there is no such item, then returns nil. The tester function must have the prototype:

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

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

T* first() const;

Returns (but does not remove) the first item in the list, or nil if the list is empty.

T* get();

Returns and removes the first item in the list, or nil if the list is empty.

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

Returns the index of the first item in the list for which the user-defined "tester" function pointed to by testFun returns TRUE. If there is no such item, then returns RW_NPOS. The tester function must have the prototype:

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

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

void insert(T* a);

Appends the item pointed to by a to the end of the list. This item cannot be inserted into more than one list, nor can it be inserted into the same list more than once.

void insertAt(size_t i, T* a);

Insert the item pointed to by a at the index position i. This position must be between zero and the number of items in the list, or an exception of type TOOL_INDEX will be thrown. The item cannot be inserted into more than one list, nor can it be inserted into the same list more than once.

RWBoolean isEmpty() const;

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

T* last() const;

Returns (but does not remove) the last item in the list, or nil if the list is empty.

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

Traverses the list and returns the number of times for which the user-defined "tester" function pointed to by testFun returned TRUE . The tester function must have the prototype:

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

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

size_t occurrencesOfReference(const T* a) const;

Returns the number of times which the item pointed to by a occurs in the list. Because items cannot be inserted into a list more than once, this function can only return zero or one.

void prepend(T* a);

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

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

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

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

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

T* removeAt(size_t i);

Removes and returns the item at index i. 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.

T* removeFirst();

Removes and returns the first item in the list, or nil if there are no items in the list.

T* removeLast();

Removes and returns the last item in the list, or nil if there are no items in the list.

T* removeReference(T* a);

Removes and returns the item with address a, or nil if there is no such item.

RWTIsvDlistIterator<T>

Synopsis

#include <rw/tidlist.h>
 
RWTIsvDlist<T> list;
RWTIsvDlistIterator<T> iterator(list);

Description

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

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

RWTIsvDlistIterator(RWTIsvDlist<T>& c);

Constructs an iterator to be used with the list c.

Public Operators

T* operator++();

Advances the iterator one position, returning a pointer to the new link, or nil if the end of the list has been reached.

T* operator--();

Reverses the iterator one position, returning a pointer to the new link, or nil if the beginning of the list has been reached.

T* operator+=(size_t n);

Advances the iterator n positions, returning a pointer to the new link, or nil if the end of the list has been reached.

T* operator-=(size_t n);

Reverses the iterator n positions, returning a pointer to the new link, or nil if the beginning of the list has been reached.

T* operator()();

Advances the iterator one position, returning a pointer to the new link, or nil if the end of the list has been reached.

Public Member Functions

RWTIsvDlist<T>* container() const;

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

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

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

void insertAfterPoint(T* a);

Inserts the link pointed to by a into the iterator's associated collection in the position immediately after the iterator's current position.

T* key() const;

Returns the link at the iterator's current position. Returns nil if the iterator is not valid.

T* remove();

Removes and returns the current link from the iterator's associated collection. Returns nil if unsuccessful. Afterwards, if successful, the iterator will be positioned at the element immediately before the removed link.

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

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

void reset();

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

void reset(RWTIsvDlist<TL>& c);

Resets the iterator to iterate over the collection c.

RWTIsvSlist<T>

Synopsis

#include <rw/tislist.h>
 
RWTIsvSlist<T> list;

Descripton

Class RWTIsvSlist<T> is a class that implements intrusive singly-linked lists.

An intrusive list is one where the member of the list must inherit from a common base class, in this case RWIsvSlink. The advantage of such a list is that memory and space requirements are kept to a minimum. The disadvantage is that the inheritance hierarchy is inflexible, making it slightly more difficult to use with an existing class. Class RWTValSlist<T> is offered as an alternative, non-intrusive, linked list.

See Stroustrup (1991; Section 8.3.1) for more information about intrusive lists.


Note: When you insert an item into an intrusive list, the actual item (not a copy) is inserted. Because each item carries only one link field, the same item cannot be inserted into more than one list, nor can it be inserted into the same list more than once.


Example

#include <rw/tislist.h>
#include <rw/rstream.h>
#include <string.h>
 
struct Symbol : public RWIsvSlink
{ char name[10];
  Symbol( const char* cs)
   { strncpy(name, cs, sizeof(name)); name[9] = '\0'; }
};
void printem(Symbol* s, void*) { cout << s->name << endl; }
main(){
  RWTIsvSlist<Symbol> list;
  list.insert( new Symbol("one") );
  list.insert( new Symbol("two") );
  list.prepend( new Symbol("zero") );
  list.apply(printem, 0);
  list.clearAndDestroy();  // Deletes the items inserted into 
                           // the list
  return 0;
}

Program Output:

zero
one
two

Public Constructors

RWTIsvSlist();

Constructs an empty list.

RWTIsvSlist(T* a);

Constructs a list with the single item pointed to by a in it.

Public Member Functions

void append(T* a);

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

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

Calls the function pointed to by applyFun to every item in the collection. This must have the prototype:

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

The item will be passed in as argument item. Client data may be passed through as parameter d.

T* at(size_t i) const;

Returns the item at index i. 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 from the list.

void clearAndDestroy();

Removes and calls delete for each item in the list. Note that this assumes that each item was allocated off the heap.

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

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

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

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

RWBoolean containsReference(const T* a) const;

Returns TRUE if the list contains an item with the address a.

size_t entries() const;

Returns the number of items currently in the list.

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

Returns the first item in the list for which the user-defined "tester" function pointed to by testFun returns TRUE. If there is no such item, then returns nil. The tester function must have the prototype:

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

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

T* first() const;

Returns (but does not remove) the first item in the list, or nil if the list is empty.

T* get();

Returns and removes the first item in the list, or nil if the list is empty.

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

Returns the index of the first item in the list for which the user-defined "tester" function pointed to by testFun returns TRUE. If there is no such item, then returns RW_NPOS. The tester function must have the prototype:

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

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

void insert(T* a);

Appends the item pointed to by a to the end of the list. This item cannot be inserted into more than one list, nor can it be inserted into the same list more than once.

void insertAt(size_t i, T* a);

Insert the item pointed to by a at the index position i. This position must be between zero and the number of items in the list, or an exception of type TOOL_INDEX will be thrown. The item cannot be inserted into more than one list, nor can it be inserted into the same list more than once.

RWBoolean isEmpty() const;

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

T* last() const;

Returns (but does not remove) the last item in the list, or nil if the list is empty.

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

Traverses the list and returns the number of times for which the user-defined "tester" function pointed to by testFun returned TRUE . The tester function must have the prototype:

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

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

size_t occurrencesOfReference(const T* a) const;

Returns the number of times which the item pointed to by a occurs in the list. Because items cannot be inserted into a list more than once, this function can only return zero or one.

void prepend(T* a);

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

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

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

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

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

T* removeAt(size_t i);

Removes and returns the item at index i. 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.

T* removeFirst();

Removes and returns the first item in the list, or nil if there are no items in the list.

T* removeLast();

Removes and returns the last item in the list, or nil if there are no items in the list. This function is relatively slow because removing the last link in a singly-linked list necessitates access to the next-to-the-last link, requiring the whole list to be searched.

T* removeReference(T* a);

Removes and returns the link with address a. The link must be in the list. In a singly-linked list this function is not very efficient.

RWTIsvSlistIterator<T>

Synopsis

#include <rw/tislist.h>
 
RWTIsvSlist<T> list;
RWTIsvSlistIterator<T> iterator(list);

Description

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

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

RWTIsvSlistIterator(RWTIsvSlist<T>& c);

Constructs an iterator to be used with the list c.

Public Operators

T* operator++();

Advances the iterator one position, returning a pointer to the new link, or nil if the end of the list has been reached.

T* operator+=(size_t n);

Advances the iterator n positions, returning a pointer to the new link, or nil if the end of the list has been reached.

T* operator()();

Advances the iterator one position, returning a pointer to the new link, or nil if the end of the list has been reached.

Public Member Functions

RWTIsvSlist<T>* container() const;

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

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

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

void insertAfterPoint(T* a);

Inserts the link pointed to by a into the iterator's associated collection in the position immediately after the iterator's current position.

T* key() const;

Returns the link at the iterator's current position. Returns nil if the iterator is not valid.

T* remove();

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

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

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

void reset();

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

void reset(RWTIsvSlist<TL>& c);

Resets the iterator to iterate over the collection c.

RWTPtrDeque<T>

Synopsis

#include <rw/tpdeque.h> 
RWTPtrDeque<T> deq;


Note: RWTPtrDeque requires the Standard C++ Library.


Description

This class maintains a pointer-based collection of values, implemented as a double-ended queue, or deque. Class T is the type pointed to by the items in the collection.

Persistence

Isomorphic

Example

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

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

Program Input:

1 2 3 4 5

Program Output:

4
2
1
3
5

Related Classes

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

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

Public Typedefs

typedef deque<T*, allocator>                   container_type;
typedef container_type::iterator               iterator;
typedef container_type::const_iterator         const_iterator;
typedef container_type::size_type              size_type;
typedef container_type::difference_type        difference_type;
typedef T*                                     value_type;
typedef T*&                                    reference;
typedef T* const&                              const_reference;

Public Constructors

RWTPtrDeque<T>();

Constructs an empty, double-ended queue.

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

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

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

Copy constructor.

RWTPtrDeque<T>(size_type n, T* a);

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

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

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

Public Member Operators

RWTPtrDeque<T>& operator=(const RWTPtrDeque<T>& deq);

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

RWTPtrDeque<T>& operator=(const deque<T*, allocator>& stddeq);

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

bool operator<(const RWTPtrDeque<T>& deq);

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

bool operator==(const RWTPtrDeque<T>& deq);

Returns true if self compares equal to deq, otherwise returns false. Two collections are equal if both have the same number of entries, and iterating through both collections produces, in turn, individual elements that compare equal to each other. Elements are dereferenced before being compared.

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

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

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

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

Public Member Functions

void append(T* a);

Adds the item a to the end of the collection.

void apply(void (*fn)(T*,void*), void* d); void apply(void (*fn)(const T*,void*), void* d) const; void apply(void (*fn)(T*&,void*), void* d);

Applies the user-defined function pointed to by fn to every item in the collection. This function must have one of the prototypes:

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

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

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

for reference semantics. Client data may be passed through parameter d.

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

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

iterator begin(); const_iterator begin() const;

Returns an iterator positioned at the first element of self.

void clear();

Clears the collection by removing all items from self.

void clearAndDestroy();

Removes all items from the collection and uses operator delete to destroy the objects pointed to by those items. Do not use this method if multiple pointers to the same object are stored.

bool contains (const T* a) const;

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

bool contains (bool (*fn)(const T*, void*), void *d) const; bool contains(bool (*fn)(T*,void*), void* d) const;

Returns true if there exists an element t in self such that the expression ((*fn)(t,d)) is true, otherwise returns false. fn points to a user-defined tester function which must have one of the prototypes:

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

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

Client data may be passed through parameter d.

iterator end(); const_iterator end() const;

Returns an iterator positioned "just past" the last element in self.

size_type entries() const;

Returns the number of items in self.

T* find(const T* a) const;

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

T* find(bool (*fn)( T*,void*), void* d) const; T* find(bool (*fn)(const T*,void*), void* d) const;

If there exists an element t in self such that the expression ((*fn)(t,d)) is true, returns t. Otherwise, returns rwnil. fn points to a user-defined tester function which must have one of the prototypes:

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

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

Client data may be passed through parameter d.

reference first(); const_reference first() const;

Returns a reference to the first element of self. If the collection is empty, the function throws an exception of type RWBoundsErr.

size_type index(const T* a) const;

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

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

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

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

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

Client data may be passed through parameter d.

bool insert(T* a);

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

void insertAt(size_type i, T* a);

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

bool isEmpty() const;

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

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

Returns a reference to the last element of self.

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

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

size_type occurrencesOf(const T* a) const;

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

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

Returns the number of elements t in self such that the expression((*fn)(t,d)) is true. fn points to a user-defined tester function which must have one of the prototypes:

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

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

Client data may be passed through parameter d.

T* popBack();

Removes and returns the last item in the collection.

T* popFront();

Removes and returns the first item in the collection.

void prepend(T* a);

Adds the item a to the beginning of the collection.

void pushBack(T* a);

Adds the item a to the end of the collection.

void pushFront(T* a);

Adds the item a to the beginning of the collection.

T* remove(const T* a);

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

T* remove(bool (*fn)(T*, void*), void* d); T* remove(bool (*fn)(const T*,void*), void* d);

Removes and returns the first element t in self such that the expression ((*fn)(t,d)) is true. Returns rwnil if there is no such element. fn points to a user-defined tester function which must have one of the prototypes:

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

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

Client data may be passed through parameter d.

size_type const T* removeAll(const_reference a);

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

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

Removes all elements t in self such that the expression ((*fn)(t,d))is true. Returns the number of items removed. fn points to a user-defined tester function which must have one of the prototypes:

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

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

Client data may be passed through parameter d.

T* removeAt(size_type i);

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

T* removeFirst();

Removes and returns the first item in the collection.

T* removeLast();

Removes and returns the first item in the collection.

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

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

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

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

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

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

Client data may be passed through parameter d.

void sort();

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

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

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

Static Public Data Member

size_type npos;

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

Related Global Operators

RWvostream& operator<<(RWvostream& strm, const RWTPtrDeque<T>& coll); RWFile& operator<<(RWFile& strm, const RWTPtrDeque<T>& coll);

Saves the collection coll onto the output stream strm, or a reference to it if it has already been saved.

RWvistream& operator>>(RWvistream& strm, RWTPtrDeque<T>& coll); RWFile& operator>>(RWFile& strm, RWTPtrDeque<T>& coll);

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

RWvistream& operator>>(RWvistream& strm, RWTPtrDeque<T>*& p); RWFile& operator>>(RWFile& strm, RWTPtrDeque<T>*& p);

Looks at the next object on the input stream strm and either creates a new collection off the heap and sets p to point to it, or sets p to point to a previously read instance. If a collection is created off the heap, then you are responsible for deleting it.