simpleHashTable.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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 "sparseArray.h"
  29. #include "algorithm/hashFunction.h"
  30. #ifndef _TSIMPLEHASHTABLE_H
  31. #define _TSIMPLEHASHTABLE_H
  32. template <class T> class SimpleHashTable : public SparseArray<T>
  33. {
  34. typedef SparseArray<T> Parent;
  35. bool mCaseSensitive;
  36. char mCaseConvBuf[1024];
  37. // [tom, 9/21/2006] This is incredibly lame and adds a pretty big speed penalty
  38. inline const char *caseConv(const char *str)
  39. {
  40. if(mCaseSensitive) return str;
  41. S32 len = dStrlen(str);
  42. if(len >= sizeof(mCaseConvBuf)) len = sizeof(mCaseConvBuf) - 1;
  43. char *dptr = mCaseConvBuf;
  44. const char *sptr = str;
  45. while(*sptr)
  46. {
  47. *dptr = dTolower(*sptr);
  48. ++sptr;
  49. ++dptr;
  50. }
  51. *dptr = 0;
  52. return mCaseConvBuf;
  53. }
  54. public:
  55. SimpleHashTable(const U32 modulusSize = 64, bool caseSensitive = true) : Parent(modulusSize), mCaseSensitive(caseSensitive)
  56. {
  57. }
  58. void insert(T* pObject, U8 *key, U32 keyLen);
  59. T* remove(U8 *key, U32 keyLen);
  60. T* retrieve(U8 *key, U32 keyLen);
  61. void insert(T* pObject, const char *key);
  62. T* remove(const char *key);
  63. T* retrieve(const char *key);
  64. };
  65. template <class T> inline void SimpleHashTable<T>::insert(T* pObject, U8 *key, U32 keyLen)
  66. {
  67. Parent::insert(pObject, ::hash(key, keyLen, 0));
  68. }
  69. template <class T> inline T* SimpleHashTable<T>::remove(U8 *key, U32 keyLen)
  70. {
  71. return Parent::remove(::hash(key, keyLen, 0));
  72. }
  73. template <class T> inline T* SimpleHashTable<T>::retrieve(U8 *key, U32 keyLen)
  74. {
  75. return Parent::retrieve(::hash(key, keyLen, 0));
  76. }
  77. template <class T> inline void SimpleHashTable<T>::insert(T* pObject, const char *key)
  78. {
  79. key = caseConv(key);
  80. insert(pObject, (U8 *)key, dStrlen(key));
  81. }
  82. template <class T> T* SimpleHashTable<T>::remove(const char *key)
  83. {
  84. key = caseConv(key);
  85. return remove((U8 *)key, dStrlen(key));
  86. }
  87. template <class T> T* SimpleHashTable<T>::retrieve(const char *key)
  88. {
  89. key = caseConv(key);
  90. return retrieve((U8 *)key, dStrlen(key));
  91. }
  92. #endif // _TSIMPLEHASHTABLE_H