StrBloomMap.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #pragma once
  2. #include "DebugCommon.h"
  3. #include "BeefySysLib/Util/BumpAllocator.h"
  4. NS_BF_DBG_BEGIN
  5. template <typename T>
  6. class StrBloomMap
  7. {
  8. public:
  9. static const int HashSize = 9973;
  10. Beefy::BumpAllocator mAlloc;
  11. struct Entry
  12. {
  13. T mValue;
  14. Entry* mNext;
  15. };
  16. struct Iterator
  17. {
  18. public:
  19. StrBloomMap* mMap;
  20. Entry* mCurEntry;
  21. int mCurBucket;
  22. public:
  23. Iterator(StrBloomMap* map)
  24. {
  25. mMap = map;
  26. mCurBucket = 0;
  27. mCurEntry = NULL;
  28. }
  29. Iterator& operator++()
  30. {
  31. if (mCurEntry != NULL)
  32. {
  33. mCurEntry = mCurEntry->mNext;
  34. if (mCurEntry != NULL)
  35. return *this;
  36. mCurBucket++;
  37. }
  38. while (mCurBucket < HashSize)
  39. {
  40. mCurEntry = mMap->mHashHeads[mCurBucket];
  41. if (mCurEntry != NULL)
  42. return *this;
  43. mCurBucket++;
  44. }
  45. return *this; // At end
  46. }
  47. bool operator!=(const Iterator& itr) const
  48. {
  49. return ((itr.mCurEntry != mCurEntry) || (itr.mCurBucket != mCurBucket));
  50. }
  51. Entry* operator*()
  52. {
  53. return mCurEntry;
  54. }
  55. };
  56. public:
  57. int GetHash(const char* str, const char* strEnd)
  58. {
  59. if (str == NULL)
  60. return 0;
  61. int curHash = 0;
  62. const char* curHashPtr = str;
  63. while ((*curHashPtr != 0) && (curHashPtr != strEnd))
  64. {
  65. char c = *curHashPtr;
  66. //if (c != ' ')
  67. curHash = ((curHash ^ *curHashPtr) << 5) - curHash;
  68. curHashPtr++;
  69. }
  70. return curHash & 0x7FFFFFFF;
  71. }
  72. public:
  73. Entry** mHashHeads;
  74. public:
  75. StrBloomMap()
  76. {
  77. mHashHeads = NULL;
  78. }
  79. void InsertUnique(int hash, T value)
  80. {
  81. if (mHashHeads == NULL)
  82. mHashHeads = (Entry**)mAlloc.AllocBytes(sizeof(Entry*) * HashSize, alignof(Entry*));
  83. int hashIdx = hash % HashSize;
  84. Entry* headEntry = mHashHeads[hashIdx];
  85. Entry* checkEntry = headEntry;
  86. while (checkEntry != NULL)
  87. {
  88. if (checkEntry->mValue == value)
  89. return;
  90. checkEntry = checkEntry->mNext;
  91. }
  92. Entry* newEntry = mAlloc.Alloc<Entry>();
  93. newEntry->mValue = value;
  94. newEntry->mNext = headEntry;
  95. mHashHeads[hashIdx] = newEntry;
  96. }
  97. Entry* FindFirst(const char* name)
  98. {
  99. if (mHashHeads == NULL)
  100. return NULL;
  101. int hash = GetHash(name, NULL) % HashSize;
  102. return mHashHeads[hash];
  103. }
  104. Iterator begin()
  105. {
  106. return ++Iterator(this);
  107. }
  108. Iterator end()
  109. {
  110. Iterator itr(this);
  111. itr.mCurBucket = HashSize;
  112. return itr;
  113. }
  114. };
  115. NS_BF_DBG_END