#include <rw/tpslist.h> RWTPtrSlist<T> list;
If you do not have the Standard C++ Library, use the interface described here. Otherwise, use the interface to RWTPtrSlist described in the Class Reference.
This class maintains a collection of pointers to type T, implemented as a singly-linked list. This is a pointer based list: pointers to objects are copied in and out of the links that make up the list.
Parameter T represents the type of object to be inserted into the list, either a class or fundamental type. The class T must have:
well-defined equality semantics (T::operator==(const T&)).
Isomorphic
In this example, a singly-linked list of RWDates is exercised.
#include <rw/tpslist.h> #include <rw/rwdate.h> #include <rw/rstream.h> main() { RWTPtrSlist<RWDate> dates; dates.insert(new RWDate(2, "June", 52)); // 6/2/52 dates.insert(new RWDate(30, "March", 46)); // 3/30/46 dates.insert(new RWDate(1, "April", 90)); // 4/1/90 // Now look for one of the dates: RWDate key(2, "June", 52); RWDate* d = dates.find(&key); if (d){ cout << "Found date " << *d << endl; } // Remove in reverse order: while (!dates.isEmpty()){ d = dates.removeLast(); cout << *d << endl; delete d; } return 0; }
Program output:
Found date June 2, 1952 April 1, 1990 March 30, 1946 June 2, 1952
RWTPtrSlist<T>();
Construct an empty list.
RWTPtrSlist<T>(const RWTPtrSlist<T>& c);
Constructs a new singly-linked list as a shallow copy of c. After construction, pointers will be shared between the two collections.
RWTPtrSlist& operator=(const RWTPtrSlist<T>& c);
Sets self to a shallow copy of c. Afterwards, pointers will be shared between the two collections.
T*& operator[](size_t i); T* const& operator[](size_t i) const;
Returns a pointer to the ith value in the list. The first variant can be used as an lvalue, the second cannot. The index i must be between zero and the number of items in the collection less one, or an exception of type RWBoundsErr will be thrown.
void append(T* a);
Appends the item pointed to by a to the end of the list.
void apply(void (*applyFun)(T*, void*), void* d);
Applies the user-defined function pointed to by applyFun to every item in the list. This function must have the prototype:
void yourFun(T* a, void* d);
This function will be called for each item in the list, with a pointer to the item as the first argument. Client data may be passed through as parameter d.
T*& at(size_t i); T* const; at(size_t i) const;
Returns a pointer to the ith value in the list. The first variant can be used as an lvalue, the second cannot. The index i must be between zero and the number of items in the collection less one, or an exception of type RWBoundsErr will be thrown.
void clear();
Removes all items from the collection.
void clearAndDestroy();
Removes all items from the collection and deletes them. Do not use this method if multiple pointers to the same object are stored.
RWBoolean contains(const T* a) const;
Returns TRUE if the list contains an object that is equal to the object pointed to by a, FALSE otherwise. Equality is measured by the class-defined equality operator for type T.
RWBoolean contains(RWBoolean (*testFun)(T*, void*),void* d) const;
Returns TRUE if the list contains an item for which the user-defined "tester" function pointed to by testFun returns TRUE . Returns FALSE otherwise. The tester function must have the prototype:
RWBoolean yourTester(T*, void* d);
This function will be called for each item in the list, with a pointer to the item as the first argument. Client data may be passed through as parameter d.
size_t entries() const;
Returns the number of items that are currently in the collection.
T* find(const T* target) const;
Returns a pointer to the first object encountered which is equal to the object pointed to by target, or nil if no such object can be found. Equality is measured by the class-defined equality operator for type T.
T* find(RWBoolean (*testFun)(T*, void*),void* d,) const;
Returns a pointer to the first object encountered for which the user-defined tester function pointed to by testFun returns TRUE, or nil if no such object can be found. The tester function must have the prototype:
RWBoolean yourTester(T*, void* d);
This function will be called for each item in the list, with a pointer to the item as the first argument. Client data may be passed through as parameter d.
T*& first(); T* const& first() const;
Returns a pointer to the first item in the list. The behavior is undefined if the list is empty.
T* get();
Returns a pointer to the first item in the list and removes the item. The behavior is undefined if the list is empty.
size_t index(const T* a);
Returns the index of the first object that is equal to the object pointed to by a, or RW_NPOS if there is no such object. Equality is measured by the class-defined equality operator for type T.
size_t index(RWBoolean (*testFun)(T*, void*),void* d) const;
Returns the index of the first object for which the user-defined tester function pointed to by testFun returns TRUE, or RW_NPOS if there is no such object. The tester function must have the prototype:
RWBoolean yourTester(T*, void* d);
This function will be called for each item in the list, with a pointer to the item as the first argument. Client data may be passed through as parameter d.
void insert(T* a);
Adds the object pointed to by a to the end of the list.
void insertAt(size_t i, T* a);
Adds the object pointed to by a at the index position i. This position must be between zero and the number of items in the list, or an exception of type RWBoundsErr will be thrown.
RWBoolean isEmpty() const;
Returns TRUE if there are no items in the list, FALSE otherwise.
T*& last(); T* const& last() const;
Returns a pointer to the last item in the list. The behavior is undefined if the list is empty.
size_t occurrencesOf(const T* a) const;
Returns the number of objects in the list that are equal to the object pointed to by a. Equality is measured by the class-defined equality operator for type T.
size_t occurrencesOf(RWBoolean (*testFun)(T*, void*),void* d) const;
Returns the number of objects in the list for which the user-defined "tester" function pointed to by testFun returns TRUE . The tester function must have the prototype:
RWBoolean yourTester(T*, void* d);
This function will be called for each item in the list, with a pointer to the item as the first argument. Client data may be passed through as parameter d.
void prepend(T* a);
Adds the item pointed to by a to the beginning of the list.
T* remove(const T* a);
Removes the first object which is equal to the object pointed to by a and returns a pointer to it, or nil if no such object could be found. Equality is measured by the class-defined equality operator for type T.
T* remove(RWBoolean (*testFun)(T*, void*),void* d);
Removes the first object for which the user-defined tester function pointed to by testFun returns TRUE and returns a pointer to it, or nil if there is no such object. The tester function must have the prototype:
RWBoolean yourTester(T*, void* d);
This function will be called for each item in the list, with a pointer to the item as the first argument. Client data may be passed through as parameter d.
size_t removeAll(const T* a);
Removes all objects which are equal to the object pointed to by a. Returns the number of objects removed. Equality is measured by the class-defined equality operator for type T.
size_t removeAll(RWBoolean (*testFun)(T*, void*),void* d);
Removes all objects for which the user-defined tester function pointed to by testFun returns TRUE. Returns the number of objects removed. The tester function must have the prototype:
RWBoolean yourTester(T*, void* d);
This function will be called for each item in the list, with a pointer to the item as the first argument. Client data may be passed through as parameter d.
T* removeAt(size_t i);
Removes the object at index i and returns a pointer to it. An exception of type RWBoundsErr will be thrown if i is not a valid index. Valid indices are from zero to the number of items in the list less one.
T* removeFirst();
Removes the first item in the list and returns a pointer to it. The behavior is undefined if the list is empty.
T* removeLast();
Removes the last item in the list and returns a pointer to it. The behavior is undefined if the list is empty. This function is relatively slow because removing the last link in a singly-linked list necessitates access to the next-to-the-last link, requiring that the whole list be searched.
RWvostream& operator<<(RWvostream& strm, const RWTPtrSlist<T>& coll); RWFile& operator<<(RWFile& strm, const RWTPtrSlist<T>& coll);
Saves the collection coll onto the output stream strm, or a reference to it if it has already been saved.
RWvistream& operator>>(RWvistream& strm, RWTPtrSlist<T>& coll); RWFile& operator>>(RWFile& strm, RWTPtrSlist<T>& coll);
Restores the contents of the collection coll from the input stream strm.
RWvistream& operator>>(RWvistream& strm, RWTPtrSlist<T>*& p); RWFile& operator>>(RWFile& strm, RWTPtrSlist<T>*& p);
Looks at the next object on the input stream strm and either creates a new collection off the heap and sets p to point to it, or sets p to point to a previously read instance. If a collection is created off the heap, then you are responsible for deleting it.