Classes | Protected Member Functions | Protected Attributes

KBaseArray< TypeSize > Class Template Reference

Search for all occurrences

Detailed Description

template<class TypeSize>
class KBaseArray< TypeSize >

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>

List of all members.

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

Constructor & Destructor Documentation

KBaseArray ( TypeSize  pTypeSize ) [inline, protected]

Constructor.

Parameters:
pTypeSize Size of one item of the array.

Definition at line 152 of file karrayul.h.

            : mTypeSize(pTypeSize)
        {
            mBaseArray  = NULL;
        }
~KBaseArray ( ) [inline, protected]

Destructor.

Definition at line 159 of file karrayul.h.

        {
            Clear();
        }

Member Function Documentation

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.

Returns:
The number of elements that conform the array's content.

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.

Remarks:
The destructor of the elements are not called.

Definition at line 174 of file karrayul.h.

void Empty ( ) [inline, protected]

All the elements of the array are removed, but the memory of content is not released.

Remarks:
The destructor of the elements are not called.

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.

Parameters:
pCapacity Minimum amount desired as capacity of allocated storage.
Returns:
The number of available slots in the array.
Remarks:
If capacity is lower than arrayCount, arrayCount is lowered to capacity.

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.

Parameters:
pCount New array size, expressed in elements.
Remarks:
If the array is upsized, the memory allocated is set to 0 and no constructor is called. Thus, this function is not appropriate for types of elements requiring initialization.

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.

Parameters:
pItemCount New array size, expressed in elements.
Remarks:
If the array is upsized, the memory allocated is set to 0 and no constructor is called. Thus, this function is not appropriate for types of elements requiring initialization.

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.

Parameters:
pItemCount The number of elements appended in elements.
Remarks:
The memory of new elements is set to 0 and no constructor is called. Thus, this function is not appropriate for types of elements requiring initialization.

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.

Parameters:
pIndex Position where to insert the item.
pItem Pointer to the item to be inserted.
Remarks:
if pIndex is greater than the number of items already in the array, the item will be appended at the end.
Returns:
The actual position where the item as been 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.

Parameters:
pIndex The position of the item to access.
Returns:
Pointer to the item.
Remarks:
This method assumes that the passed index is in the valid range of the array. No checks are made.

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.

Parameters:
pIndex The position of the item to remove.
Remarks:
If the index is not valid, nothing is performed. Otherwise, the item is removed from the array and the items are shifted to fill the empty slot.

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.

Parameters:
pIndex Index value to be validated.
Returns:
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]

Definition at line 459 of file karrayul.h.

        {
            return sizeof(KHeader);
        }
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;
        }

Member Data Documentation

char* mBaseArray [protected]

Definition at line 480 of file karrayul.h.

TypeSize mTypeSize [protected]

Definition at line 481 of file karrayul.h.


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