hashfuncs.h 6.0 KB

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