FBArrayTemplate< Type > Class Template Reference

#include <fbarray.h>

Class Description

template<class Type>
class ORSDK2019::FBArrayTemplate< Type >

Template class to contain an array of items.

Note
This utility template array only support plain old data structure (POD).

Definition at line 77 of file fbarray.h.

Public Member Functions

 FBArrayTemplate (int pItemPerBlock=10)
 Constructor. More...
 
 FBArrayTemplate (const FBArrayTemplate< Type > &pArrayTemplate)
 Copy constructor. More...
 
 ~FBArrayTemplate ()
 Destructor. More...
 
int InsertAt (int pIndex, Type pItem)
 Insert pItem at pIndex. More...
 
void RemoveAt (int pIndex)
 Remove item at pIndex. More...
 
void RemoveLast ()
 Remove the last item in the array. More...
 
bool Remove (Type &pItem)
 Remove pItem from the array. More...
 
bool RemoveIt (Type pItem)
 Remove pItem from the array. More...
 
void Clear ()
 Empty the array of all items. More...
 
Type & operator[] (int pIndex) const
 [] operator overload. More...
 
void SetAt (int pIndex, Type pItem)
 Set item at pIndex to pItem. More...
 
void SetLast (Type pItem)
 Set the last item of the array. More...
 
int GetCount () const
 Get the number of items in the array. More...
 
void SetCount (int pCount)
 Set the number of items in the array. More...
 
Type GetAt (int pIndex)
 Get item at pIndex. More...
 
Type GetLast ()
 Get last item of the array. More...
 
int Find (Type pItem)
 Find the index of pItem in the array. More...
 
int Add (Type pItem)
 Add an item to the end of the array. More...
 
Type * GetArray ()
 Get a pointer to the array of items. More...
 
FBArrayTemplate< Type > & operator= (const FBArrayTemplate< Type > &pArrayTemplate)
 Copy array of pointers without copying the associated objects. More...
 

Constructor & Destructor Documentation

FBArrayTemplate ( int  pItemPerBlock = 10)
inline

Constructor.

Parameters
pItemPerBlockNumber of items per array block (default is 10).

Definition at line 83 of file fbarray.h.

84  {
85  mArray = NULL;
86  mArrayCount = 0;
87  mBlockCount = 0;
88  mItemPerBlock = pItemPerBlock;
89  }
#define NULL
Definition: kaydara.h:179
FBArrayTemplate ( const FBArrayTemplate< Type > &  pArrayTemplate)
inline

Copy constructor.

Definition at line 92 of file fbarray.h.

93  {
94  mArray = NULL;
95  mArrayCount = 0;
96  mBlockCount = 0;
97  mItemPerBlock = 10;
98 
99  *this = pArrayTemplate;
100  }
#define NULL
Definition: kaydara.h:179
~FBArrayTemplate ( )
inline

Destructor.

Definition at line 103 of file fbarray.h.

104  {
105  Clear();
106  }
void Clear()
Empty the array of all items.
Definition: fbarray.h:185

Member Function Documentation

int Add ( Type  pItem)
inline

Add an item to the end of the array.

Parameters
pItemItem to insert into the array.
Returns
Index where pItem was inserted.

Definition at line 298 of file fbarray.h.

299  {
300  return InsertAt( mArrayCount,pItem );
301  }
int InsertAt(int pIndex, Type pItem)
Insert pItem at pIndex.
Definition: fbarray.h:113
void Clear ( )
inline

Empty the array of all items.

Definition at line 185 of file fbarray.h.

186  {
187  if (mArray!=NULL) {
188  FBFree(mArray);
189  mArray = NULL;
190  }
191  mArrayCount = 0L;
192  mBlockCount = 0L;
193  }
void FBFree(void *memblock)
General free function, actually calling standard function "free".
#define NULL
Definition: kaydara.h:179
int Find ( Type  pItem)
inline

Find the index of pItem in the array.

Parameters
pItemItem to look for in the array.
Returns
Index number of element found. Returns -1 if pItem was not found.

Definition at line 283 of file fbarray.h.

284  {
285  int Count;
286  for (Count=0; Count<mArrayCount; Count++) {
287  if (mArray[Count]==pItem) {
288  return Count;
289  }
290  }
291  return -1;
292  }
Type* GetArray ( )
inline

Get a pointer to the array of items.

Returns
Pointer to the array of items.
Warning
Gives direct access to the array pointer!

Definition at line 307 of file fbarray.h.

308  {
309  return mArray;
310  }
Type GetAt ( int  pIndex)
inline

Get item at pIndex.

Parameters
pIndexIndex of desired item.
Returns
Item specified by pIndex.

Definition at line 265 of file fbarray.h.

266  {
267  assert( pIndex<mArrayCount );
268  return mArray[pIndex];
269  }
int GetCount ( ) const
inline

Get the number of items in the array.

Returns
Number of items in the array.

Definition at line 227 of file fbarray.h.

228  {
229  return mArrayCount;
230  }
Type GetLast ( )
inline

Get last item of the array.

Returns
Last item of the array.

Definition at line 274 of file fbarray.h.

275  {
276  return mArray[mArrayCount-1];
277  }
int InsertAt ( int  pIndex,
Type  pItem 
)
inline

Insert pItem at pIndex.

Parameters
pIndexIndex to insert at.
pItemItem to insert.
Returns
Actual insertion index where pItem was inserted.

Definition at line 113 of file fbarray.h.

