FBX C++ API Reference
FbxDynamicArray< Type, Allocator > Class Template Reference

#include <fbxdynamicarray.h>

Class Description

template<typename Type, typename Allocator = FbxBaseAllocator>
class FbxDynamicArray< Type, Allocator >

Template class for dynamic array holding objects.

See also
FbxStaticArray

Definition at line 26 of file fbxdynamicarray.h.

Public Member Functions

 FbxDynamicArray ()
 Default constructor. More...
 
 FbxDynamicArray (const size_t pInitialSize)
 Constructor. More...
 
 FbxDynamicArray (const FbxDynamicArray &pArray)
 Copy constructor. More...
 
 ~FbxDynamicArray ()
 Destructor. More...
 
size_t Capacity () const
 Gets the current capacity of the array. More...
 
size_t Size () const
 Gets the size of the array. More...
 
void Reserve (const size_t pCount)
 Assures that sufficient memory is allocated to hold n objects in the array, and increases the capacity if necessary. More...
 
void PushBack (const Type &pItem, const size_t pNCopies=1)
 Appends n objects at the end of the array. More...
 
void Insert (const size_t pIndex, const Type &pItem, const size_t pNCopies=1)
 Inserts n objects at the specified position. More...
 
void PopBack (size_t pNElements=1)
 Removes n objects at the end. More...
 
void Remove (const size_t pIndex, size_t pNElements=1)
 Removes n objects at the specified position. More...
 
Type & operator[] (const size_t pIndex)
 Gets nth object in the array. More...
 
const Type & operator[] (const size_t pIndex) const
 Gets nth object in the array. More...
 
Type & First ()
 Retrieve the first item in the array. More...
 
const Type & First () const
 Retrieve the first item in the array. More...
 
Type & Last ()
 Retrieve the last item in the array. More...
 
const Type & Last () const
 Retrieve the last item in the array. More...
 
size_t Find (const Type &pItem, const size_t pStartIndex=0) const
 Find first matching element, from first to last. More...
 
FbxDynamicArrayoperator= (const FbxDynamicArray &pArray)
 Assignment operator. More...
 

Constructor & Destructor Documentation

◆ FbxDynamicArray() [1/3]

FbxDynamicArray ( )
inline

Default constructor.

Definition at line 30 of file fbxdynamicarray.h.

30  :
31  mArray(NULL),
32  mCapacity(0),
33  mSize(0),
34  mAllocator(sizeof(Type))
35  {
36  }
#define NULL
Definition: fbxarch.h:210

◆ FbxDynamicArray() [2/3]

FbxDynamicArray ( const size_t  pInitialSize)
inline

Constructor.

Parameters
pInitialSizeinitial capacity of this array

Definition at line 40 of file fbxdynamicarray.h.

40  :
41  mArray(NULL),
42  mCapacity(0),
43  mSize(0),
44  mAllocator(sizeof(Type))
45  {
46  Reserve(pInitialSize);
47  }
#define NULL
Definition: fbxarch.h:210
void Reserve(const size_t pCount)
Assures that sufficient memory is allocated to hold n objects in the array, and increases the capacit...

◆ FbxDynamicArray() [3/3]

FbxDynamicArray ( const FbxDynamicArray< Type, Allocator > &  pArray)
inline

Copy constructor.

Remarks
The copy constructor of Type will be invoked in order to copy the value of elements to the new array.

Definition at line 54 of file fbxdynamicarray.h.

54  :
55  mArray(NULL),
56  mCapacity(0),
57  mSize(0),
58  mAllocator(sizeof(Type))
59  {
60  Reserve(pArray.mCapacity);
61  CopyArray(mArray, pArray.mArray, pArray.mSize);
62  mSize = pArray.mSize;
63  }
#define NULL
Definition: fbxarch.h:210
void Reserve(const size_t pCount)
Assures that sufficient memory is allocated to hold n objects in the array, and increases the capacit...

◆ ~FbxDynamicArray()

~FbxDynamicArray ( )
inline

Destructor.

Definition at line 66 of file fbxdynamicarray.h.

67  {
68  for( size_t i = 0; i < mSize; ++i )
69  {
70  mArray[i].~Type();
71  }
72  mAllocator.FreeMemory(mArray);
73  }

