diff options
Diffstat (limited to 'core/hashfuncs.h')
-rw-r--r-- | core/hashfuncs.h | 20 |
1 files changed, 8 insertions, 12 deletions
diff --git a/core/hashfuncs.h b/core/hashfuncs.h index a41a034843..d984f6c524 100644 --- a/core/hashfuncs.h +++ b/core/hashfuncs.h @@ -49,29 +49,28 @@ * @return 32-bits hashcode */ static inline uint32_t hash_djb2(const char *p_cstr) { - const unsigned char *chr = (const unsigned char *)p_cstr; uint32_t hash = 5381; uint32_t c; - while ((c = *chr++)) + while ((c = *chr++)) { hash = ((hash << 5) + hash) + c; /* hash * 33 + c */ + } return hash; } static inline uint32_t hash_djb2_buffer(const uint8_t *p_buff, int p_len, uint32_t p_prev = 5381) { - uint32_t hash = p_prev; - for (int i = 0; i < p_len; i++) + for (int i = 0; i < p_len; i++) { hash = ((hash << 5) + hash) + p_buff[i]; /* hash * 33 + c */ + } return hash; } static inline uint32_t hash_djb2_one_32(uint32_t p_in, uint32_t p_prev = 5381) { - return ((p_prev << 5) + p_prev) + p_in; } @@ -93,19 +92,19 @@ static inline uint32_t hash_djb2_one_float(double p_in, uint32_t p_prev = 5381) } u; // Normalize +/- 0.0 and NaN values so they hash the same. - if (p_in == 0.0f) + if (p_in == 0.0f) { u.d = 0.0; - else if (Math::is_nan(p_in)) + } else if (Math::is_nan(p_in)) { u.d = Math_NAN; - else + } else { u.d = p_in; + } return ((p_prev << 5) + p_prev) + hash_one_uint64(u.i); } template <class T> static inline uint32_t make_uint32_t(T p_in) { - union { T t; uint32_t _u32; @@ -116,13 +115,11 @@ static inline uint32_t make_uint32_t(T p_in) { } static inline uint64_t hash_djb2_one_64(uint64_t p_in, uint64_t p_prev = 5381) { - return ((p_prev << 5) + p_prev) + p_in; } template <class T> static inline uint64_t make_uint64_t(T p_in) { - union { T t; uint64_t _u64; @@ -134,7 +131,6 @@ static inline uint64_t make_uint64_t(T p_in) { } struct HashMapHasherDefault { - static _FORCE_INLINE_ uint32_t hash(const String &p_string) { return p_string.hash(); } static _FORCE_INLINE_ uint32_t hash(const char *p_cstr) { return hash_djb2(p_cstr); } static _FORCE_INLINE_ uint32_t hash(const uint64_t p_int) { return hash_one_uint64(p_int); } |