#include <rw/tvordvec.h> RWTValOrderedVector<T> ordvec;
If you do not have the Standard C++ Library, use the interface described here. Otherwise, use the interface to RWTValOrderedVector described in the Class Reference.
RWTValOrderedVector<T> is an ordered collection. That is, the items in the collection have a meaningful ordered relationship with respect to one another and can be accessed by an index number. The order is set by the order of insertion. Duplicates are allowed. The class is implemented as a vector, allowing efficient insertion and retrieval from the end of the collection, but somewhat slower from the beginning of the collection.
The class T must have:
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&) const;)
a default constructor.
Note that an ordered vector has a length (the number of items returned by length() or entries()) and a capacity. Necessarily, the capacity is always greater than or equal to the length. Although elements beyond the collection's length are not used, nevertheless, in a value-based collection, they are occupied. If each instance of class T requires considerable resources, then you should ensure that the collection's capacity is not much greater than its length, otherwise unnecessary resources will be tied up.
Isomorphic
#include <rw/tvordvec.h> #include <rw/rstream.h> main() { RWTValOrderedVector<double> vec; vec.insert(22.0); vec.insert(5.3); vec.insert(-102.5); vec.insert(15.0); vec.insert(5.3); cout << vec.entries() << " entries\n" << endl; // Prints "5" for (int i=0; i<vec.length(); i++) cout << vec[i] << endl; return 0; }
Program output:
5 entries 22 5.3 -102.5 15 5.3
RWTValOrderedVector<T>(size_t capac=RWDEFAULT_CAPACITY);
Create an empty ordered vector with capacity capac. Should the number of items exceed this value, the vector will be resized automatically.
RWTValOrderedVector<T>(const RWTValOrderedVector<T>& c);
Constructs a new ordered vector as a copy of c. The copy constructor of all elements in the vector will be called. The new vector will have the same capacity and number of members as the old vector.
RWTValOrderedVector<T>& operator=(const RWTValOrderedVector& c);
Sets self to a copy of c. The copy constructor of all elements in the vector will be called. Self will have the same capacity and number of members as the old vector.
T& operator()(size_t i); const T& operator()(size_t i) const;
Returns the ith value in the vector. 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. No bounds checking is performed.
T& operator[](size_t i); const T& operator[](size_t i) const;
Returns the ith value in the vector. 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(const T& a);
Appends the value a to the end of the vector. The collection will automatically be resized if this causes the number of items in the collection to exceed the capacity.
T& at(size_t i); const T& at(size_t i) const;
Return the ith value in the vector. The first variant can be used as an lvalue, the second cannot. The index i must be between 0 and the length of the vector less one or an exception of type RWBoundsErr will be thrown.
void clear();
Removes all items from the collection.
RWBoolean contains(const T& a) const;
Returns TRUE if the collection contains an item that is equal to a. A linear search is done. Equality is measured by the class-defined equality operator.
const T* data() const;
Returns a pointer to the raw data of the vector. The contents should not be changed. Should be used with care.
size_t entries() const;
Returns the number of items currently in the collection.
RWBoolean find(const T& target, T& ret) const;
Performs a linear search and returns TRUE if the vector contains an object that is equal to the object target and puts a copy of the matching object into ret. Returns FALSE otherwise and does not touch ret. Equality is measured by the class-defined equality operator.
T& first(); const T& first() const;
Returns the first item in the collection. An exception of type RWBoundsErr will occur if the vector is empty.
size_t index(const T& a) const;
Performs a linear search, returning the index of the first item that is equal to a. Returns RW_NPOS if there is no such item. Equality is measured by the class-defined equality operator.
void insert(const T& a);
Appends the value a to the end of the vector. The collection will automatically be resized if this causes the number of items in the collection to exceed the capacity.
void insertAt(size_t i, const T& a);
Inserts the value a into the vector at index i. The item previously at position i is moved to i+1, etc. The collection will automatically be resized if this causes the number of items in the collection to exceed the capacity. The index i must be between 0 and the number of items in the vector or an exception of type RWBoundsErr will occur.
RWBoolean isEmpty() const;
Returns TRUE if there are no items in the collection, FALSE otherwise.
T& last(); const T& last() const;
Returns the last item in the collection. If there are no items in the collection then an exception of type RWBoundsErr will occur.
size_t length() const;
Returns the number of items currently in the collection.
size_t occurrencesOf(const T& a) const;
Performs a linear search, returning the number of items that are equal to a. Equality is measured by the class-defined equality operator.
void prepend(const T& a);
Prepends the value a to the beginning of the vector. The collection will automatically be resized if this causes the number of items in the collection to exceed the capacity.
RWBoolean remove(const T& a);
Performs a linear search, removing 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.
size_t removeAll(const T& a);
Removes all items which are equal to a, returning the number removed. Equality is measured by the class-defined equality operator.
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 object in the collection. An exception of type RWBoundsErr will be thrown if the list is empty.
T removeLast();
Removes and returns the last object in the collection. An exception of type RWBoundsErr will be thrown if the list is empty.
void resize(size_t N);
Changes the capacity of the collection to N. Note that the number of objects in the collection does not change, just the capacity.
RWvostream& operator<<(RWvostream& strm, const RWTValOrderedVector<T>& coll); RWFile& operator<<(RWFile& strm, const RWTValOrderedVector<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, RWTValOrderedVector<T>& coll); RWFile& operator>>(RWFile& strm, RWTValOrderedVector<T>& coll);
Restores the contents of the collection coll from the input stream strm.
RWvistream& operator>>(RWvistream& strm, RWTValOrderedVector<T>*& p); RWFile& operator>>(RWFile& strm, RWTValOrderedVector<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.