13 #ifndef _FBXSDK_CORE_BASE_DYNAMICARRAY_H_
14 #define _FBXSDK_CORE_BASE_DYNAMICARRAY_H_
26 template <
typename Type,
typename Allocator=FbxBaseAllocator>
class FbxDynamicArray
34 mAllocator(sizeof(Type))
44 mAllocator(sizeof(Type))
58 mAllocator(sizeof(Type))
61 CopyArray(mArray, pArray.mArray, pArray.mSize);
68 for(
size_t i = 0; i < mSize; ++i )
72 mAllocator.FreeMemory(mArray);
91 if( pCount > mCapacity )
94 Type* lNewArray = (Type*)mAllocator.AllocateRecords(pCount);
95 MoveArray(lNewArray, mArray, mSize);
96 mAllocator.FreeMemory(mArray);
105 void PushBack(
const Type& pItem,
const size_t pNCopies = 1)
107 if( mSize + pNCopies > mCapacity )
109 size_t lNewSize = mCapacity + mCapacity / 2;
110 if( mSize + pNCopies > lNewSize )
112 lNewSize = mSize + pNCopies;
116 FBX_ASSERT(mSize + pNCopies <= mCapacity);
117 Fill(mArray + mSize, pItem, pNCopies);
125 void Insert(
const size_t pIndex,
const Type& pItem,
const size_t pNCopies=1)
127 FBX_ASSERT(pIndex >= 0);
128 FBX_ASSERT(pIndex <= mSize);
133 else if( pIndex >= mSize )
137 else if( mSize + pNCopies > mCapacity )
139 size_t lNewSize = mCapacity + mCapacity / 2;
140 if( mSize + pNCopies > lNewSize )
142 lNewSize = mSize + pNCopies;
145 Type* lNewArray = (Type*)mAllocator.AllocateRecords(lNewSize);
146 MoveArray(lNewArray, mArray, pIndex);
147 Fill(lNewArray + pIndex, pItem, pNCopies);
148 MoveArray(lNewArray + pIndex + pNCopies, mArray + pIndex, mSize - pIndex);
149 mAllocator.FreeMemory(mArray);
152 mCapacity = lNewSize;
157 MoveArrayBackwards(mArray + pIndex + pNCopies, mArray + pIndex, mSize - pIndex);
158 Fill(mArray + pIndex, pItem, pNCopies);
167 FBX_ASSERT(pNElements <= mSize);
168 for(
size_t i = mSize - pNElements; i < mSize; ++i )
178 void Remove(
const size_t pIndex,
size_t pNElements=1)
180 FBX_ASSERT(pIndex >= 0);
181 FBX_ASSERT(pIndex <= mSize);
182 FBX_ASSERT(pIndex + pNElements <= mSize);
183 if( pIndex + pNElements >= mSize )
189 for(
size_t i = pIndex; i < pIndex + pNElements; ++i )
193 MoveOverlappingArray(&mArray[pIndex], &mArray[pIndex + pNElements], mSize - pIndex - pNElements);
202 return mArray[pIndex];
209 return mArray[pIndex];
244 size_t Find(
const Type& pItem,
const size_t pStartIndex=0)
const
246 for(
size_t i = pStartIndex; i < mSize; ++i )
248 if(
operator[](i) == pItem )
return i;
258 CopyArray(mArray, pArray.mArray, pArray.mSize);
259 mSize = pArray.mSize;
266 #ifndef DOXYGEN_SHOULD_SKIP_THIS
268 static void CopyArray(Type* pDest,
const Type* pSrc,
size_t pCount)
270 for(
int i = 0; i < int(pCount); i++ )
272 new(&(pDest[i])) Type(pSrc[i]);
276 static void MoveArray(Type* pDest,
const Type* pSrc,
size_t pCount)
278 for(
int i = 0; i < int(pCount); i++ )
280 new(&(pDest[i])) Type(pSrc[i]);
283 for(
int i = 0; i < int(pCount); i++ )
289 static void MoveOverlappingArray(Type* pDest,
const Type* pSrc,
size_t pCount)
291 for(
int i = 0; i < int(pCount); i++ )
293 new(&(pDest[i])) Type(pSrc[i]);
298 static void MoveArrayBackwards(Type* pDest,
const Type* pSrc,
size_t pCount)
300 for(
int i = 0; i < int(pCount); ++i )
302 new(&(pDest[pCount-1-i])) Type(pSrc[pCount-1-i]);
303 pSrc[pCount-1-i].~Type();
307 static void Fill(Type* pDest,
const Type& pItem,
size_t pCount)
309 for(
int i = 0; i < int(pCount); i++ )
311 new(&(pDest[i])) Type(pItem);
318 Allocator mAllocator;
Type & operator[](const size_t pIndex)
Gets nth object in the array.
FBX SDK environment definition.
const Type & operator[](const size_t pIndex) const
Gets nth object in the array.
FbxDynamicArray()
Default constructor.
FbxDynamicArray & operator=(const FbxDynamicArray &pArray)
Assignment operator.
void Insert(const size_t pIndex, const Type &pItem, const size_t pNCopies=1)
Inserts n objects at the specified position.
FbxDynamicArray(const FbxDynamicArray &pArray)
Copy constructor.
void Remove(const size_t pIndex, size_t pNElements=1)
Removes n objects at the specified position.
size_t Find(const Type &pItem, const size_t pStartIndex=0) const
Find first matching element, from first to last.
FbxDynamicArray(const size_t pInitialSize)
Constructor.
~FbxDynamicArray()
Destructor.
const Type & Last() const
Retrieve the last item in the array.
size_t Capacity() const
Gets the current capacity of the array.
Template class for dynamic array holding objects.
const Type & First() const
Retrieve the first item in the array.
void PushBack(const Type &pItem, const size_t pNCopies=1)
Appends n objects at the end of the array.
void Reserve(const size_t pCount)
Assures that sufficient memory is allocated to hold n objects in the array, and increases the capacit...
Type & Last()
Retrieve the last item in the array.
size_t Size() const
Gets the size of the array.
void PopBack(size_t pNElements=1)
Removes n objects at the end.
Type & First()
Retrieve the first item in the array.