FBX C++ API Reference
All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FbxHashMap< KEY, VALUE, HASH, Destruct, Comparator > Class Template Reference

#include <fbxhashmap.h>

Class Description

template<typename KEY, typename VALUE, typename HASH, class Destruct = FbxNoOpDestruct<VALUE>, class Comparator = FbxDefaultComparator<KEY>>
class FbxHashMap< KEY, VALUE, HASH, Destruct, Comparator >

This object represents a standard hash map.

You must provide the typename of KEY and VALUE as well as the typename of the class that contains the hash function to use to hash values. The hash class must overload operator() and be built like this.

class SimpleHash
{
public:
inline unsigned int operator() ( const int pKey ) const
{
return pKey;
}
};

Definition at line 45 of file fbxhashmap.h.

Classes

class  Iterator
 Iterate through every element in a hash map. More...
 

Public Types

typedef KEY KeyType
 
typedef VALUE ValueType
 
typedef HASH HashFunctorType
 

Public Member Functions

 FbxHashMap (int pBucketSize)
 Construct a FbxHashMap with an user-defined maximum number of elements. More...
 
 FbxHashMap ()
 Construct a FbxHashMap with the default maximum number of elements (30) More...
 
 ~FbxHashMap ()
 Clear all elements in the hash map before destroying itself. More...
 
void Clear ()
 Calls operator delete on all elements of the hashmap, de-allocating all memory and destroying them. More...
 
const Iterator Find (const KeyType &pKey) const
 Find an element in the hashmap. More...
 
VALUE Remove (const KEY &pKey)
 Remove an element in the hashmap. More...
 
ValueTypeoperator[] (const KeyType &pKey)
 Add or retrieve a KeyValuePair from the Hashmap. More...
 
Iterator Start () const
 Returns an iterator pointing on the first non-null element in the map. More...
 
Iterator End () const
 Returns an iterator pointing on the last element in the map. More...
 

Friends

class Iterator
 

Member Typedef Documentation

◆ KeyType

typedef KEY KeyType

Definition at line 48 of file fbxhashmap.h.

◆ ValueType

typedef VALUE ValueType

Definition at line 49 of file fbxhashmap.h.

◆ HashFunctorType

typedef HASH HashFunctorType

Definition at line 50 of file fbxhashmap.h.

Constructor & Destructor Documentation

◆ FbxHashMap() [1/2]

FbxHashMap ( int  pBucketSize)
inline

Construct a FbxHashMap with an user-defined maximum number of elements.

Parameters
pBucketSizeInitial maximum number of elements.

Definition at line 213 of file fbxhashmap.h.

214  {
215  mBuckets.Resize( pBucketSize );
216  }
bool Resize(const int pSize)
Inserts or erases elements at the end such that Size() becomes pSize, increasing capacity if needed...
Definition: fbxarray.h:299

◆ FbxHashMap() [2/2]

FbxHashMap ( )
inline

Construct a FbxHashMap with the default maximum number of elements (30)

Definition at line 221 of file fbxhashmap.h.

222  {
223  mBuckets.Resize(30);
224  }
bool Resize(const int pSize)
Inserts or erases elements at the end such that Size() becomes pSize, increasing capacity if needed...
Definition: fbxarray.h:299

◆ ~FbxHashMap()

~FbxHashMap ( )
inline

Clear all elements in the hash map before destroying itself.

Definition at line 229 of file fbxhashmap.h.

230  {
231  Clear();
232  mBuckets.Clear();
233  }
void Clear()
Reset the number of element to zero and free the memory allocated.
Definition: fbxarray.h:352
void Clear()
Calls operator delete on all elements of the hashmap, de-allocating all memory and destroying them...
Definition: fbxhashmap.h:238

Member Function Documentation

◆ Clear()

void Clear ( )
inline

Calls operator delete on all elements of the hashmap, de-allocating all memory and destroying them.

Definition at line 238 of file fbxhashmap.h.

239  {
240  for( int i = 0; i < mBuckets.GetCount(); ++i)
241  {
242  if( mBuckets[i] )
243  {
244  ListItem* lNext = mBuckets[i]->mNext;
245  while( lNext )
246  {
247  ListItem* lNextNext = lNext->mNext;
248  FbxDelete(lNext);
249  lNext = lNextNext;
250  }
251 
252  FbxDelete(mBuckets[i]);
253  mBuckets[i] = NULL;
254  }
255  }
256  }
#define NULL
Definition: fbxarch.h:210
void FbxDelete(T *p)
Deletion policy for pointer template classes that uses the FbxDelete() function.
Definition: fbxnew.h:341

◆ Find()

const Iterator Find ( const KeyType pKey) const
inline

Find an element in the hashmap.

If no element exist with the specified key, returns an iterator pointing on the end of the map (not an actual KeyValuePair).

Parameters
pKeyThe value of the key corresponding to the element
Returns
An Iterator referencing that element

Definition at line 264 of file fbxhashmap.h.

