Public Member Functions

FbxArray< T > Class Template Reference

Search for all occurrences

Detailed Description

template<class T>
class FbxArray< T >

Class for array of basic elements such as pointers and basic types.

This class will not call constructor and destructor for elements, thus it is not suitable for object references. Memory allocations are always done in a single contiguous memory region.

Definition at line 23 of file fbxarray.h.

#include <fbxarray.h>

List of all members.

Public Member Functions

 FbxArray ()
 Constructor.
 FbxArray (const FbxArray &pArray)
 Copy constructor.
 ~FbxArray ()
 Destructor.
int InsertAt (const int pIndex, const T pElement)
 Insert an element at the given position, growing the array if capacity is not sufficient.
int Add (const T pElement)
 Append an element at the end of the array, growing the array if capacity is not sufficient.
int AddUnique (const T pElement)
 Append an element at the end of array, if not already present, growing the array if capacity is not sufficient.
int Size () const
 Retrieve the number of element contained in the array.
int Capacity () const
 Retrieve the current allocated memory capacity of the array.
T & operator[] (int pIndex) const
 Retrieve the element at given index position in the array.
GetAt (const int pIndex) const
 Returns the value of the element at given position in the array.
GetFirst () const
 Get the first element.
GetLast () const
 Get the last element.
int Find (const T pElement, const int pStartIndex=0) const
 Find first matching element, from first to last.
int FindReverse (const T pElement, const int pStartIndex=FBXSDK_INT_MAX) const
 Find first matching element, from last to first.
bool Reserve (const int pCapacity)
 Request for allocation of additional memory without inserting new elements.
void SetAt (const int pIndex, const T pElement)
 Set the element at given position in the array.
void SetFirst (const T pElement)
 Set the value of the first element.
void SetLast (const T pElement)
 Set the value of the last element.
RemoveAt (const int pIndex)
 Remove an element at the given position in the array.
RemoveFirst ()
 Remove the first element in the array.
RemoveLast ()
 Remove the last element in the array.
bool RemoveIt (const T pElement)
 Remove first matching element in the array.
bool Resize (const int pSize)
 Inserts or erases elements at the end such that Size() becomes pSize, increasing capacity if needed.
bool Grow (const int pSize)
 Inserts elements at the end such that Size() will be increased to pSize, increasing capacity if needed.
void Clear ()
 Reset the number of element to zero and free the memory allocated.
T * GetArray () const
 Get pointer to internal array of elements.
 operator T * ()
 Cast operator.
void AddArray (const FbxArray< T > &pOther)
 Append another array at the end of this array.
void AddArrayNoDuplicate (const FbxArray< T > &pOther)
 Append the elements of another array at the end of this array if they are not present.
void RemoveArray (const FbxArray< T > &pOther)
 Remove the elements of another array from this array is they are present.
FbxArray< T > & operator= (const FbxArray< T > &pOther)
 Operator to copy elements of an array.
int GetCount () const
FBX_DEPRECATED void Empty ()
FBX_DEPRECATED int FindAfter (int pAfterIndex, T pItem) const
FBX_DEPRECATED int FindBefore (int pBeforeIndex, T pItem) const
FBX_DEPRECATED void AddMultiple (int pItemCount)

Constructor & Destructor Documentation

FbxArray ( ) [inline]

Constructor.

Definition at line 27 of file fbxarray.h.

: mSize(0), mCapacity(0), mArray(NULL){}
FbxArray ( const FbxArray< T > &  pArray) [inline]

Copy constructor.

Definition at line 30 of file fbxarray.h.

: mSize(0), mCapacity(0), mArray(NULL){ *this = pArray; }
~FbxArray ( ) [inline]

Destructor.

Remarks:
The destructor for each element will not be called.

Definition at line 34 of file fbxarray.h.

{ Clear(); }

Member Function Documentation

int InsertAt ( const int  pIndex,
const T  pElement 
) [inline]

Insert an element at the given position, growing the array if capacity is not sufficient.

Parameters:
pIndexPosition where to insert the element. Must be a positive value.
pElementElement to insert in the array.
Returns:
-1 if insert failed, otherwise the position of the inserted element in the array.
Remarks:
If the given index is greater than Size(), the element is appended at the end.

