Vector Containers

This chapter discusses the following vector containers:

WCPtrOrderedVector<Type> and WCPtrSortedVector<Type> Classes

Declared:

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


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

Requirements of Type

Both the WCPtrOrderedVector<Type> and WCPtrSortedVector<Type> classes require Type to have:

Additionally the WCPtrSortedVector class requires Type to have:

Public Member Functions

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 * );

Public Member Operators

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;

WCPtrOrderedVector<Type>::WCPtrOrderedVector()

create a WCPtrOrderedVector<Type> object

Synopsis:

#include <wcvector.h>
public:
WCPtrOrderedVector( 
    size_t = WCDEFAULT_VECTOR_LENGTH,
	unsigned = WCDEFAULT_VECTOR_RESIZE_GROW );
WCPtrOrderedVector( const WCPtrOrderedVector & );

Semantics:

There are two forms of the WCPtrOrderedVector<Type> constructor:

  • The first creates an empty WCPtrOrderedVector object that's able to store the number of elements specified in the first optional parameter, which defaults to the constant WCDEFAULT_VECTOR_LENGTH (currently defined as 10).

    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.

  • The second form is the copy constructor for the WCPtrOrderedVector class. The new vector is created with the same length and resize value as the passed vector. All of the vector elements and exception trap states are copied.

    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.

Results:

The WCPtrOrderedVector<Type> constructor creates an initialized WCPtrOrderedVector object.

See also:

WCExcept::out_of_memory, WCExcept::resize_required, WCPtrOrderedVector<Type>::operator =()

WCPtrOrderedVector<Type>::~WCPtrOrderedVector()

destroy a WCPtrOrderedVector<Type> object

Synopsis:

#include <wcvector.h>
public:
virtual ~WCPtrOrderedVector();

Semantics:

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.

Results:

The WCPtrOrderedVector object is destroyed.

See also:

WCExcept::not_empty, WCPtrOrderedVector<Type>::clear(), WCPtrOrderedVector<Type>::clearAndDestroy()

WCPtrSortedVector<Type>::WCPtrSortedVector()

create a WCPtrSortedVector<Type> object

Synopsis:

#include <wcvector.h>
public:
WCPtrSortedVector( 
    size_t = WCDEFAULT_VECTOR_LENGTH,
	unsigned = WCDEFAULT_VECTOR_RESIZE_GROW );
WCPtrSortedVector( const WCPtrSortedVector & );

Semantics:

There are two forms of the WCPtrSortedVector<Type> constructor:

  • The first creates an empty WCPtrSortedVector object that's able to store the number of elements specified in the first optional parameter, which defaults to the constant WCDEFAULT_VECTOR_LENGTH (currently defined as 10).

    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.

  • The second form is the copy constructor for the WCPtrSortedVector class. The new vector is created with the same length and resize value as the passed vector. All of the vector elements and exception trap states are copied.

    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.

Results:

The WCPtrSortedVector<Type> constructor creates an initialized WCPtrSortedVector object.

See also:

WCExcept::out_of_memory, WCExcept::resize_required, WCPtrSortedVector<Type>::operator =()

WCPtrSortedVector<Type>::~WCPtrSortedVector()

destroy a WCPtrSortedVector<Type> object

Synopsis:

#include <wcvector.h>
public:
virtual ~WCPtrSortedVector();

Semantics:

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.

Results:

The WCPtrSortedVector object is destroyed.

See also:

WCExcept::not_empty, WCPtrSortedVector<Type>::clear(), WCPtrSortedVector<Type>::clearAndDestroy()

WCPtrOrderedVector<Type>::append()

append an element to the vector

Synopsis:

#include <wcvector.h>
public:
int append( Type * );

Semantics:

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.


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

  • If the resize_required exception is enabled, it's thrown.
  • If the exception isn't enabled, the append fails if the amount the vector is to be grown (the second parameter to the constructor) is zero(0).
  • Otherwise, the vector is automatically grown by the number of elements specified to the constructor, using the resize() member function. If resize() fails, the element isn't appended to the vector, and the out_of_memory exception is thrown, if enabled.

Results:

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.

See also:

WCExcept::out_of_memory, WCExcept::resize_required, WCPtrOrderedVector<Type>::insert(), WCPtrOrderedVector<Type>::insertAt(), WCPtrOrderedVector<Type>::prepend()

WCPtrOrderedVector<Type>::clear(), WCPtrSortedVector<Type>::clear()

clear all the entries from the vector

Synopsis:

#include <wcvector.h>
public:
void clear();

Semantics:

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.

Results:

The clear() public member function clears the vector to have zero length and no entries.

See also:

WCPtrOrderedVector<Type>::~WCPtrOrderedVector(), WCPtrSortedVector<Type>::~WCPtrSortedVector(), WCPtrOrderedVector<Type>::clearAndDestroy(), WCPtrSortedVector<Type>::clearAndDestroy(), WCPtrOrderedVector<Type>::operator =(), WCPtrSortedVector<Type>::operator =()

