Hash.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #pragma once
  2. #include "../Common.h"
  3. #include "../FileStream.h"
  4. NS_BF_BEGIN
  5. class Val128
  6. {
  7. public:
  8. struct Hash
  9. {
  10. size_t operator()(const Val128& entry) const
  11. {
  12. return (size_t)(entry.mLow ^ entry.mHigh);
  13. }
  14. };
  15. struct Equals
  16. {
  17. bool operator()(const Val128& lhs, const Val128& rhs) const
  18. {
  19. return (lhs.mLow == rhs.mLow) && (lhs.mHigh == rhs.mHigh);
  20. }
  21. };
  22. public:
  23. uint64 mLow;
  24. uint64 mHigh;
  25. public:
  26. Val128()
  27. {
  28. mLow = 0;
  29. mHigh = 0;
  30. }
  31. Val128(int val)
  32. {
  33. mLow = (uint64)val;
  34. mHigh = 0;
  35. }
  36. Val128& operator=(int val)
  37. {
  38. mLow = (uint64)val;
  39. mHigh = 0;
  40. return *this;
  41. }
  42. bool IsZero()
  43. {
  44. return (mLow == 0) && (mHigh == 0);
  45. }
  46. explicit operator int()
  47. {
  48. return (int)mLow;
  49. }
  50. String ToString()
  51. {
  52. return StrFormat("%lX%lX", mHigh, mLow);
  53. }
  54. Val128 operator+(int rLow)
  55. {
  56. Val128 result;
  57. result.mLow += mLow + rLow;
  58. result.mHigh = mHigh;
  59. return result;
  60. }
  61. };
  62. static bool operator!=(const Val128& l, int rLow)
  63. {
  64. return (l.mHigh != 0) || (l.mLow != rLow);
  65. }
  66. static bool operator==(const Val128& l, const Val128& r)
  67. {
  68. return (l.mLow == r.mLow) && (l.mHigh == r.mHigh);
  69. }
  70. static bool operator!=(const Val128& l, const Val128& r)
  71. {
  72. return (l.mLow != r.mLow) || (l.mHigh != r.mHigh);
  73. }
  74. static bool operator<(const Val128& l, const Val128& r)
  75. {
  76. int* lPtr = (int*)&l.mLow;
  77. int* rPtr = (int*)&r.mLow;
  78. if (lPtr[3] != rPtr[3]) //-V557
  79. return lPtr[3] < rPtr[3]; //-V557
  80. if (lPtr[2] != rPtr[2]) //-V557
  81. return lPtr[2] < rPtr[2]; //-V557
  82. if (lPtr[1] != rPtr[1])
  83. return lPtr[1] < rPtr[1];
  84. return lPtr[0] < rPtr[0];
  85. }
  86. uint64 Hash64(uint64 hash, uint64 seed);
  87. uint64 Hash64(const void* data, int length, uint64 seed = 0);
  88. Val128 Hash128(const void* data, int length);
  89. Val128 Hash128(const void* data, int length, const Val128& seed);
  90. Val128 HashMD5(const void* data, int length);
  91. String HashEncode64(uint64 val); // Note: this only encodes the low 60 bits. Returns up to 10 characters.
  92. StringT<21> HashEncode128(Val128 val); // Returns up to 20 characters.
  93. #define HASH128_MIXIN(hashVal, data) hashVal = Hash128(&data, sizeof(data), hashVal)
  94. #define HASH128_MIXIN_PTR(hashVal, data, size) hashVal = Hash128(data, size, hashVal)
  95. #define HASH128_MIXIN_STR(hashVal, str) hashVal = Hash128(str.c_str(), (int)str.length(), hashVal)
  96. class HashContext
  97. {
  98. public:
  99. uint8 mBuf[1024];
  100. int mBufSize;
  101. int mBufOffset;
  102. #ifdef BF_PLATFORM_WINDOWS
  103. bool mDbgViz;
  104. FileStream* mDbgVizStream;
  105. #endif
  106. public:
  107. HashContext()
  108. {
  109. mBufOffset = 0;
  110. mBufSize = 0;
  111. #ifdef BF_PLATFORM_WINDOWS
  112. mDbgViz = false;
  113. mDbgVizStream = NULL;
  114. #endif
  115. }
  116. ~HashContext();
  117. void Reset();
  118. void Mixin(const void* data, int size);
  119. void Mixin(int val)
  120. {
  121. Mixin((void*)&val, (int)sizeof(val));
  122. }
  123. template <typename T>
  124. void Mixin(const T& val)
  125. {
  126. Mixin((void*)&val, (int)sizeof(val));
  127. }
  128. void MixinHashContext(HashContext& ctx);
  129. void MixinStr(const char* str);
  130. void MixinStr(const StringImpl& str);
  131. Val128 Finish128();
  132. uint64 Finish64();
  133. };
  134. NS_BF_END
  135. namespace std
  136. {
  137. template<>
  138. struct hash<Beefy::Val128>
  139. {
  140. size_t operator()(const Beefy::Val128& val) const
  141. {
  142. return val.mLow;
  143. }
  144. };
  145. }