Definition at line 41 of file fbxarray.h.

    {
        FBX_ASSERT_RETURN_VALUE(pIndex >= 0, -1);
        int lIndex = FbxMin(pIndex, mSize);
        if( mSize >= mCapacity )
        {
            int lNewCapacity = FbxMax(mCapacity * 2, 1);    //Double capacity
            T* lArray = Allocate(lNewCapacity);
            FBX_ASSERT_RETURN_VALUE(lArray, -1);
            mArray = lArray;
            mCapacity = lNewCapacity;
        }

        if( lIndex < mSize )    //Move elements to leave a space open to insert the new element
        {
            memmove(&mArray[lIndex + 1], &mArray[lIndex], (mSize - lIndex) * sizeof(T));
        }

        memcpy(&mArray[lIndex], &pElement, sizeof(T));
        mSize++;

        return lIndex;
    }
int Add ( const T  pElement) [inline]

Append an element at the end of the array, growing the array if capacity is not sufficient.

Parameters:
pElementElement to append to the array.
Returns:
-1 if add failed, otherwise the position of the added element in the array.

Definition at line 68 of file fbxarray.h.

    {
        return InsertAt(mSize, pElement);
    }
int AddUnique ( const T  pElement) [inline]

Append an element at the end of array, if not already present, growing the array if capacity is not sufficient.

Parameters:
pElementElement to append to the array.
Returns:
-1 if add failed, otherwise the position of the added element in the array.

Definition at line 76 of file fbxarray.h.

    {
        int lIndex = Find(pElement);
        return ( lIndex == -1 ) ? Add(pElement) : lIndex;
    }
int Size ( ) const [inline]

Retrieve the number of element contained in the array.

To increase the capacity without increasing the size, please use Reserve().

Returns:
The number of element in the array.
Remarks:
The size of the array cannot exceed its capacity.

Definition at line 85 of file fbxarray.h.

{ return mSize; }
int Capacity ( ) const [inline]

Retrieve the current allocated memory capacity of the array.

Returns:
The capacity of the array in number of element.
Remarks:
The capacity will always be greater or equal to its size.

Definition at line 90 of file fbxarray.h.

{ return mCapacity; }
T& operator[] ( int  pIndex) const [inline]

Retrieve the element at given index position in the array.

Parameters:
pIndexPosition of element in the array.
Returns:
A reference to the element at the specified position in the array.
Remarks:
No error will be thrown if the index is out of bounds.

Definition at line 96 of file fbxarray.h.

    {
    #ifdef _DEBUG
        FBX_ASSERT_MSG(pIndex >= 0, "Index is out of range!");
        if( pIndex >= mSize )
        {
            if( pIndex < mCapacity )
            {
                FBX_ASSERT_NOW("Index is out of range, but not outside of capacity! Call SetAt() to use reserved memory.");
            }
            else FBX_ASSERT_NOW("Index is out of range!");
        }
    #endif
        return (T&)mArray[pIndex];
    }
T GetAt ( const int  pIndex) const [inline]

Returns the value of the element at given position in the array.

Parameters:
pIndexPosition of element in the array.
Returns:
The value of the element at the specified position in the array.
Remarks:
No error will be thrown if the index is out of bounds.

Definition at line 116 of file fbxarray.h.

    {
        return operator[](pIndex);
    }
T GetFirst ( ) const [inline]

Get the first element.

Returns:
The first element.
Remarks:
The array should have at least one element and no error will be thrown if the array is empty.

Definition at line 124 of file fbxarray.h.

    {
        return GetAt(0);
    }
T GetLast ( ) const [inline]

Get the last element.

Returns:
The last element.
Remarks:
The array should have at least one element and no error will be thrown if the array is empty.

Definition at line 132 of file fbxarray.h.

    {
        return GetAt(mSize-1);
    }
int Find ( const T  pElement,
const int  pStartIndex = 0 
) const [inline]

Find first matching element, from first to last.

Parameters:
pElementThe element to be compared to each of the elements.
pStartIndexThe position to start searching from.
Returns:
Position of first matching element or -1 if there is no matching element.

Definition at line 141 of file fbxarray.h.

    {
        FBX_ASSERT_RETURN_VALUE(pStartIndex >= 0, -1);
        for( int i = pStartIndex; i < mSize; ++i )
        {
            if( GetAt(i) == pElement ) return i;
        }
        return -1;
    }
int FindReverse ( const T  pElement,
const int  pStartIndex = FBXSDK_INT_MAX 
) const [inline]

