Template class for dynamic array holding objects.
Definition at line 55 of file kdynamicarray.h.
#include <kdynamicarray.h>
Public Types |
|
| typedef VALUE_TYPE | ValueType |
| Type of the elements in the array. |
|
| typedef ALLOCATOR | AllocatorType |
| Type of the class used for allocating
memory. |
|
Public Member Functions |
|
| KDynamicArray () | |
| Default constructor. |
|
| KDynamicArray (size_t const pInitialSize) | |
| Constructor. |
|
| KDynamicArray (KDynamicArray const &pArray) | |
| Copy constructor. |
|
| ~KDynamicArray () | |
| Destructor. |
|
| size_t | GetCapacity () const |
| Gets the current capacity of the array.
|
|
| size_t | GetSize () const |
| Gets the size of the array. |
|
| void | Reserve (size_t const pCount) |
| Assures that sufficient memory is allocated
to hold n objects in the array, and increases the capacity if
necessary. |
|
| void | PushBack (ValueType const &pValue, size_t const pNCopies=1) |
| Appends n objects at the end of the array.
|
|
| void | Insert (size_t const pIndex, ValueType const &pValue, size_t const pNCopies=1) |
| Inserts n objects at the specified position.
|
|
| void | PopBack (size_t pNElements=1) |
| Removes n objects at the end. |
|
| void | Remove (size_t const pIndex, size_t pNElements=1) |
| Removes n objects at the specified position.
|
|
| ValueType & | operator[] (size_t const pIndex) |
| Gets nth object in the array. |
|
| ValueType const & | operator[] (size_t const pIndex) const |
| Gets nth object in the array. |
|
| KDynamicArray & | operator= (KDynamicArray const &pArray) |
| Assignment operator. |
|
| typedef VALUE_TYPE ValueType |
Type of the elements in the array.
Definition at line 59 of file kdynamicarray.h.
| typedef ALLOCATOR AllocatorType |
Type of the class used for allocating memory.
Definition at line 61 of file kdynamicarray.h.
| KDynamicArray | ( | ) | [inline] |
Default constructor.
Definition at line 64 of file kdynamicarray.h.
| KDynamicArray | ( | size_t const | pInitialSize | ) | [inline] |
Constructor.
| pInitialSize | initial capacity of this array |
Definition at line 75 of file kdynamicarray.h.
| KDynamicArray | ( | KDynamicArray< VALUE_TYPE, ALLOCATOR > const & | pArray | ) | [inline] |
Copy constructor.
VALUE_TYPE will be invoked
in order to copy the value of elements to the new array.Definition at line 89 of file kdynamicarray.h.
| ~KDynamicArray | ( | ) | [inline] |
Destructor.
Definition at line 101 of file kdynamicarray.h.
{
size_t i;
for (i = 0; i < mValueCount; i++)
{
mArray[i].~VALUE_TYPE();
}
mAllocator.FreeMemory(mArray);
}
| size_t GetCapacity | ( | ) | const [inline] |
Gets the current capacity of the array.
Definition at line 113 of file kdynamicarray.h.
{
return mArrayCapacity;
}
| size_t GetSize | ( | ) | const [inline] |
| void Reserve | ( | size_t const | pCount | ) | [inline] |
Assures that sufficient memory is allocated to hold n objects in the array, and increases the capacity if necessary.
| pCount | Number of objects to reserve |
Definition at line 128 of file kdynamicarray.h.
{
if (pCount > mArrayCapacity)
{
// We don't use mAllocator.PreAllocate, because we want our array
// to be continuous in memory.
void* lBuffer = mAllocator.AllocateRecords(pCount);
ValueType* lNewArray = reinterpret_cast<ValueType*>(lBuffer);
MoveArray(lNewArray, mArray, mValueCount);
mAllocator.FreeMemory(mArray);
mArray = lNewArray;
mArrayCapacity = pCount;
}
}
| void PushBack | ( | ValueType const & | pValue, |
| size_t const | pNCopies = 1 |
||
| ) | [inline] |
Appends n objects at the end of the array.
| pValue | object to append |
| pNCopies | number of copies to append |
Definition at line 149 of file kdynamicarray.h.
{
if (mValueCount + pNCopies > mArrayCapacity)
{
// grow by 50%
size_t lNewSize = mArrayCapacity + mArrayCapacity / 2;
if (mValueCount + pNCopies > lNewSize)
{
lNewSize = mValueCount + pNCopies;
}
Reserve(lNewSize);
}
K_ASSERT(mValueCount + pNCopies <= mArrayCapacity);
Fill(mArray + mValueCount, pValue, pNCopies);
mValueCount += pNCopies;
}
| void Insert | ( | size_t const | pIndex, |
| ValueType const & | pValue, | ||
| size_t const | pNCopies = 1 |
||
| ) | [inline] |
Inserts n objects at the specified position.
| pIndex | position index |
| pValue | object to insert |
| pNCopies | number of copies to append |
Definition at line 176 of file kdynamicarray.h.
{
K_ASSERT(pIndex >= 0);
K_ASSERT(pIndex <= mValueCount);
ValueType lValue = pValue; // in case pValue is in array
if (pNCopies == 0)
{
}
else if (pIndex >= mValueCount)
{
PushBack(pValue, pNCopies);
}
else if (mValueCount + pNCopies > mArrayCapacity)
{
// not enough room
// grow by 50%
size_t lNewSize = mArrayCapacity + mArrayCapacity / 2;
if (mValueCount + pNCopies > lNewSize)
{
lNewSize = mValueCount + pNCopies;
}
void* lBuffer = mAllocator.AllocateRecords(lNewSize);
ValueType* lNewArray = reinterpret_cast<ValueType*>(lBuffer);
MoveArray(lNewArray, mArray, pIndex); // copy prefix
Fill(lNewArray + pIndex, pValue, pNCopies); // copy values
MoveArray(lNewArray + pIndex + pNCopies, mArray + pIndex, mValueCount - pIndex); // copy suffix
mAllocator.FreeMemory(mArray);
mArray = lNewArray;
mValueCount += pNCopies;
mArrayCapacity = lNewSize;
}
else
{
// copy suffix backwards
MoveArrayBackwards(mArray + pIndex + pNCopies, mArray + pIndex, mValueCount - pIndex);
Fill(mArray + pIndex, pValue, pNCopies); // copy values
mValueCount += pNCopies;
}
}
| void PopBack | ( | size_t | pNElements = 1 |
) | [inline] |
Removes n objects at the end.
| pNElements | number of objects to remove |
Definition at line 225 of file kdynamicarray.h.
{
K_ASSERT(pNElements <= mValueCount);
size_t i;
for (i = mValueCount - pNElements; i < mValueCount; i++)
{
mArray[i].~VALUE_TYPE();
}
mValueCount -= pNElements;
}
| void Remove | ( | size_t const | pIndex, |
| size_t | pNElements =
1 |
||
| ) | [inline] |
Removes n objects at the specified position.
| pIndex | position index |
| pNElements | number of objects to remove |
Definition at line 242 of file kdynamicarray.h.
{
K_ASSERT(pIndex >= 0);
K_ASSERT(pIndex <= mValueCount);
K_ASSERT(pIndex + pNElements <= mValueCount);
if (pIndex + pNElements >= mValueCount)
{
PopBack(pNElements);
}
else
{
size_t i;
for (i = pIndex; i < pIndex + pNElements; i++)
{
mArray[i].~VALUE_TYPE();
}
MoveOverlappingArray(mArray + pIndex, mArray + pIndex + pNElements, mValueCount - pNElements);
mValueCount -= pNElements;
}
}
| ValueType& operator[] | ( | size_t const | pIndex | ) | [inline] |
Gets nth object in the array.
| pIndex | position index |
Definition at line 269 of file kdynamicarray.h.
{
return *(mArray + pIndex);
}
| ValueType const& operator[] | ( | size_t const | pIndex | ) | const [inline] |
Gets nth object in the array.
| pIndex | position index |
Definition at line 277 of file kdynamicarray.h.
{
return *(mArray + pIndex);
}
| KDynamicArray& operator= | ( | KDynamicArray< VALUE_TYPE, ALLOCATOR > const & | pArray | ) | [inline] |
Assignment operator.
VALUE_TYPE will be invoked
in order to copy the value of elements to the new array.Definition at line 287 of file kdynamicarray.h.
{
Reserve(pArray.mArrayCapacity);
CopyArray(mArray, pArray.mArray, pArray.mValueCount);
mValueCount = pArray.mValueCount;
return *this;
}