DArray< T > Class Template Reference

A dynamic array implementation. More...

#include <darray.h>

List of all members.

Public Member Functions

 DArray ()
 The default constructor.
 DArray (T *_array, size_t _indices)
 Initialize the DArray with an existing array.
 DArray (DArray const &_array)
 Initialize the DArray with an existing array.
 DArray (int _newStepSize)
 The secondary constructor.
 ~DArray ()
 The destructor.
void setSize (size_t _newsize)
 Sets the size of the array.
void setStepSize (int _newstepsize)
 Sets the step size used in Grow().
void setStepDouble ()
 Sets the step size to double the array size when a Grow() is necessitated.
get (size_t _index) const
 Gets the data at the given index.
void remove (size_t _index)
 Removes the data at the given index.
size_t find (T const &_data)
 Finds the data in the array.
size_t insert (T const &_newdata)
 Inserts data into the array at the first available index.
void insert (T const &_newdata, size_t _index)
 Inserts data into the array at the given index.
size_t used () const
 Indicates the number of used nodes.
size_t size () const
 Indicates the total size of the array.
bool valid (size_t _index) const
 Indicates whether a given index is valid.
void empty ()
 Empties the array but does NOT free any pointers stored in the array.
int sort (CrissCross::Data::Sorter< T > *_sortMethod)
 Sorts the array using the provided method.
int sort (CrissCross::Data::Sorter< T > &_sortMethod)
 Sorts the array using the provided method.
T & operator[] (size_t _index)
 Gets the data at the given index.
T const & operator[] (size_t _index) const
 Gets the data at the given index.
size_t mem_usage () const
 Returns the overhead caused by the data structure.
void flush ()
 Empties the array and deletes the data contained in it with the 'delete' operator.
void flushArray ()
 Empties the array and deletes the data contained in it with the 'delete []' operator.

Protected Member Functions

void grow ()
 Increases the size of the array.
void rebuildStack ()
 Rebuilds the empty node stack.
void recount ()
 Recounts the number of used nodes.
size_t getNextFree ()
 Gets the next empty node.

Protected Attributes

int m_stepSize
 The size by which to increase the size of the array when there are no more empty nodes.
size_t m_arraySize
 The current size of the array.
size_t m_numUsed
 The number of used items in the array.
T * m_array
 The actual array which stores our data.
char * m_shadow
 An array to indicate which nodes in m_array are in use.

Private Attributes

DStack< size_t > * m_emptyNodes
 A DStack containing indices of empty nodes in the array.


Detailed Description

template<class T>
class CrissCross::Data::DArray< T >

A dynamic array implementation.

Constructor & Destructor Documentation

DArray ( int  _newStepSize  ) 

The secondary constructor.

Parameter _newStepSize should be larger than 1. A step size of 1 forces the DArray to resize way too often. A step size of -1 is a magic value which makes the DArray double in size on each grow() call (offers a pretty good speedup).

Parameters:
_newStepSize The step size to use in grow().


Member Function Documentation

void empty (  ) 

Empties the array but does NOT free any pointers stored in the array.

The array must be iterated through and any pointers must be freed manually before calling this.

size_t find ( T const &  _data  ) 

Finds the data in the array.

A return value of -1 means the data couldn't be found.

Parameters:
_data The data to find.
Returns:
The index where the given data is located.

T get ( size_t  _index  )  const [inline]

Gets the data at the given index.

Parameters:
_index The index of the node to get data from.
Returns:
The data stored at the index.

size_t getNextFree (  )  [protected]

Gets the next empty node.

Typically can just pop an item off the empty_nodes stack. If there are no other empty nodes remaining, then it will automatically Grow() the array.

Returns:
An index in m_array.

void insert ( T const &  _newdata,
size_t  _index 
)

Inserts data into the array at the given index.

Parameters:
_newdata The data to put into the array.
_index The index in the array where the data should be put, regardless of existing contents.

size_t insert ( T const &  _newdata  ) 

Inserts data into the array at the first available index.

Parameters:
_newdata The data to put into the array.
Returns:
The index of the node where the data was stored.

size_t mem_usage (  )  const

Returns the overhead caused by the data structure.

Returns:
Memory usage in bytes.

T const& operator[] ( size_t  _index  )  const [inline]

Gets the data at the given index.

Parameters:
_index The index of the node to get data from.
Returns:
The data stored at the index.

T& operator[] ( size_t  _index  )  [inline]

Gets the data at the given index.

Parameters:
_index The index of the node to get data from.
Returns:
The data stored at the index.

void remove ( size_t  _index  ) 

Removes the data at the given index.

Parameters:
_index The index of the node to clear.

void setSize ( size_t  _newsize  ) 

Sets the size of the array.

Parameters:
_newsize The new array size.

void setStepSize ( int  _newstepsize  ) 

Sets the step size used in Grow().

Parameter _newStepSize should be larger than 1. A step size of 1 forces the DArray to resize way too often. A step size of -1 is a magic value which makes the DArray double in size on each grow() call (offers a pretty good speedup).

Parameters:
_newstepsize The step size to use in grow().

size_t size (  )  const [inline]

Indicates the total size of the array.

Returns:
The size of the array.

int sort ( CrissCross::Data::Sorter< T > &  _sortMethod  ) 

Sorts the array using the provided method.

Parameters:
_sortMethod The method to sort with.
Returns:
The number of assignments and comparisons to finish the sort.

int sort ( CrissCross::Data::Sorter< T > *  _sortMethod  ) 

Sorts the array using the provided method.

Parameters:
_sortMethod The method to sort with.
Returns:
The number of assignments and comparisons to finish the sort.

size_t used (  )  const [inline]

Indicates the number of used nodes.

Returns:
The number of used nodes.

bool valid ( size_t  _index  )  const [inline]

Indicates whether a given index is valid.

Tests whether the index is within the bounds of the array and is an empty node.

Parameters:
_index The index to test.
Returns:
Boolean value. True if valid, false if not.


Member Data Documentation

size_t m_arraySize [protected]

The current size of the array.

See also:
setSize

Referenced by DArray< char * >::size(), and DArray< char * >::valid().

DStack<size_t>* m_emptyNodes [private]

A DStack containing indices of empty nodes in the array.

Vastly speeds up insertions by keeping track of where empty spaces are.

int m_stepSize [protected]

The size by which to increase the size of the array when there are no more empty nodes.

If set to -1, it will double the size of the array each time the array grows.

See also:
setStepSize

setStepDouble


Generated on Sun Feb 8 11:10:00 2009 for CrissCross by  doxygen 1.5.8