// Copyright 2005-2014 Daniel James. // Copyright 2021, 2022 Peter Dimov. // Distributed under the Boost Software License, Version 1.0. // https://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. #ifndef BOOST_FUNCTIONAL_HASH_HASH_HPP #define BOOST_FUNCTIONAL_HASH_HASH_HPP #include #include #include #include #include #include #include #include #include #include #include #include #include #if defined(BOOST_DESCRIBE_CXX14) # include #endif #include #include #include #include #include #include #include #if !defined(BOOST_NO_CXX11_SMART_PTR) # include #endif #if !defined(BOOST_NO_CXX11_HDR_TYPEINDEX) #include #endif #if !defined(BOOST_NO_CXX11_HDR_SYSTEM_ERROR) #include #endif #if !defined(BOOST_NO_CXX17_HDR_OPTIONAL) #include #endif #if !defined(BOOST_NO_CXX17_HDR_VARIANT) #include #endif #if !defined(BOOST_NO_CXX17_HDR_STRING_VIEW) # include #endif namespace boost { // // boost::hash_value // // integral types // in detail/hash_integral.hpp // enumeration types template typename std::enable_if::value, std::size_t>::type hash_value( T v ) { // This should in principle return the equivalent of // // boost::hash_value( to_underlying(v) ); // // However, the C++03 implementation of underlying_type, // // conditional, make_signed, make_unsigned>::type::type // // generates a legitimate -Wconversion warning in is_signed, // because -1 is not a valid enum value when all the enumerators // are nonnegative. // // So the legacy implementation will have to do for now. return static_cast( v ); } // floating point types namespace hash_detail { template::digits> struct hash_float_impl; // float template struct hash_float_impl { static std::size_t fn( T v ) { std::uint32_t w; std::memcpy( &w, &v, sizeof( v ) ); return w; } }; // double template struct hash_float_impl { static std::size_t fn( T v ) { std::uint64_t w; std::memcpy( &w, &v, sizeof( v ) ); return hash_value( w ); } }; // 80 bit long double in 12 bytes template struct hash_float_impl { static std::size_t fn( T v ) { std::uint64_t w[ 2 ] = {}; std::memcpy( &w, &v, 80 / CHAR_BIT ); std::size_t seed = 0; seed = hash_value( w[0] ) + hash_detail::hash_mix( seed ); seed = hash_value( w[1] ) + hash_detail::hash_mix( seed ); return seed; } }; // 80 bit long double in 16 bytes template struct hash_float_impl { static std::size_t fn( T v ) { std::uint64_t w[ 2 ] = {}; std::memcpy( &w, &v, 80 / CHAR_BIT ); std::size_t seed = 0; seed = hash_value( w[0] ) + hash_detail::hash_mix( seed ); seed = hash_value( w[1] ) + hash_detail::hash_mix( seed ); return seed; } }; // 128 bit long double template struct hash_float_impl { static std::size_t fn( T v ) { std::uint64_t w[ 2 ]; std::memcpy( &w, &v, sizeof( v ) ); std::size_t seed = 0; #if defined(__FLOAT_WORD_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && __FLOAT_WORD_ORDER__ == __ORDER_BIG_ENDIAN__ seed = hash_value( w[1] ) + hash_detail::hash_mix( seed ); seed = hash_value( w[0] ) + hash_detail::hash_mix( seed ); #else seed = hash_value( w[0] ) + hash_detail::hash_mix( seed ); seed = hash_value( w[1] ) + hash_detail::hash_mix( seed ); #endif return seed; } }; } // namespace hash_detail template typename std::enable_if::value, std::size_t>::type hash_value( T v ) { return boost::hash_detail::hash_float_impl::fn( v + 0 ); } // pointer types // `x + (x >> 3)` adjustment by Alberto Barbati and Dave Harris. template std::size_t hash_value( T* const& v ) { std::uintptr_t x = reinterpret_cast( v ); return boost::hash_value( x + (x >> 3) ); } // array types template inline std::size_t hash_value( T const (&x)[ N ] ) { return boost::hash_range( x, x + N ); } template inline std::size_t hash_value( T (&x)[ N ] ) { return boost::hash_range( x, x + N ); } // complex template std::size_t hash_value( std::complex const& v ) { std::size_t re = boost::hash()( v.real() ); std::size_t im = boost::hash()( v.imag() ); return re + hash_detail::hash_mix( im ); } // pair template std::size_t hash_value( std::pair const& v ) { std::size_t seed = 0; boost::hash_combine( seed, v.first ); boost::hash_combine( seed, v.second ); return seed; } // ranges (list, set, deque...) template typename std::enable_if::value && !container_hash::is_contiguous_range::value && !container_hash::is_unordered_range::value, std::size_t>::type hash_value( T const& v ) { return boost::hash_range( v.begin(), v.end() ); } // contiguous ranges (string, vector, array) template typename std::enable_if::value, std::size_t>::type hash_value( T const& v ) { return boost::hash_range( v.data(), v.data() + v.size() ); } // unordered ranges (unordered_set, unordered_map) template typename std::enable_if::value, std::size_t>::type hash_value( T const& v ) { return boost::hash_unordered_range( v.begin(), v.end() ); } #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) && ( \ ( defined(_MSVC_STL_VERSION) && _MSVC_STL_VERSION < 142 ) || \ ( !defined(_MSVC_STL_VERSION) && defined(_CPPLIB_VER) && _CPPLIB_VER >= 520 ) ) // resolve ambiguity with unconstrained stdext::hash_value in :-/ template class L, class... T> typename std::enable_if>::value && !container_hash::is_contiguous_range>::value && !container_hash::is_unordered_range>::value, std::size_t>::type hash_value( L const& v ) { return boost::hash_range( v.begin(), v.end() ); } // contiguous ranges (string, vector, array) template class L, class... T> typename std::enable_if>::value, std::size_t>::type hash_value( L const& v ) { return boost::hash_range( v.data(), v.data() + v.size() ); } template class L, class T, std::size_t N> typename std::enable_if>::value, std::size_t>::type hash_value( L const& v ) { return boost::hash_range( v.data(), v.data() + v.size() ); } // unordered ranges (unordered_set, unordered_map) template class L, class... T> typename std::enable_if>::value, std::size_t>::type hash_value( L const& v ) { return boost::hash_unordered_range( v.begin(), v.end() ); } #endif // described classes #if defined(BOOST_DESCRIBE_CXX14) #if defined(_MSC_VER) && _MSC_VER == 1900 # pragma warning(push) # pragma warning(disable: 4100) // unreferenced formal parameter #endif template typename std::enable_if::value, std::size_t>::type hash_value( T const& v ) { static_assert( !std::is_union::value, "described unions are not supported" ); std::size_t r = 0; using Bd = describe::describe_bases; mp11::mp_for_each([&](auto D){ using B = typename decltype(D)::type; boost::hash_combine( r, (B const&)v ); }); using Md = describe::describe_members; mp11::mp_for_each([&](auto D){ boost::hash_combine( r, v.*D.pointer ); }); return r; } #if defined(_MSC_VER) && _MSC_VER == 1900 # pragma warning(pop) #endif #endif // std::unique_ptr, std::shared_ptr #if !defined(BOOST_NO_CXX11_SMART_PTR) template std::size_t hash_value( std::shared_ptr const& x ) { return boost::hash_value( x.get() ); } template std::size_t hash_value( std::unique_ptr const& x ) { return boost::hash_value( x.get() ); } #endif // std::type_index #if !defined(BOOST_NO_CXX11_HDR_TYPEINDEX) inline std::size_t hash_value( std::type_index const& v ) { return v.hash_code(); } #endif // std::error_code, std::error_condition #if !defined(BOOST_NO_CXX11_HDR_SYSTEM_ERROR) inline std::size_t hash_value( std::error_code const& v ) { std::size_t seed = 0; boost::hash_combine( seed, v.value() ); boost::hash_combine( seed, &v.category() ); return seed; } inline std::size_t hash_value( std::error_condition const& v ) { std::size_t seed = 0; boost::hash_combine( seed, v.value() ); boost::hash_combine( seed, &v.category() ); return seed; } #endif // std::nullptr_t #if !defined(BOOST_NO_CXX11_NULLPTR) template typename std::enable_if::value, std::size_t>::type hash_value( T const& /*v*/ ) { return boost::hash_value( static_cast( nullptr ) ); } #endif // std::optional #if !defined(BOOST_NO_CXX17_HDR_OPTIONAL) template std::size_t hash_value( std::optional const& v ) { if( !v ) { // Arbitrary value for empty optional. return 0x12345678; } else { return boost::hash()(*v); } } #endif // std::variant #if !defined(BOOST_NO_CXX17_HDR_VARIANT) inline std::size_t hash_value( std::monostate ) { return 0x87654321; } template std::size_t hash_value( std::variant const& v ) { std::size_t seed = 0; hash_combine( seed, v.index() ); std::visit( [&seed](auto&& x) { hash_combine(seed, x); }, v ); return seed; } #endif // // boost::hash_combine // template inline void hash_combine( std::size_t& seed, T const& v ) { seed = boost::hash_detail::hash_mix( seed + 0x9e3779b9 + boost::hash()( v ) ); } // // boost::hash_range // template inline void hash_range( std::size_t& seed, It first, It last ) { seed = hash_detail::hash_range( seed, first, last ); } template inline std::size_t hash_range( It first, It last ) { std::size_t seed = 0; hash_range( seed, first, last ); return seed; } // // boost::hash_unordered_range // template inline void hash_unordered_range( std::size_t& seed, It first, It last ) { std::size_t r = 0; std::size_t const s2( seed ); for( ; first != last; ++first ) { std::size_t s3( s2 ); hash_combine::value_type>( s3, *first ); r += s3; } seed += r; } template inline std::size_t hash_unordered_range( It first, It last ) { std::size_t seed = 0; hash_unordered_range( seed, first, last ); return seed; } // // boost::hash // template struct hash { typedef T argument_type; typedef std::size_t result_type; std::size_t operator()( T const& val ) const { return hash_value( val ); } }; #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) && ( \ ( defined(_MSVC_STL_VERSION) && _MSVC_STL_VERSION < 142 ) || \ ( !defined(_MSVC_STL_VERSION) && defined(_CPPLIB_VER) && _CPPLIB_VER >= 520 ) ) // Dinkumware has stdext::hash_value for basic_string in :-/ template struct hash< std::basic_string > { typedef std::basic_string argument_type; typedef std::size_t result_type; std::size_t operator()( std::basic_string const& val ) const { return boost::hash_value( val ); } }; #endif // boost::unordered::hash_is_avalanching namespace unordered { template struct hash_is_avalanching; template struct hash_is_avalanching< boost::hash< std::basic_string > >: std::is_integral {}; #if !defined(BOOST_NO_CXX17_HDR_STRING_VIEW) template struct hash_is_avalanching< boost::hash< std::basic_string_view > >: std::is_integral {}; #endif } // namespace unordered } // namespace boost #endif // #ifndef BOOST_FUNCTIONAL_HASH_HASH_HPP