WCPtrOrderedVector<Type>::clearAndDestroy(), WCPtrSortedVector<Type>::clearAndDestroy()

clear all entries from the vector, deleting them

Synopsis:

#include <wcvector.h>
public:
void clearAndDestroy();

Semantics:

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.

Results:

The clearAndDestroy() public member function clears the vector by deleting the objects pointed to by the vector elements and makes the vector length zero.

See also:

WCPtrOrderedVector<Type>::clear(), WCPtrSortedVector<Type>::clear()

WCPtrOrderedVector<Type>::contains(), WCPtrSortedVector<Type>::contains()

determine whether or not a given value is in the vector

Synopsis:

#include <wcvector.h>
public:
int contains( const Type * ) const;

Semantics:

The contains() public member function is used to determine if a value is contained by a vector.


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

Results:

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.

See also:

WCPtrOrderedVector<Type>::index(), WCPtrSortedVector<Type>::index(), WCPtrOrderedVector<Type>::find(), WCPtrSortedVector<Type>::find()

WCPtrOrderedVector<Type>::entries(), WCPtrSortedVector<Type>::entries()

count the elements in the vector

Synopsis:

#include <wcvector.h>
public:
unsigned entries() const;

Semantics:

The entries() public member function is used to find the number of elements that are stored in the vector.

Results:

The entries() public member function returns the number of elements in the vector.

See also:

WCPtrOrderedVector<Type>::isEmpty(), WCPtrSortedVector<Type>::isEmpty()

WCPtrOrderedVector<Type>::find(), WCPtrSortedVector<Type>::find()

find an element in the vector

Synopsis:

#include <wcvector.h>
public:
Type * find( const Type * ) const;

Semantics:

The find() public member function is used to find an element equivalent to the element passed.


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

Results:

A pointer to the first equivalent element is returned. NULL(0) is returned if the element isn't in the vector.

See also:

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()

WCPtrOrderedVector<Type>::first(), WCPtrSortedVector<Type>::first()

return the first element in the vector

Synopsis:

#include <wcvector.h>
public:
Type * first() const;

Semantics:

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:

  • The empty_container exception is thrown if it's enabled.
  • Otherwise, if the index_range exception is enabled, it's thrown.

If neither exception is enabled, a first element of the vector is added with a NULL value.

Results:

The first() public member function returns the value of the first element in the vector.

See also:

WCExcept::index_range, WCExcept::empty_container, WCPtrOrderedVector<Type>::last(), WCPtrSortedVector<Type>::last(), WCPtrOrderedVector<Type>::removeFirst(), WCPtrSortedVector<Type>::removeFirst()

WCPtrOrderedVector<Type>::index(), WCPtrSortedVector<Type>::index()

find the index of an element in the vector

Synopsis:

#include <wcvector.h>
public:
int index( const Type * ) const;

Semantics:

The index() public member function is used find the index of the first element equivalent to the passed element.


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

Results:

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.

See also:

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()

WCPtrOrderedVector<Type>::insert(), WCPtrSortedVector<Type>::insert()

insert a value into the vector

Synopsis:

#include <wcvector.h>
public:
int insert( Type * );

Semantics:

The insert() public member function inserts the value into the vector.

  • The WCPtrOrderedVector::insert() function inserts the value as the last element of the vector, and has the same semantics as the WCPtrOrderedVector::append() member function.
  • A binary search is performed to determine where the value should be inserted for the WCPtrSortedVector::insert() function.
    Note: 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:

  • If the resize_required exception is enabled, it's thrown.
  • If the exception isn't enabled, the insertion fails if the amount the vector is to be grown (the second parameter to the constructor) is zero(0).
  • Otherwise, the vector is automatically grown by the number of elements specified to the constructor, using the resize() member function. If resize() fails, the element isn't inserted to the vector and the out_of_memory exception is thrown, if enabled.

Results:

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.

See also:

WCExcept::out_of_memory, WCExcept::resize_required, WCPtrOrderedVector<Type>::append(), WCPtrSortedVector<Type>::append(), WCPtrOrderedVector<Type>::insertAt(), WCPtrOrderedVector<Type>::prepend(), WCPtrSortedVector<Type>::prepend()

WCPtrOrderedVector<Type>::insertAt()

insert an element in a specific location in the vector

Synopsis:

#include <wcvector.h>
public:
int insertAt( int, Type * );

Semantics:

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:

  • If the resize_required exception is enabled, it's thrown.
  • If the exception isn't enabled, the insertion fails if the amount the vector is to be grown (the second parameter to the constructor) is zero(0).
  • Otherwise, the vector is automatically grown by the number of elements specified to the constructor, using the resize() member function. If resize() fails, the element isn't inserted into the vector and the out_of_memory exception is thrown, if enabled.

Results:

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.

See also:

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()

WCPtrOrderedVector<Type>::isEmpty(), WCPtrSortedVector<Type>::isEmpty()

