// 32 bit implementation based off of Boost hash // Only implementing 32 bit versions integral and string based hashes #ifndef BOOST_FUNCTIONAL_HASH_HASH_32_HPP #define BOOST_FUNCTIONAL_HASH_HASH_32_HPP #include #include #include #include #include #if !BOOST_VERSION_HAS_HASH_RANGE #include #include #if BOOST_WORKAROUND(__GNUC__, < 3) \ && !defined(__SGI_STL_PORT) && !defined(_STLPORT_VERSION) #define BOOST_HASH_CHAR_TRAITS string_char_traits #else #define BOOST_HASH_CHAR_TRAITS char_traits #endif #endif // #if !BOOST_VERSION_HAS_HASH_RANGE namespace boost { // // boost::hash_value // // integral types namespace hash_detail { template sizeof(uint32_t)), bool is_unsigned = boost::is_unsigned::value, std::size_t size_t_bits = sizeof(uint32_t) * CHAR_BIT, std::size_t type_bits = sizeof(T) * CHAR_BIT> struct hash_integral_impl_32; template struct hash_integral_impl_32 { static uint32_t fn( T v ) { return static_cast( v ); } }; template struct hash_integral_impl_32 { static uint32_t fn( T v ) { typedef typename boost::make_unsigned::type U; if( v >= 0 ) { return hash_integral_impl_32::fn( static_cast( v ) ); } else { return ~hash_integral_impl_32::fn( static_cast( ~static_cast( v ) ) ); } } }; template struct hash_integral_impl_32 { static uint32_t fn( T v ) { uint32_t seed = 0; seed = static_cast( v >> 32 ) + hash_detail::hash_mix_32( seed ); seed = static_cast( v ) + hash_detail::hash_mix_32( seed ); return seed; } }; template struct hash_integral_impl_32 { static uint32_t fn( T v ) { uint32_t seed = 0; seed = static_cast( v >> 96 ) + hash_detail::hash_mix_32( seed ); seed = static_cast( v >> 64 ) + hash_detail::hash_mix_32( seed ); seed = static_cast( v >> 32 ) + hash_detail::hash_mix_32( seed ); seed = static_cast( v ) + hash_detail::hash_mix_32( seed ); return seed; } }; } // namespace hash_detail template typename boost::enable_if_::value, uint32_t>::type hash_value_32( T v ) { return hash_detail::hash_integral_impl_32::fn( v ); } // contiguous ranges (string, vector, array) #if BOOST_VERSION_HAS_HASH_RANGE template typename boost::enable_if_::value, uint32_t>::type hash_value_32( T const& v ) { return boost::hash_range_32( v.data(), v.data() + v.size() ); } #else template inline uint32_t hash_value_32( std::basic_string, A> const& v) { return boost::hash_range_32( v.data(), v.data() + v.size() ); } #endif // // boost::hash_combine // template inline void hash_combine_32( uint32_t& seed, T const& v ) { seed = boost::hash_detail::hash_mix_32( seed + 0x9e3779b9 + boost::hash_32()( v ) ); } // // boost::hash_range // template inline void hash_range_32( uint32_t& seed, It first, It last ) { seed = hash_detail::hash_range_32( seed, first, last ); } template inline uint32_t hash_range_32( It first, It last ) { uint32_t seed = 0; hash_range_32( seed, first, last ); return seed; } // // boost::hash // template struct hash_32 { typedef T argument_type; typedef uint32_t result_type; uint32_t operator()( T const& val ) const { return hash_value_32( val ); } }; } // namespace boost #undef BOOST_HASH_CHAR_TRAITS #endif // #ifndef BOOST_FUNCTIONAL_HASH_HASH_32_HPP