hashfuncs.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /*************************************************************************/
  2. /* hashfuncs.hpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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_HPP
  31. #define HASHFUNCS_HPP
  32. #include <godot_cpp/core/math.hpp>
  33. #include <godot_cpp/core/object.hpp>
  34. #include <godot_cpp/variant/aabb.hpp>
  35. #include <godot_cpp/variant/node_path.hpp>
  36. #include <godot_cpp/variant/rect2.hpp>
  37. #include <godot_cpp/variant/rect2i.hpp>
  38. #include <godot_cpp/variant/rid.hpp>
  39. #include <godot_cpp/variant/string.hpp>
  40. #include <godot_cpp/variant/string_name.hpp>
  41. #include <godot_cpp/variant/variant.hpp>
  42. #include <godot_cpp/variant/vector2.hpp>
  43. #include <godot_cpp/variant/vector2i.hpp>
  44. #include <godot_cpp/variant/vector3.hpp>
  45. #include <godot_cpp/variant/vector3i.hpp>
  46. #include <godot_cpp/variant/vector4.hpp>
  47. #include <godot_cpp/variant/vector4i.hpp>
  48. /**
  49. * Hashing functions
  50. */
  51. namespace godot {
  52. /**
  53. * DJB2 Hash function
  54. * @param C String
  55. * @return 32-bits hashcode
  56. */
  57. static inline uint32_t hash_djb2(const char *p_cstr) {
  58. const unsigned char *chr = (const unsigned char *)p_cstr;
  59. uint32_t hash = 5381;
  60. uint32_t c;
  61. while ((c = *chr++)) {
  62. hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
  63. }
  64. return hash;
  65. }
  66. static inline uint32_t hash_djb2_buffer(const uint8_t *p_buff, int p_len, uint32_t p_prev = 5381) {
  67. uint32_t hash = p_prev;
  68. for (int i = 0; i < p_len; i++) {
  69. hash = ((hash << 5) + hash) + p_buff[i]; /* hash * 33 + c */
  70. }
  71. return hash;
  72. }
  73. static inline uint32_t hash_djb2_one_32(uint32_t p_in, uint32_t p_prev = 5381) {
  74. return ((p_prev << 5) + p_prev) + p_in;
  75. }
  76. /**
  77. * Thomas Wang's 64-bit to 32-bit Hash function:
  78. * https://web.archive.org/web/20071223173210/https:/www.concentric.net/~Ttwang/tech/inthash.htm
  79. *
  80. * @param p_int - 64-bit unsigned integer key to be hashed
  81. * @return unsigned 32-bit value representing hashcode
  82. */
  83. static inline uint32_t hash_one_uint64(const uint64_t p_int) {
  84. uint64_t v = p_int;
  85. v = (~v) + (v << 18); // v = (v << 18) - v - 1;
  86. v = v ^ (v >> 31);
  87. v = v * 21; // v = (v + (v << 2)) + (v << 4);
  88. v = v ^ (v >> 11);
  89. v = v + (v << 6);
  90. v = v ^ (v >> 22);
  91. return uint32_t(v);
  92. }
  93. static inline uint32_t hash_djb2_one_float(double p_in, uint32_t p_prev = 5381) {
  94. union {
  95. double d;
  96. uint64_t i;
  97. } u;
  98. // Normalize +/- 0.0 and NaN values so they hash the same.
  99. if (p_in == 0.0f) {
  100. u.d = 0.0;
  101. } else if (std::isnan(p_in)) {
  102. u.d = NAN;
  103. } else {
  104. u.d = p_in;
  105. }
  106. return ((p_prev << 5) + p_prev) + hash_one_uint64(u.i);
  107. }
  108. template <class T>
  109. static inline uint32_t make_uint32_t(T p_in) {
  110. union {
  111. T t;
  112. uint32_t _u32;
  113. } _u;
  114. _u._u32 = 0;
  115. _u.t = p_in;
  116. return _u._u32;
  117. }
  118. static inline uint64_t hash_djb2_one_float_64(double p_in, uint64_t p_prev = 5381) {
  119. union {
  120. double d;
  121. uint64_t i;
  122. } u;
  123. // Normalize +/- 0.0 and NaN values so they hash the same.
  124. if (p_in == 0.0f) {
  125. u.d = 0.0;
  126. } else if (std::isnan(p_in)) {
  127. u.d = NAN;
  128. } else {
  129. u.d = p_in;
  130. }
  131. return ((p_prev << 5) + p_prev) + u.i;
  132. }
  133. static inline uint64_t hash_djb2_one_64(uint64_t p_in, uint64_t p_prev = 5381) {
  134. return ((p_prev << 5) + p_prev) + p_in;
  135. }
  136. template <class T>
  137. static inline uint64_t make_uint64_t(T p_in) {
  138. union {
  139. T t;
  140. uint64_t _u64;
  141. } _u;
  142. _u._u64 = 0; // in case p_in is smaller
  143. _u.t = p_in;
  144. return _u._u64;
  145. }
  146. template <class T>
  147. class Ref;
  148. struct HashMapHasherDefault {
  149. static _FORCE_INLINE_ uint32_t hash(const String &p_string) { return p_string.hash(); }
  150. static _FORCE_INLINE_ uint32_t hash(const char *p_cstr) { return hash_djb2(p_cstr); }
  151. static _FORCE_INLINE_ uint32_t hash(const uint64_t p_int) { return hash_one_uint64(p_int); }
  152. static _FORCE_INLINE_ uint32_t hash(const ObjectID &p_id) { return hash_one_uint64(p_id); }
  153. static _FORCE_INLINE_ uint32_t hash(const int64_t p_int) { return hash(uint64_t(p_int)); }
  154. static _FORCE_INLINE_ uint32_t hash(const float p_float) { return hash_djb2_one_float(p_float); }
  155. static _FORCE_INLINE_ uint32_t hash(const double p_double) { return hash_djb2_one_float(p_double); }
  156. static _FORCE_INLINE_ uint32_t hash(const uint32_t p_int) { return p_int; }
  157. static _FORCE_INLINE_ uint32_t hash(const int32_t p_int) { return (uint32_t)p_int; }
  158. static _FORCE_INLINE_ uint32_t hash(const uint16_t p_int) { return p_int; }
  159. static _FORCE_INLINE_ uint32_t hash(const int16_t p_int) { return (uint32_t)p_int; }
  160. static _FORCE_INLINE_ uint32_t hash(const uint8_t p_int) { return p_int; }
  161. static _FORCE_INLINE_ uint32_t hash(const int8_t p_int) { return (uint32_t)p_int; }
  162. static _FORCE_INLINE_ uint32_t hash(const wchar_t p_wchar) { return (uint32_t)p_wchar; }
  163. static _FORCE_INLINE_ uint32_t hash(const char16_t p_uchar) { return (uint32_t)p_uchar; }
  164. static _FORCE_INLINE_ uint32_t hash(const char32_t p_uchar) { return (uint32_t)p_uchar; }
  165. static _FORCE_INLINE_ uint32_t hash(const RID &p_rid) { return hash_one_uint64(p_rid.get_id()); }
  166. static _FORCE_INLINE_ uint32_t hash(const StringName &p_string_name) { return p_string_name.hash(); }
  167. static _FORCE_INLINE_ uint32_t hash(const NodePath &p_path) { return p_path.hash(); }
  168. template <class T>
  169. static _FORCE_INLINE_ uint32_t hash(const T *p_pointer) { return hash_one_uint64((uint64_t)p_pointer); }
  170. template <class T>
  171. static _FORCE_INLINE_ uint32_t hash(const Ref<T> &p_ref) { return hash_one_uint64((uint64_t)p_ref.operator->()); }
  172. static _FORCE_INLINE_ uint32_t hash(const Vector2i &p_vec) {
  173. uint32_t h = hash_djb2_one_32(p_vec.x);
  174. return hash_djb2_one_32(p_vec.y, h);
  175. }
  176. static _FORCE_INLINE_ uint32_t hash(const Vector3i &p_vec) {
  177. uint32_t h = hash_djb2_one_32(p_vec.x);
  178. h = hash_djb2_one_32(p_vec.y, h);
  179. return hash_djb2_one_32(p_vec.z, h);
  180. }
  181. static _FORCE_INLINE_ uint32_t hash(const Vector4i &p_vec) {
  182. uint32_t h = hash_murmur3_one_32(p_vec.x);
  183. h = hash_murmur3_one_32(p_vec.y, h);
  184. h = hash_murmur3_one_32(p_vec.z, h);
  185. h = hash_murmur3_one_32(p_vec.w, h);
  186. return hash_fmix32(h);
  187. }
  188. static _FORCE_INLINE_ uint32_t hash(const Vector2 &p_vec) {
  189. uint32_t h = hash_djb2_one_float(p_vec.x);
  190. return hash_djb2_one_float(p_vec.y, h);
  191. }
  192. static _FORCE_INLINE_ uint32_t hash(const Vector3 &p_vec) {
  193. uint32_t h = hash_djb2_one_float(p_vec.x);
  194. h = hash_djb2_one_float(p_vec.y, h);
  195. return hash_djb2_one_float(p_vec.z, h);
  196. }
  197. static _FORCE_INLINE_ uint32_t hash(const Vector4 &p_vec) {
  198. uint32_t h = hash_murmur3_one_real(p_vec.x);
  199. h = hash_murmur3_one_real(p_vec.y, h);
  200. h = hash_murmur3_one_real(p_vec.z, h);
  201. h = hash_murmur3_one_real(p_vec.w, h);
  202. return hash_fmix32(h);
  203. }
  204. static _FORCE_INLINE_ uint32_t hash(const Rect2i &p_rect) {
  205. uint32_t h = hash_djb2_one_32(p_rect.position.x);
  206. h = hash_djb2_one_32(p_rect.position.y, h);
  207. h = hash_djb2_one_32(p_rect.size.x, h);
  208. return hash_djb2_one_32(p_rect.size.y, h);
  209. }
  210. static _FORCE_INLINE_ uint32_t hash(const Rect2 &p_rect) {
  211. uint32_t h = hash_djb2_one_float(p_rect.position.x);
  212. h = hash_djb2_one_float(p_rect.position.y, h);
  213. h = hash_djb2_one_float(p_rect.size.x, h);
  214. return hash_djb2_one_float(p_rect.size.y, h);
  215. }
  216. static _FORCE_INLINE_ uint32_t hash(const AABB &p_aabb) {
  217. uint32_t h = hash_djb2_one_float(p_aabb.position.x);
  218. h = hash_djb2_one_float(p_aabb.position.y, h);
  219. h = hash_djb2_one_float(p_aabb.position.z, h);
  220. h = hash_djb2_one_float(p_aabb.size.x, h);
  221. h = hash_djb2_one_float(p_aabb.size.y, h);
  222. return hash_djb2_one_float(p_aabb.size.z, h);
  223. }
  224. // static _FORCE_INLINE_ uint32_t hash(const void* p_ptr) { return uint32_t(uint64_t(p_ptr))*(0x9e3779b1L); }
  225. };
  226. template <typename T>
  227. struct HashMapComparatorDefault {
  228. static bool compare(const T &p_lhs, const T &p_rhs) {
  229. return p_lhs == p_rhs;
  230. }
  231. };
  232. template <>
  233. struct HashMapComparatorDefault<float> {
  234. static bool compare(const float &p_lhs, const float &p_rhs) {
  235. return (p_lhs == p_rhs) || (std::isnan(p_lhs) && std::isnan(p_rhs));
  236. }
  237. };
  238. template <>
  239. struct HashMapComparatorDefault<double> {
  240. static bool compare(const double &p_lhs, const double &p_rhs) {
  241. return (p_lhs == p_rhs) || (std::isnan(p_lhs) && std::isnan(p_rhs));
  242. }
  243. };
  244. template <>
  245. struct HashMapComparatorDefault<Vector2> {
  246. static bool compare(const Vector2 &p_lhs, const Vector2 &p_rhs) {
  247. return ((p_lhs.x == p_rhs.x) || (std::isnan(p_lhs.x) && std::isnan(p_rhs.x))) && ((p_lhs.y == p_rhs.y) || (std::isnan(p_lhs.y) && std::isnan(p_rhs.y)));
  248. }
  249. };
  250. template <>
  251. struct HashMapComparatorDefault<Vector3> {
  252. static bool compare(const Vector3 &p_lhs, const Vector3 &p_rhs) {
  253. return ((p_lhs.x == p_rhs.x) || (std::isnan(p_lhs.x) && std::isnan(p_rhs.x))) && ((p_lhs.y == p_rhs.y) || (std::isnan(p_lhs.y) && std::isnan(p_rhs.y))) && ((p_lhs.z == p_rhs.z) || (std::isnan(p_lhs.z) && std::isnan(p_rhs.z)));
  254. }
  255. };
  256. constexpr uint32_t HASH_TABLE_SIZE_MAX = 29;
  257. const uint32_t hash_table_size_primes[HASH_TABLE_SIZE_MAX] = {
  258. 5,
  259. 13,
  260. 23,
  261. 47,
  262. 97,
  263. 193,
  264. 389,
  265. 769,
  266. 1543,
  267. 3079,
  268. 6151,
  269. 12289,
  270. 24593,
  271. 49157,
  272. 98317,
  273. 196613,
  274. 393241,
  275. 786433,
  276. 1572869,
  277. 3145739,
  278. 6291469,
  279. 12582917,
  280. 25165843,
  281. 50331653,
  282. 100663319,
  283. 201326611,
  284. 402653189,
  285. 805306457,
  286. 1610612741,
  287. };
  288. } // namespace godot
  289. #endif // HASHFUNCS_HPP