Game Development Community

Fix to Templated HashTable

by William Todd Scott · in Torque Game Engine Advanced · 06/30/2007 (10:53 am) · 1 replies

Hi all,

In the process of removing the STL stuff I had previously added to the engine, I came across the hashtable/map code in tDictionary. h. This was very cool because I thought I was going to have to write my own version....no need.

The map and hashtable implementation wasn't quite completed. There were a couple of function declarations related to const iterators that had never been defined, because they aren't currently being used by the engine. I needed to use them, so it was simple to implement these functions.

In case someone else wants to use these classes here are the necessary changes to the tDictionary.h file:

On line 429 implement the const version of find() for the hash table:

template<typename Key, typename Value>
typename HashTable<Key,Value>::ConstIterator HashTable<Key,Value>::find(const Key& key) const
{
   if (mTableSize)
      for (Node* itr = mTable[_index(key)]; itr; itr = itr->mNext)
         if ( KeyCmp::equals<Key>( itr->mPair.key, key ) )
            return ConstIterator(this,itr);
   return ConstIterator(this,0);
}

On line 622 implement the const version of find() for the map:

template<typename Key, typename Value, class Sequence>
typename Map<Key,Value,Sequence>::ConstIterator Map<Key,Value,Sequence>::find(const Key& key) const
{
   return mMap.find(key);
}

Thats is all that is needed to get this very helpful class working in const functions.

Todd

#1
07/07/2007 (12:54 pm)
Cool, thanks Todd. We're using it in our game for quick string lookups of our state machine as well as a few other things. Thanks for the fix :)