Classes | Public Member Functions | Protected Member Functions | Protected Attributes

KBaseArrayFast< TypeSize > Class Template Reference

Search for all occurrences

Detailed Description

template<size_t TypeSize>
class KBaseArrayFast< TypeSize >

A fast implementation of KBaseArray.

It removes barriers which may prevent compiler generating optimized code.

Definition at line 490 of file karrayul.h.

#include <karrayul.h>

List of all members.

Classes

struct   KHeader

Public Member Functions

int  GetCount () const
  Get number of pointers in the array.
void  Clear ()
  Remove all pointers without deleting the associated objects.
void  Empty ()
  Fast empty, set object count to zero but don't free any memory.
int  Reserve (int pCapacity)
  Set array capacity to contain at least the specified number of elements without reallocating.
void  SetCount (int pCount)
  Set arrayCount to specified number of elements. The array capacity is adjusted accordingly.
void  Resize (int pItemCount)
void  AddMultiple (int pItemCount)

Protected Member Functions

  KBaseArrayFast ()
  Constructor.
  ~KBaseArrayFast ()
  Destructor.
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.
KHeader *const  GetHeader () const
KHeader GetHeader ()
int  GetHeaderOffset () const
int  GetArrayCount () const
void  SetArrayCount (int pArrayCount)
int  GetBlockCount () const
void  SetBlockCount (int pArrayCount)

Protected Attributes

int  mArrayCount
char *  mBaseArray

Constructor & Destructor Documentation

KBaseArrayFast ( ) [inline, protected]

Constructor.

Definition at line 683 of file karrayul.h.

~KBaseArrayFast ( ) [inline, protected]

Destructor.

Definition at line 690 of file karrayul.h.

                                    {
                Clear ();
            }

Member Function Documentation

int GetCount ( ) const [inline]

Get number of pointers in the array.

Returns:
The number of items in the array.

Definition at line 496 of file karrayul.h.

{ return GetArrayCount(); }
void Clear ( ) [inline]

Remove all pointers without deleting the associated objects.

Definition at line 499 of file karrayul.h.

void Empty ( ) [inline]

Fast empty, set object count to zero but don't free any memory.

Definition at line 511 of file karrayul.h.

            {
                #ifdef KFBX_PRIVATE
                #ifdef _DEBUG
                    memset( mBaseArray+ GetHeaderOffset() ,0,GetArrayCount()*TypeSize);
                #endif
            #endif
                SetArrayCount(0);
            }
int Reserve ( int  pCapacity ) [inline]

Set array capacity to contain at least the specified number of elements without reallocating.

Parameters:
pCapacity Number of items that can be stored in the array before reallocating the memory.
Returns:
The number of available slots in the array.
Remarks:
If capacity is lower than arrayCount, arrayCount is lowered to capacity.

Definition at line 527 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*TypeSize;
                    const kUInt lNewArraySize = lNewBlockCount*KFBX_ARRAYUL_BLOCKSIZE*TypeSize;

                    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*TypeSize, 0, (size_t) (lNewArraySize-pCapacity*TypeSize) );
                        SetArrayCount(pCapacity);
                    }

                    SetBlockCount(lNewBlockCount);
                }

                return GetBlockCount()*KFBX_ARRAYUL_BLOCKSIZE;
            }
void SetCount ( int  pCount ) [inline]

Set arrayCount to specified number of elements. The array capacity is adjusted accordingly.

Force the array of elements to a given size.

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 576 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]

Definition at line 597 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*TypeSize;
                const kUInt lNewArraySize   = lNewBlockCount*KFBX_ARRAYUL_BLOCKSIZE*TypeSize;

                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*TypeSize, 0, (size_t) (lNewArraySize-pItemCount*TypeSize) );

                SetBlockCount(lNewBlockCount);
                SetArrayCount(pItemCount);
            }
void AddMultiple ( int  pItemCount ) [inline]

Definition at line 629 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*TypeSize;
                    const kUInt lNewArraySize = lNewBlockCount*KFBX_ARRAYUL_BLOCKSIZE*TypeSize;

                    #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.

Reimplemented in KArrayTemplate< void * >.