Member Function Documentation

◆ Capacity()

size_t Capacity ( ) const
inline

Gets the current capacity of the array.

Definition at line 76 of file fbxdynamicarray.h.

77  {
78  return mCapacity;
79  }

◆ Size()

size_t Size ( ) const
inline

Gets the size of the array.

Definition at line 82 of file fbxdynamicarray.h.

83  {
84  return mSize;
85  }

◆ Reserve()

void Reserve ( const size_t  pCount)
inline

Assures that sufficient memory is allocated to hold n objects in the array, and increases the capacity if necessary.

Parameters
pCountNumber of objects to reserve

Definition at line 89 of file fbxdynamicarray.h.

90  {
91  if( pCount > mCapacity )
92  {
93  //We don't use mAllocator.PreAllocate, because we want our array to be continuous in memory.
94  Type* lNewArray = (Type*)mAllocator.AllocateRecords(pCount);
95  MoveArray(lNewArray, mArray, mSize);
96  mAllocator.FreeMemory(mArray);
97  mArray = lNewArray;
98  mCapacity = pCount;
99  }
100  }

◆ PushBack()

void PushBack ( const Type &  pItem,
const size_t  pNCopies = 1 
)
inline

Appends n objects at the end of the array.

Parameters
pItemobject to append
pNCopiesnumber of copies to append

Definition at line 105 of file fbxdynamicarray.h.

106  {
107  if( mSize + pNCopies > mCapacity )
108  {
109  size_t lNewSize = mCapacity + mCapacity / 2; //grow by 50%
110  if( mSize + pNCopies > lNewSize )
111  {
112  lNewSize = mSize + pNCopies;
113  }
114  Reserve(lNewSize);
115  }
116  FBX_ASSERT(mSize + pNCopies <= mCapacity);
117  Fill(mArray + mSize, pItem, pNCopies);
118  mSize += pNCopies;
119  }
void Reserve(const size_t pCount)
Assures that sufficient memory is allocated to hold n objects in the array, and increases the capacit...

◆ Insert()

void Insert ( const size_t  pIndex,
const Type &  pItem,
const size_t  pNCopies = 1 
)
inline

Inserts n objects at the specified position.

Parameters
pIndexposition index
pItemobject to insert
pNCopiesnumber of copies to append

Definition at line 125 of file fbxdynamicarray.h.

126  {
127  FBX_ASSERT(pIndex >= 0);
128  FBX_ASSERT(pIndex <= mSize);
129  Type lValue = pItem; // in case pItem is in array
130  if( pNCopies == 0 )
131  {
132  }
133  else if( pIndex >= mSize )
134  {
135  PushBack(pItem, pNCopies);
136  }
137  else if( mSize + pNCopies > mCapacity )
138  {
139  size_t lNewSize = mCapacity + mCapacity / 2; //not enough room, grow by 50%
140  if( mSize + pNCopies > lNewSize )
141  {
142  lNewSize = mSize + pNCopies;
143  }
144 
145  Type* lNewArray = (Type*)mAllocator.AllocateRecords(lNewSize);
146  MoveArray(lNewArray, mArray, pIndex); // copy prefix
147  Fill(lNewArray + pIndex, pItem, pNCopies); // copy values
148  MoveArray(lNewArray + pIndex + pNCopies, mArray + pIndex, mSize - pIndex); // copy suffix
149  mAllocator.FreeMemory(mArray);
150  mArray = lNewArray;
151  mSize += pNCopies;
152  mCapacity = lNewSize;
153  }
154  else
155  {
156  // copy suffix backwards
157  MoveArrayBackwards(mArray + pIndex + pNCopies, mArray + pIndex, mSize - pIndex);
158  Fill(mArray + pIndex, pItem, pNCopies); // copy values
159  mSize += pNCopies;
160  }
161  }
void PushBack(const Type &pItem, const size_t pNCopies=1)
Appends n objects at the end of the array.

◆ PopBack()

void PopBack ( size_t  pNElements = 1)
inline

Removes n objects at the end.

Parameters
pNElementsnumber of objects to remove

Definition at line 165 of file fbxdynamicarray.h.

