Template version base class used to store an array of fixed-size elements.
This class is used internally and only contains interfaces used for memory management of array elements.
Definition at line 130 of file karrayul.h.
#include <karrayul.h>
Classes |
|
struct | KHeader |
Protected Member Functions |
|
KBaseArray (TypeSize pTypeSize) | |
Constructor. |
|
~KBaseArray () | |
Destructor. |
|
int | GetTypeSize () const |
int | GetCount () const |
Get the number of elements in the array.
|
|
void | Clear () |
All the elements of the array are removed
and the memory of content is released. |
|
void | Empty () |
All the elements of the array are removed,
but the memory of content is not released. |
|
int | Reserve (int pCapacity) |
Set the capacity of the allocated storage
space for the elements of the array. |
|
void | SetCount (int pCount) |
Resizes the array to contain the specific
elements. |
|
void | Resize (int pItemCount) |
Resizes the array to contain the specific
elements. |
|
void | AddMultiple (int pItemCount) |
Append the specific number of elements to
the end the array. |
|
int | InsertAt (int pIndex, void *pItem) |
Insert an item at the given position.
|
|
void * | GetAt (int pIndex) |
Get the item at the given position. |
|
void | RemoveAt (int pIndex) |
Removes the item at the given position.
|
|
bool | ValidateIndex (int pIndex) const |
Check that the given position is inside the
array boundaries. |
|
const KHeader * | GetHeader () const |
KHeader * | GetHeader () |
int | GetHeaderOffset () const |
int | GetArrayCount () const |
void | SetArrayCount (int pArrayCount) |
int | GetBlockCount () const |
void | SetBlockCount (int pArrayCount) |
Protected Attributes |
|
char * | mBaseArray |
TypeSize | mTypeSize |
KBaseArray | ( | TypeSize | pTypeSize | ) | [inline, protected] |
Constructor.
pTypeSize | Size of one item of the array. |
Definition at line 152 of file karrayul.h.
: mTypeSize(pTypeSize) { mBaseArray = NULL; }
~KBaseArray | ( | ) | [inline, protected] |
int GetTypeSize | ( | ) | const [inline, protected] |
Definition at line 164 of file karrayul.h.
{ return mTypeSize.GetTypeSize(); }
int GetCount | ( | ) | const [inline, protected] |
Get the number of elements in the array.
Definition at line 169 of file karrayul.h.
{ return GetArrayCount(); }
void Clear | ( | ) | [inline, protected] |
All the elements of the array are removed and the memory of content is released.
Definition at line 174 of file karrayul.h.
{ if (mBaseArray!=NULL) { KBaseArrayFree(mBaseArray); mBaseArray = NULL; } }
void Empty | ( | ) | [inline, protected] |
All the elements of the array are removed, but the memory of content is not released.
Definition at line 186 of file karrayul.h.
{ #ifdef KFBX_PRIVATE #ifdef _DEBUG memset(mBaseArray+ GetHeaderOffset(), 0, GetArrayCount()*GetTypeSize()); #endif #endif SetArrayCount(0); }
int Reserve | ( | int | pCapacity | ) | [inline, protected] |
Set the capacity of the allocated storage space for the elements of the array.
pCapacity | Minimum amount desired as capacity of allocated storage. |
Definition at line 202 of file karrayul.h.
{ #ifdef KFBX_PRIVATE K_ASSERT( pCapacity > 0 ); #endif if( pCapacity ) { const kUInt lTempNewBlockCount = ( (kUInt) (pCapacity + KFBX_ARRAYUL_BLOCKSIZE - 1 ) / KFBX_ARRAYUL_BLOCKSIZE ); const kUInt lNewBlockCount = (lTempNewBlockCount > 1 ? lTempNewBlockCount : 1); int lArrayCount = GetArrayCount(); int lBlockCount = GetBlockCount(); const kUInt lOldArraySize = lArrayCount*GetTypeSize(); const kUInt lNewArraySize = lNewBlockCount*KFBX_ARRAYUL_BLOCKSIZE*GetTypeSize(); if (lNewBlockCount != (kUInt) lBlockCount) { char* lBaseArray = KBaseArrayRealloc(mBaseArray, (size_t) lNewArraySize+ GetHeaderOffset() ); if (!lBaseArray) return GetBlockCount()*KFBX_ARRAYUL_BLOCKSIZE; mBaseArray = lBaseArray; } if( lNewBlockCount > (kUInt) lBlockCount ) { memset( ((char*)mBaseArray+ GetHeaderOffset() ) + lOldArraySize, 0, (size_t) (lNewArraySize-lOldArraySize) ); SetArrayCount(lArrayCount); } else if (pCapacity < lArrayCount) { memset( ((char*)mBaseArray+ GetHeaderOffset() ) + pCapacity*GetTypeSize(), 0, (size_t) (lNewArraySize-pCapacity*GetTypeSize()) ); SetArrayCount(pCapacity); } SetBlockCount(lNewBlockCount); } return GetBlockCount()*KFBX_ARRAYUL_BLOCKSIZE; }
void SetCount | ( | int | pCount | ) | [inline, protected] |
Resizes the array to contain the specific elements.
pCount | New array size, expressed in elements. |
Definition at line 248 of file karrayul.h.
{ #ifdef KFBX_PRIVATE #ifdef _DEBUG if (pCount<0) { K_ASSERT_MSG_NOW (_T("ArrayUL : Item count can't be negative")); return ; } #endif #endif int lArrayCount = GetArrayCount(); if (pCount > lArrayCount) { AddMultiple( pCount-lArrayCount); } else { SetArrayCount(pCount); } }
void Resize | ( | int | pItemCount | ) | [inline, protected] |
Resizes the array to contain the specific elements.
pItemCount | New array size, expressed in elements. |
Definition at line 275 of file karrayul.h.
{ #ifdef KFBX_PRIVATE K_ASSERT( pItemCount >= 0 ); #endif const kUInt lTempNewBlockCount = ( (kUInt) (pItemCount + KFBX_ARRAYUL_BLOCKSIZE - 1 ) / KFBX_ARRAYUL_BLOCKSIZE ); const kUInt lNewBlockCount = (lTempNewBlockCount > 1 ? lTempNewBlockCount : 1); int lArrayCount = GetArrayCount(); int lBlockCount = GetBlockCount(); const kUInt lOldArraySize = lArrayCount*GetTypeSize(); const kUInt lNewArraySize = lNewBlockCount*KFBX_ARRAYUL_BLOCKSIZE*GetTypeSize(); if (lNewBlockCount != (kUInt) lBlockCount) { char* lBaseArray = KBaseArrayRealloc(mBaseArray, (size_t) lNewArraySize+ GetHeaderOffset() ); if (!lBaseArray) return; mBaseArray = lBaseArray; } if( lNewBlockCount > (kUInt) lBlockCount ) memset( ((char*)mBaseArray+ GetHeaderOffset() ) + lOldArraySize, 0, (size_t) (lNewArraySize-lOldArraySize) ); else if (pItemCount < lArrayCount) memset( ((char*)mBaseArray+ GetHeaderOffset() ) + pItemCount*GetTypeSize(), 0, (size_t) (lNewArraySize-pItemCount*GetTypeSize()) ); SetBlockCount(lNewBlockCount); SetArrayCount(pItemCount); }
void AddMultiple | ( | int | pItemCount | ) | [inline, protected] |
Append the specific number of elements to the end the array.
pItemCount | The number of elements appended in elements. |
Definition at line 313 of file karrayul.h.
{ #ifdef KFBX_PRIVATE K_ASSERT( pItemCount > 0 ); #endif if( pItemCount ) { int lArrayCount = GetArrayCount(); int lBlockCount = GetBlockCount(); const kUInt lTempNewBlockCount = ( (kUInt) (lArrayCount+pItemCount + KFBX_ARRAYUL_BLOCKSIZE - 1 ) / KFBX_ARRAYUL_BLOCKSIZE ); const kUInt lNewBlockCount = (lTempNewBlockCount > 1 ? lTempNewBlockCount : 1); const kUInt lOldArraySize = lArrayCount*GetTypeSize(); const kUInt lNewArraySize = lNewBlockCount*KFBX_ARRAYUL_BLOCKSIZE*GetTypeSize(); #ifdef KFBX_PRIVATE K_ASSERT( lOldArraySize < lNewArraySize ); #endif if( lNewBlockCount > (kUInt) lBlockCount ) { char* lBaseArray = KBaseArrayRealloc(mBaseArray, (size_t) lNewArraySize+ GetHeaderOffset() ); if (!lBaseArray) return; mBaseArray = lBaseArray; lBlockCount = lNewBlockCount; } memset( ((char*)mBaseArray+ GetHeaderOffset() ) + lOldArraySize, 0, (size_t) (lNewArraySize-lOldArraySize) ); SetArrayCount ( lArrayCount + pItemCount ); SetBlockCount (lBlockCount); } }
int InsertAt | ( | int | pIndex, |
void * | pItem | ||
) | [inline, protected] |
Insert an item at the given position.
pIndex | Position where to insert the item. |
pItem | Pointer to the item to be inserted. |
Definition at line 355 of file karrayul.h.
{ int lArrayCount = GetArrayCount(); int lBlockCount = GetBlockCount(); #ifdef KFBX_PRIVATE K_ASSERT( pIndex >= 0 ); #endif if (pIndex>lArrayCount) { pIndex = GetArrayCount(); } if (lArrayCount>= lBlockCount*KFBX_ARRAYUL_BLOCKSIZE) { // must Alloc.Realloc some new space // double the number of blocks. lBlockCount = ( 0 == lBlockCount ) ? 1 : lBlockCount * 2; char* lBaseArray = KBaseArrayRealloc(mBaseArray, (size_t) (lBlockCount*KFBX_ARRAYUL_BLOCKSIZE*GetTypeSize()) + GetHeaderOffset() ); if(!lBaseArray) return -1; mBaseArray = lBaseArray; } if (pIndex<lArrayCount) { // This is an insert memmove (&(mBaseArray[(pIndex+1)*GetTypeSize()+ GetHeaderOffset() ]), &(mBaseArray[(pIndex)*GetTypeSize()+ GetHeaderOffset()] ), GetTypeSize()*(lArrayCount-pIndex)); } memmove (&(mBaseArray[(pIndex)*GetTypeSize()+ GetHeaderOffset() ]), pItem, GetTypeSize()); SetArrayCount(lArrayCount+1); SetBlockCount(lBlockCount); return pIndex; }
void* GetAt | ( | int | pIndex | ) | [inline, protected] |
Get the item at the given position.
pIndex | The position of the item to access. |
Definition at line 401 of file karrayul.h.
{ return &(mBaseArray[(pIndex)*GetTypeSize()+ GetHeaderOffset() ]); }
void RemoveAt | ( | int | pIndex | ) | [inline, protected] |
Removes the item at the given position.
pIndex | The position of the item to remove. |
Definition at line 409 of file karrayul.h.
{ #if defined(_DEBUG) && !defined(KARCH_ENV_MACOSX) if (!ValidateIndex( pIndex )) { return; } #endif int lArrayCount = GetArrayCount(); if (pIndex+1<lArrayCount) { memmove (&(mBaseArray[(pIndex)*GetTypeSize()+ GetHeaderOffset() ]), &(mBaseArray[(pIndex+1)*GetTypeSize()+ GetHeaderOffset() ]), GetTypeSize()*(lArrayCount-pIndex-1)); } SetArrayCount( lArrayCount-1 ); #ifdef _DEBUG memset( &(mBaseArray[(GetArrayCount())*GetTypeSize()+ GetHeaderOffset() ]),0,GetTypeSize()); #endif }
bool ValidateIndex | ( | int | pIndex | ) | const [inline, protected] |
Check that the given position is inside the array boundaries.
pIndex | Index value to be validated. |
true
if the index value is within the array
boundaries. false
otherwise.Definition at line 435 of file karrayul.h.
{ int lArrayCount = GetArrayCount(); if (pIndex>=0 && pIndex<lArrayCount) { return true; } else { #ifdef KFBX_PRIVATE K_ASSERT_MSG_NOW(_T("ArrayTemplate : Index out of range")); #endif return false; } }
const KHeader* GetHeader | ( | ) | const [inline, protected] |
Definition at line 451 of file karrayul.h.
{ return (const KHeader *)mBaseArray; }
KHeader* GetHeader | ( | ) | [inline, protected] |
Definition at line 455 of file karrayul.h.
{ return (KHeader*)mBaseArray; }
int GetHeaderOffset | ( | ) | const [inline, protected] |
int GetArrayCount | ( | ) | const [inline, protected] |
Definition at line 463 of file karrayul.h.
{ return GetHeader() ? GetHeader()->mArrayCount : 0; }
void SetArrayCount | ( | int | pArrayCount | ) | [inline, protected] |
Definition at line 467 of file karrayul.h.
{ if (GetHeader()) GetHeader()->mArrayCount = pArrayCount; }
int GetBlockCount | ( | ) | const [inline, protected] |
Definition at line 471 of file karrayul.h.
{ return GetHeader() ? GetHeader()->mBlockCount : 0; }
void SetBlockCount | ( | int | pArrayCount | ) | [inline, protected] |
Definition at line 475 of file karrayul.h.
{ if (GetHeader()) GetHeader()->mBlockCount=pArrayCount; }
char* mBaseArray
[protected] |
Definition at line 480 of file karrayul.h.
TypeSize mTypeSize
[protected] |
Definition at line 481 of file karrayul.h.