hashfuncs.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*************************************************************************/
  2. /* hashfuncs.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef HASHFUNCS_H
  31. #define HASHFUNCS_H
  32. #include "math_defs.h"
  33. #include "math_funcs.h"
  34. #include "typedefs.h"
  35. #include "ustring.h"
  36. /**
  37. * Hashing functions
  38. */
  39. /**
  40. * DJB2 Hash function
  41. * @param C String
  42. * @return 32-bits hashcode
  43. */
  44. static inline uint32_t hash_djb2(const char *p_cstr) {
  45. const unsigned char *chr = (const unsigned char *)p_cstr;
  46. uint32_t hash = 5381;
  47. uint32_t c;
  48. while ((c = *chr++))
  49. hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
  50. return hash;
  51. }
  52. static inline uint32_t hash_djb2_buffer(const uint8_t *p_buff, int p_len, uint32_t p_prev = 5381) {
  53. uint32_t hash = p_prev;
  54. for (int i = 0; i < p_len; i++)
  55. hash = ((hash << 5) + hash) + p_buff[i]; /* hash * 33 + c */
  56. return hash;
  57. }
  58. static inline uint32_t hash_djb2_one_32(uint32_t p_in, uint32_t p_prev = 5381) {
  59. return ((p_prev << 5) + p_prev) + p_in;
  60. }
  61. static inline uint32_t hash_one_uint64(const uint64_t p_int) {
  62. uint64_t v = p_int;
  63. v = (~v) + (v << 18); // v = (v << 18) - v - 1;
  64. v = v ^ (v >> 31);
  65. v = v * 21; // v = (v + (v << 2)) + (v << 4);
  66. v = v ^ (v >> 11);
  67. v = v + (v << 6);
  68. v = v ^ (v >> 22);
  69. return (int)v;
  70. }
  71. static inline uint32_t hash_djb2_one_float(double p_in, uint32_t p_prev = 5381) {
  72. union {
  73. double d;
  74. uint64_t i;
  75. } u;
  76. // Normalize +/- 0.0 and NaN values so they hash the same.
  77. if (p_in == 0.0f)
  78. u.d = 0.0;
  79. else if (Math::is_nan(p_in))
  80. u.d = Math_NAN;
  81. else
  82. u.d = p_in;
  83. return ((p_prev << 5) + p_prev) + hash_one_uint64(u.i);
  84. }
  85. template <class T>
  86. static inline uint32_t make_uint32_t(T p_in) {
  87. union {
  88. T t;
  89. uint32_t _u32;
  90. } _u;
  91. _u._u32 = 0;
  92. _u.t = p_in;
  93. return _u._u32;
  94. }
  95. static inline uint64_t hash_djb2_one_64(uint64_t p_in, uint64_t p_prev = 5381) {
  96. return ((p_prev << 5) + p_prev) + p_in;
  97. }
  98. template <class T>
  99. static inline uint64_t make_uint64_t(T p_in) {
  100. union {
  101. T t;
  102. uint64_t _u64;
  103. } _u;
  104. _u._u64 = 0; // in case p_in is smaller
  105. _u.t = p_in;
  106. return _u._u64;
  107. }
  108. struct HashMapHasherDefault {
  109. static _FORCE_INLINE_ uint32_t hash(const String &p_string) { return p_string.hash(); }
  110. static _FORCE_INLINE_ uint32_t hash(const char *p_cstr) { return hash_djb2(p_cstr); }
  111. static _FORCE_INLINE_ uint32_t hash(const uint64_t p_int) { return hash_one_uint64(p_int); }
  112. static _FORCE_INLINE_ uint32_t hash(const int64_t p_int) { return hash(uint64_t(p_int)); }
  113. static _FORCE_INLINE_ uint32_t hash(const float p_float) { return hash_djb2_one_float(p_float); }
  114. static _FORCE_INLINE_ uint32_t hash(const double p_double) { return hash_djb2_one_float(p_double); }
  115. static _FORCE_INLINE_ uint32_t hash(const uint32_t p_int) { return p_int; }
  116. static _FORCE_INLINE_ uint32_t hash(const int32_t p_int) { return (uint32_t)p_int; }
  117. static _FORCE_INLINE_ uint32_t hash(const uint16_t p_int) { return p_int; }
  118. static _FORCE_INLINE_ uint32_t hash(const int16_t p_int) { return (uint32_t)p_int; }
  119. static _FORCE_INLINE_ uint32_t hash(const uint8_t p_int) { return p_int; }
  120. static _FORCE_INLINE_ uint32_t hash(const int8_t p_int) { return (uint32_t)p_int; }
  121. static _FORCE_INLINE_ uint32_t hash(const wchar_t p_wchar) { return (uint32_t)p_wchar; }
  122. //static _FORCE_INLINE_ uint32_t hash(const void* p_ptr) { return uint32_t(uint64_t(p_ptr))*(0x9e3779b1L); }
  123. };
  124. template <typename T>
  125. struct HashMapComparatorDefault {
  126. static bool compare(const T &p_lhs, const T &p_rhs) {
  127. return p_lhs == p_rhs;
  128. }
  129. bool compare(const float &p_lhs, const float &p_rhs) {
  130. return (p_lhs == p_rhs) || (Math::is_nan(p_lhs) && Math::is_nan(p_rhs));
  131. }
  132. bool compare(const double &p_lhs, const double &p_rhs) {
  133. return (p_lhs == p_rhs) || (Math::is_nan(p_lhs) && Math::is_nan(p_rhs));
  134. }
  135. };
  136. #endif