Find first matching element, from last to first.

Parameters:
pElementThe element to be compared to each of the elements.
pStartIndexThe position to start searching from.
Returns:
Position of first matching element or -1 if there is no matching element.

Definition at line 155 of file fbxarray.h.

    {
        for( int i = FbxMin(pStartIndex, mSize-1); i >= 0; --i )
        {
            if( GetAt(i) == pElement ) return i;
        }
        return -1;
    }
bool Reserve ( const int  pCapacity) [inline]

Request for allocation of additional memory without inserting new elements.

After the memory has been reserved, please use SetAt() to initialize elements.

Parameters:
pCapacityThe number of additional element memory allocation requested.
Returns:
true if the memory allocation succeeded or if the capacity is unchanged, false otherwise.
Remarks:
If the requested capacity is less than or equal to the current capacity, this call has no effect. In either case, Size() is unchanged.

Definition at line 168 of file fbxarray.h.

    {
        FBX_ASSERT_RETURN_VALUE(pCapacity > 0, false);
        if( pCapacity > mCapacity )
        {
            T* lArray = Allocate(pCapacity);
            FBX_ASSERT_RETURN_VALUE(lArray, false);
            mArray = lArray;
            mCapacity = pCapacity;

            //Initialize new memory to zero
            memset(&mArray[mSize], 0, (mCapacity - mSize) * sizeof(T));
        }
        return true;
    }
void SetAt ( const int  pIndex,
const T  pElement 
) [inline]

Set the element at given position in the array.

Parameters:
pIndexPosition of element in the array.
pElementThe new element.
Remarks:
If the index is outside range, and outside capacity, this call has no effect. However, if index is within capacity range, element count is increased such that Size() will become pIndex + 1.

Definition at line 189 of file fbxarray.h.

    {
        if( pIndex >= mSize )
        {
            FBX_ASSERT_RETURN(pIndex < mCapacity);
            mSize = pIndex + 1;
        }
        memcpy(&mArray[pIndex], &pElement, sizeof(T));
    }
void SetFirst ( const T  pElement) [inline]

Set the value of the first element.

Parameters:
pElementThe new value of the last element.
Remarks:
The array should have at least one element and no error will be thrown if the array is empty.

Definition at line 202 of file fbxarray.h.

    {
        SetAt(0, pElement);
    }
void SetLast ( const T  pElement) [inline]

Set the value of the last element.

Parameters:
pElementThe new value of the last element.
Remarks:
The array should have at least one element and no error will be thrown if the array is empty.

Definition at line 210 of file fbxarray.h.

    {
        SetAt(mSize-1, pElement);
    }
T RemoveAt ( const int  pIndex) [inline]

Remove an element at the given position in the array.

Parameters:
pIndexPosition of the element to remove.
Returns:
Removed element.
Remarks:
No error will be thrown if the index is out of bounds.

Definition at line 219 of file fbxarray.h.

    {
        T lElement = GetAt(pIndex);
        if( pIndex + 1 < mSize )
        {
            memmove(&mArray[pIndex], &mArray[pIndex + 1], (mSize - pIndex - 1) * sizeof(T));
        }
        mSize--;
        return lElement;
    }
T RemoveFirst ( ) [inline]

Remove the first element in the array.

Returns:
Removed element.
Remarks:
The array should have at least one element and no error will be thrown if the array is empty.

Definition at line 233 of file fbxarray.h.

    {
        return RemoveAt(0);
    }
T RemoveLast ( ) [inline]

Remove the last element in the array.

Returns:
Removed element.
Remarks:
The array should have at least one element and no error will be thrown if the array is empty.

Definition at line 241 of file fbxarray.h.

    {
        return RemoveAt(mSize-1);
    }
bool RemoveIt ( const T  pElement) [inline]

Remove first matching element in the array.

Parameters:
pElementElement to be removed.
Returns:
true if a matching element is found and removed, false otherwise.

Definition at line 249 of file fbxarray.h.

    {
        int Index = Find(pElement);
        if( Index >= 0 )
        {
            RemoveAt(Index);
            return true;
        }
        return false;
    }
bool Resize ( const int  pSize) [inline]

Inserts or erases elements at the end such that Size() becomes pSize, increasing capacity if needed.

Please use SetAt() to initialize any new elements.

