tSimpleHashTable.h 3.6 KB

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