![]() |
Shadowrun: Awakened 29 September 2011 - Build 871
|
Array based implementation of a list. More...
#include <DS_List.h>
Public Member Functions | |
| void | Clear (bool doNotDeallocateSmallBlocks, const char *file, unsigned int line) |
| Clear the list. | |
| void | Compress (const char *file, unsigned int line) |
| Frees overallocated members, to use the minimum memory necessary. | |
| list_type & | Get (const unsigned int position) const |
| Access an element by its index in the array. | |
| unsigned int | GetIndexOf (const list_type &input) const |
| Returns the index of the specified item or MAX_UNSIGNED_LONG if not found. | |
| void | Insert (const list_type &input, const unsigned int position, const char *file, unsigned int line) |
| Insert an element at position position in the list. | |
| void | Insert (const list_type &input, const char *file, unsigned int line) |
| Insert at the end of the list. | |
| List () | |
| Default constructor. | |
| List (const List &original_copy) | |
| Copy constructor. | |
| List & | operator= (const List &original_copy) |
| Assign one list to another. | |
| list_type & | operator[] (const unsigned int position) const |
| Access an element by its index in the array. | |
| list_type & | Pop (void) |
| Pop an element from the end of the stack. | |
| void | Preallocate (unsigned countNeeded, const char *file, unsigned int line) |
| Preallocate the list, so it needs fewer reallocations at runtime. | |
| void | Push (const list_type &input, const char *file, unsigned int line) |
| Push an element at the end of the stack. | |
| void | RemoveAtIndex (const unsigned int position) |
| Delete the element at position position. | |
| void | RemoveAtIndexFast (const unsigned int position) |
| Delete the element at position position. | |
| void | RemoveFromEnd (const unsigned num=1) |
| Delete the element at the end of the list. | |
| void | Replace (const list_type &input, const list_type filler, const unsigned int position, const char *file, unsigned int line) |
| Replace the value at position by input. | |
| void | Replace (const list_type &input) |
| Replace the last element of the list by input. | |
| unsigned int | Size (void) const |
| ~List () | |
Private Attributes | |
| unsigned int | allocation_size |
| Size of array. | |
| unsigned int | list_size |
| Number of elements in the list. | |
| list_type * | listArray |
| An array of user values. | |
| DataStructures::List< list_type >::List | ( | ) |
Definition at line 129 of file DS_List.h.
{
allocation_size = 0;
listArray = 0;
list_size = 0;
}
| DataStructures::List< list_type >::~List | ( | ) |
Definition at line 137 of file DS_List.h.
References _FILE_AND_LINE_, and RakNet::OP_DELETE_ARRAY().
{
if (allocation_size>0)
RakNet::OP_DELETE_ARRAY(listArray, _FILE_AND_LINE_);
}
| DataStructures::List< list_type >::List | ( | const List< list_type > & | original_copy | ) |
| [in] | original_copy | The list to duplicate |
Definition at line 145 of file DS_List.h.
References _FILE_AND_LINE_, DataStructures::List< list_type >::list_size, and DataStructures::List< list_type >::listArray.
{
// Allocate memory for copy
if ( original_copy.list_size == 0 )
{
list_size = 0;
allocation_size = 0;
}
else
{
listArray = RakNet::OP_NEW_ARRAY<list_type >( original_copy.list_size , _FILE_AND_LINE_ );
for ( unsigned int counter = 0; counter < original_copy.list_size; ++counter )
listArray[ counter ] = original_copy.listArray[ counter ];
// Don't call constructors, assignment operators, etc.
//memcpy(listArray, original_copy.listArray, original_copy.list_size*sizeof(list_type));
list_size = allocation_size = original_copy.list_size;
}
}
| void DataStructures::List< list_type >::Clear | ( | bool | doNotDeallocateSmallBlocks, |
| const char * | file, | ||
| unsigned int | line | ||
| ) |
Definition at line 444 of file DS_List.h.
References RakNet::OP_DELETE_ARRAY().
Referenced by DataStructures::WeightedGraph< node_type, weight_type, allow_unlinkedNodes >::GetShortestPath().
{
if ( allocation_size == 0 )
return;
if (allocation_size>512 || doNotDeallocateSmallBlocks==false)
{
RakNet::OP_DELETE_ARRAY(listArray, file, line);
allocation_size = 0;
listArray = 0;
}
list_size = 0;
}
| void DataStructures::List< list_type >::Compress | ( | const char * | file, |
| unsigned int | line | ||
| ) |
Definition at line 459 of file DS_List.h.
References RakNet::OP_DELETE_ARRAY().
{
list_type * new_array;
if ( allocation_size == 0 )
return ;
new_array = RakNet::OP_NEW_ARRAY<list_type >( allocation_size , file, line );
// copy old array over
for ( unsigned int counter = 0; counter < list_size; ++counter )
new_array[ counter ] = listArray[ counter ];
// Don't call constructors, assignment operators, etc.
//memcpy(new_array, listArray, list_size*sizeof(list_type));
// set old array to point to the newly allocated array
RakNet::OP_DELETE_ARRAY(listArray, file, line);
listArray = new_array;
}
| list_type & DataStructures::List< list_type >::Get | ( | const unsigned int | position | ) | const [inline] |
| unsigned int DataStructures::List< list_type >::GetIndexOf | ( | const list_type & | input | ) | const |
| [in] | input | The element to check for |
| MAX_UNSIGNED_LONG | The object is not in the list |
| [Integer] | The index of the element in the list |
Definition at line 428 of file DS_List.h.
References MAX_UNSIGNED_LONG.
{
for ( unsigned int i = 0; i < list_size; ++i )
if ( listArray[ i ] == input )
return i;
return MAX_UNSIGNED_LONG;
}
| void DataStructures::List< list_type >::Insert | ( | const list_type & | input, |
| const unsigned int | position, | ||
| const char * | file, | ||
| unsigned int | line | ||
| ) |
| [in] | input | The new element. |
| [in] | position | The position of the new element. |
Definition at line 236 of file DS_List.h.
References RakNet::OP_DELETE_ARRAY(), and RakAssert.
Referenced by DataStructures::WeightedGraph< node_type, weight_type, allow_unlinkedNodes >::GetShortestPath(), DataStructures::WeightedGraph< node_type, weight_type, allow_unlinkedNodes >::GetSpanningTree(), and DataStructures::Tree< TreeType >::LevelOrderTraversal().
{
#ifdef _DEBUG
if (position>list_size)
{
RakAssert( position <= list_size );
}
#endif
// Reallocate list if necessary
if ( list_size == allocation_size )
{
// allocate twice the currently allocated memory
list_type * new_array;
if ( allocation_size == 0 )
allocation_size = 16;
else
allocation_size *= 2;
new_array = RakNet::OP_NEW_ARRAY<list_type >( allocation_size , file, line );
// copy old array over
for ( unsigned int counter = 0; counter < list_size; ++counter )
new_array[ counter ] = listArray[ counter ];
// Don't call constructors, assignment operators, etc.
//memcpy(new_array, listArray, list_size*sizeof(list_type));
// set old array to point to the newly allocated and twice as large array
RakNet::OP_DELETE_ARRAY(listArray, file, line);
listArray = new_array;
}
// Move the elements in the list to make room
for ( unsigned int counter = list_size; counter != position; counter-- )
listArray[ counter ] = listArray[ counter - 1 ];
// Don't call constructors, assignment operators, etc.
//memmove(listArray+position+1, listArray+position, (list_size-position)*sizeof(list_type));
// Insert the new item at the correct spot
listArray[ position ] = input;
++list_size;
}
| void DataStructures::List< list_type >::Insert | ( | const list_type & | input, |
| const char * | file, | ||
| unsigned int | line | ||
| ) |
| [in] | input | The new element. |
Definition at line 287 of file DS_List.h.
References RakNet::OP_DELETE_ARRAY().
{
// Reallocate list if necessary
if ( list_size == allocation_size )
{
// allocate twice the currently allocated memory
list_type * new_array;
if ( allocation_size == 0 )
allocation_size = 16;
else
allocation_size *= 2;
new_array = RakNet::OP_NEW_ARRAY<list_type >( allocation_size , file, line );
if (listArray)
{
// copy old array over
for ( unsigned int counter = 0; counter < list_size; ++counter )
new_array[ counter ] = listArray[ counter ];
// Don't call constructors, assignment operators, etc.
//memcpy(new_array, listArray, list_size*sizeof(list_type));
// set old array to point to the newly allocated and twice as large array
RakNet::OP_DELETE_ARRAY(listArray, file, line);
}
listArray = new_array;
}
// Insert the new item at the correct spot
listArray[ list_size ] = input;
++list_size;
}
| List< list_type > & DataStructures::List< list_type >::operator= | ( | const List< list_type > & | original_copy | ) |
Definition at line 169 of file DS_List.h.
References _FILE_AND_LINE_, DataStructures::List< list_type >::list_size, and DataStructures::List< list_type >::listArray.
{
if ( ( &original_copy ) != this )
{
Clear( false, _FILE_AND_LINE_ );
// Allocate memory for copy
if ( original_copy.list_size == 0 )
{
list_size = 0;
allocation_size = 0;
}
else
{
listArray = RakNet::OP_NEW_ARRAY<list_type >( original_copy.list_size , _FILE_AND_LINE_ );
for ( unsigned int counter = 0; counter < original_copy.list_size; ++counter )
listArray[ counter ] = original_copy.listArray[ counter ];
// Don't call constructors, assignment operators, etc.
//memcpy(listArray, original_copy.listArray, original_copy.list_size*sizeof(list_type));
list_size = allocation_size = original_copy.list_size;
}
}
return *this;
}
| list_type & DataStructures::List< list_type >::operator[] | ( | const unsigned int | position | ) | const [inline] |
| list_type & DataStructures::List< list_type >::Pop | ( | void | ) | [inline] |
| void DataStructures::List< list_type >::Preallocate | ( | unsigned | countNeeded, |
| const char * | file, | ||
| unsigned int | line | ||
| ) |
Definition at line 482 of file DS_List.h.
References RakNet::OP_DELETE_ARRAY().
{
unsigned amountToAllocate = allocation_size;
if (allocation_size==0)
amountToAllocate=16;
while (amountToAllocate < countNeeded)
amountToAllocate<<=1;
if ( allocation_size < amountToAllocate)
{
// allocate twice the currently allocated memory
list_type * new_array;
allocation_size=amountToAllocate;
new_array = RakNet::OP_NEW_ARRAY< list_type >( allocation_size , file, line );
if (listArray)
{
// copy old array over
for ( unsigned int counter = 0; counter < list_size; ++counter )
new_array[ counter ] = listArray[ counter ];
// Don't call constructors, assignment operators, etc.
//memcpy(new_array, listArray, list_size*sizeof(list_type));
// set old array to point to the newly allocated and twice as large array
RakNet::OP_DELETE_ARRAY(listArray, file, line);
}
listArray = new_array;
}
}
| void DataStructures::List< list_type >::Push | ( | const list_type & | input, |
| const char * | file, | ||
| unsigned int | line | ||
| ) |
| [in] | input | The new element. |
Definition at line 220 of file DS_List.h.
Referenced by RakNet::VariableListDeltaTracker::WriteVar().
{
Insert(input, file, line);
}
| void DataStructures::List< list_type >::RemoveAtIndex | ( | const unsigned int | position | ) |
| [in] | position | The index of the element to delete |
Definition at line 381 of file DS_List.h.
References RakAssert.
{
#ifdef _DEBUG
if (position >= list_size)
{
RakAssert( position < list_size );
return;
}
#endif
if ( position < list_size )
{
// Compress the array
for ( unsigned int counter = position; counter < list_size - 1 ; ++counter )
listArray[ counter ] = listArray[ counter + 1 ];
// Don't call constructors, assignment operators, etc.
// memmove(listArray+position, listArray+position+1, (list_size-1-position) * sizeof(list_type));
RemoveFromEnd();
}
}
| void DataStructures::List< list_type >::RemoveAtIndexFast | ( | const unsigned int | position | ) |
| void DataStructures::List< list_type >::RemoveFromEnd | ( | const unsigned | num = 1 | ) | [inline] |
| void DataStructures::List< list_type >::Replace | ( | const list_type & | input, |
| const list_type | filler, | ||
| const unsigned int | position, | ||
| const char * | file, | ||
| unsigned int | line | ||
| ) | [inline] |
If the size of the list is less than position, it increase the capacity of the list and fill slot with filler.
| [in] | input | The element to replace at position position. |
| [in] | filler | The element use to fill new allocated capacity. |
| [in] | position | The position of input in the list. |
Definition at line 326 of file DS_List.h.
References RakNet::OP_DELETE_ARRAY(), and RakAssert.
{
if ( ( list_size > 0 ) && ( position < list_size ) )
{
// Direct replacement
listArray[ position ] = input;
}
else
{
if ( position >= allocation_size )
{
// Reallocate the list to size position and fill in blanks with filler
list_type * new_array;
allocation_size = position + 1;
new_array = RakNet::OP_NEW_ARRAY<list_type >( allocation_size , file, line );
// copy old array over
for ( unsigned int counter = 0; counter < list_size; ++counter )
new_array[ counter ] = listArray[ counter ];
// Don't call constructors, assignment operators, etc.
//memcpy(new_array, listArray, list_size*sizeof(list_type));
// set old array to point to the newly allocated array
RakNet::OP_DELETE_ARRAY(listArray, file, line);
listArray = new_array;
}
// Fill in holes with filler
while ( list_size < position )
listArray[ list_size++ ] = filler;
// Fill in the last element with the new item
listArray[ list_size++ ] = input;
#ifdef _DEBUG
RakAssert( list_size == position + 1 );
#endif
}
}
| void DataStructures::List< list_type >::Replace | ( | const list_type & | input | ) | [inline] |
| unsigned int DataStructures::List< list_type >::Size | ( | void | ) | const [inline] |
Definition at line 438 of file DS_List.h.
Referenced by DataStructures::WeightedGraph< node_type, weight_type, allow_unlinkedNodes >::GetSpanningTree(), RakNet::VariableListDeltaTracker::IsPastEndOfList(), DataStructures::Tree< TreeType >::LevelOrderTraversal(), and RakNet::VariableListDeltaTracker::WriteVar().
{
return list_size;
}
unsigned int DataStructures::List< list_type >::allocation_size [private] |
unsigned int DataStructures::List< list_type >::list_size [private] |
Definition at line 123 of file DS_List.h.
Referenced by DataStructures::List< list_type >::List(), and DataStructures::List< list_type >::operator=().
list_type* DataStructures::List< list_type >::listArray [private] |
Definition at line 120 of file DS_List.h.
Referenced by DataStructures::List< list_type >::List(), and DataStructures::List< list_type >::operator=().
Copyright © 2007-2010 by The Shadowrun: Awakened Team. This work is licensed under the GNU Lesser General Public License 3.