166  {
167  FBX_ASSERT(pNElements <= mSize);
168  for( size_t i = mSize - pNElements; i < mSize; ++i )
169  {
170  mArray[i].~Type();
171  }
172  mSize -= pNElements;
173  }

◆ Remove()

void Remove ( const size_t  pIndex,
size_t  pNElements = 1 
)
inline

Removes n objects at the specified position.

Parameters
pIndexposition index
pNElementsnumber of objects to remove

Definition at line 178 of file fbxdynamicarray.h.

179  {
180  FBX_ASSERT(pIndex >= 0);
181  FBX_ASSERT(pIndex <= mSize);
182  FBX_ASSERT(pIndex + pNElements <= mSize);
183  if( pIndex + pNElements >= mSize )
184  {
185  PopBack(pNElements);
186  }
187  else
188  {
189  for( size_t i = pIndex; i < pIndex + pNElements; ++i )
190  {
191  mArray[i].~Type();
192  }
193  MoveOverlappingArray(&mArray[pIndex], &mArray[pIndex + pNElements], mSize - pIndex - pNElements);
194  mSize -= pNElements;
195  }
196  }
void PopBack(size_t pNElements=1)
Removes n objects at the end.

◆ operator[]() [1/2]

Type& operator[] ( const size_t  pIndex)
inline

Gets nth object in the array.

Parameters
pIndexposition index

Definition at line 200 of file fbxdynamicarray.h.

201  {
202  return mArray[pIndex];
203  }

◆ operator[]() [2/2]

const Type& operator[] ( const size_t  pIndex) const
inline

Gets nth object in the array.

Parameters
pIndexposition index

Definition at line 207 of file fbxdynamicarray.h.

208  {
209  return mArray[pIndex];
210  }

◆ First() [1/2]

Type& First ( )
inline

Retrieve the first item in the array.

Returns
The first item in the array.

Definition at line 214 of file fbxdynamicarray.h.

215  {
216  return operator[](0);
217  }
Type & operator[](const size_t pIndex)
Gets nth object in the array.

◆ First() [2/2]

const Type& First ( ) const
inline

Retrieve the first item in the array.

Returns
The first item in the array.

Definition at line 221 of file fbxdynamicarray.h.

222  {
223  return operator[](0);
224  }
Type & operator[](const size_t pIndex)
Gets nth object in the array.

◆ Last() [1/2]

Type& Last ( )
inline

Retrieve the last item in the array.

Returns
The last item in the array.

Definition at line 228 of file fbxdynamicarray.h.

229  {
230  return operator[](mSize-1);
231  }
Type & operator[](const size_t pIndex)
Gets nth object in the array.

◆ Last() [2/2]

const Type& Last ( ) const
inline

Retrieve the last item in the array.

Returns
The last item in the array.

Definition at line 235 of file fbxdynamicarray.h.

236  {
237  return operator[](mSize-1);
238  }
Type & operator[](const size_t pIndex)
Gets nth object in the array.

◆ Find()

size_t Find ( const Type &  pItem,
const size_t  pStartIndex = 0 
) const
inline

Find first matching element, from first to last.

Parameters
pItemThe item to try to find in the array.
pStartIndexThe index to start searching from.
Returns
Index of the first matching item, otherwise returns -1 (equivalent of SIZE_MAX for size_t).

Definition at line 244 of file fbxdynamicarray.h.

245  {
246  for( size_t i = pStartIndex; i < mSize; ++i )
247  {
248  if( operator[](i) == pItem ) return i;
249  }
250  return -1;
251  }

◆ operator=()

FbxDynamicArray& operator= ( const FbxDynamicArray< Type, Allocator > &  pArray)
inline

Assignment operator.

Remarks
The copy constructor of Type will be invoked in order to copy the value of elements to the new array.

Definition at line 255 of file fbxdynamicarray.h.

256  {
257  Reserve(pArray.mCapacity);
258  CopyArray(mArray, pArray.mArray, pArray.mSize);
259  mSize = pArray.mSize;
260  return *this;
261  }
void Reserve(const size_t pCount)
Assures that sufficient memory is allocated to hold n objects in the array, and increases the capacit...

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