Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   Related Pages  

rdArray< T > Class Template Reference

A class for storing an array of values of type T. More...

#include <rdArray.h>

List of all members.

Public Member Functions

virtual ~rdArray ()
 Destructor.

 rdArray (const T &aDefaultValue, int aSize=0, int aCapacity=rdArray_CAPMIN)
 Default constructor.

 rdArray (const rdArray< T > &aArray)
 Coppy constructor.

T & operator[] (int aIndex) const
 Get the array element at a specified index.

rdArray< T > & operator= (const rdArray< T > &aArray)
 Assign this array to a specified array.

bool computeNewCapacity (int aMinCapacity, int &rNewCapacity)
 Compute a new capacity that is at least as large as a specified minimum capacity; this method does not change the capacity, it simply computes a new recommended capacity.

bool ensureCapacity (int aCapacity)
 Ensure that the capacity of this array is at least the specified amount.

void trim ()
 Trim the capacity of this array so that it is one larger than the size of this array.

int getCapacity () const
 Get the capacity of this storage instance.

void setCapacityIncrement (int aIncrement)
 Set the amount by which the capacity is increased when the capacity of of the array in exceeded.

int getCapacityIncrement () const
 Get the amount by which the capacity is increased.

bool setSize (int aSize)
 Set the size of the array.

int getSize () const
 Get the size of the array.

int append (const T &aValue)
 Append a value onto the array.

int append (const rdArray< T > &aArray)
 Append an array of values.

int insert (int aIndex, const T &aValue)
 Insert a value into the array at a specified index.

int remove (int aIndex)
 Remove a value from the array at a specified index.

void set (int aIndex, const T &aValue)
 Set the value at a specified index.

T & get (int aIndex) const
 Get the value at a specified array index.

T & getLast () const
 Get the last value in the array.

int searchBinary (const T &aValue, bool aFindFirst=false, int aLo=-1, int aHi=-1) const
 Search for the largest value in the array that is less than or equal to a specified value.


Protected Attributes

int _size
 Size of the array.

int _capacity
 Current capacity of the array.

int _capacityIncrement
 Increment by which the current capacity is increased when the capacity of this storage instance is reached.

_defaultValue
 Default value of elements.

T * _array
 Array of values.


Private Member Functions

void setNull ()
 Set all member variables to their null or default values.


Detailed Description

template<class T>
class rdArray< T >

A class for storing an array of values of type T.

The capacity of the class grows as needed. To use this template for a class of type T, class T should implement the following methods: default constructor, copy constructor, assignment operator (=), equality operator (==), and less than operator (<).

Version:
1.0
Author:
Frank C. Anderson


Constructor & Destructor Documentation

template<class T>
virtual rdArray< T >::~rdArray  )  [inline, virtual]
 

Destructor.

When the array is deleted, references to elements of this array become invalid.

template<class T>
rdArray< T >::rdArray const T &  aDefaultValue,
int  aSize = 0,
int  aCapacity = rdArray_CAPMIN
[inline]
 

Default constructor.

Parameters:
aDefaultValue Default value of an array element. This value is used to initialize array elements as the size of the array is changed.
aSize Initial size of the array. The array elements are initialized to aDefaultValue.
aCapacity Initial capacity of the array. The initial capacity is guarranteed to be at least as large as aSize + 1.

template<class T>
rdArray< T >::rdArray const rdArray< T > &  aArray  )  [inline]
 

Coppy constructor.

Parameters:
aArray Array to be copied.


Member Function Documentation

template<class T>
int rdArray< T >::append const T &  aValue  )  [inline]
 

Append a value onto the array.

Parameters:
aValue Value to be appended.
Returns:
Index to the new first empty element (or end) of the array.

template<class T>
bool rdArray< T >::computeNewCapacity int  aMinCapacity,
int &  rNewCapacity
[inline]
 

Compute a new capacity that is at least as large as a specified minimum capacity; this method does not change the capacity, it simply computes a new recommended capacity.

If the capacity increment is negative, the current capacity is doubled until the computed capacity is greater than or equal to the specified minimum capacity. If the capacity increment is positive, the current capacity increment by this amount until the computed capacity is greater than or equal to the specified minimum capacity. If the capacity increment is zero, the computed capacity is set to the current capacity and false is returned.

Parameters:
rNewCapacity New computed capacity.
aMinCapacity Minimum new computed capacity. The computed capacity is incremented until it is at least as large as aMinCapacity, assuming the capacity increment is not zero.
Returns:
True if the new capacity was increased, false otherwise (i.e., if the capacity increment is set to 0).
See also:
setCapacityIncrement()

template<class T>
bool rdArray< T >::ensureCapacity int  aCapacity  )  [inline]
 

Ensure that the capacity of this array is at least the specified amount.

Note that the newly allocated array elements are not initialized.

Parameters:
aCapacity Desired capacity.
Returns:
true if the capacity was successfully obtained, false otherwise.

template<class T>
T& rdArray< T >::get int  aIndex  )  const [inline]
 

Get the value at a specified array index.

If the index is negative or passed the end of the array, an exception is thrown.

For faster execution, the array elements can be accessed through the overloaded operator[], which does no bounds checking.

Parameters:
aIndex Index of the desired array element.
Returns:
Reference to the array element.
Exceptions:
rdException if (aIndex<0)||(aIndex>=_size).
See also:
operator[].

