diff --git a/include/boost/functional/hash/hash.hpp b/include/boost/functional/hash/hash.hpp index c1d463e..52037ad 100644 --- a/include/boost/functional/hash/hash.hpp +++ b/include/boost/functional/hash/hash.hpp @@ -10,6 +10,10 @@ #if !defined(BOOST_FUNCTIONAL_HASH_HASH_HPP) #define BOOST_FUNCTIONAL_HASH_HASH_HPP +#if defined(_MSC_VER) && (_MSC_VER >= 1020) +# pragma once +#endif + #include #include #include @@ -41,6 +45,11 @@ namespace boost std::size_t hash_value(long); std::size_t hash_value(unsigned long); +#if defined(BOOST_HAS_LONG_LONG) + std::size_t hash_value(long long); + std::size_t hash_value(unsigned long long); +#endif + #if !BOOST_WORKAROUND(__DMC__, BOOST_TESTED_AT(0x847)) template std::size_t hash_value(T* const&); #else @@ -79,8 +88,55 @@ namespace boost template std::size_t hash_value(std::multimap const& v); + template + std::size_t hash_value(std::complex const&); + // Implementation + namespace hash_detail + { + template + inline size_t hash_value_signed(T val) + { + const 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) + / size_t_bits; + + size_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 ^= (size_t) (positive >> i) + (seed<<6) + (seed>>2); + } + seed ^= (size_t) val + (seed<<6) + (seed>>2); + + return seed; + } + + template + inline size_t hash_value_unsigned(T val) + { + const 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) + / size_t_bits; + + size_t seed = 0; + + // Hopefully, this loop can be unrolled. + for(unsigned int i = length * size_t_bits; i > 0; i -= size_t_bits) + { + seed ^= (size_t) (val >> i) + (seed<<6) + (seed>>2); + } + seed ^= (size_t) val + (seed<<6) + (seed>>2); + + return seed; + } + } + #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x551)) inline std::size_t hash_value(bool v) { @@ -108,6 +164,18 @@ namespace boost return static_cast(v); } +#if defined(BOOST_HAS_LONG_LONG) + inline std::size_t hash_value(long long v) + { + return hash_detail::hash_value_signed(v); + } + + inline std::size_t hash_value(unsigned long long v) + { + return hash_detail::hash_value_unsigned(v); + } +#endif + // Implementation by Alberto Barbati and Dave Harris. #if !BOOST_WORKAROUND(__DMC__, BOOST_TESTED_AT(0x847)) template std::size_t hash_value(T* const& v) @@ -266,6 +334,15 @@ namespace boost return hash_range(v.begin(), v.end()); } + template + std::size_t hash_value(std::complex const& v) + { + boost::hash hasher; + std::size_t seed = hasher(v.imag()); + seed ^= hasher(v.real()) + (seed<<6) + (seed>>2); + return seed; + } + // // boost::hash //