VarVec.h



Classes

ValVec -- dynamic array of arbitrary values (full description)
PtrVec -- dynamic array of pointers (full description)
LinPool -- dynamic linear pool of objects. (full description)

template<class T> class ValVec

Interface

Public Members
~ValVec( void )
ValVec( size_t capacity = 0, size_t increment = 0 )
ValVec( const T &fill, size_t capacity, size_t increment )
ValVec( const ValVec& )
ValVec& operator =( const ValVec& )
const T& operator ()( size_t index ) const
T& operator ()( size_t index )
const T& operator []( size_t index ) const
T& operator []( size_t index )
T& at( size_t index )
size_t length( void ) const
size_t append( const T& )
size_t insert( size_t count, size_t offset = 0 )
size_t cut( size_t count, size_t offset = 0 )
void remove( size_t offset )
size_t keep( size_t count )
T& fill( void )
bool fillExists( void ) const
void unsetFill( void )
void clear( void )

Description

This is a template for a general-purpose dynamic array. The array grows automatically as needed, but reallocation occurs only when the length exceeds the capacity. The capacity is increased in large blocks, the size of which may be optimized. A fill value may be defined, in which case it is used to initialize new elements of the array, but not new capacity. Which is to say that initialization is deferred until the array grows into its capacity. The public data member, increment_, specifies the amount by which the capacity is increased during reallocation. By default, increment_ is zero, which causes the capacity to double upon each reallocation. A non-zero increment_ is simply added to the capacity upon each reallocation. The capacity is extended by this amount or by whatever greater amount is necessary to accommodate the new length of the array.

Member Description

~ValVec( void )

Destructor.

ValVec( size_t capacity = 0, size_t increment = 0 )

Default constructor: optionally specify initial capacity and reallocation increment.

ValVec( const T &fill, size_t capacity, size_t increment )

Alternate constructor: define a fill value in addition to the parameters of the default constructor. class T must have well-defined copy semantics. The fill value does not exist unless it is defined.

ValVec( const ValVec& )

Copy constructor. The initial capacity is the current capacity of the duplicated array.

ValVec& operator =( const ValVec& )

Assignment/copy operator: does not decrease the capacity.

const T& operator ()( size_t index ) const

Efficient array operator (const version): no bounds checking.

T& operator ()( size_t index )

Efficient array operator (non-const version): no bounds checking.

const T& operator []( size_t index ) const

Bounds-checking array operator (const version): throws sxBoundsError.

T& operator []( size_t index )

Bounds-checking array operator (non-const version): throws sxBoundsError.

T& at( size_t index )

at method: bounds-adjusting array operator. Returns the array element at the specified index, extending the array as necessary to bring it within bounds. The fill value, if defined, is the initializer for any new elements.

size_t length( void ) const

length method: returns current occupied length of array.

size_t append( const T& )

append method: efficiently insert given element at end of array. Avoids redundant initialization of new array element, except for when a reallocation is required. Returns the new length.

size_t insert( size_t count, size_t offset = 0 )

insert method: insert new array elements. count specifies the number of new elements, and offset specifies where in the array to insert them. By default the new elements are appended. The fill value, if defined, is the initializer for the new elements. offset refers to the end of the array: the first new element is located at index (length - offset). Returns the new length. Throws sxBoundsError if offset is greater than length.

size_t cut( size_t count, size_t offset = 0 )

cut method: remove array elements. count specifies the number of elements to remove, and offset specifies which elements to remove. By default elements are removed from the end of the array. The unused capacity grows by this amount. offset refers to the end of the array: the first removed element is located at index (length - offset - count). Returns the new length. Throws sxBoundsError if (offset+count) is greater than length.

void remove( size_t offset )

remove method: removes the element specified by offset. This is basically a wrapper for the cut method cut(1, length-offset-1)

size_t keep( size_t count )