determine whether or not the vector is empty

Synopsis:

#include <wcvector.h>
public:
int isEmpty() const;

Semantics:

The isEmpty() public member function is used to determine if a vector object has any entries in it.

Results:

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.

See also:

WCPtrOrderedVector<Type>::entries(), WCPtrSortedVector<Type>::entries()

WCPtrOrderedVector<Type>::last(), WCPtrSortedVector<Type>::last()

return the last element in the vector

Synopsis:

#include <wcvector.h>
public:
Type * last() const;

Semantics:

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:

  • The empty_container exception is thrown if it's enabled.
  • Otherwise, if the index_range exception is enabled, it's thrown.

If neither exception is enabled, a first element of the vector is added with a NULL value.

Results:

The last() public member function returns the value of the last element in the vector.

See also:

WCExcept::index_range, WCExcept::empty_container, WCPtrOrderedVector<Type>::first(), WCPtrSortedVector<Type>::first(), WCPtrOrderedVector<Type>::removeLast(), WCPtrSortedVector<Type>::removeLast()

WCPtrOrderedVector<Type>::occurrencesOf(), WCPtrSortedVector<Type>::occurrencesOf()

count the occurrences of a given element

Synopsis:

#include <wcvector.h>
public:
int occurrencesOf( const Type * ) const;

Semantics:

The occurrencesOf() public member function returns the number of elements contained in the vector that are equivalent to the passed value.


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

Results:

The occurrencesOf() public member function returns the number of elements equivalent to the passed value.

See also:

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()

WCPtrOrderedVector<Type>::operator [ ](), WCPtrSortedVector<Type>::operator [ ]()

index into the vector

Synopsis:

#include <wcvector.h>
public:
Type * & operator [ ]( int );
Type * const & operator [ ]( int ) const;

Semantics:

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:

  • The empty_container exception is thrown if it's enabled.
  • Otherwise, if the index_range exception is enabled, it's 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.


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

Results:

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.

See also:

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()

WCPtrOrderedVector<Type>::operator =(), WCPtrSortedVector<Type>::operator =()

assign one vector to another

Synopsis:

#include <wcvector.h>
public:
WCPtrOrderedVector & 
    WCPtrOrderedVector::operator =( 
        const WCPtrOrderedVector & );
WCPtrSortedVector & 
    WCPtrSortedVector::operator =( 
        const WCPtrSortedVector & );

Semantics:

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.

Results:

The operator =() public member function assigns the left hand side vector to be a copy of the right hand side.

See also:

WCExcept::out_of_memory, WCPtrOrderedVector<Type>::clear(), WCPtrSortedVector<Type>::clear(), WCPtrOrderedVector<Type>::clearAndDestroy(), WCPtrSortedVector<Type>::clearAndDestroy()

WCPtrOrderedVector<Type>::operator ==(), WCPtrSortedVector<Type>::operator ==()

determine whether or not two vectors are equivalent

Synopsis:

#include <wcvector.h>
public:
int WCPtrOrderedVector::operator ==( 
    const WCPtrOrderedVector & ) const;
int WCPtrSortedVector::operator ==( 
    const WCPtrSortedVector & ) const;

Semantics:

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.

Results:

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.

WCPtrOrderedVector<Type>::prepend()

insert an element at the beginning of the vector

Synopsis:

#include <wcvector.h>
public:
int prepend( Type * );

Semantics:

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.


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

  • If the resize_required exception is enabled, it's thrown.
  • If the exception isn't enabled, the prepend fails if the amount the vector is to be grown (the second parameter to the constructor) is zero(0).
  • Otherwise, the vector is automatically grown by the number of elements specified to the constructor, using the resize() member function. If resize() fails, the element isn't inserted into the vector, and the out_of_memory exception is thrown, if enabled.

Results:

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.

See also:

WCExcept::out_of_memory, WCExcept::resize_required, WCPtrSortedVector<Type>::append(), WCPtrSortedVector<Type>::insert(), WCPtrSortedVector<Type>::insertAt()

WCPtrOrderedVector<Type>::remove(), WCPtrSortedVector<Type>::remove()

remove an element from the vector

Synopsis:

#include <wcvector.h>
public:
Type * remove( const Type * );

Semantics:

The remove() public member function removes the first element in the vector that's equivalent to the passed value.


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

Results:

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.

See also:

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()

WCPtrOrderedVector<Type>::removeAll(), WCPtrSortedVector<Type>::removeAll()

remove all occurences of an element from the vector

Synopsis:

#include <wcvector.h>
public:
unsigned removeAll( const Type * );

Semantics:

The removeAll() public member function removes all elements in the vector that are equivalent to the passed value.


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

Results:

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.

See also:

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()

WCPtrOrderedVector<Type>::removeAt(), WCPtrSortedVector<Type>::removeAt()

remove the element at the given index into the vector

Synopsis:

#include <wcvector.h>
public:
Type * removeAt( int );

Semantics:

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.

Results:

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.

