diff --git a/parallel_hashmap/phmap.h b/parallel_hashmap/phmap.h index 1fe4a4f..1870231 100644 --- a/parallel_hashmap/phmap.h +++ b/parallel_hashmap/phmap.h @@ -3992,8 +3992,7 @@ public: template struct HashEq { - //using Hash = phmap::Hash; - using Hash = std::hash; + using Hash = phmap::Hash; using Eq = std::equal_to; }; @@ -4013,12 +4012,16 @@ struct HashEq { using Eq = std::equal_to; }; +#endif + +#if PHMAP_HAVE_STD_STRING_VIEW + struct StringHash { using is_transparent = void; - size_t operator()(phmap::string_view v) const { - return phmap::Hash{}(v); + size_t operator()(std::string_view v) const { + return phmap::Hash{}(v); } }; @@ -4028,7 +4031,7 @@ struct StringHashEq using Hash = StringHash; struct Eq { using is_transparent = void; - bool operator()(phmap::string_view lhs, phmap::string_view rhs) const { + bool operator()(std::string_view lhs, std::string_view rhs) const { return lhs == rhs; } }; @@ -4038,7 +4041,7 @@ template <> struct HashEq : StringHashEq {}; template <> -struct HashEq : StringHashEq {}; +struct HashEq : StringHashEq {}; #endif @@ -4050,8 +4053,8 @@ struct HashEq using is_transparent = void; template size_t operator()(const U& ptr) const { - //return phmap::Hash{}(HashEq::ToPtr(ptr)); - return std::hash{}(HashEq::ToPtr(ptr)); + return phmap::Hash{}(HashEq::ToPtr(ptr)); + //return std::hash{}(HashEq::ToPtr(ptr)); } }; diff --git a/parallel_hashmap/phmap_base.h b/parallel_hashmap/phmap_base.h index aede512..a63bf05 100644 --- a/parallel_hashmap/phmap_base.h +++ b/parallel_hashmap/phmap_base.h @@ -1707,7 +1707,7 @@ struct nullopt_t explicit constexpr nullopt_t(init_t& /*unused*/) {} }; -extern const nullopt_t nullopt; +constexpr nullopt_t nullopt(nullopt_t::init); namespace optional_internal { diff --git a/parallel_hashmap/phmap_utils.h b/parallel_hashmap/phmap_utils.h index d65200a..8ac91eb 100644 --- a/parallel_hashmap/phmap_utils.h +++ b/parallel_hashmap/phmap_utils.h @@ -34,7 +34,212 @@ // limitations under the License. // --------------------------------------------------------------------------- +//#include +#include +#define PHMAP_HASH_CLASS std::hash +namespace phmap +{ + +template T phmap_min(T a, T b) { return a < b ? a : b; } +template T phmap_max(T a, T b) { return a >= b ? a : b; } + +template +struct Hash +{ + inline size_t operator()(const T &__v) const noexcept + { + PHMAP_HASH_CLASS hasher; + return hasher(__v); + } +}; + +template +struct Hash +{ + static size_t spp_log2 (size_t val) noexcept + { + size_t res = 0; + while (val > 1) + { + val >>= 1; + res++; + } + return res; + } + + inline size_t operator()(const T *__v) const noexcept + { + static const size_t shift = 3; // spp_log2(1 + sizeof(T)); // T might be incomplete! + const uintptr_t i = (const uintptr_t)__v; + return static_cast(i >> shift); + } +}; + +// from http://burtleburtle.net/bob/hash/integer.html +// fast and efficient for power of two table sizes where we always +// consider the last bits. +// --------------------------------------------------------------- +inline size_t spp_mix_32(uint32_t a) +{ + a = a ^ (a >> 4); + a = (a ^ 0xdeadbeef) + (a << 5); + a = a ^ (a >> 11); + return static_cast(a); +} + +// More thorough scrambling as described in +// https://gist.github.com/badboy/6267743 +// ---------------------------------------- +inline size_t spp_mix_64(uint64_t a) +{ + a = (~a) + (a << 21); // a = (a << 21) - a - 1; + a = a ^ (a >> 24); + a = (a + (a << 3)) + (a << 8); // a * 265 + a = a ^ (a >> 14); + a = (a + (a << 2)) + (a << 4); // a * 21 + a = a ^ (a >> 28); + a = a + (a << 31); + return static_cast(a); +} + +template +struct spp_unary_function +{ + typedef ArgumentType argument_type; + typedef ResultType result_type; +}; + +template <> +struct Hash : public spp_unary_function +{ + inline size_t operator()(bool __v) const noexcept + { return static_cast(__v); } +}; + +template <> +struct Hash : public spp_unary_function +{ + inline size_t operator()(char __v) const noexcept + { return static_cast(__v); } +}; + +template <> +struct Hash : public spp_unary_function +{ + inline size_t operator()(signed char __v) const noexcept + { return static_cast(__v); } +}; + +template <> +struct Hash : public spp_unary_function +{ + inline size_t operator()(unsigned char __v) const noexcept + { return static_cast(__v); } +}; + +template <> +struct Hash : public spp_unary_function +{ + inline size_t operator()(wchar_t __v) const noexcept + { return static_cast(__v); } +}; + +template <> +struct Hash : public spp_unary_function +{ + inline size_t operator()(int16_t __v) const noexcept + { return spp_mix_32(static_cast(__v)); } +}; + +template <> +struct Hash : public spp_unary_function +{ + inline size_t operator()(uint16_t __v) const noexcept + { return spp_mix_32(static_cast(__v)); } +}; + +template <> +struct Hash : public spp_unary_function +{ + inline size_t operator()(int32_t __v) const noexcept + { return spp_mix_32(static_cast(__v)); } +}; + +template <> +struct Hash : public spp_unary_function +{ + inline size_t operator()(uint32_t __v) const noexcept + { return spp_mix_32(static_cast(__v)); } +}; + +template <> +struct Hash : public spp_unary_function +{ + inline size_t operator()(int64_t __v) const noexcept + { return spp_mix_64(static_cast(__v)); } +}; + +template <> +struct Hash : public spp_unary_function +{ + inline size_t operator()(uint64_t __v) const noexcept + { return spp_mix_64(static_cast(__v)); } +}; + +template <> +struct Hash : public spp_unary_function +{ + inline size_t operator()(float __v) const noexcept + { + // -0.0 and 0.0 should return same hash + uint32_t *as_int = reinterpret_cast(&__v); + return (__v == 0) ? static_cast(0) : spp_mix_32(*as_int); + } +}; + +template <> +struct Hash : public spp_unary_function +{ + inline size_t operator()(double __v) const noexcept + { + // -0.0 and 0.0 should return same hash + uint64_t *as_int = reinterpret_cast(&__v); + return (__v == 0) ? static_cast(0) : spp_mix_64(*as_int); + } +}; + +template struct Combiner +{ + inline void operator()(T& seed, T value); +}; + +template struct Combiner +{ + inline void operator()(T& seed, T value) + { + seed ^= value + 0x9e3779b9 + (seed << 6) + (seed >> 2); + } +}; + +template struct Combiner +{ + inline void operator()(T& seed, T value) + { + seed ^= value + T(0xc6a4a7935bd1e995) + (seed << 6) + (seed >> 2); + } +}; + +template +inline void hash_combine(std::size_t& seed, T const& v) +{ + spp_::Hash hasher; + Combiner combiner; + + combiner(seed, hasher(v)); +} + +} #endif // phmap_utils_h_guard_