keep method: just like the cut method, it resets the length of the vector by count, but it always starts from the end. The elements, however are not deleted and rebuilt with the default, but rather left as they are for the user to reuse.

T& fill( void )

fill method: return the fill value, defining it if necessary. If the fill value is not defined, a default value is created using the default constructor for class T. The returned object is an lvalue, to which a new fill value may be assigned.

bool fillExists( void ) const

fillExists method: returns true if the fill value is defined.

void unsetFill( void )

unsetFill method: undefine and destroy the current fill value, if it is defined.

void clear( void )

clear method: reset every value to the fill value. If no fill is defined, nothing is done!


template<class T> class PtrVec

Interface

~PtrVec( void )
PtrVec( bool internal=true, size_t capacity=0, size_t increment=0 )
PtrVec( const PtrVec&, bool internalize = true )
PtrVec& copy( const PtrVec&, bool internalize )
PtrVec& operator =( const PtrVec &obj )
const T*& operator ()( size_t index ) const
T*& operator ()( size_t index )
const T*& operator []( size_t index ) const
T*& operator []( size_t index )
T*& at( size_t index )
size_t length( void ) const
size_t entries( void ) const
size_t append( T* )
size_t insert( size_t count, size_t offset = 0 )
size_t add( T* )
int index( const T* )
size_t cut( size_t count, size_t offset = 0 )
void internalize( void )
void externalize( void )
bool internal( void ) const

Description

This is a template for a dynamic array of pointers. The design is very similar to the general-purpose version, ValVec, but specialized according to the memory management issues peculiar to storing pointers. This version uses nil as the fill value, which cannot be customized or disabled. By default, the cut method uses the delete operator to free pointers as they are removed from the array. Before disposing the array, the destructor clears it out with cut. This behavior is avoided if the pointers are designated as external, or shared. In that case it is entirely up to the user to free pointers left dangling by cut. When copying the array, one must choose whether the copy will share with the original the objects referenced by the pointers (shallow copy), or whether the copy will have its own internal duplicates.

--------------- WARNING ---------------

The user must provide a specialization of PtrDup() for every polymorphic class that will instantiate this template. Failure to do this may result in unexpected truncation of derived objects. The template methods use PtrDup() when duplication of objects is required, but duplicating a polymorphic object requires assistance from the object itself. For example, consider class B:

class B { public: virtual B* duplicate(void) const = 0; }; inline B* PtrDup(const B *b) { return b ? b->duplicate() : 0; }

To avoid confusion and mistakes, place the specialization immediately after the declaration of class B. If class B does not have a duplicator use the following specialization instead:

inline B* PtrDup(const B *b) { if (b) throw sxUnimplemented("PtrDup","class B has no duplicator"); else return 0; }

Member Description

~PtrVec( void )

Destructor.

PtrVec( bool internal=true, size_t capacity=0, size_t increment=0 )

Default constructor: optionally specify initial capacity, reallocation increment, and whether the pointers are internal.

PtrVec( const PtrVec&, bool internalize = true )

Copy constructor: optionally specify either a shallow copy or an internalized copy (the default). The initial capacity is the current capacity of the duplicated array.

PtrVec& copy( const PtrVec&, bool internalize )

copy method: does not decrease the capacity. The copy is shallow if internalize is false; otherwise the referenced objects are duplicated and the copy is internal, in which case class T must have well-defined copy semantics.

PtrVec& operator =( const PtrVec &obj )

Assignment/copy operator: does not decrease the capacity. This is not a shallow copy, so class T must have well-defined copy semantics.

const T*& operator ()( size_t index ) const

Efficient array operator (const version): no bounds checking.

T*& operator ()( size_t index )

Efficient array operator (non-const version): no bounds checking.

const T*& operator []( size_t index ) const

Bounds-checking array operator (const version): throws sxBoundsError.

T*& operator []( size_t index )

Bounds-checking array operator (non-const version): throws sxBoundsError.

T*& at( size_t index )