See also:

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()

WCPtrOrderedVector<Type>::removeFirst(), WCPtrSortedVector<Type>::removeFirst()

remove the first element in the vector

Synopsis:

#include <wcvector.h>
public:
Type * removeFirst();

Semantics:

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.

Results:

The removeFirst() public member function removes the first element from the vector. If the vector was empty, NULL(0) is returned.

See also:

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()

WCPtrOrderedVector<Type>::removeLast(), WCPtrSortedVector<Type>::removeLast()

remove the last element from the vector

Synopsis:

#include <wcvector.h>
public:
Type * removeLast();

Semantics:

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.

Results:

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.

See also:

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()

WCPtrOrderedVector<Type>::resize(), WCPtrSortedVector<Type>::resize()

change the size of the vector

Synopsis:

#include <wcvector.h>
public:
int resize( size_t new_size );

Semantics:

The resize() public member function is used to change the vector size to be able to store new_size elements:

  • If new_size is larger than the previous vector size, all elements are copied into the newly sized vector, and new elements can be added using the append(), insert(), insertAt(), and prepend() member functions.
  • If the vector is resized to a smaller size, the first new_size elements are copied (all vector elements if the vector contained new_size or fewer elements). The objects pointed to by the remaining elements aren't deleted.

If the resize can't be performed and the out_of_memory exception is enabled, it's thrown.

Results:

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.

See also:

WCExcept::out_of_memory

WCPtrVector<Type> Class

Declared:

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.

Requirements of Type

The WCPtrVector<Type> class requires nothing from Type.

Public Member Functions

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

Public Member Operators

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;

WCPtrVector<Type>::WCPtrVector()

create a WCPtrVector<Type> object

Synopsis:

#include <wcvector.h>
public:
WCPtrVector( size_t = 0 );
WCPtrVector( size_t, const Type * );
WCPtrVector( const WCPtrVector & );

Semantics:

There are three forms of the public WCPtrVector<Type> constructor:

  • The first creates a WCPtrVector<Type> object that's able to store the number of elements specified in the optional parameter, which defaults to zero. All vector elements are initialized to NULL(0).

    If the vector object can't be fully initialized, the vector is created with length zero.

  • The second form creates a WCPtrVector<Type> object able to store the number of elements specified by the first parameter. All vector elements are initialized to the pointer value given by the second parameter.

    If the vector object can't be fully initialized, the vector is created with length zero.

  • The third form is the copy constructor for the WCPtrVector<Type> class. The new vector is created with the same length as the given vector. All of the vector elements and exception trap states are copied.

    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.

Results:

The public WCPtrVector<Type> constructor creates an initialized WCPtrVector<Type> object.

See also:

WCExcept::out_of_memory, WCPtrVector<Type>::~WCPtrVector(), WCPtrVector<Type>::operator =()

WCPtrVector<Type>::~WCPtrVector()

destroy a WCPtrVector<Type> object

Synopsis:

#include <wcvector.h>
public:
virtual ~WCPtrVector();

Semantics:

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.

Results:

The WCPtrVector<Type> object is destroyed.

See also:

WCExcept::not_empty, WCPtrVector<Type>::clear(), WCPtrVector<Type>::clearAndDestroy()

WCPtrVector<Type>::clear()

clear the vector

Synopsis:

#include <wcvector.h>
public:
void clear();

Semantics:

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.

Results:

The clear() public member function clears the vector to have length zero and no vector elements.

See also:

WCPtrVector<Type>::~WCPtrVector(), WCPtrVector<Type>::clearAndDestroy(), WCPtrVector<Type>::operator =()

WCPtrVector<Type>::clearAndDestroy()

clear the vector, destroying the elements

Synopsis:

#include <wcvector.h>
public:
void clearAndDestroy();

Semantics:

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.

Results:

The clearAndDestroy() public member function clears the vector by deleting the objects pointed to by the vector elements, and makes the vector length zero.

See also:

WCPtrVector<Type>::clear()

WCPtrVector<Type>::length()

calculate the length of the vector

Synopsis:

#include <wcvector.h>
public:
size_t length() const;

Semantics:

The length() public member function is used to find the number of elements that can be stored in the WCPtrVector<Type> object.

Results:

The length() public member function returns the length of the vector.

See also:

WCPtrVector<Type>::resize()

WCPtrVector<Type>::operator [ ]()

index into the vector

Synopsis:

#include <wcvector.h>
public:
Type * & operator [ ]( int );
Type * const & operator [ ]( int ) const;

Semantics:

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 the empty_container exception is enabled, it's thrown.
  • Otherwise, the index_range exception is thrown, if enabled.

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.

Results:

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.

See also:

WCExcept::empty_container, WCExcept::index_range, WCExcept::out_of_memory, WCExcept::resize_required, WCPtrVector<Type>::resize()

WCPtrVector<Type>::operator =()

assign one vector to another

Synopsis:

#include <wcvector.h>
public:
WCPtrVector & operator =( const WCPtrVector & );

