#include <rw/tvslist.h> RWTValSlist<T> list;
If you do not have the Standard C++ Library, use the interface described here. Otherwise, use the interface to RWTValSlist described in the Class Reference.
This class maintains a collection of values, implemented as a singly-linked list. This is a value based list: objects are copied in and out of the links that make up the list. Unlike intrusive lists (see class RWTIsvSlist<T>) the objects need not inherit from a link class. However, this makes the class slightly less efficient than the intrusive lists because of the need to allocate a new link off the heap with every insertion and to make a copy of the object in the newly allocated link.
Parameter T represents the type of object to be inserted into the list, either a class or fundamental type. The class T must have:
A default constructor;
well-defined copy semantics (T::T(const T&) or equivalent);
well-defined assignment semantics (T::operator=(const T&) or equivalent);
well-defined equality semantics (T::operator==(const T&)).
Isomorphic
In this example, a singly-linked list of RWDates is exercised.
#include <rw/tvslist.h> #include <rw/rwdate.h> #include <rw/rstream.h> main() { RWTValSlist<RWDate> dates; dates.insert(RWDate(2, "June", 52)); // 6/2/52 dates.insert(RWDate(30, "March", 46)); // 3/30/46 dates.insert(RWDate(1, "April", 90)); // 4/1/90 // Now look for one of the dates: RWDate ret; if (dates.find(RWDate(2, "June", 52), ret)){ cout << "Found date " << ret << endl; } // Remove in reverse order: while (!dates.isEmpty()) cout << dates.removeLast() << endl; return 0; }
Program output:
Found date June 2, 1952 April 1, 1990 March 30, 1946 June 2, 1952
RWTValSlist<T>();
Construct an empty list.
RWTValSlist<T>(const RWTValSlist<T>& list);
Construct a copy of the list list. Depending on the nature of the copy constructor of T, this could be relatively expensive because every item in the list must be copied.
RWTValSlist& operator=(const RWTValSlist<T>& list);
Sets self to a copy of the list list. Depending on the nature of the copy constructor of T, this could be relatively expensive because every item in the list must be copied.
T& operator[](size_t i);
Returns a reference to the item at index i. The results can be used as an lvalue. An exception of type 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.
const T& operator[](size_t i) const;
Returns a copy of the item at index i. The results cannot be used as an lvalue. An exception of type 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.
void append(const T& a);
Adds the item a to the end of the list.
void apply(void (*applyFun)(T&, void*), void* d);
Applies the user-defined function pointed to by applyFun to every item in the list. This function must have prototype:
void yourFun(T& a, void* d);
Client data may be passed through as parameter d.
T& at(size_t i);
Returns a reference to the item at index i. The results can be used as an lvalue. An exception of type 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.
const T& at(size_t i) const;
Returns a copy of the item at index i. The results cannot be used as an lvalue. An exception of type 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.
void clear();
Removes all items from the list. Their destructors, if any, will be called.
RWBoolean contains(const T& a) const;
Returns TRUE if the list contains an object that is equal to the object a. Returns FALSE otherwise. Equality is measured by the class-defined equality operator.
RWBoolean contains(RWBoolean (*testFun)(const T&, void*), void* d) const;
Returns TRUE if the list contains an item for which the user-defined "tester" function pointed to by testFun returns TRUE . Returns FALSE otherwise. The tester function must have the prototype:
RWBoolean yourTester(const T&, void* d);
For each item in the list this function will be called with the item as the first argument. Client data may be passed through as parameter d.
size_t entries() const;
Returns the number of items that are currently in the collection.
RWBoolean find(const T& target, T& k) const;
Returns TRUE if the list contains an object that is equal to the object target and puts a copy of the matching object into k. Returns FALSE otherwise and does not touch k. Equality is measured by the class-defined equality operator. If you do not need a copy of the found object, use contains() instead.
RWBoolean find(RWBoolean (*testFun)(const T&, void*),void* d, T& k) const;
Returns TRUE if the list contains an object for which the user-defined tester function pointed to by testFun returns TRUE and puts a copy of the matching object into k. Returns FALSE otherwise and does not touch k. The tester function must have the prototype:
RWBoolean yourTester(const T&, void* d);
For each item in the list this function will be called with the item as the first argument. Client data may be passed through as parameter d. If you do not need a copy of the found object, use contains() instead.
T& first(); const T& first() const;
Returns but does not remove the first item in the list. The behavior is undefined if the list is empty.
T get();
Returns and removes the first item in the list. The behavior is undefined if the list is empty.
size_t index(const T& a);
Returns the index of the first object that is equal to the object a, or RW_NPOS if there is no such object. Equality is measured by the class-defined equality operator.
size_t index(RWBoolean (*testFun)(const T&, void*),void* d) const;
Returns the index of the first object for which the user-defined tester function pointed to by testFun returns TRUE, or RW_NPOS if there is no such object. The tester function must have the prototype:
RWBoolean yourTester(const T&, void* d);
For each item in the list this function will be called with the item as the first argument. Client data may be passed through as parameter d.
void insert(const T& a);
Adds the item a to the end of the list.
void insertAt(size_t i, const T& a);
Insert the item a at the index position i. This position must be between zero and the number of items in the list, or an exception of type RWBoundsErr will be thrown.
RWBoolean isEmpty() const;
Returns TRUE if there are no items in the list, FALSE otherwise.
T& last(); const T& last() const;
Returns but does not remove the last item in the list. The behavior is undefined if the list is empty.
size_t occurrencesOf(const T& a) const;
Returns the number of objects in the list that are equal to the object a. Equality is measured by the class-defined equality operator.
size_t occurrencesOf(RWBoolean (*testFun)(const T&, void*),void* d) const;
Returns the number of objects in the list for which the user-defined "tester" function pointed to by testFun returns TRUE . The tester function must have the prototype:
RWBoolean yourTester(const T&, void* d);
For each item in the list this function will be called with the item as the first argument. Client data may be passed through as parameter d.
void prepend(const T& a);
Adds the item a to the beginning of the list.
RWBoolean remove(const T& a);
Removes the first object which is equal to the object a and returns TRUE. Returns FALSE if there is no such object. Equality is measured by the class-defined equality operator.
RWBoolean remove(RWBoolean (*testFun)(const T&, void*), void* d);
Removes the first object for which the user-defined tester function pointed to by testFun returns TRUE, and returns TRUE. Returns FALSE if there is no such object. The tester function must have the prototype:
RWBoolean yourTester(const T&, void* d);
For each item in the list this function will be called with the item as the first argument. Client data may be passed through as parameter d.
size_t removeAll(const T& a);
Removes all objects which are equal to the object a. Returns the number of objects removed. Equality is measured by the class-defined equality operator.
size_t removeAll(RWBoolean (*testFun)(const T&, void*),void* d);
Removes all objects for which the user-defined tester function pointed to by testFun returns TRUE. Returns the number of objects removed. The tester function must have the prototype:
RWBoolean yourTester(const T&, void* d);
For each item in the list this function will be called with the item as the first argument. Client data may be passed through as parameter d.
T removeAt(size_t i);
Removes and returns the object at index i. An exception of type 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 and returns the first item in the list. The behavior is undefined if the list is empty.
T removeLast();
Removes and returns the last item in the list. The behavior is undefined if the list is empty. This function is relatively slow because removing the last link in a singly-linked list necessitates access to the next-to-the-last link, requiring the whole list to be searched.
RWvostream& operator<<(RWvostream& strm, const RWTValSlist<T>& coll); RWFile& operator<<(RWFile& strm, const RWTValSlist<T>& coll);
Saves the collection coll onto the output stream strm, or a reference to it if it has already been saved.
RWvistream& operator>>(RWvistream& strm, RWTValSlist<T>& coll); RWFile& operator>>(RWFile& strm, RWTValSlist<T>& coll);
Restores the contents of the collection coll from the input stream strm.
RWvistream& operator>>(RWvistream& strm, RWTValSlist<T>*& p); RWFile& operator>>(RWFile& strm, RWTValSlist<T>*& p);
Looks at the next object on the input stream strm and either creates a new collection off the heap and sets p to point to it, or sets p to point to a previously read instance. If a collection is created off the heap, then you are responsible for deleting it.