FbxArray< T > Class Template Reference
 
 
 
FbxArray< T > Class Template Reference

This reference page is linked to from the following overview topics: FBX SDK 2014.


#include <fbxarray.h>


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

Examples:

ExportScene03/main.cxx, SwitchBinding/main.cxx, ViewScene/main.cxx, ViewScene/SceneCache.h, ViewScene/SceneContext.cxx, and ViewScene/SceneContext.h.

Definition at line 23 of file fbxarray.h.

List of all members.

Public Member Functions

  FbxArray ()
  Constructor.
  FbxArray (const FbxArray &pArray)
  Copy constructor.
  FbxArray (int pCapacity)
  Reserve 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.

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 ( int  pCapacity ) [inline]

Reserve constructor.

Definition at line 33 of file fbxarray.h.

: mSize(0), mCapacity(0), mArray(NULL){ if( pCapacity > 0 ) Reserve(pCapacity); }
~FbxArray ( ) [inline]

Destructor.

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

Definition at line 37 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:
pIndex Position where to insert the element. Must be a positive value.
pElement Element 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 44 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:
pElement Element to append to the array.
Returns:
-1 if add failed, otherwise the position of the added element in the array.

Definition at line 71 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:
pElement Element to append to the array.
Returns:
-1 if add failed, otherwise the position of the added element in the array.

Definition at line 79 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 88 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 93 of file fbxarray.h.

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

Retrieve the element at given index position in the array.

Parameters:
pIndex Position 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 99 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:
pIndex Position 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 119 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 127 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 135 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:
pElement The element to be compared to each of the elements.
pStartIndex The position to start searching from.
Returns:
Position of first matching element or -1 if there is no matching element.

Definition at line 144 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:
pElement The element to be compared to each of the elements.
pStartIndex The position to start searching from.
Returns:
Position of first matching element or -1 if there is no matching element.

Definition at line 158 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:
pCapacity The 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 171 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:
pIndex Position of element in the array.
pElement The 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 192 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:
pElement The 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 205 of file fbxarray.h.

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

Set the value of the last element.

Parameters:
pElement The 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 213 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:
pIndex Position of the element to remove.
Returns:
Removed element.
Remarks:
No error will be thrown if the index is out of bounds.

Definition at line 222 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 236 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 244 of file fbxarray.h.

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

Remove first matching element in the array.

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

Definition at line 252 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:
pSize The 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 267 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:
pSize The 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 297 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 304 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 316 of file fbxarray.h.

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

Cast operator.

Definition at line 319 of file fbxarray.h.

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

Append another array at the end of this array.

Parameters:
pOther The other array to append to this array.

Definition at line 323 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:
pArray Another array.

Definition at line 333 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:
pArray Another array.

Definition at line 343 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 353 of file fbxarray.h.

        {
                if( this != &pOther )
                {
                        if( Resize(pOther.mSize) )
                        {
                                memcpy(mArray, pOther.mArray, pOther.mSize * sizeof(T));
                        }
                }
                return *this;
        }

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