template<class T>
T& rdArray< T >::getLast  )  const [inline]
 

Get the last value in the array.

Returns:
Last value in the array.
Exceptions:
rdException if the array is empty.

template<class T>
int rdArray< T >::getSize  )  const [inline]
 

Get the size of the array.

Returns:
Size of the array.

template<class T>
int rdArray< T >::insert int  aIndex,
const T &  aValue
[inline]
 

Insert a value into the array at a specified index.

This method is relatively computationally costly since many of the array elements may need to be shifted.

Parameters:
aValue Value to be inserted.
aIndex Index at which to insert the new value. All current elements from aIndex to the end of the array are shifted one place in the direction of the end of the array. If the specified index is greater than the current size of the array, the size of the array is increased to aIndex+1 and the itervening new elements are initialized to the default value that was specified at the time of construction.
Returns:
Size of the array after the insertion.

template<class T>
rdArray<T>& rdArray< T >::operator= const rdArray< T > &  aArray  )  [inline]
 

Assign this array to a specified array.

This operator makes a complete copy of the specified array; all member variables are copied. So, the result is two identical, independent arrays.

Parameters:
aArray Array to be copied.
Returns:
Reference to this array.

template<class T>
T& rdArray< T >::operator[] int  aIndex  )  const [inline]
 

Get the array element at a specified index.

This overloaded operator can be used both to set and get element values:
array(2);
value = array[i];
[i] = value;

This operator is intended for accessing array elements with as little overhead as possible, so no error checking is performed. The caller must make sure the specified index is within the bounds of the array. If error checking is desired, use rdArray::get().

Parameters:
aIndex Index of the desired element (0 <= aIndex < _size).
Returns:
Reference to the array element.
See also:
get().

template<class T>
int rdArray< T >::remove int  aIndex  )  [inline]
 

Remove a value from the array at a specified index.

This method is relatively computationally costly since many of the array elements may need to be shifted.

Parameters:
aIndex Index of the value to remove. All elements from aIndex to the end of the array are shifted one place toward the beginning of the array. If aIndex is less than 0 or greater than or equal to the current size of the array, no element is removed.
Returns:
Size of the array after the removal.

template<class T>
int rdArray< T >::searchBinary const T &  aValue,
bool  aFindFirst = false,
int  aLo = -1,
int  aHi = -1
const [inline]
 

Search for the largest value in the array that is less than or equal to a specified value.

If there is more than one element with this largest value, the index of the first of these elements can optionally be found, but this can be up to twice as costly.

This method assumes that the array element values monotonically increase as the array index increases. Note that monotonically increase means never decrease, so it is permissible for elements to

A binary search is performed (i.e., the array is repeatedly subdivided into two bins one of which must contain the specified until the approprite element is identified), so the performance of this method is approximately ln(n), where n is the size of the array.

Parameters:
aValue Value to which the array elements are compared.
aFindFirst If true, find the first element that satisfies the search. If false, the index of any element that satisfies the search can be returned- which index will be returned depends on the length of the array and is therefore somewhat arbitrary. By default, this flag is false.
aLo Lowest array index to consider in the search.
aHi Highest array index to consider in the search.
Returns:
Index of the array element that has the largest value that is less than or equal to aValue. If there is more than one such elements with the same value and aFindFirst is set to true, the index of the first of these elements is returned. If an error is encountered (e.g., the array is empty), or the array contains no element that is less than or equal to aValue, -1 is returned.

template<class T>
void rdArray< T >::set int  aIndex,
const T &  aValue
[inline]
 

Set the value at a specified index.

Parameters:
aIndex Index of the array element to be set. It is permissible for aIndex to be past the current end of the array- the capacity will be increased if necessary. Values between the current end of the array and aIndex are not initialized.
aValue Value.

template<class T>
void rdArray< T >::setCapacityIncrement int  aIncrement  )  [inline]
 

Set the amount by which the capacity is increased when the capacity of of the array in exceeded.

If the specified increment is negative or this method is called with no argument, the capacity is set to double whenever the capacity is exceeded.

Parameters:
aIncrement Desired capacity increment.

template<class T>
bool rdArray< T >::setSize int  aSize  )  [inline]
 

Set the size of the array.

This method can be used to either increase or decrease the size of the array. If this size of the array is increased, the new elements are initialized to the default value that was specified at the time of construction.

Note that the size of an array is different than its capacity. The size indicates how many valid elements are stored in an array. The capacity indicates how much the size of the array can be increased without allocated more memory. At all times size <= capacity.

Parameters:
aSize Desired size of the array. The size must be greater than or equal to zero.

template<class T>
void rdArray< T >::trim  )  [inline]
 

Trim the capacity of this array so that it is one larger than the size of this array.

This is useful for reducing the amount of memory used by this array. This capacity is kept at one larger than the size so that, for example, an array of characters can be treated as a NULL terminated string.


Member Data Documentation

template<class T>
int rdArray< T >::_capacityIncrement [protected]
 

Increment by which the current capacity is increased when the capacity of this storage instance is reached.

If negative, capacity doubles.

template<class T>
int rdArray< T >::_size [protected]
 

Size of the array.

Also the index of the first empty array element.


The documentation for this class was generated from the following file:
Generated on Wed Aug 20 02:17:09 2003 for Simulation Software by doxygen1.3