13 #ifndef _FBXSDK_CORE_BASE_ARRAY_H_
14 #define _FBXSDK_CORE_BASE_ARRAY_H_
33 FbxArray(
const int pCapacity) : mSize(0), mCapacity(0), mArray(
NULL){
if( pCapacity > 0 )
Reserve(pCapacity); }
48 inline int InsertAt(
const int pIndex,
const T& pElement,
bool pCompact=
false)
50 FBX_ASSERT_RETURN_VALUE(pIndex >= 0, -1);
51 int lIndex =
FbxMin(pIndex, mSize);
52 if( mSize >= mCapacity )
54 T lElement = pElement;
55 int lNewCapacity =
FbxMax(pCompact ? mCapacity + 1 : mCapacity * 2, 1);
56 T* lArray = Allocate(lNewCapacity);
57 FBX_ASSERT_RETURN_VALUE(lArray, -1);
59 mCapacity = lNewCapacity;
66 if( (&pElement >= &mArray[lIndex]) && (&pElement < &mArray[mSize]) )
68 T lElement = pElement;
71 memmove(&mArray[lIndex + 1], &mArray[lIndex], (mSize - lIndex) *
sizeof(T));
74 memcpy(&mArray[lIndex], &pElement,
sizeof(T));
83 inline int Add(
const T& pElement)
93 int lIndex =
Find(pElement);
94 return ( lIndex == -1 ) ?
Add(pElement) : lIndex;
102 return InsertAt(mSize, pElement,
true);
108 inline int Size()
const {
return mSize; }
122 FBX_ASSERT_MSG(pIndex >= 0,
"Index is out of range!");
123 if( pIndex >= mSize )
125 if( pIndex < mCapacity )
127 FBX_ASSERT_NOW(
"Index is out of range, but not outside of capacity! Call SetAt() to use reserved memory.");
129 else FBX_ASSERT_NOW(
"Index is out of range!");
132 return (T&)mArray[pIndex];
139 inline T
GetAt(
const int pIndex)
const
157 return GetAt(mSize-1);
164 inline int Find(
const T& pElement,
const int pStartIndex=0)
const
166 FBX_ASSERT_RETURN_VALUE(pStartIndex >= 0, -1);
167 for(
int i = pStartIndex; i < mSize; ++i )
169 if(
operator[](i) == pElement )
return i;
180 for(
int i =
FbxMin(pStartIndex, mSize-1); i >= 0; --i )
182 if(
operator[](i) == pElement )
return i;
193 FBX_ASSERT_RETURN_VALUE(pCapacity > 0,
false);
194 if( pCapacity > mCapacity )
196 T* lArray = Allocate(pCapacity);
197 FBX_ASSERT_RETURN_VALUE(lArray,
false);
199 mCapacity = pCapacity;
202 memset(&mArray[mSize], 0, (mCapacity - mSize) *
sizeof(T));
212 inline void SetAt(
const int pIndex,
const T& pElement)
214 FBX_ASSERT_RETURN(pIndex < mCapacity);
215 if( pIndex >= mSize ) mSize = pIndex + 1;
216 if( mArray ) memcpy(&mArray[pIndex], &pElement,
sizeof(T));
232 SetAt(mSize-1, pElement);
241 T lElement =
GetAt(pIndex);
242 if( pIndex + 1 < mSize )
244 memmove(&mArray[pIndex], &mArray[pIndex + 1], (mSize - pIndex - 1) *
sizeof(T));
271 int Index =
Find(pElement);
286 if( pIndex + pCount < mSize )
288 memmove(&mArray[pIndex], &mArray[pIndex + pCount], (mSize - pIndex - pCount) *
sizeof(T));
299 if( pSize == mSize && mSize == mCapacity )
return true;
307 FBX_ASSERT_RETURN_VALUE(pSize > 0,
false);
308 if( pSize != mCapacity )
310 T* lArray = Allocate(pSize);
311 FBX_ASSERT_RETURN_VALUE(lArray,
false);
315 if( pSize > mCapacity )
317 memset(&mArray[mSize], 0, (pSize - mSize) *
sizeof(T));
328 inline bool Grow(
const int pSize)
330 return Resize(mSize + pSize);
338 return Resize(mSize - pSize);
365 qsort(mArray, mSize,
sizeof(T), pCompareFunc);
372 inline operator T* (){
return mArray ? (T*)mArray :
NULL; }
378 if(
Grow(pOther.mSize) )
380 memcpy(&mArray[mSize - pOther.mSize], pOther.mArray, pOther.mSize *
sizeof(T));
388 for(
int i = 0, c = pOther.mSize; i < c; ++i )
398 for(
int i = 0, c = pOther.mSize; i < c; ++i )
408 if(
this != &pOther )
410 if(
Resize(pOther.mSize) )
412 memcpy(mArray, pOther.mArray, pOther.mSize *
sizeof(T));
422 if(
this == &pOther )
return true;
423 if( mSize != pOther.mSize )
return false;
424 return memcmp(mArray, pOther.mArray,
sizeof(T) * mSize) == 0;
430 #ifndef DOXYGEN_SHOULD_SKIP_THIS
431 inline int GetCount()
const {
return mSize; }
434 inline T* Allocate(
const int pCapacity)
436 return (T*)FbxRealloc(mArray, pCapacity *
sizeof(T));
443 #if defined(FBXSDK_COMPILER_MSC)
446 FBX_ASSERT_STATIC(
FBXSDK_IS_SIMPLE_TYPE(T) || __is_enum(T) || (__has_trivial_constructor(T)&&__has_trivial_destructor(T)) || !FBXSDK_IS_INCOMPATIBLE_WITH_ARRAY(T));
455 for(
int i = 0, c = pArray.
Size(); i < c; ++i )
465 for(
int i = 0, c = pArray.
Size(); i < c; ++i )
475 for(
int i = 0, c = pArray.
Size(); i < c; ++i )
477 (pArray[i])->Destroy();
bool RemoveIt(const T &pElement)
Remove first matching element in the array.
FBX SDK environment definition.
T & operator[](const int pIndex) const
Retrieve a reference of the element at given index position in the array.
void FbxArrayDelete(FbxArray< T > &pArray)
Call FbxDelete on each element of the array, and then clear it.
FBXSDK_INCOMPATIBLE_WITH_ARRAY_TEMPLATE(FbxArray< T >)
Make sure to break build if someone try to make FbxArray< />>, which is not supported...
void FbxArrayDestroy(FbxArray< T > &pArray)
Call Destroy on each element of the array, and then clear it.
int Size() const
Retrieve the number of element contained in the array.
bool Grow(const int pSize)
Increase size of array by the specified size.
bool Compact()
Compact the array so that its capacity is the same as its size.
int Capacity() const
Retrieve the current allocated memory capacity of the array.
void Sort(CompareFunc pCompareFunc)
Sort the array using the specified compare function pointer.
T RemoveAt(const int pIndex)
Remove an element at the given position in the array.
T GetAt(const int pIndex) const
Retrieve a copy of the element at given index position in the array.
void FbxDelete(T *p)
Deletion policy for pointer template classes that uses the FbxDelete() function.
int AddCompact(const T &pElement)
Append an element at the end of the array, growing the array by one element if capacity is not suffic...
bool Shrink(const int pSize)
Reduce size of array by the specified size.
void RemoveArray(const FbxArray< T > &pOther)
Remove the elements of another array from this array is they are present.
T RemoveFirst()
Remove the first element in the array.
int(* CompareFunc)(const void *, const void *)
Element compare function pointer definition.
int AddUnique(const T &pElement)
Append an element at the end of array, if not already present, doubling the array if capacity is not ...
void Clear()
Reset the number of element to zero and free the memory allocated.
int Add(const T &pElement)
Append an element at the end of the array, doubling the array if capacity is not sufficient.
FbxArray(const int pCapacity)
Reserve constructor.
void RemoveRange(const int pIndex, const int pCount)
Remove a range of elements at the given position in the array.
void SetAt(const int pIndex, const T &pElement)
Set the element at given position in the array.
bool operator==(const FbxArray< T > &pOther) const
Operator to compare elements of an array.
const FbxChar FbxMin(const FbxChar)
void AddArrayNoDuplicate(const FbxArray< T > &pOther)
Append the elements of another array at the end of this array if they are not present.
FbxArray< T > & operator=(const FbxArray< T > &pOther)
Operator to copy elements of an array.
#define FBXSDK_IS_SIMPLE_TYPE(T)
void FbxArrayFree(FbxArray< T > &pArray)
Call FbxFree on each element of the array, and then clear it.
T * GetArray() const
Get pointer to internal array of elements.
void SetFirst(const T &pElement)
Set the value of the first element.
T RemoveLast()
Remove the last element in the array.
const FbxChar FbxMax(const FbxChar)
void SetLast(const T &pElement)
Set the value of the last element.
bool Resize(const int pSize)
Inserts or erases elements at the end such that Size() becomes pSize, increasing capacity if needed...
bool Reserve(const int pCapacity)
Request for allocation of additional memory without inserting new elements.
int InsertAt(const int pIndex, const T &pElement, bool pCompact=false)
Insert an element at the given position, growing the array if capacity is not sufficient.
FbxArray(const FbxArray &pArray)
Copy constructor.
int FindReverse(const T &pElement, const int pStartIndex=0x7fffffff) const
Find first matching element, from last to first.
T GetLast() const
Retrieve a copy of the last element.
void AddArray(const FbxArray< T > &pOther)
Append another array at the end of this array.
Class for array of basic elements such as pointers and basic types.
T GetFirst() const
Retrieve a copy of the first element.
int Find(const T &pElement, const int pStartIndex=0) const
Find first matching element, from first to last.