265  {
266  unsigned int lIndex = mHashFunctor(pKey);
267  lIndex = lIndex % mBuckets.GetCount();
268  ListItem* lItem = mBuckets[lIndex];
269  while( lItem )
270  {
271  if( Comparator::CompareIt( lItem->mKey, pKey ) )
272  {
273  Iterator lIt( this, lIndex, lItem );
274  return lIt;
275  }
276  lItem = lItem->mNext;
277  }
278 
279  return End();
280  }
Iterator End() const
Returns an iterator pointing on the last element in the map.
Definition: fbxhashmap.h:372
friend class Iterator
Definition: fbxhashmap.h:406

◆ Remove()

VALUE Remove ( const KEY &  pKey)
inline

Remove an element in the hashmap.

Parameters
pKeyThe key value of the element to remove
Returns
The value of the element that was just deleted. If the element does not exist, a value created with its default constructor will be returned

Definition at line 287 of file fbxhashmap.h.

288  {
289  unsigned int lIndex = mHashFunctor(pKey);
290  lIndex = lIndex % mBuckets.GetCount();
291  ListItem* lItem = mBuckets.GetAt(lIndex);
292  ListItem* lLastItem = NULL;
293 
294  while( lItem )
295  {
296  if( lItem->mKey == pKey )
297  {
298  if( lLastItem )
299  lLastItem->mNext = lItem->mNext;
300 
301  if( mBuckets.GetAt(lIndex) == lItem )
302  mBuckets.SetAt(lIndex, lItem->mNext );
303 
304  VALUE lValue = lItem->mValue;
305  FbxDelete(lItem);
306 
307  return lValue;
308  }
309 
310  lLastItem = lItem;
311  lItem = lItem->mNext;
312  }
313 
314  return VALUE();
315  }
#define NULL
Definition: fbxarch.h:210
void FbxDelete(T *p)
Deletion policy for pointer template classes that uses the FbxDelete() function.
Definition: fbxnew.h:341
T GetAt(const int pIndex) const
Retrieve a copy of the element at given index position in the array.
Definition: fbxarray.h:139
void SetAt(const int pIndex, const T &pElement)
Set the element at given position in the array.
Definition: fbxarray.h:212

◆ operator[]()

ValueType& operator[] ( const KeyType pKey)
inline

Add or retrieve a KeyValuePair from the Hashmap.

If there is already an entry in the map for an element with key value specified in parameter, the value will be returned. Otherwise, a new entry will be created with this key value and the default value for ValueType will be returned. It can be modified using the assignment operator

Parameters
pKeyThe key for which to retrieve/add a value.
Returns
Value of the element referenced by the key specified in parameter.

Definition at line 324 of file fbxhashmap.h.

325  {
326  unsigned int lIndex = 0;
327  Iterator lIt = InternalFind( pKey, lIndex);
328  if( lIt != End() )
329  {
330  return lIt.mCurrentItem->mValue;
331  }
332 
333  lIndex = lIndex % mBuckets.GetCount();
334  ListItem* lItem = FbxNew< ListItem >();
335  lItem->mNext = NULL;
336  lItem->mKey = pKey;
337 
338  if( !mBuckets.GetAt(lIndex) )
339  {
340  mBuckets.SetAt(lIndex, lItem);
341  }
342  else
343  {
344  lItem->mNext = mBuckets.GetAt(lIndex);
345  mBuckets.SetAt(lIndex, lItem);
346  }
347 
348  return lItem->mValue;
349  }
#define NULL
Definition: fbxarch.h:210
Iterator End() const
Returns an iterator pointing on the last element in the map.
Definition: fbxhashmap.h:372
T GetAt(const int pIndex) const
Retrieve a copy of the element at given index position in the array.
Definition: fbxarray.h:139
void SetAt(const int pIndex, const T &pElement)
Set the element at given position in the array.
Definition: fbxarray.h:212
friend class Iterator
Definition: fbxhashmap.h:406

◆ Start()

Iterator Start ( ) const
inline

Returns an iterator pointing on the first non-null element in the map.

Returns
An iterator pointing on the first non-null element in the map.

Definition at line 354 of file fbxhashmap.h.

355  {
356  for( int i = 0; i < mBuckets.GetCount(); ++i )
357  {
358  if( mBuckets[i] )
359  {
360  Iterator lIt( this, i, mBuckets[i] );
361  return lIt;
362  }
363  }
364 
365  return End();
366  }
Iterator End() const
Returns an iterator pointing on the last element in the map.
Definition: fbxhashmap.h:372
friend class Iterator
Definition: fbxhashmap.h:406

◆ End()

Iterator End ( ) const
inline

Returns an iterator pointing on the last element in the map.

This is not an actual KeyValuePair but but an iterator pointing on a null element.

Returns
Iterator pointing on a null value at the end of the map

Definition at line 372 of file fbxhashmap.h.

373  {
374  Iterator lIt( this, 0, NULL );
375  return lIt;
376  }
#define NULL
Definition: fbxarch.h:210
friend class Iterator
Definition: fbxhashmap.h:406

Friends And Related Function Documentation

◆ Iterator

friend class Iterator
friend

Definition at line 406 of file fbxhashmap.h.


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