Semantics:

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.

Results:

The operator =() public member function assigns the left hand side vector to be a copy of the right hand side.

See also:

WCExcept::out_of_memory, WCPtrVector<Type>::clear(), WCPtrVector<Type>::clearAndDestroy()

WCPtrVector<Type>::operator ==()

determine whether or not two vectors are equivalent

Synopsis:

#include <wcvector.h>
public:
int operator ==( const WCPtrVector & ) const;

Semantics:

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.

Results:

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.

WCPtrVector<Type>::resize()

change the size of the vector

Synopsis:

#include <wcvector.h>
public:
int resize( size_t new_size );

Semantics:

The resize() public member function is used to change the vector size to be able to store new_size elements:

  • If new_size is larger than the previous vector size, all elements are copied into the newly sized vector, and new elements are initialized to NULL(0).
  • If the vector is resized to a smaller size, the first new_size elements are copied. The objects pointed to by the remaining elements aren't deleted.

If the resize can't be performed and the out_of_memory exception is enabled, it's thrown.

Results:

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.

See also:

WCExcept::out_of_memory

WCValOrderedVector<Type> and WCValSortedVector<Type> Classes

Declared:

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:

  • An ordered vector maintains the order in which elements are added, and allows more than one occurrence of an equivalent element.
  • A sorted vector allows only one occurrence of an equivalent element, and inserts elements in a sorted order. A sorted vector is less efficient when inserting elements, but can provide a faster retrieval time.

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.


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

Requirements of Type

Both the WCValOrderedVector<Type> and WCValSortedVector<Type> classes require Type to have:

  • a default constructor
        Type::Type()
        
    
  • a well defined copy constructor
        Type::Type( const Type & )
        
    
  • a well defined assignment operator
        Type & operator =( const Type & )
        
    
  • the following override of operator new(), if Type overrides the global operator new()
        void * operator new( size_t, void *ptr ) 
        { return( ptr ); }
        
    
  • a well defined equivalence operator with constant parameters
        int operator ==( const Type & ) const
        
    

Additionally the WCValSortedVector class requires Type to have:

  • a well defined less-than operator with constant parameters
        int operator <( const Type & ) const
        
    

Public Member Functions

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 & );

Public Member Operators

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;

WCValOrderedVector<Type>::WCValOrderedVector()

create a WCValOrderedVector<Type> object

Synopsis:

#include <wcvector.h>
public:
WCValOrderedVector( 
    size_t = WCDEFAULT_VECTOR_LENGTH,
	unsigned = WCDEFAULT_VECTOR_RESIZE_GROW );
WCValOrderedVector( const WCValOrderedVector & );

Semantics:

There are two forms of the WCValOrderedVector<Type> constructor:

  • The first creates an empty WCValOrderedVector object that's able to store the number of elements specified in the first optional parameter, which defaults to the constant WCDEFAULT_VECTOR_LENGTH (currently defined as 10).

    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.

  • The second form is the copy constructor for the WCValOrderedVector class. The new vector is created with the same length and resize value as the passed vector. All of the vector elements and exception trap states are copied.

    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.

Results:

The WCValOrderedVector<Type> constructor creates an initialized WCValOrderedVector object.

See also:

WCExcept::out_of_memory, WCExcept::resize_required, WCValOrderedVector<Type>::operator =()

WCValOrderedVector<Type>::~WCValOrderedVector()

destroy a WCValOrderedVector<Type> object

Synopsis:

#include <wcvector.h>
public:
virtual ~WCValOrderedVector();

Semantics:

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.

Results:

The WCValOrderedVector object is destroyed.

See also:

WCExcept::not_empty, WCValOrderedVector<Type>::clear()

WCValSortedVector<Type>::WCValSortedVector()

create a WCValSortedVector<Type> object

Synopsis:

#include <wcvector.h>
public:
WCValSortedVector( 
    size_t = WCDEFAULT_VECTOR_LENGTH,
	unsigned = WCDEFAULT_VECTOR_RESIZE_GROW );
WCValSortedVector( const WCValSortedVector & );

Semantics:

There are two forms of the WCValSortedVector<Type> constructor:

  • The first creates an empty WCValSortedVector object that's able to store the number of elements specified in the first optional parameter, which defaults to the constant WCDEFAULT_VECTOR_LENGTH (currently defined as 10).

    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.

  • The second form is the copy constructor for the WCValSortedVector class. The new vector is created with the same length and resize value as the passed vector. All of the vector elements and exception trap states are copied.

    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.

Results:

The WCValSortedVector<Type> constructor creates an empty initialized WCValSortedVector object.

See also:

WCExcept::out_of_memory, WCExcept::resize_required WCValOrderedVector<Type>::operator =()

WCValSortedVector<Type>::~WCValSortedVector()

destroy a WCValSortedVector<Type> object

Synopsis:

#include <wcvector.h>
public:
virtual ~WCValSortedVector();

Semantics:

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.

Results:

The WCValSortedVector object is destroyed.

See also:

WCExcept::not_empty, WCValOrderedVector<Type>::clear()

WCValOrderedVector<Type>::append()

append an element to the vector

Synopsis:

#include <wcvector.h>
public:
int append( const Type & );

Semantics:

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.


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

  • If the resize_required exception is enabled, it's thrown.
  • If the exception isn't enabled, the append fails if the amount the vector is to be grown (the second parameter to the constructor) is zero(0).
  • Otherwise, the vector is automatically grown by the number of elements specified to the constructor, using the resize() member function. If resize() fails, the element isn't appended to the vector and the out_of_memory exception is thrown, if it's enabled.

Results:

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.

See also:

WCExcept::out_of_memory, WCExcept::resize_required, WCValOrderedVector<Type>::insert(), WCValOrderedVector<Type>::insertAt(), WCValOrderedVector<Type>::prepend()

WCValOrderedVector<Type>::clear(), WCValSortedVector<Type>::clear()

clear the vector

Synopsis:

#include <wcvector.h>
public:
void clear();

Semantics:

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.

Results:

The clear() public member function clears the vector to have length zero and no entries.

See also:

WCValOrderedVector<Type>::~WCValOrderedVector(), WCValSortedVector<Type>::~WCValSortedVector(), WCValOrderedVector<Type>::operator =(), WCValSortedVector<Type>::operator =()

WCValOrderedVector<Type>::contains(), WCValSortedVector<Type>::contains()

determine whether or not a given value is in the vector

Synopsis:

#include <wcvector.h>
public:
int contains( const Type & ) const;

Semantics:

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.

Results:

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.

See also:

WCValOrderedVector<Type>::index(), WCValSortedVector<Type>::index(), WCValOrderedVector<Type>::find(), WCValSortedVector<Type>::find()

WCValOrderedVector<Type>::entries(), WCValSortedVector<Type>::entries()

count the elements in the vector

Synopsis:

#include <wcvector.h>
public:
unsigned entries() const;

Semantics:

The entries() public member function is used to find the number of elements that are stored in the vector.

Results:

The entries() public member function returns the number of elements in the vector.

See also:

WCValOrderedVector<Type>::isEmpty(), WCValSortedVector<Type>::isEmpty()

WCValOrderedVector<Type>::find(), WCValSortedVector<Type>::find()

find an element in the vector

Synopsis:

#include <wcvector.h>
public:
int find( const Type &, Type & ) const;

Semantics:

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.

Results:

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.

See also:

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()

WCValOrderedVector<Type>::first(), WCValSortedVector<Type>::first()

return the first element in the vector

Synopsis:

#include <wcvector.h>
public:
Type first() const;

Semantics:

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:

  • The empty_container exception is thrown if it's enabled.
  • Otherwise, if the index_range exception is enabled, it's thrown.

If neither exception is enabled, a first element of the vector is added with a default value.

Results:

The first() public member function returns the value of the first element in the vector.

See also:

WCExcept::index_range, WCExcept::empty_container, WCValOrderedVector<Type>::last(), WCValSortedVector<Type>::last(), WCValOrderedVector<Type>::removeFirst(), WCValSortedVector<Type>::removeFirst()

WCValOrderedVector<Type>::index(), WCValSortedVector<Type>::index()

find the index of an element

Synopsis:

#include <wcvector.h>
public:
int index( const Type & ) const;

Semantics:

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.

Results:

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.

See also:

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()

WCValOrderedVector<Type>::insert(), WCValSortedVector<Type>::insert()

insert an element into the vector

Synopsis:

#include <wcvector.h>
public:
int insert( const Type & );

Semantics:

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:

  • If the resize_required exception is enabled, it's thrown.
  • If the exception isn't enabled, the insertion fails if the amount the vector is to be grown (the second parameter to the constructor) is zero(0).
  • Otherwise, the vector is automatically grown by the number of elements specified to the constructor, using the resize() member function. If resize() fails, the element isn't added to the vector, and the out_of_memory exception is thrown, if enabled.

Results:

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.

See also:

WCExcept::out_of_memory, WCExcept::resize_required, WCValOrderedVector<Type>::append(), WCValOrderedVector<Type>::insertAt(), WCValOrderedVector<Type>::prepend()

WCValOrderedVector<Type>::insertAt()

insert an element in a specific location in the vector

Synopsis:

#include <wcvector.h>
public:
int insertAt( int, const Type & );

Semantics:

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.


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

  • If the resize_required exception is enabled, it's thrown.
  • If the exception isn't enabled, the insertion fails if the amount the vector is to be grown (the second parameter to the constructor) is zero(0).
  • Otherwise, the vector is automatically grown by the number of elements specified to the constructor, using the resize() member function. If resize() fails, the element isn't inserted into the vector and the out_of_memory exception is thrown, if enabled.

Results:

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.

See also:

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()

WCValOrderedVector<Type>::isEmpty(), WCValSortedVector<Type>::isEmpty()