Parameters:
pSizeThe new count of elements to set the array to. Must be greater or equal to zero.
Returns:
true if the memory (re)allocation succeeded, false otherwise.
Remarks:
If the requested element count is less than or equal to the current count, elements are freed from memory. Otherwise, the array grows and elements are unchanged.

Definition at line 264 of file fbxarray.h.

    {
        if( pSize == 0 )
        {
            Clear();
            return true;
        }

        FBX_ASSERT_RETURN_VALUE(pSize > 0, false);
        if( pSize != mCapacity )
        {
            T* lArray = Allocate(pSize);
            FBX_ASSERT_RETURN_VALUE(lArray, false);
            mArray = lArray;
        }

        if( pSize > mCapacity ) //Initialize new memory to zero
        {
            memset(&mArray[mSize], 0, (pSize - mSize) * sizeof(T));
        }

        mSize = pSize;
        mCapacity = pSize;

        return true;
    }
bool Grow ( const int  pSize) [inline]

Inserts elements at the end such that Size() will be increased to pSize, increasing capacity if needed.

Please use SetAt() to initialize new elements.

Parameters:
pSizeThe number of elements to insert into the array. Must be greater or equal to zero.
Returns:
true if the memory (re)allocation succeeded, false otherwise.

Definition at line 294 of file fbxarray.h.

    {
        return Resize(mSize + pSize);
    }
void Clear ( ) [inline]

Reset the number of element to zero and free the memory allocated.

Remarks:
This only free the memory allocated by the array, and doesn't call the destructor of each element.

Definition at line 301 of file fbxarray.h.

    {
        if( mArray != NULL )
        {
            mSize = 0;
            mCapacity = 0;
            FbxFree(mArray);
            mArray = NULL;
        }
    }
T* GetArray ( ) const [inline]

Get pointer to internal array of elements.

Definition at line 313 of file fbxarray.h.

{ return mArray ? (T*)mArray : NULL; }
operator T * ( ) [inline]

Cast operator.

Definition at line 316 of file fbxarray.h.

{ return (T*)mArray; }
void AddArray ( const FbxArray< T > &  pOther) [inline]

Append another array at the end of this array.

Parameters:
pOtherThe other array to append to this array.

Definition at line 320 of file fbxarray.h.

    {
        if( Grow(pOther.mSize) )
        {
            memcpy(&mArray[mSize - pOther.mSize], pOther.mArray, pOther.mSize * sizeof(T));
        }
    }
void AddArrayNoDuplicate ( const FbxArray< T > &  pOther) [inline]

Append the elements of another array at the end of this array if they are not present.

Parameters:
pArrayAnother array.

Definition at line 330 of file fbxarray.h.

    {
        for( int i = 0, c = pOther.mSize; i < c; ++i )
        {
            AddUnique(pOther[i]);
        }
    }
void RemoveArray ( const FbxArray< T > &  pOther) [inline]

Remove the elements of another array from this array is they are present.

Parameters:
pArrayAnother array.

Definition at line 340 of file fbxarray.h.

    {
        for( int i = 0, c = pOther.mSize; i < c; ++i )
        {
            RemoveIt(pOther[i]);
        }
    }
FbxArray<T>& operator= ( const FbxArray< T > &  pOther) [inline]

Operator to copy elements of an array.

Returns:
this array containing a copy of pOther elements.

Definition at line 350 of file fbxarray.h.

    {
        if( this != &pOther )
        {
            if( Resize(pOther.mSize) )
            {
                memcpy(mArray, pOther.mArray, pOther.mSize * sizeof(T));
            }
        }
        return *this;
    }
int GetCount ( ) const [inline]

Definition at line 366 of file fbxarray.h.

{ return mSize; }
FBX_DEPRECATED void Empty ( ) [inline]

Definition at line 367 of file fbxarray.h.

{ Clear(); }
FBX_DEPRECATED int FindAfter ( int  pAfterIndex,
pItem 
) const [inline]

Definition at line 368 of file fbxarray.h.

{ return Find(pItem, pAfterIndex+1); }
FBX_DEPRECATED int FindBefore ( int  pBeforeIndex,
pItem 
) const [inline]

Definition at line 369 of file fbxarray.h.

{ return FindReverse(pItem, pBeforeIndex-1); }
FBX_DEPRECATED void AddMultiple ( int  pItemCount) [inline]

Definition at line 370 of file fbxarray.h.

{ Grow(pItemCount); }

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