Definition at line 701 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*TypeSize) + GetHeaderOffset() );
                    if(!lBaseArray)
                        return -1;
                    mBaseArray = lBaseArray;
                }

                if (pIndex<lArrayCount)
                {
                    // This is an insert
                    memmove (&(mBaseArray[(pIndex+1)*TypeSize+ GetHeaderOffset() ]), &(mBaseArray[(pIndex)*TypeSize+ GetHeaderOffset()] ), TypeSize*(lArrayCount-pIndex));
                }

                memmove (&(mBaseArray[(pIndex)*TypeSize+ GetHeaderOffset() ]), pItem, TypeSize);

                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 747 of file karrayul.h.

{ return &(mBaseArray[(pIndex)*TypeSize+ 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.

Reimplemented in KArrayTemplate< Type >, KArrayTemplate< kReference >, KArrayTemplate< HKFCurveNode >, KArrayTemplate< KFbxNode * >, KArrayTemplate< KArrayTemplate< KElement > * >, KArrayTemplate< KFbxLayerElement * >, KArrayTemplate< double >, KArrayTemplate< AccumulatorEntry * >, KArrayTemplate< KFbxProperty >, KArrayTemplate< KFbxReference * >, KArrayTemplate< KFbxPoseInfo * >, KArrayTemplate< int >, KArrayTemplate< KFbxDataType >, KArrayTemplate< ReaderPluginEntry * >, KArrayTemplate< AESequence * >, KArrayTemplate< InputData >, KArrayTemplate< KFbxPolygon >, KArrayTemplate< WriterPluginEntry * >, KArrayTemplate< KFbxObject * >, KArrayTemplate< KFbxLocalizationManager * >, KArrayTemplate< KStringListItem * >, KArrayTemplate< KFbxVector4 >, KArrayTemplate< KFbxScene * >, KArrayTemplate< void * >, KArrayTemplate< KFbxLayer * >, KArrayTemplate< Type * >, KArrayTemplate< KFbxMesh * >, KArrayTemplate< KArrayTemplate< int > * >, KArrayTemplate< NameCell * >, KArrayTemplate< KFbxTakeInfo * >, KArrayTemplate< KFbxDocument * >, KArrayTemplate< KFbxCluster * >, KArrayTemplate< KFbxTexture * >, KArrayTemplate< KFbxSurfaceMaterial * >, KArrayTemplate< KXRefManagerProject * >, KArrayTemplate< KString * >, and KArrayTemplate< KLayerInfo * >.

Definition at line 755 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)*TypeSize+ GetHeaderOffset() ]), &(mBaseArray[(pIndex+1)*TypeSize+ GetHeaderOffset() ]), TypeSize*(lArrayCount-pIndex-1));
                }

                SetArrayCount( lArrayCount-1 );

            #ifdef _DEBUG
                memset( &(mBaseArray[(GetArrayCount())*TypeSize+ GetHeaderOffset() ]),0,TypeSize);
            #endif
            }
bool ValidateIndex ( int  pIndex ) const [inline, protected]

Check that the given position is inside the array boundaries.

Parameters:
pIndex Index value to validate.
Returns:
true if the index value is within the array boundaries. false otherwise.

Definition at line 783 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;
                }
            }
KHeader* const GetHeader ( ) const [inline, protected]

Definition at line 799 of file karrayul.h.

            {
                return (KHeader* const)mBaseArray;
            }
KHeader* GetHeader ( ) [inline, protected]

Definition at line 803 of file karrayul.h.

            {
                return (KHeader*)mBaseArray;
            }
int GetHeaderOffset ( ) const [inline, protected]

Definition at line 807 of file karrayul.h.

            {
                return sizeof(KHeader);
            }
int GetArrayCount ( ) const [inline, protected]

Definition at line 811 of file karrayul.h.

            {
                return mArrayCount;
            }
void SetArrayCount ( int  pArrayCount ) [inline, protected]

Definition at line 815 of file karrayul.h.

            {
                mArrayCount = pArrayCount;
            }
int GetBlockCount ( ) const [inline, protected]

Definition at line 819 of file karrayul.h.

            {
                return GetHeader() ? GetHeader()->mBlockCount : 0;
            }
void SetBlockCount ( int  pArrayCount ) [inline, protected]

Definition at line 823 of file karrayul.h.

            {
                if (GetHeader()) GetHeader()->mBlockCount = pArrayCount;
            }

Member Data Documentation

int mArrayCount [protected]

Definition at line 829 of file karrayul.h.

char* mBaseArray [protected]

Definition at line 830 of file karrayul.h.


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