My Project
Public Member Functions | List of all members
ParaEngine::array< T > Class Template Reference

Self reallocating template array (like stl vector) with additional features. More...

#include <Array.h>

Public Member Functions

 array (DWORD start_count)
 Constructs a array and allocates an initial chunk of memory. More...
 
 array (const array< T > &other)
 Copy constructor.
 
 ~array ()
 Destructor. More...
 
void reallocate (DWORD new_size)
 Reallocates the array, make it bigger or smaller. More...
 
void push_back (const T &element)
 Adds an element at back of array. More...
 
void clear ()
 Clears the array and deletes all allocated memory.
 
void set_pointer (T *newPointer, DWORD size)
 Sets pointer to new array, using this as new workspace. More...
 
void set_free_when_destroyed (bool f)
 Sets if the array should delete the memory it used. More...
 
void set_used (DWORD usedNow)
 Sets the size of the array. More...
 
void operator= (const array< T > &other)
 Assignment operator.
 
T & operator[] (DWORD index)
 Direct access operator.
 
const T & operator[] (DWORD index) const
 Direct access operator.
 
const T & getLast () const
 Gets last frame.
 
T & getLast ()
 Gets last frame.
 
T * pointer ()
 Returns a pointer to the array. More...
 
const T * const_pointer () const
 Returns a const pointer to the array. More...
 
DWORD size () const
 Returns size of used array. More...
 
DWORD allocated_size () const
 Returns amount memory allocated. More...
 
bool empty () const
 Returns true if array is empty. More...
 
void sort ()
 Sorts the array using heapsort. More...
 
int binary_search (const T &element)
 Performs a binary search for an element, returns -1 if not found. More...
 
int binary_search (const T &element, int left, int right)
 Performs a binary search for an element, returns -1 if not found. More...
 
int linear_search (T &element)
 Finds an element in linear time, which is very slow. More...
 
int linear_reverse_search (T &element)
 Finds an element in linear time, which is very slow. More...
 
void erase (DWORD index)
 Erases an element from the array. More...
 
void erase (DWORD index, int count)
 Erases some elements from the array. More...
 
void set_sorted (bool _is_sorted)
 Sets if the array is sorted.
 

Detailed Description

template<class T>
class ParaEngine::array< T >

Self reallocating template array (like stl vector) with additional features.

Some features are: Heap sorting, binary search methods, easier debugging.

Constructor & Destructor Documentation

§ array()

template<class T>
ParaEngine::array< T >::array ( DWORD  start_count)
inline

Constructs a array and allocates an initial chunk of memory.

Parameters
start_countAmount of elements to allocate.

§ ~array()

template<class T>
ParaEngine::array< T >::~array ( )
inline

Destructor.

Frees allocated memory, if set_free_when_destroyed was not set to false by the user before.

Member Function Documentation

§ allocated_size()

template<class T>
DWORD ParaEngine::array< T >::allocated_size ( ) const
inline

Returns amount memory allocated.

Returns
Returns amount of memory allocated. The amount of bytes allocated would be allocated_size() * sizeof(ElementsUsed);

§ binary_search() [1/2]

template<class T>
int ParaEngine::array< T >::binary_search ( const T &  element)
inline

Performs a binary search for an element, returns -1 if not found.

The array will be sorted before the binary search if it is not already sorted.

Parameters
elementElement to search for.
Returns
Returns position of the searched element if it was found, otherwise -1 is returned.

§ binary_search() [2/2]

template<class T>
int ParaEngine::array< T >::binary_search ( const T &  element,
int  left,
int  right 
)
inline

Performs a binary search for an element, returns -1 if not found.

The array will be sorted before the binary search if it is not already sorted.

Parameters
elementElement to search for.
leftFirst left index
rightLast right index.
Returns
Returns position of the searched element if it was found, otherwise -1 is returned.

§ const_pointer()

template<class T>
const T* ParaEngine::array< T >::const_pointer ( ) const
inline

Returns a const pointer to the array.

Returns
Pointer to the array.

§ empty()

template<class T>
bool ParaEngine::array< T >::empty ( ) const
inline

Returns true if array is empty.

Returns
True if the array is empty, false if not.

§ erase() [1/2]

template<class T>
void ParaEngine::array< T >::erase ( DWORD  index)
inline

Erases an element from the array.

May be slow, because all elements following after the erased element have to be copied.

Parameters
indexIndex of element to be erased.

§ erase() [2/2]

template<class T>
void ParaEngine::array< T >::erase ( DWORD  index,
int  count 
)
inline

Erases some elements from the array.

may be slow, because all elements following after the erased element have to be copied.

Parameters
indexIndex of the first element to be erased.
countAmount of elements to be erased.

§ linear_reverse_search()

template<class T>
int ParaEngine::array< T >::linear_reverse_search ( T &  element)
inline

Finds an element in linear time, which is very slow.

Use binary_search for faster finding. Only works if =operator is implemented.

Parameters
elementElement to search for.
Returns
Returns position of the searched element if it was found, otherwise -1 is returned.

§ linear_search()

template<class T>
int ParaEngine::array< T >::linear_search ( T &  element)
inline

Finds an element in linear time, which is very slow.

Use binary_search for faster finding. Only works if =operator is implemented.

Parameters
elementElement to search for.
Returns
Returns position of the searched element if it was found, otherwise -1 is returned.

§ pointer()

template<class T>
T* ParaEngine::array< T >::pointer ( )
inline

Returns a pointer to the array.

Returns
Pointer to the array.

§ push_back()

template<class T>
void ParaEngine::array< T >::push_back ( const T &  element)
inline

Adds an element at back of array.

If the array is to small to add this new element, the array is made bigger.

Parameters
elementElement to add at the back of the array.

§ reallocate()

template<class T>
void ParaEngine::array< T >::reallocate ( DWORD  new_size)
inline

Reallocates the array, make it bigger or smaller.

Parameters
new_sizeNew size of array.

§ set_free_when_destroyed()

template<class T>
void ParaEngine::array< T >::set_free_when_destroyed ( bool  f)
inline

Sets if the array should delete the memory it used.

Parameters
fIf true, the array frees the allocated memory in its destructor, otherwise not. The default is true.

§ set_pointer()

template<class T>
void ParaEngine::array< T >::set_pointer ( T *  newPointer,
DWORD  size 
)
inline

Sets pointer to new array, using this as new workspace.

Parameters
newPointerPointer to new array of elements.
sizeSize of the new array.

§ set_used()

template<class T>
void ParaEngine::array< T >::set_used ( DWORD  usedNow)
inline

Sets the size of the array.

Parameters
usedNowAmount of elements now used.

§ size()

template<class T>
DWORD ParaEngine::array< T >::size ( ) const
inline

Returns size of used array.

Returns
Size of elements in the array.

§ sort()

template<class T>
void ParaEngine::array< T >::sort ( )
inline

Sorts the array using heapsort.

There is no additional memory waste and the algorithm performs (O) n log n in worst case.


The documentation for this class was generated from the following file: