Torque3D Documentation / _generateds / tSimpleHashTable.h

tSimpleHashTable.h

Engine/source/core/tSimpleHashTable.h

More...

Classes:

Detailed Description

  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2012 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24// [tom, 9/19/2006] Simple hash table. Not intended to replace map<>, but it is
 25// generally good enough for simple things that you don't need to iterate.
 26//
 27// Note: If you move this to another project, you need the updated tSparseArray.h
 28// as well as hashFunction.cc/h
 29
 30#include "platform/platform.h"
 31
 32#include "core/tSparseArray.h"
 33#include "core/util/hashFunction.h"
 34#include "core/strings/stringFunctions.h"
 35
 36#ifndef _TSIMPLEHASHTABLE_H
 37#define _TSIMPLEHASHTABLE_H
 38
 39template <class T> class SimpleHashTable : public SparseArray<T>
 40{
 41   typedef SparseArray<T> Parent;
 42
 43   bool mCaseSensitive;
 44
 45   char mCaseConvBuf[1024];
 46   
 47   // [tom, 9/21/2006] This is incredibly lame and adds a pretty big speed penalty
 48   inline const char *caseConv(const char *str)
 49   {
 50      if(mCaseSensitive)   return str;
 51
 52      S32 len = dStrlen(str);
 53      if(len >= sizeof(mCaseConvBuf))  len = sizeof(mCaseConvBuf) - 1;
 54
 55      char *dptr = mCaseConvBuf;
 56      const char *sptr = str;
 57      while(*sptr)
 58      {
 59         *dptr = dTolower(*sptr);
 60         ++sptr;
 61         ++dptr;
 62      }
 63      *dptr = 0;
 64
 65      return mCaseConvBuf;
 66   }
 67
 68public:
 69   SimpleHashTable(const U32 modulusSize = 64, bool caseSensitive = true) : Parent(modulusSize), mCaseSensitive(caseSensitive)
 70   {
 71   }
 72
 73   void insert(T* pObject, U8 *key, U32 keyLen);
 74   T*   remove(U8 *key, U32 keyLen);
 75   T*   retreive(U8 *key, U32 keyLen);
 76
 77   void insert(T* pObject, const char *key);
 78   T*   remove(const char *key);
 79   T*   retreive(const char *key);
 80};
 81
 82template <class T> inline void SimpleHashTable<T>::insert(T* pObject, U8 *key, U32 keyLen)
 83{
 84   Parent::insert(pObject, Torque::hash(key, keyLen, 0));
 85}
 86
 87template <class T> inline T* SimpleHashTable<T>::remove(U8 *key, U32 keyLen)
 88{
 89   return Parent::remove(Torque::hash(key, keyLen, 0));
 90}
 91
 92template <class T> inline T* SimpleHashTable<T>::retreive(U8 *key, U32 keyLen)
 93{
 94   return Parent::retreive(Torque::hash(key, keyLen, 0));
 95}
 96
 97template <class T> inline void SimpleHashTable<T>::insert(T* pObject, const char *key)
 98{
 99   key = caseConv(key);
100   insert(pObject, (U8 *)key, dStrlen(key));
101}
102
103template <class T> T* SimpleHashTable<T>::remove(const char *key)
104{
105   key = caseConv(key);
106   return remove((U8 *)key, dStrlen(key));
107}
108
109template <class T> T* SimpleHashTable<T>::retreive(const char *key)
110{
111   key = caseConv(key);
112   return retreive((U8 *)key, dStrlen(key));
113}
114
115
116#endif // _TSIMPLEHASHTABLE_H
117