// Copyright 2005-2007 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. #if !defined(BOOST_FUNCTIONAL_HASH_HASH_HPP) #define BOOST_FUNCTIONAL_HASH_HASH_HPP #include #include #include #include #include #if defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) #include #endif #if defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING) #include #endif #if BOOST_WORKAROUND(BOOST_MSVC, < 1300) #include #endif namespace boost { std::size_t hash_value(bool); std::size_t hash_value(char); std::size_t hash_value(unsigned char); std::size_t hash_value(signed char); std::size_t hash_value(short); std::size_t hash_value(unsigned short); std::size_t hash_value(int); std::size_t hash_value(unsigned int); std::size_t hash_value(long); std::size_t hash_value(unsigned long); #if !defined(BOOST_NO_INTRINSIC_WCHAR_T) std::size_t hash_value(wchar_t); #endif #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__, <= 0x848) template std::size_t hash_value(T* const&); #else template std::size_t hash_value(T*); #endif #if !defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING) template< class T, unsigned N > std::size_t hash_value(const T (&array)[N]); template< class T, unsigned N > std::size_t hash_value(T (&array)[N]); #endif std::size_t hash_value(float v); std::size_t hash_value(double v); std::size_t hash_value(long double v); template std::size_t hash_value(std::basic_string, A> const&); template std::size_t hash_value(std::pair const&); template std::size_t hash_value(std::vector const&); template std::size_t hash_value(std::list const& v); template std::size_t hash_value(std::deque const& v); template std::size_t hash_value(std::set const& v); template std::size_t hash_value(std::multiset const& v); template std::size_t hash_value(std::map const& v); 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 std::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; std::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 ^= (std::size_t) (positive >> i) + (seed<<6) + (seed>>2); } seed ^= (std::size_t) val + (seed<<6) + (seed>>2); return seed; } template inline std::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; std::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 ^= (std::size_t) (val >> i) + (seed<<6) + (seed>>2); } seed ^= (std::size_t) val + (seed<<6) + (seed>>2); return seed; } } inline std::size_t hash_value(bool v) { return static_cast(v); } inline std::size_t hash_value(char v) { return static_cast(v); } inline std::size_t hash_value(unsigned char v) { return static_cast(v); } inline std::size_t hash_value(signed char v) { return static_cast(v); } inline std::size_t hash_value(short v) { return static_cast(v); } inline std::size_t hash_value(unsigned short v) { return static_cast(v); } inline std::size_t hash_value(int v) { return static_cast(v); } inline std::size_t hash_value(unsigned int v) { return static_cast(v); } inline std::size_t hash_value(long v) { return static_cast(v); } inline std::size_t hash_value(unsigned long v) { return static_cast(v); } #if !defined(BOOST_NO_INTRINSIC_WCHAR_T) inline std::size_t hash_value(wchar_t v) { return static_cast(v); } #endif #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__, <= 0x848) template std::size_t hash_value(T* const& v) #else template std::size_t hash_value(T* v) #endif { std::size_t x = static_cast( reinterpret_cast(v)); return x + (x >> 3); } #if BOOST_WORKAROUND(BOOST_MSVC, < 1300) template inline void hash_combine(std::size_t& seed, T& v) #else template inline void hash_combine(std::size_t& seed, T const& v) #endif { boost::hash hasher; seed ^= hasher(v) + 0x9e3779b9 + (seed<<6) + (seed>>2); } template inline std::size_t hash_range(It first, It last) { std::size_t seed = 0; for(; first != last; ++first) { hash_combine(seed, *first); } return seed; } template inline void hash_range(std::size_t& seed, It first, It last) { for(; first != last; ++first) { hash_combine(seed, *first); } } #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x551)) template inline std::size_t hash_range(T* first, T* last) { std::size_t seed = 0; for(; first != last; ++first) { boost::hash hasher; seed ^= hasher(*first) + 0x9e3779b9 + (seed<<6) + (seed>>2); } return seed; } template inline void hash_range(std::size_t& seed, T* first, T* last) { for(; first != last; ++first) { boost::hash hasher; seed ^= hasher(*first) + 0x9e3779b9 + (seed<<6) + (seed>>2); } } #endif #if !defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING) template< class T, unsigned N > inline std::size_t hash_value(const T (&array)[N]) { return hash_range(array, array + N); } template< class T, unsigned N > inline std::size_t hash_value(T (&array)[N]) { return hash_range(array, array + N); } #endif template inline std::size_t hash_value(std::basic_string, A> const& v) { return hash_range(v.begin(), v.end()); } inline std::size_t hash_value(float v) { return boost::hash_detail::float_hash_value(v); } inline std::size_t hash_value(double v) { return boost::hash_detail::float_hash_value(v); } inline std::size_t hash_value(long double v) { return boost::hash_detail::float_hash_value(v); } template std::size_t hash_value(std::pair const& v) { std::size_t seed = 0; hash_combine(seed, v.first); hash_combine(seed, v.second); return seed; } template std::size_t hash_value(std::vector const& v) { return hash_range(v.begin(), v.end()); } template std::size_t hash_value(std::list const& v) { return hash_range(v.begin(), v.end()); } template std::size_t hash_value(std::deque const& v) { return hash_range(v.begin(), v.end()); } template std::size_t hash_value(std::set const& v) { return hash_range(v.begin(), v.end()); } template std::size_t hash_value(std::multiset const& v) { return hash_range(v.begin(), v.end()); } template std::size_t hash_value(std::map const& v) { return hash_range(v.begin(), v.end()); } template std::size_t hash_value(std::multimap const& v) { 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 // #if !BOOST_WORKAROUND(BOOST_MSVC, < 1300) #define BOOST_HASH_SPECIALIZE(type) \ template <> struct hash \ : public std::unary_function \ { \ std::size_t operator()(type v) const \ { \ return boost::hash_value(v); \ } \ }; #define BOOST_HASH_SPECIALIZE_REF(type) \ template <> struct hash \ : public std::unary_function \ { \ std::size_t operator()(type const& v) const \ { \ return boost::hash_value(v); \ } \ }; #else #define BOOST_HASH_SPECIALIZE(type) \ template <> struct hash \ : public std::unary_function \ { \ std::size_t operator()(type v) const \ { \ return boost::hash_value(v); \ } \ }; \ \ template <> struct hash \ : public std::unary_function \ { \ std::size_t operator()(const type v) const \ { \ return boost::hash_value(v); \ } \ }; #define BOOST_HASH_SPECIALIZE_REF(type) \ template <> struct hash \ : public std::unary_function \ { \ std::size_t operator()(type const& v) const \ { \ return boost::hash_value(v); \ } \ }; \ \ template <> struct hash \ : public std::unary_function \ { \ std::size_t operator()(type const& v) const \ { \ return boost::hash_value(v); \ } \ }; #endif BOOST_HASH_SPECIALIZE(bool) BOOST_HASH_SPECIALIZE(char) BOOST_HASH_SPECIALIZE(signed char) BOOST_HASH_SPECIALIZE(unsigned char) #if !defined(BOOST_NO_INTRINSIC_WCHAR_T) BOOST_HASH_SPECIALIZE(wchar_t) #endif BOOST_HASH_SPECIALIZE(short) BOOST_HASH_SPECIALIZE(unsigned short) BOOST_HASH_SPECIALIZE(int) BOOST_HASH_SPECIALIZE(unsigned int) BOOST_HASH_SPECIALIZE(long) BOOST_HASH_SPECIALIZE(unsigned long) BOOST_HASH_SPECIALIZE(float) BOOST_HASH_SPECIALIZE(double) BOOST_HASH_SPECIALIZE(long double) BOOST_HASH_SPECIALIZE_REF(std::string) #if !defined(BOOST_NO_STD_WSTRING) BOOST_HASH_SPECIALIZE_REF(std::wstring) #endif #undef BOOST_HASH_SPECIALIZE #undef BOOST_HASH_SPECIALIZE_REF #if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) template struct hash : public std::unary_function { std::size_t operator()(T* v) const { #if !BOOST_WORKAROUND(__SUNPRO_CC, <= 0x590) return boost::hash_value(v); #else std::size_t x = static_cast( reinterpret_cast(v)); return x + (x >> 3); #endif } }; #else namespace hash_detail { template struct hash_impl; template <> struct hash_impl { template struct inner : public std::unary_function { std::size_t operator()(T val) const { #if !BOOST_WORKAROUND(__SUNPRO_CC, <= 590) return boost::hash_value(val); #else std::size_t x = static_cast( reinterpret_cast(val)); return x + (x >> 3); #endif } }; }; } template struct hash : public boost::hash_detail::hash_impl::value> ::BOOST_NESTED_TEMPLATE inner { }; #endif } #endif // BOOST_FUNCTIONAL_HASH_HASH_HPP //////////////////////////////////////////////////////////////////////////////// #if !defined(BOOST_HASH_NO_EXTENSIONS) \ && !defined(BOOST_FUNCTIONAL_HASH_EXTENSIONS_HPP) #define BOOST_FUNCTIONAL_HASH_EXTENSIONS_HPP namespace boost { #if defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING) namespace hash_detail { template struct call_hash_impl { template struct inner { static std::size_t call(T const& v) { using namespace boost; return hash_value(v); } }; }; template <> struct call_hash_impl { template struct inner { #if !BOOST_WORKAROUND(BOOST_MSVC, < 1300) static std::size_t call(Array const& v) #else static std::size_t call(Array& v) #endif { const int size = sizeof(v) / sizeof(*v); return boost::hash_range(v, v + size); } }; }; template struct call_hash : public call_hash_impl::value> ::BOOST_NESTED_TEMPLATE inner { }; } #endif // BOOST_NO_FUNCTION_TEMPLATE_ORDERING #if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) template struct hash : std::unary_function { #if !defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING) std::size_t operator()(T const& val) const { return hash_value(val); } #else std::size_t operator()(T const& val) const { return hash_detail::call_hash::call(val); } #endif }; #if BOOST_WORKAROUND(__DMC__, <= 0x848) template struct hash : std::unary_function { std::size_t operator()(const T* val) const { return boost::hash_range(val, val+n); } }; #endif #else // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION // On compilers without partial specialization, boost::hash // has already been declared to deal with pointers, so just // need to supply the non-pointer version. namespace hash_detail { template struct hash_impl; #if !BOOST_WORKAROUND(BOOST_MSVC, < 1300) template <> struct hash_impl { template struct inner : std::unary_function { #if !defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING) std::size_t operator()(T const& val) const { return hash_value(val); } #else std::size_t operator()(T const& val) const { return hash_detail::call_hash::call(val); } #endif }; }; #else // Visual C++ 6.5 // There's probably a more elegant way to Visual C++ 6.5 to work // but I don't know what it is. template struct hash_impl_msvc { template struct inner : public std::unary_function { std::size_t operator()(T const& val) const { return hash_detail::call_hash::call(val); } std::size_t operator()(T& val) const { return hash_detail::call_hash::call(val); } }; }; template <> struct hash_impl_msvc { template struct inner : public std::unary_function { std::size_t operator()(T& val) const { return hash_detail::call_hash::call(val); } }; }; template struct hash_impl_msvc2 : public hash_impl_msvc::value> ::BOOST_NESTED_TEMPLATE inner {}; template <> struct hash_impl { template struct inner : public hash_impl_msvc2 {}; }; #endif // Visual C++ 6.5 } #endif // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION } #endif