114  {
115  if (pIndex>mArrayCount)
116  {
117  pIndex = mArrayCount;
118  }
119 
120  if (mArrayCount>= mBlockCount*mItemPerBlock)
121  {
122  // must allocate or reallocate block of items
123  mBlockCount++;
124  mArray = (Type *)FBRealloc( mArray,(size_t)(mBlockCount*mItemPerBlock*sizeof(Type)));
125  }
126 
127  if (pIndex<mArrayCount)
128  {
129  // This is an insert
130  memmove (&(mArray[pIndex+1]),&(mArray[pIndex]),sizeof(Type)*(mArrayCount-pIndex));
131  }
132 
133  mArray[pIndex] = pItem;
134  mArrayCount++;
135 
136  return pIndex;
137  }
void * FBRealloc(void *memblock, size_t size)
General allocation function, actually calling standard function "realloc".
FBArrayTemplate<Type>& operator= ( const FBArrayTemplate< Type > &  pArrayTemplate)
inline

Copy array of pointers without copying the associated objects.

Parameters
pArrayTemplateArray to copy from.
Returns
Pointer to the this class.

Definition at line 316 of file fbarray.h.

317  {
318  if (this != &pArrayTemplate)
319  {
320  Clear();
321 
322  mItemPerBlock = pArrayTemplate.mItemPerBlock;
323 
324  SetCount(pArrayTemplate.GetCount());
325  memcpy(mArray, pArrayTemplate.mArray, sizeof(Type) * pArrayTemplate.GetCount());
326  }
327 
328  return (*this);
329  }
void SetCount(int pCount)
Set the number of items in the array.
Definition: fbarray.h:234
void Clear()
Empty the array of all items.
Definition: fbarray.h:185
Type& operator[] ( int  pIndex) const
inline

[] operator overload.

Parameters
pIndexIndex of item to access.
Returns
Item corresponding to pIndex.

Definition at line 199 of file fbarray.h.

200  {
201  assert( pIndex<mArrayCount );
202  return mArray[pIndex];
203  }
bool Remove ( Type &  pItem)
inline

Remove pItem from the array.

Parameters
pItemItem to remove.
Returns
Operation was successful (true or false).

Definition at line 160 of file fbarray.h.

161  {
162  int Index = Find( pItem );
163  if (Index>=0) {
164  RemoveAt( Index );
165  return true;
166  }
167  return false;
168  }
void RemoveAt(int pIndex)
Remove item at pIndex.
Definition: fbarray.h:142
#define Index(a0, a1)
Definition: Python-ast.h:517
int Find(Type pItem)
Find the index of pItem in the array.
Definition: fbarray.h:283
void RemoveAt ( int  pIndex)
inline

Remove item at pIndex.

Parameters
pIndexIndex to remove item from.

Definition at line 142 of file fbarray.h.

143  {
144  assert( pIndex<mArrayCount );
145  if (pIndex+1<mArrayCount) {
146  memmove (&(mArray[pIndex]),&(mArray[pIndex+1]),sizeof(Type)*(mArrayCount-pIndex-1));
147  }
148  mArrayCount --;
149  memset (&(mArray[mArrayCount]),0,sizeof(Type)); // Cleanup last element to make sure we don't access it later
150  }
bool RemoveIt ( Type  pItem)
inline

Remove pItem from the array.

Parameters
pItemItem to remove.
Returns
Outcome of removal (true or false).

Definition at line 174 of file fbarray.h.

175  {
176  int Index = Find( pItem );
177  if (Index>=0) {
178  RemoveAt( Index );
179  return true;
180  }
181  return false;
182  }
void RemoveAt(int pIndex)
Remove item at pIndex.
Definition: fbarray.h:142
#define Index(a0, a1)
Definition: Python-ast.h:517
int Find(Type pItem)
Find the index of pItem in the array.
Definition: fbarray.h:283
void RemoveLast ( )
inline

Remove the last item in the array.

Definition at line 154 of file fbarray.h.

154 { RemoveAt( mArrayCount-1 ); }
void RemoveAt(int pIndex)
Remove item at pIndex.
Definition: fbarray.h:142
void SetAt ( int  pIndex,
Type  pItem 
)
inline

Set item at pIndex to pItem.

Parameters
pIndexIndex of item to set.
pItemItem to copy into the array.

Definition at line 209 of file fbarray.h.

210  {
211  assert( pIndex<mArrayCount );
212  mArray[pIndex] = pItem;
213  }
void SetCount ( int  pCount)
inline

Set the number of items in the array.

Definition at line 234 of file fbarray.h.

235  {
236  if (pCount > mArrayCount)
237  {
238  if( pCount )
239  {
240  const int lTempNewBlockCount = ( (int) (mArrayCount+pCount + mItemPerBlock - 1 ) / mItemPerBlock );
241  const int lNewBlockCount = (lTempNewBlockCount > 1 ? lTempNewBlockCount : 1);
242 
243  const int lOldArraySize = mArrayCount*sizeof(Type);
244  const int lNewArraySize = lNewBlockCount*mItemPerBlock*sizeof(Type);
245 
246  if( lNewBlockCount > (int) mBlockCount )
247  {
248  mArray = (Type *)FBRealloc( mArray, (size_t) lNewArraySize );
249  mBlockCount = lNewBlockCount;
250  }
251 
252  memset( ((char *)mArray) + lOldArraySize, 0, (size_t) (lNewArraySize-lOldArraySize) );
253  mArrayCount += pCount;
254  }
255  } else
256  {
257  mArrayCount = pCount;
258  }
259  }
void * FBRealloc(void *memblock, size_t size)
General allocation function, actually calling standard function "realloc".
int
Definition: code.h:72
void SetLast ( Type  pItem)
inline

Set the last item of the array.

Parameters
pItemItem to copy as the last item of the array
Warning
Will write over last item in the array!

Definition at line 219 of file fbarray.h.

220  {
221  SetAt(mArrayCount-1,pItem );
222  }
void SetAt(int pIndex, Type pItem)
Set item at pIndex to pItem.
Definition: fbarray.h:209

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