This chapter discusses the following vector containers:
wcvector.h
The WCPtrOrderedVector<Type> and WCPtrSortedVector<Type> classes are templated classes used to store objects in a vector. Ordered and sorted vectors are powerful arrays that can be resized, and provide an abstract interface to insert, find and remove elements:
Elements can't be inserted into these vectors by assigning to a vector index. Vectors automatically grow when necessary to insert an element if the resize_required exception isn't enabled.
In the description of each member function, the text Type is used to indicate the template parameter defining the type pointed to by the pointers stored in the vector.
Note that lookups are performed on the types pointed to, not just by comparing pointers. Two pointer elements are equivalent if the values they point to are equivalent. The values pointed to don't need to be the same object. |
The WCPtrOrderedVector class stores elements in the order in which they're inserted using the insert(), append(), prepend() and insertAt() member functions. Linear searches are performed to locate entries, and the less-than operator isn't required.
The WCPtrSortedVector class stores elements in ascending order, so Type must provide a less-than operator. Insertions are more expensive than inserting or appending into an ordered vector, since entries must be moved to make room for the new element. A binary search is used to locate elements in a sorted vector, making searches quicker than in the ordered vector.
Take care, when using the WCPtrSortedVector class, not to change the ordering of the vector elements. An object pointed to by a vector element must not be changed so that it isn't equivalent to the value when the pointer was inserted into the vector. The index operator and the member functions find(), first() and last() all return pointers to the elements pointed to by the vector elements. Lookups assume elements are in sorted order, so you shouldn't use the returned pointers to change the ordering of the value pointed to. |
The WCPtrVector class is also available. It provides a resizable and boundary-safe vector similar to standard arrays.
The WCExcept class is a base class of the WCPtrOrderedVector<Type> and WCPtrSortedVector<Type> classes, and provides the exceptions() member function. This member function controls the exceptions that can be thrown by the WCPtrOrderedVector<Type> and WCPtrSortedVector<Type> objects. No exceptions are enabled unless they're set by the exceptions() member function.
Both the WCPtrOrderedVector<Type> and WCPtrSortedVector<Type> classes require Type to have:
int operator ==( const Type & ) const
Additionally the WCPtrSortedVector class requires Type to have:
int operator <( const Type & ) const
The following member functions are declared in the public interface:
WCPtrOrderedVector( size_t = WCDEFAULT_VECTOR_LENGTH, unsigned = WCDEFAULT_VECTOR_RESIZE_GROW ); WCPtrOrderedVector( const WCPtrOrderedVector & ); virtual ~WCPtrOrderedVector(); WCPtrSortedVector( size_t = WCDEFAULT_VECTOR_LENGTH, unsigned = WCDEFAULT_VECTOR_RESIZE_GROW ); WCPtrSortedVector( const WCPtrSortedVector & ); virtual ~WCPtrSortedVector(); void clear(); void clearAndDestroy(); int contains( const Type * ) const; unsigned entries() const; Type * find( const Type * ) const; Type * first() const; int index( const Type * ) const; int insert( Type * ); int isEmpty() const; Type * last() const; int occurrencesOf( const Type * ) const; Type * remove( const Type * ); unsigned removeAll( const Type * ); Type * removeAt( int ); Type * removeFirst(); Type * removeLast(); int resize( size_t );
The following public member functions are available for the WCPtrOrderedVector class only:
int append( Type * ); int insertAt( int, Type * ); int prepend( Type * );
The following member operators are declared in the public interface:
Type * & operator [ ]( int ); Type * const & operator [ ]( int ) const; WCPtrOrderedVector & WCPtrOrderedVector::operator =( const WCPtrOrderedVector & ); WCPtrSortedVector & WCPtrSortedVector::operator =( const WCPtrSortedVector & ); int WCPtrOrderedVector::operator ==( const WCPtrOrderedVector & ) const; int WCPtrSortedVector::operator ==( const WCPtrSortedVector & ) const;
create a WCPtrOrderedVector<Type> object
#include <wcvector.h> public: WCPtrOrderedVector( size_t = WCDEFAULT_VECTOR_LENGTH, unsigned = WCDEFAULT_VECTOR_RESIZE_GROW ); WCPtrOrderedVector( const WCPtrOrderedVector & );
There are two forms of the WCPtrOrderedVector<Type> constructor:
If the resize_required exception isn't enabled, then the second optional parameter is used to specify the value to increase the vector size when an element is inserted into a full vector. If zero(0) is specified as the second parameter, any attempt to insert into a full vector fails. This parameter defaults to the constant WCDEFAULT_VECTOR_RESIZE_GROW (currently defined as 5).
If the vector object can't be fully initialized, the vector is created with length zero.
If the new vector can't be fully created, it will have length zero. The out_of_memory exception is thrown if it's enabled in the vector being copied.
The WCPtrOrderedVector<Type> constructor creates an initialized WCPtrOrderedVector object.
WCExcept::out_of_memory, WCExcept::resize_required, WCPtrOrderedVector<Type>::operator =()
destroy a WCPtrOrderedVector<Type> object
#include <wcvector.h> public: virtual ~WCPtrOrderedVector();
The WCPtrOrderedVector<Type> destructor is the destructor for the WCPtrOrderedVector class.
If the vector isn't length zero and the not_empty exception is enabled, it's thrown. Otherwise, the vector entries are cleared using the clear() member function. The objects to which the vector entries point aren't deleted unless the clearAndDestroy() member function is explicitly called before the destructor is called.
The call to this destructor is inserted implicitly by the compiler at the point where the WCPtrOrderedVector object goes out of scope.
The WCPtrOrderedVector object is destroyed.
WCExcept::not_empty, WCPtrOrderedVector<Type>::clear(), WCPtrOrderedVector<Type>::clearAndDestroy()
create a WCPtrSortedVector<Type> object
#include <wcvector.h> public: WCPtrSortedVector( size_t = WCDEFAULT_VECTOR_LENGTH, unsigned = WCDEFAULT_VECTOR_RESIZE_GROW ); WCPtrSortedVector( const WCPtrSortedVector & );
There are two forms of the WCPtrSortedVector<Type> constructor:
If the resize_required exception isn't enabled, then the second optional parameter is used to specify the value to increase the vector size when an element is inserted into a full vector. If zero(0) is specified as the second parameter, any attempt to insert into a full vector fails. This parameter defaults to the constant WCDEFAULT_VECTOR_RESIZE_GROW (currently defined as 5).
If the vector object can't be fully initialized, the vector is created with length zero.
If the new vector can't be fully created, it will have length zero. The out_of_memory exception is thrown if it's enabled in the vector being copied.
The WCPtrSortedVector<Type> constructor creates an initialized WCPtrSortedVector object.
WCExcept::out_of_memory, WCExcept::resize_required, WCPtrSortedVector<Type>::operator =()
destroy a WCPtrSortedVector<Type> object
#include <wcvector.h> public: virtual ~WCPtrSortedVector();
The WCPtrSortedVector<Type> destructor is the destructor for the WCPtrSortedVector class.
If the vector isn't length zero and the not_empty exception is enabled, it's thrown. Otherwise, the vector entries are cleared using the clear() member function. The objects to which the vector entries point aren't deleted unless the clearAndDestroy() member function is explicitly called before the destructor is called.
The call to this destructor is inserted implicitly by the compiler at the point where the WCPtrSortedVector object goes out of scope.
The WCPtrSortedVector object is destroyed.
WCExcept::not_empty, WCPtrSortedVector<Type>::clear(), WCPtrSortedVector<Type>::clearAndDestroy()
append an element to the vector
#include <wcvector.h> public: int append( Type * );
The append() public member function appends the passed element to be the last element in the vector. This member function has the same semantics as the WCPtrOrderedVector::insert() member function.
This function isn't provided by the WCPtrSortedVector class, since all elements must be inserted in sorted order by the insert() member function. |
Several different results can occur if the vector isn't large enough for the new element:
The append() public member function appends an element to the WCPtrOrderedVector object. A TRUE (non-zero) value is returned if the append is successful. If the append fails, a FALSE (zero) value is returned.
WCExcept::out_of_memory, WCExcept::resize_required, WCPtrOrderedVector<Type>::insert(), WCPtrOrderedVector<Type>::insertAt(), WCPtrOrderedVector<Type>::prepend()
clear all the entries from the vector
#include <wcvector.h> public: void clear();
The clear() public member function is used to clear the vector so that it contains no entries, and is of size zero. Objects pointed to by the vector elements aren't deleted.
The vector object isn't destroyed and re-created by this function, so the object destructor isn't invoked.
The clear() public member function clears the vector to have zero length and no entries.
WCPtrOrderedVector<Type>::~WCPtrOrderedVector(), WCPtrSortedVector<Type>::~WCPtrSortedVector(), WCPtrOrderedVector<Type>::clearAndDestroy(), WCPtrSortedVector<Type>::clearAndDestroy(), WCPtrOrderedVector<Type>::operator =(), WCPtrSortedVector<Type>::operator =()
clear all entries from the vector, deleting them
#include <wcvector.h> public: void clearAndDestroy();
The clearAndDestroy() public member function is used to clear the vector to have length zero and delete the objects pointed to by the vector elements.
The vector object isn't destroyed and re-created by this function, so the vector object destructor isn't invoked.
The clearAndDestroy() public member function clears the vector by deleting the objects pointed to by the vector elements and makes the vector length zero.
WCPtrOrderedVector<Type>::clear(), WCPtrSortedVector<Type>::clear()
determine whether or not a given value is in the vector
#include <wcvector.h> public: int contains( const Type * ) const;
The contains() public member function is used to determine if a value is contained by a vector.
Comparisons are done on the objects pointed to, not the pointers themselves. |
A linear search is used by the WCPtrOrderedVector class to find the value. The WCPtrSortedVector class uses a binary search.
The contains() public member function returns a TRUE (non-zero) value if the element is found in the vector. A FALSE (zero) value is returned if the vector doesn't contain the element.
WCPtrOrderedVector<Type>::index(), WCPtrSortedVector<Type>::index(), WCPtrOrderedVector<Type>::find(), WCPtrSortedVector<Type>::find()
count the elements in the vector
#include <wcvector.h> public: unsigned entries() const;
The entries() public member function is used to find the number of elements that are stored in the vector.
The entries() public member function returns the number of elements in the vector.
WCPtrOrderedVector<Type>::isEmpty(), WCPtrSortedVector<Type>::isEmpty()
find an element in the vector
#include <wcvector.h> public: Type * find( const Type * ) const;
The find() public member function is used to find an element equivalent to the element passed.
Comparisons are done on the objects pointed to, not the pointers themselves. |
The WCPtrOrderedVector class uses a linear search to find the element; the WCPtrSortedVector class uses a binary search.
A pointer to the first equivalent element is returned. NULL(0) is returned if the element isn't in the vector.
WCPtrOrderedVector<Type>::contains(), WCPtrSortedVector<Type>::contains(), WCPtrOrderedVector<Type>::first(), WCPtrSortedVector<Type>::first(), WCPtrOrderedVector<Type>::index(), WCPtrSortedVector<Type>::index(), WCPtrOrderedVector<Type>::last(), WCPtrSortedVector<Type>::last(), WCPtrOrderedVector<Type>::occurrencesOf(), WCPtrSortedVector<Type>::occurrencesOf(), WCPtrOrderedVector<Type>::remove(), WCPtrSortedVector<Type>::remove()
return the first element in the vector
#include <wcvector.h> public: Type * first() const;
The first() public member function returns the first element in the vector. The element isn't removed from the vector.
If the vector is empty, one of two exceptions can be thrown:
If neither exception is enabled, a first element of the vector is added with a NULL value.
The first() public member function returns the value of the first element in the vector.
WCExcept::index_range, WCExcept::empty_container, WCPtrOrderedVector<Type>::last(), WCPtrSortedVector<Type>::last(), WCPtrOrderedVector<Type>::removeFirst(), WCPtrSortedVector<Type>::removeFirst()
find the index of an element in the vector
#include <wcvector.h> public: int index( const Type * ) const;
The index() public member function is used find the index of the first element equivalent to the passed element.
Comparisons are done on the objects pointed to, not the pointers themselves. |
A linear search is used by the WCPtrOrderedVector class to find the element; the WCPtrSortedVector class uses a binary search.
The index() public member function returns the index of the first element equivalent to the parameter. If the passed value isn't contained in the vector, negative one (-1) is returned.
WCPtrOrderedVector<Type>::contains(), WCPtrSortedVector<Type>::contains(), WCPtrOrderedVector<Type>::find(), WCPtrSortedVector<Type>::find(), WCPtrOrderedVector<Type>::insertAt(), WCPtrOrderedVector<Type>::operator [ ](), WCPtrSortedVector<Type>::operator [ ](), WCPtrOrderedVector<Type>::removeAt(), WCPtrSortedVector<Type>::removeAt()
insert a value into the vector
#include <wcvector.h> public: int insert( Type * );
The insert() public member function inserts the value into the vector.
Comparisons are done on the objects pointed to, not the pointers themselves. |
Any elements greater than the inserted value are copied up one index so that the new element is after all elements with value less than or equal to it.
Several different results can occur if the vector isn't large enough for the new element:
The insert() public member function inserts an element in to the vector. A TRUE (non-zero) value is returned if the insertion is successful. If the insertion fails, a FALSE (zero) value is returned.
WCExcept::out_of_memory, WCExcept::resize_required, WCPtrOrderedVector<Type>::append(), WCPtrSortedVector<Type>::append(), WCPtrOrderedVector<Type>::insertAt(), WCPtrOrderedVector<Type>::prepend(), WCPtrSortedVector<Type>::prepend()
insert an element in a specific location in the vector
#include <wcvector.h> public: int insertAt( int, Type * );
The insertAt() public member function inserts the second argument into the vector before the element at the index given by the first argument. If the passed index is equal to the number of entries in the vector, the new value is appended to the vector as the last element. All vector elements with indexes greater than or equal to the first parameter are copied up one index.
This function isn't provided by the WCPtrSortedVector class, since all elements must be inserted in sorted order by the insert() member function.
If the passed index is negative or greater than the number of entries in the vector and the index_range exception is enabled, it's thrown. If the exception isn't enabled, the new element is inserted as the first element when the index is negative, or as the last element when the index is too large.
Several different results can occur if the vector isn't large enough for the new element:
The insertAt() public member function inserts an element into the WCPtrOrderedVector object before the element at the given index. A TRUE (non-zero) value is returned if the insertion is successful; if it fails, a FALSE (zero) value is returned.
WCExcept::index_range, WCExcept::out_of_memory, WCExcept::resize_required, WCPtrOrderedVector<Type>::append(), WCPtrOrderedVector<Type>::insert(), WCPtrOrderedVector<Type>::prepend(), WCPtrOrderedVector<Type>::operator [ ](), WCPtrOrderedVector<Type>::removeAt()
determine whether or not the vector is empty
#include <wcvector.h> public: int isEmpty() const;
The isEmpty() public member function is used to determine if a vector object has any entries in it.
A TRUE value (non-zero) is returned if the vector object doesn't have any vector elements in it. A FALSE (zero) result is returned if the vector contains at least one element.
WCPtrOrderedVector<Type>::entries(), WCPtrSortedVector<Type>::entries()
return the last element in the vector
#include <wcvector.h> public: Type * last() const;
The last() public member function returns the last element in the vector. The element isn't removed from the vector.
If the vector is empty, one of two exceptions can be thrown:
If neither exception is enabled, a first element of the vector is added with a NULL value.
The last() public member function returns the value of the last element in the vector.
WCExcept::index_range, WCExcept::empty_container, WCPtrOrderedVector<Type>::first(), WCPtrSortedVector<Type>::first(), WCPtrOrderedVector<Type>::removeLast(), WCPtrSortedVector<Type>::removeLast()
count the occurrences of a given element
#include <wcvector.h> public: int occurrencesOf( const Type * ) const;
The occurrencesOf() public member function returns the number of elements contained in the vector that are equivalent to the passed value.
Comparisons are done on the objects pointed to, not the pointers themselves. |
A linear search is used by the WCPtrOrderedVector class to find the value; the WCPtrSortedVector class uses a binary search.
The occurrencesOf() public member function returns the number of elements equivalent to the passed value.
WCPtrOrderedVector<Type>::contains(), WCPtrSortedVector<Type>::contains(), WCPtrOrderedVector<Type>::find(), WCPtrSortedVector<Type>::find(), WCPtrOrderedVector<Type>::index(), WCPtrSortedVector<Type>::index(), WCPtrOrderedVector<Type>::operator [ ](), WCPtrSortedVector<Type>::operator [ ](), WCPtrOrderedVector<Type>::removeAll(), WCPtrSortedVector<Type>::removeAll()
index into the vector
#include <wcvector.h> public: Type * & operator [ ]( int ); Type * const & operator [ ]( int ) const;
operator [ ]() is the vector index operator. A reference to the object stored in the vector at the given index is returned. If a constant vector is indexed, a reference to a constant element is returned.
The append(), insert(), insertAt() and prepend() member functions are used to insert a new element into a vector, and the remove(), removeAll(), removeAt(), removeFirst() and removeLast() member functions remove elements. The index operator can't be used to change the number of entries in the vector. Searches may be performed using the find() and index() member functions.
If the vector is empty, one of two exceptions can be thrown:
If neither exception is enabled, a first element of the vector is added with a NULL value. This element is added so that a reference to a valid vector element can be returned.
If the index value is negative and the index_range exception is enabled, it's thrown. An attempt to index an element with index greater than or equal to the number of entries in the vector will also cause the index_range exception to be thrown if enabled. If the exception isn't enabled, attempting to index a negative element will index the first element in the vector, and attempting to index an element after the last entry will index the last element.
Take care, when using the WCPtrSortedVector class, not to change the ordering of the vector elements. The result returned by the index operator must not be assigned to or modified in such a way that it's no longer equivalent (according to Type's equivalence operator) to the value inserted into the vector. Failure to comply may cause lookups to work incorrectly, since the binary search algorithm assumes elements are in sorted order. |
The operator [ ]() public member function returns a reference to the element at the given index. If the index is invalid, a reference to the closest valid element is returned. The result of the non-constant index operator may be assigned to.
WCExcept::empty_container, WCExcept::index_range, WCPtrOrderedVector<Type>::append(), WCPtrOrderedVector<Type>::find(), WCPtrSortedVector<Type>::find(), WCPtrOrderedVector<Type>::first(), WCPtrSortedVector<Type>::first(), WCPtrOrderedVector<Type>::index(), WCPtrSortedVector<Type>::index(), WCPtrOrderedVector<Type>::insert(), WCPtrSortedVector<Type>::insert(), WCPtrOrderedVector<Type>::insertAt(), WCPtrOrderedVector<Type>::isEmpty(), WCPtrSortedVector<Type>::isEmpty(), WCPtrOrderedVector<Type>::last(), WCPtrSortedVector<Type>::last(), WCPtrOrderedVector<Type>::prepend(), WCPtrOrderedVector<Type>::remove(), WCPtrSortedVector<Type>::remove(), WCPtrOrderedVector<Type>::removeAt(), WCPtrSortedVector<Type>::removeAt(), WCPtrOrderedVector<Type>::removeAll(), WCPtrSortedVector<Type>::removeAll(), WCPtrOrderedVector<Type>::removeFirst(), WCPtrSortedVector<Type>::removeFirst(), WCPtrOrderedVector<Type>::removeLast(), WCPtrSortedVector<Type>::removeLast()
assign one vector to another
#include <wcvector.h> public: WCPtrOrderedVector & WCPtrOrderedVector::operator =( const WCPtrOrderedVector & ); WCPtrSortedVector & WCPtrSortedVector::operator =( const WCPtrSortedVector & );
The operator =() public member function is the assignment operator for the class. The left hand side vector is first cleared using the clear() member function, and then the right hand side vector is copied. The left hand side vector is made to have the same length and growth amount as the right hand side (the growth amount is the second argument passed to the right hand side vector constructor). All of the vector elements and exception trap states are copied.
If the left hand side vector can't be fully created, it will have length zero. The out_of_memory exception is thrown if it's enabled in the right hand side vector.
The operator =() public member function assigns the left hand side vector to be a copy of the right hand side.
WCExcept::out_of_memory, WCPtrOrderedVector<Type>::clear(), WCPtrSortedVector<Type>::clear(), WCPtrOrderedVector<Type>::clearAndDestroy(), WCPtrSortedVector<Type>::clearAndDestroy()
determine whether or not two vectors are equivalent
#include <wcvector.h> public: int WCPtrOrderedVector::operator ==( const WCPtrOrderedVector & ) const; int WCPtrSortedVector::operator ==( const WCPtrSortedVector & ) const;
The operator ==() public member function is the equivalence operator for the class. Two vector objects are equivalent if they're the same object and share the same address.
A TRUE (non-zero) value is returned if the left hand side and right hand side vectors are the same object. A FALSE (zero) value is returned otherwise.
insert an element at the beginning of the vector
#include <wcvector.h> public: int prepend( Type * );
The prepend() public member function inserts the passed element to be the first element in the vector. All vector elements contained in the vector are copied up one index.
This function isn't provided by the WCPtrSortedVector class, since all elements must be inserted in sorted order by the insert() member function. |
Several different results can occur if the vector isn't large enough for the new element:
The prepend() public member function prepends an element to the WCPtrOrderedVector object. A TRUE (non-zero) value is returned if the insertion is successful; if it fails, a FALSE (zero) value is returned.
WCExcept::out_of_memory, WCExcept::resize_required, WCPtrSortedVector<Type>::append(), WCPtrSortedVector<Type>::insert(), WCPtrSortedVector<Type>::insertAt()
remove an element from the vector
#include <wcvector.h> public: Type * remove( const Type * );
The remove() public member function removes the first element in the vector that's equivalent to the passed value.
Comparisons are done on the objects pointed to, not the pointers themselves. |
All vector elements stored after the removed elements are copied down one index.
A linear search is used by the WCPtrOrderedVector class to find the element being removed; the WCPtrSortedVector class uses a binary search.
The remove() public member function removes the first element in the vector that's equivalent to the passed value. The removed pointer is returned. If the vector didn't contain an equivalent value, NULL(0) is returned.
WCPtrOrderedVector<Type>::clear(), WCPtrSortedVector<Type>::clear(), WCPtrOrderedVector<Type>::clearAndDestroy(), WCPtrSortedVector<Type>::clearAndDestroy(), WCPtrOrderedVector<Type>::find(), WCPtrSortedVector<Type>::find(), WCPtrOrderedVector<Type>::removeAll(), WCPtrSortedVector<Type>::removeAll(), WCPtrOrderedVector<Type>::removeAt(), WCPtrSortedVector<Type>::removeAt(), WCPtrOrderedVector<Type>::removeFirst(), WCPtrSortedVector<Type>::removeFirst(), WCPtrOrderedVector<Type>::removeLast(), WCPtrSortedVector<Type>::removeLast()
remove all occurences of an element from the vector
#include <wcvector.h> public: unsigned removeAll( const Type * );
The removeAll() public member function removes all elements in the vector that are equivalent to the passed value.
Comparisons are done on the objects pointed to, not the pointers themselves. |
All vector elements stored after the removed elements are copied down one or more indexes to take the place of the removed elements.
A linear search is used by the WCPtrOrderedVector class to find the elements being removed; the WCPtrSortedVector class uses a binary search.
The removeAll() public member function removes all elements in the vector that are equivalent to the passed value. The number of elements removed is returned.
WCPtrOrderedVector<Type>::clear(), WCPtrSortedVector<Type>::clear(), WCPtrOrderedVector<Type>::clearAndDestroy(), WCPtrSortedVector<Type>::clearAndDestroy(), WCPtrOrderedVector<Type>::find(), WCPtrSortedVector<Type>::find(), WCPtrOrderedVector<Type>::occurrencesOf(), WCPtrSortedVector<Type>::occurrencesOf(), WCPtrOrderedVector<Type>::remove(), WCPtrSortedVector<Type>::remove(), WCPtrOrderedVector<Type>::removeAt(), WCPtrSortedVector<Type>::removeAt(), WCPtrOrderedVector<Type>::removeFirst(), WCPtrSortedVector<Type>::removeFirst(), WCPtrOrderedVector<Type>::removeLast(), WCPtrSortedVector<Type>::removeLast()
remove the element at the given index into the vector
#include <wcvector.h> public: Type * removeAt( int );
The removeAt() public member function removes the element at the given index. All vector elements stored after the removed elements are copied down one index.
If the vector is empty and the empty_container exception is enabled, it's thrown.
If an attempt to remove an element with a negative index is made and the index_range exception is enabled, it's thrown. If the exception isn't enabled, the first element is removed from the vector. Attempting to remove an element with index greater or equal to the number of entries in the vector also causes the index_range exception to be thrown if enabled. The last element in the vector is removed if the exception isn't enabled.
The removeAt() public member function removes the element with the given index. If the index is invalid, the closest element to the given index is removed. The removed pointer is returned. If the vector is empty, NULL(0) is returned.
WCExcept::empty_container, WCExcept::index_range, WCPtrOrderedVector<Type>::clear(), WCPtrSortedVector<Type>::clear(), WCPtrOrderedVector<Type>::clearAndDestroy(), WCPtrSortedVector<Type>::clearAndDestroy(), WCPtrOrderedVector<Type>::insertAt(), WCPtrSortedVector<Type>::insertAt(), WCPtrOrderedVector<Type>::operator [ ](), WCPtrSortedVector<Type>::operator [ ](), WCPtrOrderedVector<Type>::remove(), WCPtrSortedVector<Type>::remove(), WCPtrOrderedVector<Type>::removeAll(), WCPtrSortedVector<Type>::removeAll(), WCPtrOrderedVector<Type>::removeFirst(), WCPtrSortedVector<Type>::removeFirst(), WCPtrOrderedVector<Type>::removeLast(), WCPtrSortedVector<Type>::removeLast()
remove the first element in the vector
#include <wcvector.h> public: Type * removeFirst();
The removeFirst() public member function removes the first element from a vector. All other vector elements are copied down one index.
If the vector is empty and the empty_container exception is enabled, it's thrown.
The removeFirst() public member function removes the first element from the vector. If the vector was empty, NULL(0) is returned.
WCExcept::empty_container, WCPtrOrderedVector<Type>::clear(), WCPtrSortedVector<Type>::clear(), WCPtrOrderedVector<Type>::clearAndDestroy(), WCPtrSortedVector<Type>::clearAndDestroy(), WCPtrOrderedVector<Type>::first(), WCPtrSortedVector<Type>::first(), WCPtrOrderedVector<Type>::remove(), WCPtrSortedVector<Type>::remove(), WCPtrOrderedVector<Type>::removeAt(), WCPtrSortedVector<Type>::removeAt(), WCPtrOrderedVector<Type>::removeAll(), WCPtrSortedVector<Type>::removeAll(), WCPtrOrderedVector<Type>::removeLast(), WCPtrSortedVector<Type>::removeLast()
remove the last element from the vector
#include <wcvector.h> public: Type * removeLast();
The removeLast() public member function removes the last element from a vector. If the vector is empty and the empty_container exception is enabled, it's thrown.
The removeLast() public member function removes the last element from the vector. The removed pointer is returned. If the vector was empty, NULL(0) is returned.
WCExcept::empty_container, WCPtrOrderedVector<Type>::clear(), WCPtrSortedVector<Type>::clear(), WCPtrOrderedVector<Type>::clearAndDestroy(), WCPtrSortedVector<Type>::clearAndDestroy(), WCPtrOrderedVector<Type>::last(), WCPtrSortedVector<Type>::last(), WCPtrOrderedVector<Type>::remove(), WCPtrSortedVector<Type>::remove(), WCPtrOrderedVector<Type>::removeAt(), WCPtrSortedVector<Type>::removeAt(), WCPtrOrderedVector<Type>::removeAll(), WCPtrSortedVector<Type>::removeAll(), WCPtrOrderedVector<Type>::removeFirst(), WCPtrSortedVector<Type>::removeFirst()
change the size of the vector
#include <wcvector.h> public: int resize( size_t new_size );
The resize() public member function is used to change the vector size to be able to store new_size elements:
If the resize can't be performed and the out_of_memory exception is enabled, it's thrown.
The vector is resized to new_size. A TRUE value (non-zero) is returned if the resize is successful. A FALSE (zero) result is returned if the resize fails.
wcvector.h
The WCPtrVector<Type> class is a templated class used to store objects in a vector. Vectors are similar to arrays, but vectors perform bounds checking and can be resized. Elements are inserted into the vector by assigning to a vector index.
The WCPtrOrderedVector and WCPtrSortedVector classes are also available. They provide a more abstract view of the vector and additional functionality, including finding and removing elements.
In the description of each member function, the text Type is used to indicate the template parameter defining the type pointed to by the pointers stored in the vector.
The WCExcept class is a base class of the WCPtrVector<Type> class, and provides the exceptions() member function. This member function controls the exceptions that can be thrown by the WCPtrVector<Type> object. No exceptions are enabled unless they're set by the exceptions() member function.
The WCPtrVector<Type> class requires nothing from Type.
The following member functions are declared in the public interface:
WCPtrVector( size_t = 0 ); WCPtrVector( size_t, const Type * ); WCPtrVector( const WCPtrVector & ); virtual ~WCPtrVector(); void clear(); void clearAndDestroy(); size_t length() const; int resize( size_t );
The following member operators are declared in the public interface:
Type * & operator [ ]( int ); Type * const & operator [ ]( int ) const; WCPtrVector & operator =( const WCPtrVector & ); int operator ==( const WCPtrVector & ) const;
create a WCPtrVector<Type> object
#include <wcvector.h> public: WCPtrVector( size_t = 0 ); WCPtrVector( size_t, const Type * ); WCPtrVector( const WCPtrVector & );
There are three forms of the public WCPtrVector<Type> constructor:
If the vector object can't be fully initialized, the vector is created with length zero.
If the vector object can't be fully initialized, the vector is created with length zero.
If the new vector can't be fully created, it will have length zero. The out_of_memory exception is thrown if it's enabled in the vector being copied.
The public WCPtrVector<Type> constructor creates an initialized WCPtrVector<Type> object.
WCExcept::out_of_memory, WCPtrVector<Type>::~WCPtrVector(), WCPtrVector<Type>::operator =()
destroy a WCPtrVector<Type> object
#include <wcvector.h> public: virtual ~WCPtrVector();
The public WCPtrVector<Type> destructor is the destructor for the WCPtrVector<Type> class.
If the vector isn't length zero and the not_empty exception is enabled, it's thrown. Otherwise, the vector elements are cleared using the clear() member function. The objects to which the vector elements point aren't deleted unless the clearAndDestroy() member function is explicitly called before the destructor is called.
The call to this destructor is inserted implicitly by the compiler at the point where the WCPtrVector<Type> object goes out of scope.
The WCPtrVector<Type> object is destroyed.
WCExcept::not_empty, WCPtrVector<Type>::clear(), WCPtrVector<Type>::clearAndDestroy()
clear the vector
#include <wcvector.h> public: void clear();
The clear() public member function is used to clear the vector so that it's of length zero. Objects pointed to by the vector elements aren't deleted. The vector object isn't destroyed and re-created by this function, so the object destructor isn't invoked.
The clear() public member function clears the vector to have length zero and no vector elements.
WCPtrVector<Type>::~WCPtrVector(), WCPtrVector<Type>::clearAndDestroy(), WCPtrVector<Type>::operator =()
clear the vector, destroying the elements
#include <wcvector.h> public: void clearAndDestroy();
The clearAndDestroy() public member function is used to clear the vector to have length zero and delete the objects pointed to by the vector elements. The vector object isn't destroyed and re-created by this function, so the vector object destructor isn't invoked.
The clearAndDestroy() public member function clears the vector by deleting the objects pointed to by the vector elements, and makes the vector length zero.
calculate the length of the vector
#include <wcvector.h> public: size_t length() const;
The length() public member function is used to find the number of elements that can be stored in the WCPtrVector<Type> object.
The length() public member function returns the length of the vector.
index into the vector
#include <wcvector.h> public: Type * & operator [ ]( int ); Type * const & operator [ ]( int ) const;
operator [ ]() is the vector index operator. A reference to the object stored in the vector at the given index is returned. If a constant vector is indexed, a reference to a constant element is returned. The index operator of a non-constant vector is the only way to insert an element into the vector.
If an attempt to access an element with index greater than or equal to the length of a non-constant vector is made and the resize_required exception is enabled, it's thrown. If the exception isn't enabled, the vector is automatically resized using the resize() member function to have length the index value plus one. New vector elements are initialized to NULL(0).
If the resize fails, and the out_of_memory exception is enabled, it's thrown. If the exception isn't enabled and the resize failed, the last element is indexed (a new element if the vector was length zero). If a negative value is used to index the non-constant vector and the index_range exception is enabled, it's thrown. If the exception isn't enabled and the vector is empty, the resize_required exception may be thrown.
An attempt to index an empty constant vector may cause one of two exceptions to be thrown:
If neither exception is enabled, a first vector element is added and indexed (so that a reference to a valid element can be returned).
Indexing with a negative value or a value greater than or equal to the length of a constant vector causes the index_range exception to be thrown, if enabled.
The operator [ ]() public member function returns a reference to the element at the given index. If the index is invalid, a reference to the closest valid element is returned. The result of the non-constant index operator may be assigned to.
WCExcept::empty_container, WCExcept::index_range, WCExcept::out_of_memory, WCExcept::resize_required, WCPtrVector<Type>::resize()
assign one vector to another
#include <wcvector.h> public: WCPtrVector & operator =( const WCPtrVector & );
The operator =() public member function is the assignment operator for the WCPtrVector<Type> class. The left hand side vector is first cleared using the clear() member function, and then the right hand side vector is copied. The left hand side vector is made to have the same length as the right hand side. All of the vector elements and exception trap states are copied.
If the left hand side vector can't be fully created, it will have length zero. The out_of_memory exception is thrown if it's enabled in the right hand side vector.
The operator =() public member function assigns the left hand side vector to be a copy of the right hand side.
WCExcept::out_of_memory, WCPtrVector<Type>::clear(), WCPtrVector<Type>::clearAndDestroy()
determine whether or not two vectors are equivalent
#include <wcvector.h> public: int operator ==( const WCPtrVector & ) const;
The operator ==() public member function is the equivalence operator for the WCPtrVector<Type> class. Two vector objects are equivalent if they're the same object and share the same address.
A TRUE (non-zero) value is returned if the left hand side and right hand side vectors are the same object. A FALSE (zero) value is returned otherwise.
change the size of the vector
#include <wcvector.h> public: int resize( size_t new_size );
The resize() public member function is used to change the vector size to be able to store new_size elements:
If the resize can't be performed and the out_of_memory exception is enabled, it's thrown.
The vector is resized to new_size. A TRUE value (non-zero) is returned if the resize is successful. A FALSE (zero) result is returned if the resize fails.
wcvector.h
The WCValOrderedVector<Type> and WCValSortedVector<Type> classes are templated classes used to store objects in a vector. Ordered and Sorted vectors are powerful arrays that can be resized, and provide an abstract interface to insert, find and remove elements:
Elements can't be inserted into these vectors by assigning to a vector index. Vectors automatically grow when necessary to insert an element if the resize_required exception isn't enabled.
In the description of each member function, the text Type is used to indicate the template parameter defining the type of the elements stored in the vector.
Values are copied into the vector, which could be undesirable if the stored objects are complicated and copying is expensive. Value vectors shouldn't be used to store objects of a base class if any derived types of different sizes would be stored in the vector, or if the destructor for a derived class must be called.
The WCValOrderedVector class stores elements in the order in which they're inserted using the insert(), append(), prepend() and insertAt() member functions. Linear searches are performed to locate entries, and the less-than operator isn't required.
The WCValSortedVector class stores elements in ascending order. Type must provide a less-than operator. Insertions are more expensive than inserting or appending into an ordered vector, since entries must be moved to make room for the new element. A binary search is used to locate elements in a sorted vector, making searches quicker than in the ordered vector.
Take care, when using the WCValSortedVector class, not to change the ordering of the vector elements. The result returned by the index operator mustn't be assigned to or modified in such a way that it's no longer equivalent to the value inserted into the vector. Lookups assume elements are in sorted order. |
The WCValVector class is also available. It provides a resizable and boundary safe vector similar to standard arrays.
The WCExcept class is a base class of the WCValOrderedVector<Type> and WCValSortedVector<Type> classes, and provides the exceptions() member function. This member function controls the exceptions that can be thrown by the WCValOrderedVector<Type> and WCValSortedVector<Type> objects. No exceptions are enabled unless they're set by the exceptions() member function.
Both the WCValOrderedVector<Type> and WCValSortedVector<Type> classes require Type to have:
Type::Type()
Type::Type( const Type & )
Type & operator =( const Type & )
void * operator new( size_t, void *ptr ) { return( ptr ); }
int operator ==( const Type & ) const
Additionally the WCValSortedVector class requires Type to have:
int operator <( const Type & ) const
The following member functions are declared in the public interface:
WCValOrderedVector( size_t = WCDEFAULT_VECTOR_LENGTH, unsigned = WCDEFAULT_VECTOR_RESIZE_GROW ); WCValOrderedVector( const WCValOrderedVector & ); virtual ~WCValOrderedVector(); WCValSortedVector( size_t = WCDEFAULT_VECTOR_LENGTH, unsigned = WCDEFAULT_VECTOR_RESIZE_GROW ); WCValSortedVector( const WCValSortedVector & ); virtual ~WCValSortedVector(); void clear(); int contains( const Type & ) const; unsigned entries() const; int find( const Type &, Type & ) const; Type first() const; int index( const Type & ) const; int insert( const Type & ); int isEmpty() const; Type last() const; int occurrencesOf( const Type & ) const; int remove( const Type & ); unsigned removeAll( const Type & ); int removeAt( int ); int removeFirst(); int removeLast(); int resize( size_t );
The following public member functions are available for the WCValOrderedVector class only:
int append( const Type & ); int insertAt( int, const Type & ); int prepend( const Type & );
The following member operators are declared in the public interface:
Type & operator [ ]( int ); const Type & operator [ ]( int ) const; WCValOrderedVector & WCValOrderedVector::operator =( const WCValOrderedVector & ); WCValSortedVector & WCValSortedVector::operator =( const WCValSortedVector & ); int WCValOrderedVector::operator ==( const WCValOrderedVector & ) const; int WCValSortedVector::operator ==( const WCValSortedVector & ) const;
create a WCValOrderedVector<Type> object
#include <wcvector.h> public: WCValOrderedVector( size_t = WCDEFAULT_VECTOR_LENGTH, unsigned = WCDEFAULT_VECTOR_RESIZE_GROW ); WCValOrderedVector( const WCValOrderedVector & );
There are two forms of the WCValOrderedVector<Type> constructor:
If the resize_required exception isn't enabled, then the second optional parameter is used to specify the value to increase the vector size when an element is inserted into a full vector. If zero(0) is specified as the second parameter, any attempt to insert into a full vector fails. This parameter defaults to the constant WCDEFAULT_VECTOR_RESIZE_GROW (currently defined as 5).
If the vector object can't be fully initialized, the vector is created with length zero.
If the new vector can't be fully created, it will have length zero. The out_of_memory exception is thrown if it's enabled in the vector being copied.
The WCValOrderedVector<Type> constructor creates an initialized WCValOrderedVector object.
WCExcept::out_of_memory, WCExcept::resize_required, WCValOrderedVector<Type>::operator =()
destroy a WCValOrderedVector<Type> object
#include <wcvector.h> public: virtual ~WCValOrderedVector();
The WCValOrderedVector<Type> destructor is the destructor for the WCValOrderedVector class. If the vector isn't length zero and the not_empty exception is enabled, it's thrown. Otherwise, the vector entries are cleared using the clear() member function.
The call to this destructor is inserted implicitly by the compiler at the point where the WCValOrderedVector object goes out of scope.
The WCValOrderedVector object is destroyed.
WCExcept::not_empty, WCValOrderedVector<Type>::clear()
create a WCValSortedVector<Type> object
#include <wcvector.h> public: WCValSortedVector( size_t = WCDEFAULT_VECTOR_LENGTH, unsigned = WCDEFAULT_VECTOR_RESIZE_GROW ); WCValSortedVector( const WCValSortedVector & );
There are two forms of the WCValSortedVector<Type> constructor:
If the resize_required exception isn't enabled, then the second optional parameter is used to specify the value to increase the vector size when an element is inserted into a full vector. If zero(0) is specified as the second parameter, any attempt to insert into a full vector fails. This parameter defaults to the constant WCDEFAULT_VECTOR_RESIZE_GROW (currently defined as 5).
If the vector object can't be fully initialized, the vector is created with length zero.
If the new vector can't be fully created, it will have length zero. The out_of_memory exception is thrown if it's enabled in the vector being copied.
The WCValSortedVector<Type> constructor creates an empty initialized WCValSortedVector object.
WCExcept::out_of_memory, WCExcept::resize_required WCValOrderedVector<Type>::operator =()
destroy a WCValSortedVector<Type> object
#include <wcvector.h> public: virtual ~WCValSortedVector();
The WCValSortedVector<Type> destructor is the destructor for the WCValSortedVector class. If the vector isn't length zero and the not_empty exception is enabled, it's thrown. Otherwise, the vector entries are cleared using the clear() member function.
The call to this destructor is inserted implicitly by the compiler at the point where the WCValSortedVector object goes out of scope.
The WCValSortedVector object is destroyed.
WCExcept::not_empty, WCValOrderedVector<Type>::clear()
append an element to the vector
#include <wcvector.h> public: int append( const Type & );
The append() public member function adds the passed element to the end of the vector. The data stored in the vector is a copy of the data passed as a parameter. This member function has the same semantics as the WCValOrderedVector::insert() member function.
This function isn't provided by the WCValSortedVector class, since all elements must be inserted in sorted order by the insert() member function. |
Several different results can occur if the vector isn't large enough for the new element:
The append() public member function appends an element to the WCValOrderedVector object. A TRUE (non-zero) value is returned if the append is successful; if it fails, FALSE (zero) is returned.
WCExcept::out_of_memory, WCExcept::resize_required, WCValOrderedVector<Type>::insert(), WCValOrderedVector<Type>::insertAt(), WCValOrderedVector<Type>::prepend()
clear the vector
#include <wcvector.h> public: void clear();
The clear() public member function is used to clear the vector so that it contains no entries, and is of size zero. Elements stored in the vector are destroyed using Type's destructor. The vector object isn't destroyed and re-created by this function, so the object destructor isn't invoked.
The clear() public member function clears the vector to have length zero and no entries.
WCValOrderedVector<Type>::~WCValOrderedVector(), WCValSortedVector<Type>::~WCValSortedVector(), WCValOrderedVector<Type>::operator =(), WCValSortedVector<Type>::operator =()
determine whether or not a given value is in the vector
#include <wcvector.h> public: int contains( const Type & ) const;
The contains() public member function is used to determine if a value is contained by a vector. A linear search is used by the WCValOrderedVector class to find the value; the WCValSortedVector class uses a binary search.
The contains() public member function returns a TRUE (non-zero) value if the element is found in the vector. It returns a FALSE (zero) value if the vector doesn't contain the element.
WCValOrderedVector<Type>::index(), WCValSortedVector<Type>::index(), WCValOrderedVector<Type>::find(), WCValSortedVector<Type>::find()
count the elements in the vector
#include <wcvector.h> public: unsigned entries() const;
The entries() public member function is used to find the number of elements that are stored in the vector.
The entries() public member function returns the number of elements in the vector.
WCValOrderedVector<Type>::isEmpty(), WCValSortedVector<Type>::isEmpty()
find an element in the vector
#include <wcvector.h> public: int find( const Type &, Type & ) const;
The find() public member function is used to find an element equivalent to the first argument. The WCValOrderedVector class uses a linear search to find the element, and the WCValSortedVector class uses a binary search.
If an equivalent element is found, a TRUE (non-zero) value is returned, and the second parameter is assigned the first equivalent value. A FALSE (zero) value is returned and the second parameter is unchanged if the element is not in the vector.
WCValOrderedVector<Type>::contains(), WCValSortedVector<Type>::contains(), WCValOrderedVector<Type>::first(), WCValSortedVector<Type>::first(), WCValOrderedVector<Type>::index(), WCValSortedVector<Type>::index(), WCValOrderedVector<Type>::last(), WCValSortedVector<Type>::last(), WCValOrderedVector<Type>::occurrencesOf(), WCValSortedVector<Type>::occurrencesOf(), WCValOrderedVector<Type>::remove(), WCValSortedVector<Type>::remove()
return the first element in the vector
#include <wcvector.h> public: Type first() const;
The first() public member function returns the first element in the vector. The element isn't removed from the vector.
If the vector is empty, one of two exceptions can be thrown:
If neither exception is enabled, a first element of the vector is added with a default value.
The first() public member function returns the value of the first element in the vector.
WCExcept::index_range, WCExcept::empty_container, WCValOrderedVector<Type>::last(), WCValSortedVector<Type>::last(), WCValOrderedVector<Type>::removeFirst(), WCValSortedVector<Type>::removeFirst()
find the index of an element
#include <wcvector.h> public: int index( const Type & ) const;
The index() public member function is used find the index of the first element equivalent to the passed element. A linear search is used by the WCValOrderedVector class to find the element; the WCValSortedVector class uses a binary search.
The index() public member function returns the index of the first element equivalent to the parameter. If the passed value isn't contained in the vector, negative one (-1) is returned.
WCValOrderedVector<Type>::contains(), WCValSortedVector<Type>::contains(), WCValOrderedVector<Type>::find(), WCValSortedVector<Type>::find(), WCValOrderedVector<Type>::insertAt(), WCValOrderedVector<Type>::operator [ ](), WCValSortedVector<Type>::operator [ ](), WCValOrderedVector<Type>::removeAt(), WCValSortedVector<Type>::removeAt()
insert an element into the vector
#include <wcvector.h> public: int insert( const Type & );
The insert() public member function inserts the value into the vector. The data stored in the vector is a copy of the data passed as a parameter.
The WCValOrderedVector::insert() function inserts the value as the last element of the vector, and has the same semantics as the WCValOrderedVector::append() member function.
A binary search is performed to determine where the value should be inserted for the WCValSortedVector::insert() function. Any elements greater than the inserted value are copied up one index (using Type's assignment operator), so that the new element is after all elements with value less than or equal to it.
Several different results can occur if the vector isn't large enough for the new element:
The insert() public member function inserts an element in to the vector. A TRUE (non-zero) value is returned if the insertion is successful; If it fails, a FALSE (zero) value is returned.
WCExcept::out_of_memory, WCExcept::resize_required, WCValOrderedVector<Type>::append(), WCValOrderedVector<Type>::insertAt(), WCValOrderedVector<Type>::prepend()
insert an element in a specific location in the vector
#include <wcvector.h> public: int insertAt( int, const Type & );
The insertAt() public member function inserts the second argument into the vector before the element at index given by the first argument. If the passed index is equal to the number of entries in the vector, the new value is appended to the vector as the last element. The data stored in the vector is a copy of the data passed as a parameter. All vector elements with indexes greater than or equal to the first parameter are copied (using Type's assignment operator) up one index.
This function isn't provided by the WCValSortedVector class, since all elements must be inserted in sorted order by the insert() member function. |
If the passed index is negative or greater than the number of entries in the vector and the index_range exception is enabled, it's thrown. If the exception isn't enabled, the new element is inserted as the first element when the index is negative, or as the last element when the index is too large.
Several different results can occur if the vector isn't large enough for the new element:
The insertAt() public member function inserts an element into the WCValOrderedVector object before the element at the given index. A TRUE (non-zero) value is returned if the insertion is successful. If the insertion fails, a FALSE (zero) value is returned.
WCExcept::index_range, WCExcept::out_of_memory, WCExcept::resize_required, WCValOrderedVector<Type>::append(), WCValOrderedVector<Type>::insert(), WCValOrderedVector<Type>::prepend(), WCValOrderedVector<Type>::operator [ ](), WCValOrderedVector<Type>::removeAt()
determine whether or not the vector is empty
#include <wcvector.h> public: int isEmpty() const;
The isEmpty() public member function is used to determine if a vector object has any entries in it.
A TRUE value (non-zero) is returned if the vector object doesn't have any vector elements in it. A FALSE (zero) result is returned if the vector contains at least one element.
WCValOrderedVector<Type>::entries(), WCValSortedVector<Type>::entries()
return the last element of the vector
#include <wcvector.h> public: Type last() const;
The last() public member function returns the last element in the vector. The element isn't removed from the vector.
If the vector is empty, one of two exceptions can be thrown:
If neither exception is enabled, a first element of the vector is added with a default value.
The last() public member function returns the value of the last element in the vector.
WCExcept::index_range, WCExcept::empty_container, WCValOrderedVector<Type>::first(), WCValSortedVector<Type>::first(), WCValOrderedVector<Type>::removeLast(), WCValSortedVector<Type>::removeLast()
count the occurrences of an element
#include <wcvector.h> public: int occurrencesOf( const Type & ) const;
The occurrencesOf() public member function returns the number of elements contained in the vector that are equivalent to the passed value.
A linear search is used by the WCValOrderedVector class to find the value; the WCValSortedVector class uses a binary search.
The occurrencesOf() public member function returns the number of elements equivalent to the passed value.
WCValOrderedVector<Type>::contains(), WCValSortedVector<Type>::contains(), WCValOrderedVector<Type>::find(), WCValSortedVector<Type>::find(), WCValOrderedVector<Type>::index(), WCValSortedVector<Type>::index(), WCValOrderedVector<Type>::operator [ ](), WCValSortedVector<Type>::operator [ ](), WCValOrderedVector<Type>::removeAll(), WCValSortedVector<Type>::removeAll()
index into the vector
#include <wcvector.h> public: Type & operator [ ]( int ); const Type & operator [ ]( int ) const;
operator [ ]() is the vector index operator. A reference to the object stored in the vector at the given index is returned. If a constant vector is indexed, a reference to a constant element is returned.
The append(), insert(), insertAt() and prepend() member functions are used to insert a new element into a vector, and the remove(), removeAll(), removeAt(), removeFirst() and removeLast() member functions remove elements. The index operator can't be used to change the number of entries in the vector. Searches may be performed using the find() and index() member functions.
If the vector is empty, one of two exceptions can be thrown:
If neither exception is enabled, a first element of the vector is added with a default value. This element is added so that a reference to a valid vector element can be returned.
If the index value is negative and the index_range exception is enabled, it's thrown. An attempt to index an element with index greater than or equal to the number of entries in the vector will also cause the index_range exception to be thrown if it's enabled. If the exception isn't enabled, attempting to index a negative element indexes the first element in the vector, and attempting to index an element after the last entry indexes the last element.
Take care, when using the WCValSortedVector class, not to change the ordering of the vector elements. The result returned by the index operator mustn't be assigned to or modified in such a way that it's no longer equivalent (by Type's equivalence operator) to the value inserted into the vector. Failure to comply may cause lookups to work incorrectly, since the binary search algorithm assumes elements are in sorted order. |
The operator [ ]() public member function returns a reference to the element at the given index. If the index is invalid, a reference to the closest valid element is returned. The result of the non-constant index operator may be assigned to.
WCExcept::empty_container, WCExcept::index_range, WCValOrderedVector<Type>::append(), WCValOrderedVector<Type>::find(), WCValSortedVector<Type>::find(), WCValOrderedVector<Type>::first(), WCValSortedVector<Type>::first(), WCValOrderedVector<Type>::index(), WCValSortedVector<Type>::index(), WCValOrderedVector<Type>::insert(), WCValSortedVector<Type>::insert(), WCValOrderedVector<Type>::insertAt(), WCValOrderedVector<Type>::isEmpty(), WCValSortedVector<Type>::isEmpty(), WCValOrderedVector<Type>::last(), WCValSortedVector<Type>::last(), WCValOrderedVector<Type>::prepend(), WCValOrderedVector<Type>::remove(), WCValSortedVector<Type>::remove(), WCValOrderedVector<Type>::removeAt(), WCValSortedVector<Type>::removeAt(), WCValOrderedVector<Type>::removeAll(), WCValSortedVector<Type>::removeAll(), WCValOrderedVector<Type>::removeFirst(), WCValSortedVector<Type>::removeFirst(), WCValOrderedVector<Type>::removeLast(), WCValSortedVector<Type>::removeLast()
assign one vector to another
#include <wcvector.h> public: WCValOrderedVector & WCValOrderedVector::operator =( const WCValOrderedVector & ); WCValSortedVector & WCValSortedVector::operator =( const WCValSortedVector & );
The operator =() public member function is the assignment operator for the class. The left hand side vector is first cleared using the clear() member function, and then the right hand side vector is copied. The left hand side vector is made to have the same length and growth amount as the right hand side (the growth amount is the second argument passed to the right hand side vector constructor). All of the vector elements and exception trap states are copied.
If the left hand side vector can't be fully created, it will have length zero. The out_of_memory exception is thrown if it's enabled in the right hand side vector.
The operator =() public member function sets the left hand side vector to be a copy of the right hand side.
WCExcept::out_of_memory, WCValOrderedVector<Type>::clear(), WCValSortedVector<Type>::clear()
determine whether or not two vectors are equivalent
#include <wcvector.h> public: int WCValOrderedVector::operator ==( const WCValOrderedVector & ) const; int WCValSortedVector::operator ==( const WCValSortedVector & ) const;
The operator ==() public member function is the equivalence operator for the class. Two vector objects are equivalent if they're the same object and share the same address.
A TRUE (non-zero) value is returned if the left hand side and right hand side vectors are the same object. A FALSE (zero) value is returned otherwise.
insert an element at the beginning of the vector
#include <wcvector.h> public: int prepend( const Type & );
The prepend() public member function inserts the passed element as the first element in the vector. The data stored in the vector is a copy of the data passed as a parameter. All vector elements contained in the vector are copied (using Type's assignment operator) up one index.
This function isn't provided by the WCValSortedVector class, since all elements must be inserted in sorted order by the insert() member function. |
Several different results can occur if the vector isn't large enough for the new element:
The prepend() public member function prepends an element to the WCValOrderedVector object. A TRUE (non-zero) value is returned if the insertion is successful. If the insertion fails, a FALSE (zero) value is returned.
WCExcept::out_of_memory, WCExcept::resize_required, WCValOrderedVector<Type>::append(), WCValOrderedVector<Type>::insert(), WCValOrderedVector<Type>::insertAt()
remove an element from the vector
#include <wcvector.h> public: int remove( const Type & );
The remove() public member function removes the first element in the vector that's equivalent to the passed value. All vector elements stored after the removed elements are copied (using Type's assignment operator) down one index.
A linear search is used by the WCValOrderedVector class to find the element being removed; the WCValSortedVector class uses a binary search.
The remove() public member function removes the first element in the vector that's equivalent to the passed value. A TRUE (non-zero) value is returned if an equivalent element was contained in the vector and removed. If the vector didn't contain an equivalent value, a FALSE (zero) value is returned.
WCValOrderedVector<Type>::clear(), WCValSortedVector<Type>::clear(), WCValOrderedVector<Type>::find(), WCValSortedVector<Type>::find(), WCValOrderedVector<Type>::removeAll(), WCValSortedVector<Type>::removeAll(), WCValOrderedVector<Type>::removeAt(), WCValSortedVector<Type>::removeAt(), WCValOrderedVector<Type>::removeFirst(), WCValSortedVector<Type>::removeFirst(), WCValOrderedVector<Type>::removeLast(), WCValSortedVector<Type>::removeLast()
remove all occurrences of an element from the vector
#include <wcvector.h> public: unsigned removeAll( const Type & );
The removeAll() public member function removes all elements in the vector that are equivalent to the passed value. All vector elements stored after the removed elements are copied (using Type's assignment operator) down one or more indexes to take the place of the removed elements.
A linear search is used by the WCValOrderedVector class to find the elements being removed; the WCValSortedVector class uses a binary search.
The removeAll() public member function removes all elements in the vector that are equivalent to the passed value. The number of elements removed is returned.
WCValOrderedVector<Type>::clear(), WCValSortedVector<Type>::clear(), WCValOrderedVector<Type>::find(), WCValSortedVector<Type>::find(), WCValOrderedVector<Type>::occurrencesOf(), WCValSortedVector<Type>::occurrencesOf(), WCValOrderedVector<Type>::remove(), WCValSortedVector<Type>::remove(), WCValOrderedVector<Type>::removeAt(), WCValSortedVector<Type>::removeAt(), WCValOrderedVector<Type>::removeFirst(), WCValSortedVector<Type>::removeFirst(), WCValOrderedVector<Type>::removeLast(), WCValSortedVector<Type>::removeLast()
remove the element with the given index from the vector
#include <wcvector.h> public: int removeAt( int );
The removeAt() public member function removes the element at the given index. All vector elements stored after the removed elements are copied (using Type's assignment operator) down one index.
If the vector is empty and the empty_container exception is enabled, it's thrown.
If an attempt is made to remove an element with a negative index and the index_range exception is enabled, it's thrown. If the exception isn't enabled, the first element is removed from the vector. Attempting to remove an element with index greater or equal to the number of entries in the vector also causes the index_range exception to be thrown if enabled. The last element in the vector is removed if the exception isn't enabled.
The removeAt() public member function removes the element with the given index. If the index is invalid, the closest element to the given index is removed. A TRUE (non-zero) value is returned if an element was removed. If the vector was empty, a FALSE (zero) value is returned.
WCValOrderedVector<Type>::clear(), WCValSortedVector<Type>::clear(), WCValOrderedVector<Type>::insertAt(), WCValOrderedVector<Type>::operator [ ](), WCValSortedVector<Type>::operator [ ](), WCValOrderedVector<Type>::remove(), WCValSortedVector<Type>::remove(), WCValOrderedVector<Type>::removeAll(), WCValSortedVector<Type>::removeAll(), WCValOrderedVector<Type>::removeFirst(), WCValSortedVector<Type>::removeFirst(), WCValOrderedVector<Type>::removeLast(), WCValSortedVector<Type>::removeLast()
remove the first element from the vector
#include <wcvector.h> public: int removeFirst();
The removeFirst() public member function removes the first element from a vector. All other vector elements are copied (using Type's assignment operator) down one index.
If the vector is empty and the empty_container exception is enabled, it's thrown.
The removeFirst() public member function removes the first element from the vector. A TRUE (non-zero) value is returned if an element was removed. If the vector was empty, FALSE (zero) value is returned.
WCValOrderedVector<Type>::clear(), WCValSortedVector<Type>::clear(), WCValOrderedVector<Type>::first(), WCValSortedVector<Type>::first(), WCValOrderedVector<Type>::remove(), WCValSortedVector<Type>::remove(), WCValOrderedVector<Type>::removeAt(), WCValSortedVector<Type>::removeAt(), WCValOrderedVector<Type>::removeAll(), WCValSortedVector<Type>::removeAll(), WCValOrderedVector<Type>::removeLast(), WCValSortedVector<Type>::removeLast()
remove the last element from the vector
#include <wcvector.h> public: int removeLast();
The removeLast() public member function removes the last element from a vector. If the vector is empty and the empty_container exception is enabled, it's thrown.
The removeLast() public member function removes the last element from the vector. A TRUE (non-zero) value is returned if an element was removed. If the vector was empty, a FALSE (zero) value is returned.
WCValOrderedVector<Type>::clear(), WCValSortedVector<Type>::clear(), WCValOrderedVector<Type>::last(), WCValSortedVector<Type>::last(), WCValOrderedVector<Type>::remove(), WCValSortedVector<Type>::remove(), WCValOrderedVector<Type>::removeAt(), WCValSortedVector<Type>::removeAt(), WCValOrderedVector<Type>::removeAll(), WCValSortedVector<Type>::removeAll(), WCValOrderedVector<Type>::removeFirst(), WCValSortedVector<Type>::removeFirst()
change the size of the vector
#include <wcvector.h> public: int resize( size_t new_size );
The resize() public member function is used to change the vector size to be able to store new_size elements:
If the resize can't be performed and the out_of_memory exception is enabled, it's thrown.
The vector is resized to new_size. A TRUE value (non-zero) is returned if the resize is successful. A FALSE (zero) result is returned if the resize fails.
wcvector.h
The WCValVector<Type> class is a templated class used to store objects in a vector. Vectors are similar to arrays, but vectors perform bound checking, and can be resized. Elements are inserted into the vector by assigning to a vector index.
The WCValOrderedVector and WCValSortedVector classes are also available. They provide a more abstract view of the vector and additional functionality, including finding and removing elements.
Values are copied into the vector, which could be undesirable if the stored objects are complicated and copying is expensive. Value vectors shouldn't be used to store objects of a base class if any derived types of different sizes would be stored in the vector, or if the destructor for a derived class must be called.
In the description of each member function, the text Type is used to indicate the template parameter defining the type of the elements stored in the vector.
The WCExcept class is a base class of the WCValVector<Type> class, and provides the exceptions() member function. This member function controls the exceptions that can be thrown by the WCValVector<Type> object. No exceptions are enabled unless they're set by the exceptions() member function.
The WCValVector<Type> class requires Type to have:
Type::Type()
Type::Type( const Type & )
void * operator new( size_t, void *ptr ) { return( ptr ); }
The following member functions are declared in the public interface:
WCValVector( size_t = 0 ); WCValVector( size_t, const Type & ); WCValVector( const WCValVector & ); virtual ~WCValVector(); void clear(); size_t length() const; int resize( size_t );
The following member operators are declared in the public interface:
Type & operator [ ]( int ); const Type & operator [ ]( int ) const; WCValVector & operator =( const WCValVector & ); int operator ==( const WCValVector & ) const;
create a WCValVector<Type> object
#include <wcvector.h> public: WCValVector( size_t = 0 ); WCValVector( size_t, const Type & );
There are three forms of the public WCValVector<Type> constructor:
If the vector object can't be fully initialized, the vector is created with length zero.
If the vector object can't be fully initialized, the vector is created with length zero.
If the new vector can't be fully created, it will have length zero. The out_of_memory exception is thrown if it's enabled in the vector being copied.
The public WCValVector<Type> constructor creates an initialized WCValVector<Type> object of the specified length.
WCExcept::out_of_memory, WCValVector<Type>::~WCValVector(), WCValVector<Type>::operator =()
destroy a WCValVector<Type> object
#include <wcvector.h> public: virtual ~WCValVector();
The public WCValVector<Type> destructor is the destructor for the WCValVector<Type> class. If the vector isn't of length zero and the not_empty exception is enabled, it's thrown. Otherwise, the vector elements are cleared using the clear() member function.
The call to this destructor is inserted implicitly by the compiler at the point where the WCValVector<Type> object goes out of scope.
The WCValVector<Type> object is destroyed.
WCExcept::not_empty, WCValVector<Type>::clear()
clear the vector
#include <wcvector.h> public: void clear();
The clear() public member function is used to clear the vector so that it's of length zero. Elements stored in the vector are destroyed using Type's destructor. The vector object isn't destroyed and re-created by this function, so the object destructor isn't invoked.
The clear() public member function clears the vector to have length zero and no vector elements.
WCValVector<Type>::~WCValVector(), WCValVector<Type>::operator =()
calculate the length of the vector
#include <wcvector.h> public: size_t length() const;
The length() public member function is used to find the number of elements that can be stored in the WCValVector<Type> object.
The length() public member function returns the length of the vector.
index into the vector
#include <wcvector.h> public: Type & operator [ ]( int ); const Type & operator [ ]( int ) const;
operator [ ]() is the vector index operator. A reference to the object stored in the vector at the given index is returned. If a constant vector is indexed, a reference to a constant element is returned. The index operator of a non-constant vector is the only way to insert an element into the vector.
If an attempt to access an element with index greater than or equal to the length of a non-constant vector is made and the resize_required exception is enabled, it's thrown. If the exception isn't enabled, the vector is automatically resized using the resize() member function to have length the index value plus one. New vector elements are initialized using Type's default constructor.
If the resize failed, and the out_of_memory exception is enabled, it's thrown. If the exception isn't enabled and the resize failed, the last element is indexed (a new element if the vector was length zero). If a negative value is used to index the non-constant vector and the index_range exception is enabled, it's thrown. If the exception isn't enabled and the vector is empty, the resize_required exception may be thrown.
An attempt to index an empty constant vector may cause one of two exceptions to be thrown:
If neither exception is enabled, a first vector element is added and indexed (so that a reference to a valid element can be returned).
Indexing with a negative value or a value greater than or equal to the length of a constant vector causes the index_range exception to be thrown, if enabled.
The operator [ ]() public member function returns a reference to the element at the given index. If the index is invalid, a reference to the closest valid element is returned. The result of the non-constant index operator may be assigned to.
WCExcept::empty_container, WCExcept::index_range, WCExcept::out_of_memory, WCExcept::resize_required, WCValVector<Type>::resize()
assign one vector to another
#include <wcvector.h> public: WCValVector & operator =( const WCValVector & );
The operator =() public member function is the assignment operator for the WCValVector<Type> class. The left hand side vector is first cleared using the clear() member function, and then the right hand side vector is copied. The left hand side vector is made to have the same length as the right hand side. All of the vector elements and exception trap states are copied.
If the left hand side vector can't be fully created, it will have a length of zero. The out_of_memory exception is thrown if it's enabled in the right hand side vector.
The operator =() public member function assigns the left hand side vector to be a copy of the right hand side.
WCExcept::out_of_memory, WCValVector<Type>::clear()
determine whether or not two vectors are equivalent
#include <wcvector.h> public: int operator ==( const WCValVector & ) const;
The operator ==() public member function is the equivalence operator for the WCValVector<Type> class. Two vector objects are equivalent if they're the same object and share the same address.
A TRUE (non-zero) value is returned if the left hand side and right hand side vectors are the same object. A FALSE (zero) value is returned otherwise.
resize the vector
#include <wcvector.h> public: int resize( size_t new_size );
The resize() public member function is used to change the vector size to be able to store new_size elements:
If the resize can't be performed and the out_of_memory exception is enabled, it's thrown.
The vector is resized to new_size. A TRUE value (non-zero) is returned if the resize is successful. A FALSE (zero) result is returned if the resize fails.