hashfuncs.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*************************************************************************/
  2. /* hashfuncs.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 "core/math/math_defs.h"
  33. #include "core/math/math_funcs.h"
  34. #include "core/node_path.h"
  35. #include "core/string_name.h"
  36. #include "core/typedefs.h"
  37. #include "core/ustring.h"
  38. /**
  39. * Hashing functions
  40. */
  41. /**
  42. * DJB2 Hash function
  43. * @param C String
  44. * @return 32-bits hashcode
  45. */
  46. static inline uint32_t hash_djb2(const char *p_cstr) {
  47. const unsigned char *chr = (const unsigned char *)p_cstr;
  48. uint32_t hash = 5381;
  49. uint32_t c;
  50. while ((c = *chr++))
  51. hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
  52. return hash;
  53. }
  54. static inline uint32_t hash_djb2_buffer(const uint8_t *p_buff, int p_len, uint32_t p_prev = 5381) {
  55. uint32_t hash = p_prev;
  56. for (int i = 0; i < p_len; i++)
  57. hash = ((hash << 5) + hash) + p_buff[i]; /* hash * 33 + c */
  58. return hash;
  59. }
  60. static inline uint32_t hash_djb2_one_32(uint32_t p_in, uint32_t p_prev = 5381) {
  61. return ((p_prev << 5) + p_prev) + p_in;
  62. }
  63. static inline uint32_t hash_one_uint64(const uint64_t p_int) {
  64. uint64_t v = p_int;
  65. v = (~v) + (v << 18); // v = (v << 18) - v - 1;
  66. v = v ^ (v >> 31);
  67. v = v * 21; // v = (v + (v << 2)) + (v << 4);
  68. v = v ^ (v >> 11);
  69. v = v + (v << 6);
  70. v = v ^ (v >> 22);
  71. return (int)v;
  72. }
  73. static inline uint32_t hash_djb2_one_float(double p_in, uint32_t p_prev = 5381) {
  74. union {
  75. double d;
  76. uint64_t i;
  77. } u;
  78. // Normalize +/- 0.0 and NaN values so they hash the same.
  79. if (p_in == 0.0f)
  80. u.d = 0.0;
  81. else if (Math::is_nan(p_in))
  82. u.d = Math_NAN;
  83. else
  84. u.d = p_in;
  85. return ((p_prev << 5) + p_prev) + hash_one_uint64(u.i);
  86. }
  87. template <class T>
  88. static inline uint32_t make_uint32_t(T p_in) {
  89. union {
  90. T t;
  91. uint32_t _u32;
  92. } _u;
  93. _u._u32 = 0;
  94. _u.t = p_in;
  95. return _u._u32;
  96. }
  97. static inline uint64_t hash_djb2_one_64(uint64_t p_in, uint64_t p_prev = 5381) {
  98. return ((p_prev << 5) + p_prev) + p_in;
  99. }
  100. template <class T>
  101. static inline uint64_t make_uint64_t(T p_in) {
  102. union {
  103. T t;
  104. uint64_t _u64;
  105. } _u;
  106. _u._u64 = 0; // in case p_in is smaller
  107. _u.t = p_in;
  108. return _u._u64;
  109. }
  110. struct HashMapHasherDefault {
  111. static _FORCE_INLINE_ uint32_t hash(const String &p_string) { return p_string.hash(); }
  112. static _FORCE_INLINE_ uint32_t hash(const char *p_cstr) { return hash_djb2(p_cstr); }
  113. static _FORCE_INLINE_ uint32_t hash(const uint64_t p_int) { return hash_one_uint64(p_int); }
  114. static _FORCE_INLINE_ uint32_t hash(const int64_t p_int) { return hash(uint64_t(p_int)); }
  115. static _FORCE_INLINE_ uint32_t hash(const float p_float) { return hash_djb2_one_float(p_float); }
  116. static _FORCE_INLINE_ uint32_t hash(const double p_double) { return hash_djb2_one_float(p_double); }
  117. static _FORCE_INLINE_ uint32_t hash(const uint32_t p_int) { return p_int; }
  118. static _FORCE_INLINE_ uint32_t hash(const int32_t p_int) { return (uint32_t)p_int; }
  119. static _FORCE_INLINE_ uint32_t hash(const uint16_t p_int) { return p_int; }
  120. static _FORCE_INLINE_ uint32_t hash(const int16_t p_int) { return (uint32_t)p_int; }
  121. static _FORCE_INLINE_ uint32_t hash(const uint8_t p_int) { return p_int; }
  122. static _FORCE_INLINE_ uint32_t hash(const int8_t p_int) { return (uint32_t)p_int; }
  123. static _FORCE_INLINE_ uint32_t hash(const wchar_t p_wchar) { return (uint32_t)p_wchar; }
  124. static _FORCE_INLINE_ uint32_t hash(const StringName &p_string_name) { return p_string_name.hash(); }
  125. static _FORCE_INLINE_ uint32_t hash(const NodePath &p_path) { return p_path.hash(); }
  126. //static _FORCE_INLINE_ uint32_t hash(const void* p_ptr) { return uint32_t(uint64_t(p_ptr))*(0x9e3779b1L); }
  127. };
  128. template <typename T>
  129. struct HashMapComparatorDefault {
  130. static bool compare(const T &p_lhs, const T &p_rhs) {
  131. return p_lhs == p_rhs;
  132. }
  133. bool compare(const float &p_lhs, const float &p_rhs) {
  134. return (p_lhs == p_rhs) || (Math::is_nan(p_lhs) && Math::is_nan(p_rhs));
  135. }
  136. bool compare(const double &p_lhs, const double &p_rhs) {
  137. return (p_lhs == p_rhs) || (Math::is_nan(p_lhs) && Math::is_nan(p_rhs));
  138. }
  139. };
  140. #endif