diff options
Diffstat (limited to 'source/blender/blenlib/BLI_hash.hh')
-rw-r--r-- | source/blender/blenlib/BLI_hash.hh | 62 |
1 files changed, 24 insertions, 38 deletions
diff --git a/source/blender/blenlib/BLI_hash.hh b/source/blender/blenlib/BLI_hash.hh index 5cd4ce3c1a9..b14a4ca933c 100644 --- a/source/blender/blenlib/BLI_hash.hh +++ b/source/blender/blenlib/BLI_hash.hh @@ -38,7 +38,7 @@ * multiple `operator()` in a specialization of #DefaultHash. All those methods have to compute the * same hash for values that compare equal. * - * The computed hash is an unsigned 32 bit integer. Ideally, the hash function would generate + * The computed hash is an unsigned 64 bit integer. Ideally, the hash function would generate * uniformly random hash values for a set of keys. However, in many cases trivial hash functions * are faster and produce a good enough distribution. In general it is better when more information * is in the lower bits of the hash. By choosing a good probing strategy, the effects of a bad hash @@ -49,7 +49,7 @@ * There are three main ways to provide a hash table implementation with a custom hash function. * * - When you want to provide a default hash function for your own custom type: Add a `hash` - * member function to it. The function should return `uint32_t` and take no arguments. This + * member function to it. The function should return `uint64_t` and take no arguments. This * method will be called by the default implementation of #DefaultHash. It will automatically be * used by hash table implementations. * @@ -58,7 +58,7 @@ * either global or BLI namespace. * * template<> struct blender::DefaultHash<TheType> { - * uint32_t operator()(const TheType &value) const { + * uint64_t operator()(const TheType &value) const { * return ...; * } * }; @@ -68,7 +68,7 @@ * table explicitly. * * struct MyCustomHash { - * uint32_t operator()(const TheType &value) const { + * uint64_t operator()(const TheType &value) const { * return ...; * } * }; @@ -91,7 +91,7 @@ namespace blender { * that you have to implement a hash function using one of three strategies listed above. */ template<typename T> struct DefaultHash { - uint32_t operator()(const T &value) const + uint64_t operator()(const T &value) const { return value.hash(); } @@ -101,7 +101,7 @@ template<typename T> struct DefaultHash { * Use the same hash function for const and non const variants of a type. */ template<typename T> struct DefaultHash<const T> { - uint32_t operator()(const T &value) const + uint64_t operator()(const T &value) const { return DefaultHash<T>{}(value); } @@ -109,9 +109,9 @@ template<typename T> struct DefaultHash<const T> { #define TRIVIAL_DEFAULT_INT_HASH(TYPE) \ template<> struct DefaultHash<TYPE> { \ - uint32_t operator()(TYPE value) const \ + uint64_t operator()(TYPE value) const \ { \ - return (uint32_t)value; \ + return (uint64_t)value; \ } \ } @@ -127,43 +127,29 @@ TRIVIAL_DEFAULT_INT_HASH(int16_t); TRIVIAL_DEFAULT_INT_HASH(uint16_t); TRIVIAL_DEFAULT_INT_HASH(int32_t); TRIVIAL_DEFAULT_INT_HASH(uint32_t); - -template<> struct DefaultHash<uint64_t> { - uint32_t operator()(uint64_t value) const - { - uint32_t low = (uint32_t)value; - uint32_t high = (uint32_t)(value >> 32); - return low ^ (high * 0x45d9f3b); - } -}; - -template<> struct DefaultHash<int64_t> { - uint32_t operator()(uint64_t value) const - { - return DefaultHash<uint64_t>{}((uint64_t)value); - } -}; +TRIVIAL_DEFAULT_INT_HASH(int64_t); +TRIVIAL_DEFAULT_INT_HASH(uint64_t); /** * One should try to avoid using floats as keys in hash tables, but sometimes it is convenient. */ template<> struct DefaultHash<float> { - uint32_t operator()(float value) const + uint64_t operator()(float value) const { return *(uint32_t *)&value; } }; template<> struct DefaultHash<bool> { - uint32_t operator()(bool value) const + uint64_t operator()(bool value) const { - return (uint32_t)(value != false) * 1298191; + return (uint64_t)(value != false) * 1298191; } }; -inline uint32_t hash_string(StringRef str) +inline uint64_t hash_string(StringRef str) { - uint32_t hash = 5381; + uint64_t hash = 5381; for (char c : str) { hash = hash * 33 + c; } @@ -175,21 +161,21 @@ template<> struct DefaultHash<std::string> { * Take a #StringRef as parameter to support heterogeneous lookups in hash table implementations * when std::string is used as key. */ - uint32_t operator()(StringRef value) const + uint64_t operator()(StringRef value) const { return hash_string(value); } }; template<> struct DefaultHash<StringRef> { - uint32_t operator()(StringRef value) const + uint64_t operator()(StringRef value) const { return hash_string(value); } }; template<> struct DefaultHash<StringRefNull> { - uint32_t operator()(StringRef value) const + uint64_t operator()(StringRef value) const { return hash_string(value); } @@ -199,26 +185,26 @@ template<> struct DefaultHash<StringRefNull> { * While we cannot guarantee that the lower 4 bits of a pointer are zero, it is often the case. */ template<typename T> struct DefaultHash<T *> { - uint32_t operator()(const T *value) const + uint64_t operator()(const T *value) const { uintptr_t ptr = (uintptr_t)value; - uint32_t hash = (uint32_t)(ptr >> 4); + uint64_t hash = (uint64_t)(ptr >> 4); return hash; } }; template<typename T> struct DefaultHash<std::unique_ptr<T>> { - uint32_t operator()(const std::unique_ptr<T> &value) const + uint64_t operator()(const std::unique_ptr<T> &value) const { return DefaultHash<T *>{}(value.get()); } }; template<typename T1, typename T2> struct DefaultHash<std::pair<T1, T2>> { - uint32_t operator()(const std::pair<T1, T2> &value) const + uint64_t operator()(const std::pair<T1, T2> &value) const { - uint32_t hash1 = DefaultHash<T1>{}(value.first); - uint32_t hash2 = DefaultHash<T2>{}(value.second); + uint64_t hash1 = DefaultHash<T1>{}(value.first); + uint64_t hash2 = DefaultHash<T2>{}(value.second); return hash1 ^ (hash2 * 33); } }; |