determine whether or not the vector is empty

Synopsis:

#include <wcvector.h>
public:
int isEmpty() const;

Semantics:

The isEmpty() public member function is used to determine if a vector object has any entries in it.

Results:

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.

See also:

WCValOrderedVector<Type>::entries(), WCValSortedVector<Type>::entries()

WCValOrderedVector<Type>::last(), WCValSortedVector<Type>::last()

return the last element of the vector

Synopsis:

#include <wcvector.h>
public:
Type last() const;

Semantics:

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:

  • The empty_container exception is thrown if it's enabled.
  • Otherwise, if the index_range exception is enabled, it's thrown.

If neither exception is enabled, a first element of the vector is added with a default value.

Results:

The last() public member function returns the value of the last element in the vector.

See also:

WCExcept::index_range, WCExcept::empty_container, WCValOrderedVector<Type>::first(), WCValSortedVector<Type>::first(), WCValOrderedVector<Type>::removeLast(), WCValSortedVector<Type>::removeLast()

WCValOrderedVector<Type>::occurrencesOf(), WCValSortedVector<Type>::occurrencesOf()

count the occurrences of an element

Synopsis:

#include <wcvector.h>
public:
int occurrencesOf( const Type & ) const;

Semantics:

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.

Results:

The occurrencesOf() public member function returns the number of elements equivalent to the passed value.

See also:

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()

WCValOrderedVector<Type>::operator [ ](), WCValSortedVector<Type>::operator [ ]()

index into the vector

Synopsis:

#include <wcvector.h>
public:
Type & operator [ ]( int );
const Type & operator [ ]( int ) const;

Semantics:

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:

  • The empty_container exception is thrown if it's enabled.
  • Otherwise, if the index_range exception is enabled, it's 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.


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

Results:

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.

See also:

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()

WCValOrderedVector<Type>::operator =(), WCValSortedVector<Type>::operator =()

assign one vector to another

Synopsis:

#include <wcvector.h>
public:
WCValOrderedVector & 
    WCValOrderedVector::operator =(
        const WCValOrderedVector & );
WCValSortedVector & 
    WCValSortedVector::operator =(
        const WCValSortedVector & );

Semantics:

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.

Results:

The operator =() public member function sets the left hand side vector to be a copy of the right hand side.

See also:

WCExcept::out_of_memory, WCValOrderedVector<Type>::clear(), WCValSortedVector<Type>::clear()

WCValOrderedVector<Type>::operator ==(), WCValSortedVector<Type>::operator ==()

determine whether or not two vectors are equivalent

Synopsis:

#include <wcvector.h>
public:
int WCValOrderedVector::operator ==( 
        const WCValOrderedVector & ) const;
int WCValSortedVector::operator ==( 
        const WCValSortedVector & ) const;

Semantics:

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.

Results:

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.

WCValOrderedVector<Type>::prepend()

insert an element at the beginning of the vector

Synopsis:

#include <wcvector.h>
public:
int prepend( const Type & );

Semantics:

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.


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

  • If the resize_required exception is enabled, it's thrown.
  • If the exception isn't enabled, the prepend fails if the amount the vector is to be grown (the second parameter to the constructor) is zero(0).
  • Otherwise, the vector is automatically grown by the number of elements specified to the constructor, using the resize() member function. If resize() fails, the element isn't inserted to the vector and the out_of_memory exception is thrown, if enabled.

Results:

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.

See also:

WCExcept::out_of_memory, WCExcept::resize_required, WCValOrderedVector<Type>::append(), WCValOrderedVector<Type>::insert(), WCValOrderedVector<Type>::insertAt()

WCValOrderedVector<Type>::remove(), WCValSortedVector<Type>::remove()

remove an element from the vector

Synopsis:

#include <wcvector.h>
public:
int remove( const Type & );

Semantics:

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.

Results:

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.

See also:

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()

WCValOrderedVector<Type>::removeAll(), WCValSortedVector<Type>::removeAll()

remove all occurrences of an element from the vector

Synopsis:

#include <wcvector.h>
public:
unsigned removeAll( const Type & );

Semantics:

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.

Results:

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.

See also:

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()

WCValOrderedVector<Type>::removeAt(), WCValSortedVector<Type>::removeAt()

remove the element with the given index from the vector

Synopsis:

#include <wcvector.h>
public:
int removeAt( int );

Semantics:

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.

Results:

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.

See also:

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()

WCValOrderedVector<Type>::removeFirst(), WCValSortedVector<Type>::removeFirst()

remove the first element from the vector

Synopsis:

#include <wcvector.h>
public:
int removeFirst();

Semantics:

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.

Results:

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.

See also:

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()

WCValOrderedVector<Type>::removeLast(), WCValSortedVector<Type>::removeLast()

remove the last element from the vector

Synopsis:

#include <wcvector.h>
public:
int removeLast();

Semantics:

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.

Results:

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.

See also:

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()

