// Copyright 2005-2014 Daniel James. // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // Based on Peter Dimov's proposal // http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2005/n1756.pdf // issue 6.18. // // This also contains public domain code from MurmurHash. From the // MurmurHash header: // MurmurHash3 was written by Austin Appleby, and is placed in the public // domain. The author hereby disclaims copyright to this source code. #if !defined(CCSTD_FUNCTIONAL_HASH_HASH_HPP) #define CCSTD_FUNCTIONAL_HASH_HASH_HPP #include "boost/container_hash/hash.hpp" #include "base/std/hash/hash_fwd.hpp" #include #include #include "base/std/hash/detail/hash_float.hpp" #include #include #include #include #include #include #include #if defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) #include #endif #if !defined(BOOST_NO_CXX11_HDR_TYPEINDEX) #include #endif #if !defined(BOOST_NO_CXX11_HDR_SYSTEM_ERROR) #include #endif #if defined(BOOST_MSVC) #pragma warning(push) #if BOOST_MSVC >= 1400 #pragma warning(disable:6295) // Ill-defined for-loop : 'unsigned int' values // are always of range '0' to '4294967295'. // Loop executes infinitely. #endif #endif #if BOOST_WORKAROUND(__GNUC__, < 3) \ && !defined(__SGI_STL_PORT) && !defined(_STLPORT_VERSION) #define CCSTD_HASH_CHAR_TRAITS string_char_traits #else #define CCSTD_HASH_CHAR_TRAITS char_traits #endif #if defined(_MSC_VER) # define CCSTD_FUNCTIONAL_HASH_ROTL32(x, r) _rotl(x,r) #else # define CCSTD_FUNCTIONAL_HASH_ROTL32(x, r) (x << r) | (x >> (32 - r)) #endif // Detect whether standard library has C++17 headers #if !defined(CCSTD_HASH_CXX17) # if defined(BOOST_MSVC) # if defined(_HAS_CXX17) && _HAS_CXX17 # define CCSTD_HASH_CXX17 1 # endif # elif defined(__cplusplus) && __cplusplus >= 201703 # define CCSTD_HASH_CXX17 1 # endif #endif #if !defined(CCSTD_HASH_CXX17) # define CCSTD_HASH_CXX17 0 #endif #if CCSTD_HASH_CXX17 && defined(__has_include) # if !defined(CCSTD_HASH_HAS_STRING_VIEW) && __has_include() # define CCSTD_HASH_HAS_STRING_VIEW 1 # endif # if !defined(CCSTD_HASH_HAS_OPTIONAL) && __has_include() # define CCSTD_HASH_HAS_OPTIONAL 1 # endif # if !defined(CCSTD_HASH_HAS_VARIANT) && __has_include() # define CCSTD_HASH_HAS_VARIANT 1 # endif #endif #if !defined(CCSTD_HASH_HAS_STRING_VIEW) # define CCSTD_HASH_HAS_STRING_VIEW 0 #endif #if !defined(CCSTD_HASH_HAS_OPTIONAL) # define CCSTD_HASH_HAS_OPTIONAL 0 #endif #if !defined(CCSTD_HASH_HAS_VARIANT) # define CCSTD_HASH_HAS_VARIANT 0 #endif #if CCSTD_HASH_HAS_STRING_VIEW # include #endif #if CCSTD_HASH_HAS_OPTIONAL # include #endif #if CCSTD_HASH_HAS_VARIANT # include #endif namespace ccstd { namespace hash_detail { #if defined(BOOST_NO_CXX98_FUNCTION_BASE) template struct hash_base { typedef T argument_type; typedef hash_t result_type; }; #else template struct hash_base : std::unary_function {}; #endif struct enable_hash_value { typedef hash_t type; }; template struct basic_numbers {}; template struct long_numbers; template struct ulong_numbers; template struct float_numbers {}; template <> struct basic_numbers : ccstd::hash_detail::enable_hash_value {}; template <> struct basic_numbers : ccstd::hash_detail::enable_hash_value {}; template <> struct basic_numbers : ccstd::hash_detail::enable_hash_value {}; template <> struct basic_numbers : ccstd::hash_detail::enable_hash_value {}; template <> struct basic_numbers : ccstd::hash_detail::enable_hash_value {}; template <> struct basic_numbers : ccstd::hash_detail::enable_hash_value {}; template <> struct basic_numbers : ccstd::hash_detail::enable_hash_value {}; template <> struct basic_numbers : ccstd::hash_detail::enable_hash_value {}; template <> struct basic_numbers : ccstd::hash_detail::enable_hash_value {}; template <> struct basic_numbers : ccstd::hash_detail::enable_hash_value {}; #if !defined(BOOST_NO_INTRINSIC_WCHAR_T) template <> struct basic_numbers : ccstd::hash_detail::enable_hash_value {}; #endif #if !defined(BOOST_NO_CXX11_CHAR16_T) template <> struct basic_numbers : ccstd::hash_detail::enable_hash_value {}; #endif #if !defined(BOOST_NO_CXX11_CHAR32_T) template <> struct basic_numbers : ccstd::hash_detail::enable_hash_value {}; #endif // long_numbers is defined like this to allow for separate // specialization for long_long and int128_type, in case // they conflict. template struct long_numbers2 {}; template struct ulong_numbers2 {}; template struct long_numbers : long_numbers2 {}; template struct ulong_numbers : ulong_numbers2 {}; #if !defined(BOOST_NO_LONG_LONG) template <> struct long_numbers : ccstd::hash_detail::enable_hash_value {}; template <> struct ulong_numbers : ccstd::hash_detail::enable_hash_value {}; #endif #if defined(BOOST_HAS_INT128) template <> struct long_numbers2 : ccstd::hash_detail::enable_hash_value {}; template <> struct ulong_numbers2 : ccstd::hash_detail::enable_hash_value {}; #endif template <> struct float_numbers : ccstd::hash_detail::enable_hash_value {}; template <> struct float_numbers : ccstd::hash_detail::enable_hash_value {}; template <> struct float_numbers : ccstd::hash_detail::enable_hash_value {}; } template typename ccstd::hash_detail::basic_numbers::type hash_value(T); template typename ccstd::hash_detail::long_numbers::type hash_value(T); template typename ccstd::hash_detail::ulong_numbers::type hash_value(T); template typename boost::enable_if, hash_t>::type hash_value(T); #if !BOOST_WORKAROUND(__DMC__, <= 0x848) template hash_t hash_value(T* const&); #else template hash_t hash_value(T*); #endif #if !defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING) template< class T, unsigned N > hash_t hash_value(const T (&x)[N]); template< class T, unsigned N > hash_t hash_value(T (&x)[N]); #endif template hash_t hash_value( std::basic_string, A> const&); #if CCSTD_HASH_HAS_STRING_VIEW template hash_t hash_value( std::basic_string_view > const&); #endif template typename ccstd::hash_detail::float_numbers::type hash_value(T); #if CCSTD_HASH_HAS_OPTIONAL template hash_t hash_value(std::optional const&); #endif #if CCSTD_HASH_HAS_VARIANT hash_t hash_value(std::monostate); template hash_t hash_value(std::variant const&); #endif #if !defined(BOOST_NO_CXX11_HDR_TYPEINDEX) hash_t hash_value(std::type_index); #endif #if !defined(BOOST_NO_CXX11_HDR_SYSTEM_ERROR) hash_t hash_value(std::error_code const&); hash_t hash_value(std::error_condition const&); #endif // Implementation namespace hash_detail { template inline hash_t hash_value_signed(T val) { const unsigned int size_t_bits = std::numeric_limits::digits; // ceiling(std::numeric_limits::digits / size_t_bits) - 1 const int length = (std::numeric_limits::digits - 1) / static_cast(size_t_bits); hash_t seed = 0; T positive = val < 0 ? -1 - val : val; // Hopefully, this loop can be unrolled. for(unsigned int i = length * size_t_bits; i > 0; i -= size_t_bits) { seed ^= (hash_t) (positive >> i) + (seed<<6) + (seed>>2); } seed ^= (hash_t) val + (seed<<6) + (seed>>2); return seed; } template inline hash_t hash_value_unsigned(T val) { const unsigned int size_t_bits = std::numeric_limits::digits; // ceiling(std::numeric_limits::digits / size_t_bits) - 1 const int length = (std::numeric_limits::digits - 1) / static_cast(size_t_bits); hash_t seed = 0; // Hopefully, this loop can be unrolled. for(unsigned int i = length * size_t_bits; i > 0; i -= size_t_bits) { seed ^= (hash_t) (val >> i) + (seed<<6) + (seed>>2); } seed ^= (hash_t) val + (seed<<6) + (seed>>2); return seed; } template struct hash_combine_impl { template inline static SizeT fn(SizeT seed, SizeT value) { seed ^= value + 0x9e3779b9 + (seed<<6) + (seed>>2); return seed; } }; template<> struct hash_combine_impl<32> { inline static std::uint32_t fn(std::uint32_t h1, std::uint32_t k1) { const std::uint32_t c1 = 0xcc9e2d51; const std::uint32_t c2 = 0x1b873593; k1 *= c1; k1 = CCSTD_FUNCTIONAL_HASH_ROTL32(k1,15); k1 *= c2; h1 ^= k1; h1 = CCSTD_FUNCTIONAL_HASH_ROTL32(h1,13); h1 = h1*5+0xe6546b64; return h1; } }; template<> struct hash_combine_impl<64> { inline static std::uint64_t fn(std::uint64_t h, std::uint64_t k) { const std::uint64_t m = (std::uint64_t(0xc6a4a793) << 32) + 0x5bd1e995; const int r = 47; k *= m; k ^= k >> r; k *= m; h ^= k; h *= m; // Completely arbitrary number, to prevent 0's // from hashing to 0. h += 0xe6546b64; return h; } }; } template typename ccstd::hash_detail::basic_numbers::type hash_value(T v) { return static_cast(v); } template typename ccstd::hash_detail::long_numbers::type hash_value(T v) { return hash_detail::hash_value_signed(v); } template typename ccstd::hash_detail::ulong_numbers::type hash_value(T v) { return hash_detail::hash_value_unsigned(v); } template typename boost::enable_if, hash_t>::type hash_value(T v) { return static_cast(v); } // Implementation by Alberto Barbati and Dave Harris. #if !BOOST_WORKAROUND(__DMC__, <= 0x848) template hash_t hash_value(T* const& v) #else template hash_t hash_value(T* v) #endif { #if defined(__VMS) && __INITIAL_POINTER_SIZE == 64 // for some reason ptrdiff_t on OpenVMS compiler with // 64 bit is not 64 bit !!! hash_t x = static_cast( reinterpret_cast(v)); #else hash_t x = static_cast( reinterpret_cast(v)); #endif return x + (x >> 3); } #if defined(BOOST_MSVC) #pragma warning(push) #if BOOST_MSVC <= 1400 #pragma warning(disable:4267) // 'argument' : conversion from 'size_t' to // 'unsigned int', possible loss of data // A misguided attempt to detect 64-bit // incompatability. #endif #endif template inline void hash_combine(hash_t& seed, T const& v) { ccstd::hash hasher; seed = ccstd::hash_detail::hash_combine_impl::fn(seed, hasher(v)); } #if defined(BOOST_MSVC) #pragma warning(pop) #endif template inline hash_t hash_range(It first, It last) { hash_t seed = 0; for(; first != last; ++first) { hash_combine::value_type>(seed, *first); } return seed; } template inline void hash_range(hash_t& seed, It first, It last) { for(; first != last; ++first) { hash_combine::value_type>(seed, *first); } } #if BOOST_WORKAROUND(BOOST_BORLANDC, BOOST_TESTED_AT(0x551)) template inline hash_t hash_range(T* first, T* last) { hash_t seed = 0; for(; first != last; ++first) { ccstd::hash hasher; seed ^= hasher(*first) + 0x9e3779b9 + (seed<<6) + (seed>>2); } return seed; } template inline void hash_range(hash_t& seed, T* first, T* last) { for(; first != last; ++first) { ccstd::hash hasher; seed ^= hasher(*first) + 0x9e3779b9 + (seed<<6) + (seed>>2); } } #endif #if !defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING) template< class T, unsigned N > inline hash_t hash_value(const T (&x)[N]) { return hash_range(x, x + N); } template< class T, unsigned N > inline hash_t hash_value(T (&x)[N]) { return hash_range(x, x + N); } #endif template inline hash_t hash_value( std::basic_string, A> const& v) { return hash_range(v.begin(), v.end()); } #if CCSTD_HASH_HAS_STRING_VIEW template inline hash_t hash_value( std::basic_string_view > const& v) { return hash_range(v.begin(), v.end()); } #endif template typename ccstd::hash_detail::float_numbers::type hash_value(T v) { return ccstd::hash_detail::float_hash_value(v); } #if CCSTD_HASH_HAS_OPTIONAL template inline hash_t hash_value(std::optional const& v) { if (!v) { // Arbitray value for empty optional. return 0x12345678; } else { ccstd::hash hf; return hf(*v); } } #endif #if CCSTD_HASH_HAS_VARIANT inline hash_t hash_value(std::monostate) { return 0x87654321; } template inline hash_t hash_value(std::variant const& v) { hash_t seed = 0; hash_combine(seed, v.index()); std::visit([&seed](auto&& x) { hash_combine(seed, x); }, v); return seed; } #endif #if !defined(BOOST_NO_CXX11_HDR_TYPEINDEX) inline hash_t hash_value(std::type_index v) { size_t hash = v.hash_code(); #ifndef INTPTR_MAX #error "no INTPTR_MAX" #endif #ifndef INT64_MAX #error "no INT64_MAX" #endif #if INTPTR_MAX == INT64_MAX hash = (hash >> 32) ^ (hash & 0xFFFFFFFF); #endif return static_cast(hash); } #endif #if !defined(BOOST_NO_CXX11_HDR_SYSTEM_ERROR) inline hash_t hash_value(std::error_code const& v) { hash_t seed = 0; hash_combine(seed, v.value()); hash_combine(seed, &v.category()); return seed; } inline hash_t hash_value(std::error_condition const& v) { hash_t seed = 0; hash_combine(seed, v.value()); hash_combine(seed, &v.category()); return seed; } #endif // // ccstd::hash // // Define the specializations required by the standard. The general purpose // ccstd::hash is defined later in extensions.hpp if // CCSTD_HASH_NO_EXTENSIONS is not defined. // CCSTD_HASH_SPECIALIZE - define a specialization for a type which is // passed by copy. // // CCSTD_HASH_SPECIALIZE_REF - define a specialization for a type which is // passed by const reference. // // These are undefined later. #define CCSTD_HASH_SPECIALIZE(type) \ template <> struct hash \ : public ccstd::hash_detail::hash_base \ { \ hash_t operator()(type v) const \ { \ return ccstd::hash_value(v); \ } \ }; #define CCSTD_HASH_SPECIALIZE_REF(type) \ template <> struct hash \ : public ccstd::hash_detail::hash_base \ { \ hash_t operator()(type const& v) const \ { \ return ccstd::hash_value(v); \ } \ }; #define CCSTD_HASH_SPECIALIZE_TEMPLATE_REF(type) \ struct hash \ : public ccstd::hash_detail::hash_base \ { \ hash_t operator()(type const& v) const \ { \ return ccstd::hash_value(v); \ } \ }; CCSTD_HASH_SPECIALIZE(bool) CCSTD_HASH_SPECIALIZE(char) CCSTD_HASH_SPECIALIZE(signed char) CCSTD_HASH_SPECIALIZE(unsigned char) #if !defined(BOOST_NO_INTRINSIC_WCHAR_T) CCSTD_HASH_SPECIALIZE(wchar_t) #endif #if !defined(BOOST_NO_CXX11_CHAR16_T) CCSTD_HASH_SPECIALIZE(char16_t) #endif #if !defined(BOOST_NO_CXX11_CHAR32_T) CCSTD_HASH_SPECIALIZE(char32_t) #endif CCSTD_HASH_SPECIALIZE(short) CCSTD_HASH_SPECIALIZE(unsigned short) CCSTD_HASH_SPECIALIZE(int) CCSTD_HASH_SPECIALIZE(unsigned int) CCSTD_HASH_SPECIALIZE(long) CCSTD_HASH_SPECIALIZE(unsigned long) CCSTD_HASH_SPECIALIZE(float) CCSTD_HASH_SPECIALIZE(double) CCSTD_HASH_SPECIALIZE(long double) CCSTD_HASH_SPECIALIZE_REF(std::string) #if !defined(BOOST_NO_STD_WSTRING) && !defined(BOOST_NO_INTRINSIC_WCHAR_T) CCSTD_HASH_SPECIALIZE_REF(std::wstring) #endif #if !defined(BOOST_NO_CXX11_CHAR16_T) CCSTD_HASH_SPECIALIZE_REF(std::basic_string) #endif #if !defined(BOOST_NO_CXX11_CHAR32_T) CCSTD_HASH_SPECIALIZE_REF(std::basic_string) #endif #if CCSTD_HASH_HAS_STRING_VIEW CCSTD_HASH_SPECIALIZE_REF(std::string_view) # if !defined(BOOST_NO_STD_WSTRING) && !defined(BOOST_NO_INTRINSIC_WCHAR_T) CCSTD_HASH_SPECIALIZE_REF(std::wstring_view) # endif # if !defined(BOOST_NO_CXX11_CHAR16_T) CCSTD_HASH_SPECIALIZE_REF(std::basic_string_view) # endif # if !defined(BOOST_NO_CXX11_CHAR32_T) CCSTD_HASH_SPECIALIZE_REF(std::basic_string_view) # endif #endif #if !defined(BOOST_NO_LONG_LONG) CCSTD_HASH_SPECIALIZE(boost::long_long_type) CCSTD_HASH_SPECIALIZE(boost::ulong_long_type) #endif #if defined(BOOST_HAS_INT128) CCSTD_HASH_SPECIALIZE(boost::int128_type) CCSTD_HASH_SPECIALIZE(boost::uint128_type) #endif #if CCSTD_HASH_HAS_OPTIONAL template CCSTD_HASH_SPECIALIZE_TEMPLATE_REF(std::optional) #endif #if !defined(CCSTD_HASH_HAS_VARIANT) template CCSTD_HASH_SPECIALIZE_TEMPLATE_REF(std::variant) CCSTD_HASH_SPECIALIZE(std::monostate) #endif #if !defined(BOOST_NO_CXX11_HDR_TYPEINDEX) CCSTD_HASH_SPECIALIZE(std::type_index) #endif #undef CCSTD_HASH_SPECIALIZE #undef CCSTD_HASH_SPECIALIZE_REF #undef CCSTD_HASH_SPECIALIZE_TEMPLATE_REF // Specializing ccstd::hash for pointers. #if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) template struct hash : public ccstd::hash_detail::hash_base { hash_t operator()(T* v) const { #if !BOOST_WORKAROUND(__SUNPRO_CC, <= 0x590) return ccstd::hash_value(v); #else hash_t x = static_cast( reinterpret_cast(v)); return x + (x >> 3); #endif } }; #else // For compilers without partial specialization, we define a // ccstd::hash for all remaining types. But hash_impl is only defined // for pointers in 'extensions.hpp' - so when CCSTD_HASH_NO_EXTENSIONS // is defined there will still be a compile error for types not supported // in the standard. namespace hash_detail { template struct hash_impl; template <> struct hash_impl { template struct inner : public ccstd::hash_detail::hash_base { hash_t operator()(T val) const { #if !BOOST_WORKAROUND(__SUNPRO_CC, <= 590) return ccstd::hash_value(val); #else hash_t x = static_cast( reinterpret_cast(val)); return x + (x >> 3); #endif } }; }; } template struct hash : public ccstd::hash_detail::hash_impl::value> ::BOOST_NESTED_TEMPLATE inner { }; #endif } #undef CCSTD_HASH_CHAR_TRAITS #undef CCSTD_FUNCTIONAL_HASH_ROTL32 #if defined(BOOST_MSVC) #pragma warning(pop) #endif #endif // CCSTD_FUNCTIONAL_HASH_HASH_HPP // Include this outside of the include guards in case the file is included // twice - once with CCSTD_HASH_NO_EXTENSIONS defined, and then with it // undefined. #if !defined(CCSTD_HASH_NO_EXTENSIONS) \ && !defined(CCSTD_FUNCTIONAL_HASH_EXTENSIONS_HPP) #include "base/std/hash/extensions.hpp" #endif