at method: bounds-adjusting array operator. Returns the array element at the specified index, extending the array as necessary to bring it within bounds. Any new elements are set to nil.

size_t length( void ) const

length method: returns current occupied length of array.

size_t entries( void ) const

entries method: return the number of non-NULL entries in vector NOTE: this is different from length(), which also counts NULL pointers.

size_t append( T* )

append method: efficiently insert given element at end of array. Avoids redundant initialization of new array element, except for when a reallocation is required. Returns the new length.

size_t insert( size_t count, size_t offset = 0 )

insert method: insert new array elements. count specifies the number of new elements, and offset specifies where in the array to insert them. By default the new elements are appended. The new elements are initialized with the nil pointer value. offset refers to the end of the array: the first new element is located at index (length - offset). Returns the new length. Throws sxBoundsError if offset is greater than length.

size_t add( T* )

add method: add a new array element in the first available empty slot. This should be used when the ordering of elements is not important and empty slots are to be minimized. Returns the index of the newly inserted element.

int index( const T* )

index method: return the index of the given element if it exists in the array, else return -1.

size_t cut( size_t count, size_t offset = 0 )

cut method: remove array elements. count specifies the number of elements to remove, and offset specifies which elements to remove. By default elements are removed from the end of the array. The unused capacity grows by this amount. offset refers to the end of the array: the first removed element is located at index (length - offset - count). Returns the new length. Throws sxBoundsError if (offset+count) is greater than length.

void internalize( void )

internalize method: replace external pointers with internal copies of the referenced objects. class T must have well-defined copy semantics. Does nothing if already internal.

void externalize( void )

externalize method: change status of pointers to external. Beware that internalize does not undo this; once the pointers are designated external they cannot just be re-designated as internal. Confusion on this point will yield dangling pointers.

bool internal( void ) const

internal method: returns true if the pointers are internal.


template<class T> class LinPool

Interface

~LinPool( void )
LinPool( size_t capacity=0, size_t increment=0 )
const T*& operator ()( size_t index ) const
T*& operator ()( size_t index )
const T*& operator []( size_t index ) const
T*& operator []( size_t index )
T*& use( void )
size_t length( void ) const
size_t free( size_t count = 0)

Description

This is a template for a dynamic pool of objects. The design is very similar to the dynamic array of pointers. A pool is defined to be an array of pointers to preallocated default objects. Whenever a new object is needed, it can be accessed from the pool with the use() member function. The size of the pool extends automatically if its initial limit is reached. This pool is a linear pool, i.e. we can rely upon their index to be sequential. So in order to return a specific object into the pool's disposal, all objects having larger indices have to be free (like a reverse LIFO - last out first back). The free member function returns objects to the pool's disposal. Upon destruction, all pool objects are destroyed using their destructor. It does not make sense to have a copy constructor or assignment op.

Member Description

~LinPool( void )

Destructor.

LinPool( size_t capacity=0, size_t increment=0 )

Default constructor: optionally specify initial capacity, reallocation increment, and whether the pointers are internal.

const T*& operator ()( size_t index ) const

Efficient array operator (const version): no bounds checking.

T*& operator ()( size_t index )

Efficient array operator (non-const version): no bounds checking.

const T*& operator []( size_t index ) const

Bounds-checking array operator (const version): throws sxBoundsError.

T*& operator []( size_t index )

Bounds-checking array operator (non-const version): throws sxBoundsError.

T*& use( void )

use method: bounds-adjusting operator that returns the pool element as an lvalue to be used by the user. It is a combination of append() and at() of VarVec and PtrVec. If the bounds of the pool array are reached, it is extended by increment_.

size_t length( void ) const

length method: returns current occupied length of the pool.

size_t free( size_t count = 0)

free method: declare pool objects as free for new use. If no argument is given, all elements are free to use again. Else, the number of elements specified is freed up from the end.


© Copyright The Johns Hopkins University 1999, All Rights Reserved.
Peter Z. Kunszt,