///////////////////////////////////////////////////////////////////////////// // // Copyright 2005-2014 Daniel James. // Copyright 2021, 2022 Peter Dimov. // Copyright 2024 Ion Gaztanaga. // 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. // // The original C++11 implementation was done by Peter Dimov // The C++03 porting was done by Ion Gaztanaga. // // The goal of this header is to avoid Intrusive's hard dependency on ContainerHash, // which adds additional dependencies and the minimum supported C++ standard can // differ between both libraries. However, a compatibility protocol is used so that // users compatible with ContainerHash are also compatible with Intrusive: // // - If users define `hash_value` (as required by boost::hash) for their classes // are automatically compatible with Intrusive unordered containers. // // - If users include boost/container_hash/hash.hpp in their headers, Intrusive // unordered containers will take advantage of boost::hash compatibility hash functions // (such as hashing functions for range-compatible types, standard containers, etc.) // // See http://www.boost.org/libs/intrusive for documentation. // ///////////////////////////////////////////////////////////////////////////// #ifndef BOOST_INTRUSIVE_HASH_HASH_HPP #define BOOST_INTRUSIVE_HASH_HASH_HPP #include #include #include #include #include #include #include #include #include #include namespace boost { template struct hash; } //namespace boost //Fallback function to call boost::hash if scalar type and ADL fail. //The user must include boost/container_hash/hash.hpp when to make this call work, //this allows boost::intrusive to be compatible with boost::hash without //a mandatory physical (header inclusion) dependency namespace boost_intrusive_adl { template inline std::size_t hash_value(const T& v) { return boost::hash()(v); } } namespace boost { namespace intrusive { namespace detail { //ADL-based lookup hash call template inline typename detail::disable_if_c::value, std::size_t>::type hash_value_dispatch(const T& v) { //Try ADL lookup, if it fails, boost_intrusive_adl::hash_value will retry with boost::hash using boost_intrusive_adl::hash_value; return hash_value(v); } template typename enable_if_c::value, std::size_t>::type hash_value( T v ) { return static_cast( v ); } //////////////////////////////////////////////////////////// // // floating point types // //////////////////////////////////////////////////////////// template struct hash_float_impl; // float template struct hash_float_impl { static std::size_t fn( T v ) { boost::uint32_t w; std::memcpy( &w, &v, sizeof( v ) ); return w; } }; // double template struct hash_float_impl { static std::size_t fn( T v ) { boost::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 ) { boost::uint64_t w[ 2 ] = {}; std::memcpy( &w, &v, 80 / CHAR_BIT ); std::size_t seed = 0; seed = hash_value( w[0] ) + (hash_mix)( seed ); seed = hash_value( w[1] ) + (hash_mix)( seed ); return seed; } }; #if (LDBL_MAX_10_EXP == 4932) // 80 bit long double in 16 bytes template struct hash_float_impl { static std::size_t fn( T v ) { boost::uint64_t w[ 2 ] = {}; std::memcpy( &w, &v, 80 / CHAR_BIT ); std::size_t seed = 0; seed = hash_value( w[0] ) + (hash_mix)( seed ); seed = hash_value( w[1] ) + (hash_mix)( seed ); return seed; } }; #elif (LDBL_MAX_10_EXP > 4932) // 128 bit long double template struct hash_float_impl { static std::size_t fn( T v ) { boost::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_mix)( seed ); seed = hash_value( w[0] ) + (hash_mix)( seed ); #else seed = hash_value( w[0] ) + (hash_mix)( seed ); seed = hash_value( w[1] ) + (hash_mix)( seed ); #endif return seed; } }; #endif //#if (LDBL_MAX_10_EXP == 4932) template typename enable_if_c::value, std::size_t>::type hash_value( T v ) { return boost::intrusive::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::size_t x = reinterpret_cast( v ); return hash_value( x + (x >> 3) ); } //////////////////////////////////////////////////////////// // // std::nullptr_t // //////////////////////////////////////////////////////////// #if !defined(BOOST_NO_CXX11_NULLPTR) template typename enable_if_c::value, std::size_t>::type hash_value( T const &) { return (hash_value)( static_cast( nullptr ) ); } #endif //////////////////////////////////////////////////////////// // // Array types // //////////////////////////////////////////////////////////// //Forward declaration or internal hash functor, for array iteration template struct internal_hash_functor; template inline std::size_t hash_value_dispatch( T const (&x)[ N ] ) { std::size_t seed = 0; for(std::size_t i = 0; i != N; ++i){ hash_combine_size_t(seed, internal_hash_functor()(x[i])); } return seed; } template inline std::size_t hash_value_dispatch( T (&x)[ N ] ) { std::size_t seed = 0; for (std::size_t i = 0; i != N; ++i) { hash_combine_size_t(seed, internal_hash_functor()(x[i])); } return seed; } //////////////////////////////////////////////////////////// // // Scalar types, calls proper overload // //////////////////////////////////////////////////////////// template inline typename detail::enable_if_c::value, std::size_t>::type hash_value_dispatch(const T &v) { return boost::intrusive::detail::hash_value(v); } //Internal "anonymous" hash functor, first selects between "built-in" scalar/array types //and ADL-based lookup template struct internal_hash_functor { inline std::size_t operator()(T const& val) const { return ::boost::intrusive::detail::hash_value_dispatch(val); } }; } // namespace detail { } // namespace intrusive { } // namespace boost #include #endif // #ifndef BOOST_INTRUSIVE_HASH_HASH_HPP