WCValOrderedVector<Type>::resize(), WCValSortedVector<Type>::resize()

change the size of the vector

Synopsis:

#include <wcvector.h>
public:
int resize( size_t new_size );

Semantics:

The resize() public member function is used to change the vector size to be able to store new_size elements:

  • If new_size is larger than the previous vector size, all elements are copied (using Type's copy constructor) into the newly sized vector, and new elements can be added using the append(), insert(), insertAt(), and prepend() member functions.
  • If the vector is resized to a smaller size, the first new_size elements are copied (all vector elements if the vector contained new_size or fewer elements). The remaining elements are destroyed using Type's destructor.

If the resize can't be performed and the out_of_memory exception is enabled, it's thrown.

Results:

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.

See also:

WCExcept::out_of_memory

WCValVector<Type> Class

Declared:

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.

Requirements of Type

The WCValVector<Type> class requires Type to have:

  • a default constructor
        Type::Type()
        
    
  • a well defined copy constructor
        Type::Type( const Type & )
        
    
  • the following override of operator new(), only if Type overrides the global operator new()
        void * operator new( size_t, void *ptr ) 
        { return( ptr ); }
        
    

Public Member Functions

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

Public Member Operators

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;

WCValVector<Type>::WCValVector()

create a WCValVector<Type> object

Synopsis:

#include <wcvector.h>
public:
WCValVector( size_t = 0 );
WCValVector( size_t, const Type & );

Semantics:

There are three forms of the public WCValVector<Type> constructor:

  • The first creates a WCValVector<Type> object that's able to store the number of elements specified in the optional parameter, which defaults to zero. All vector elements are initialized with Type's default constructor.

    If the vector object can't be fully initialized, the vector is created with length zero.

  • The second form also creates a WCValVector<Type> object that's able to store the number of elements specified by the first parameter. All vector elements are initialized to the value of the second parameter using Type's copy constructor.

    If the vector object can't be fully initialized, the vector is created with length zero.

  • The third form is the copy constructor for the WCValVector<Type> class. The new vector is created with the same length as the given vector. All of the vector elements and exception trap states are copied.

    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.

Results:

The public WCValVector<Type> constructor creates an initialized WCValVector<Type> object of the specified length.

See also:

WCExcept::out_of_memory, WCValVector<Type>::~WCValVector(), WCValVector<Type>::operator =()

WCValVector<Type>::~WCValVector()

destroy a WCValVector<Type> object

Synopsis:

#include <wcvector.h>
public:
virtual ~WCValVector();

Semantics:

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.

Results:

The WCValVector<Type> object is destroyed.

See also:

WCExcept::not_empty, WCValVector<Type>::clear()

WCValVector<Type>::clear()

clear the vector

Synopsis:

#include <wcvector.h>
public:
void clear();

Semantics:

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.

Results:

The clear() public member function clears the vector to have length zero and no vector elements.

See also:

WCValVector<Type>::~WCValVector(), WCValVector<Type>::operator =()

WCValVector<Type>::length()

calculate the length of the vector

Synopsis:

#include <wcvector.h>
public:
size_t length() const;

Semantics:

The length() public member function is used to find the number of elements that can be stored in the WCValVector<Type> object.

Results:

The length() public member function returns the length of the vector.

See also:

WCValVector<Type>::resize()

WCValVector<Type>::operator [ ]()

index into the vector

Synopsis:

#include <wcvector.h>
public:
Type & operator [ ]( int );
const Type & operator [ ]( int ) const;

Semantics:

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 the empty_container exception is enabled, it's thrown.
  • Otherwise, the index_range exception is thrown, if enabled.

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.

Results:

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.

See also:

WCExcept::empty_container, WCExcept::index_range, WCExcept::out_of_memory, WCExcept::resize_required, WCValVector<Type>::resize()

WCValVector<Type>::operator =()

assign one vector to another

Synopsis:

#include <wcvector.h>
public:
WCValVector & operator =( const WCValVector & );

Semantics:

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.

Results:

The operator =() public member function assigns the left hand side vector to be a copy of the right hand side.

See also:

WCExcept::out_of_memory, WCValVector<Type>::clear()

WCValVector<Type>::operator ==()

determine whether or not two vectors are equivalent

Synopsis:

#include <wcvector.h>
public:
int operator ==( const WCValVector & ) const;

Semantics:

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.

Results:

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.

WCValVector<Type>::resize()

resize the vector

Synopsis:

#include <wcvector.h>
public:
int resize( size_t new_size );

Semantics:

The resize() public member function is used to change the vector size to be able to store new_size elements:

  • If new_size is larger than the previous vector size, all elements are copied (using Type's copy constructor) into the newly sized vector, and new elements are initialized with Type's default constructor.
  • If the vector is resized to a smaller size, the first new_size elements are copied. The remaining elements are destroyed using Type's destructor.

If the resize can't be performed and the out_of_memory exception is enabled, it's thrown.

Results:

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.

See also:

WCExcept::out_of_memory