diff --git a/hash.cpp b/hash.cpp index aaac446e..2a7cada1 100644 --- a/hash.cpp +++ b/hash.cpp @@ -20,9 +20,8 @@ #include "hash.hpp" -// Thomas Wang's 32 bit mixing function uint32_t -hash (uint32_t key) { +util::wang32 (uint32_t key) { uint32_t c2 = 0x27d4eb2d; // a prime or an odd constant key = (key ^ 61) ^ (key >> 16); key = key + (key << 3); @@ -33,9 +32,8 @@ hash (uint32_t key) { } -// Thomas Wang's 64 bit mixing function uint64_t -hash (uint64_t key) { +util::wang64 (uint64_t key) { key = (~key) + (key << 21); // key = (key << 21) - key - 1; key = key ^ (key >> 24); key = (key + (key << 3)) + (key << 8); // key * 265 @@ -48,6 +46,3 @@ hash (uint64_t key) { } -uintptr_t -hash (void *const key) - { return hash ((uintptr_t)key); } diff --git a/hash.hpp b/hash.hpp index f6df3498..25c1cc61 100644 --- a/hash.hpp +++ b/hash.hpp @@ -21,9 +21,24 @@ #define __UTIL_HASH_HPP #include +#include + +// All hashes are unsuitable for cryptographic operations nnless noted. +namespace util { + // Fast integer mixing operations by Thomas Wang. + uint32_t wang32 (uint32_t key); + uint64_t wang64 (uint64_t key); + + // General hashes for when you really just don't care about implementation + inline uint32_t hash (uint32_t key) { return wang32 (key); } + inline uint64_t hash (uint64_t key) { return wang64 (key); } + inline uintptr_t hash (const void *key) { + return sizeof (uintptr_t) == 32 ? wang32 (reinterpret_cast (key)) : + wang64 (reinterpret_cast (key)); + } +} + + -uint32_t hash (uint32_t key); -uint64_t hash (uint64_t key); -uintptr_t hash (void * const); #endif