// Copyright 2005-2009 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. // This implements the extensions to the standard. // It's undocumented, so you shouldn't use it.... #if !defined(BOOST_FUNCTIONAL_HASH_EXTENSIONS_HPP) #define BOOST_FUNCTIONAL_HASH_EXTENSIONS_HPP #include #if defined(BOOST_HAS_PRAGMA_ONCE) #pragma once #endif #include #include #include #include #include #if !defined(BOOST_NO_CXX11_HDR_ARRAY) # include #endif #if !defined(BOOST_NO_CXX11_HDR_TUPLE) # include #endif #if !defined(BOOST_NO_CXX11_HDR_MEMORY) # include #endif #if defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING) #include #endif namespace lslboost { 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&); template std::size_t hash_value(std::pair const& v) { std::size_t seed = 0; lslboost::hash_combine(seed, v.first); lslboost::hash_combine(seed, v.second); return seed; } inline std::size_t hash_range( std::vector::iterator first, std::vector::iterator last) { std::size_t seed = 0; for(; first != last; ++first) { hash_combine(seed, *first); } return seed; } inline std::size_t hash_range( std::vector::const_iterator first, std::vector::const_iterator last) { std::size_t seed = 0; for(; first != last; ++first) { hash_combine(seed, *first); } return seed; } inline void hash_range( std::size_t& seed, std::vector::iterator first, std::vector::iterator last) { for(; first != last; ++first) { hash_combine(seed, *first); } } inline void hash_range( std::size_t& seed, std::vector::const_iterator first, std::vector::const_iterator last) { for(; first != last; ++first) { hash_combine(seed, *first); } } template std::size_t hash_value(std::vector const& v) { return lslboost::hash_range(v.begin(), v.end()); } template std::size_t hash_value(std::list const& v) { return lslboost::hash_range(v.begin(), v.end()); } template std::size_t hash_value(std::deque const& v) { return lslboost::hash_range(v.begin(), v.end()); } template std::size_t hash_value(std::set const& v) { return lslboost::hash_range(v.begin(), v.end()); } template std::size_t hash_value(std::multiset const& v) { return lslboost::hash_range(v.begin(), v.end()); } template std::size_t hash_value(std::map const& v) { return lslboost::hash_range(v.begin(), v.end()); } template std::size_t hash_value(std::multimap const& v) { return lslboost::hash_range(v.begin(), v.end()); } template std::size_t hash_value(std::complex const& v) { lslboost::hash hasher; std::size_t seed = hasher(v.imag()); seed ^= hasher(v.real()) + (seed<<6) + (seed>>2); return seed; } #if !defined(BOOST_NO_CXX11_HDR_ARRAY) template std::size_t hash_value(std::array const& v) { return lslboost::hash_range(v.begin(), v.end()); } #endif #if !defined(BOOST_NO_CXX11_HDR_TUPLE) namespace hash_detail { template inline typename lslboost::enable_if_c<(I == std::tuple_size::value), void>::type hash_combine_tuple(std::size_t&, T const&) { } template inline typename lslboost::enable_if_c<(I < std::tuple_size::value), void>::type hash_combine_tuple(std::size_t& seed, T const& v) { lslboost::hash_combine(seed, std::get(v)); lslboost::hash_detail::hash_combine_tuple(seed, v); } template inline std::size_t hash_tuple(T const& v) { std::size_t seed = 0; lslboost::hash_detail::hash_combine_tuple<0>(seed, v); return seed; } } #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) template inline std::size_t hash_value(std::tuple const& v) { return lslboost::hash_detail::hash_tuple(v); } #else inline std::size_t hash_value(std::tuple<> const& v) { return lslboost::hash_detail::hash_tuple(v); } template inline std::size_t hash_value(std::tuple const& v) { return lslboost::hash_detail::hash_tuple(v); } template inline std::size_t hash_value(std::tuple const& v) { return lslboost::hash_detail::hash_tuple(v); } template inline std::size_t hash_value(std::tuple const& v) { return lslboost::hash_detail::hash_tuple(v); } template inline std::size_t hash_value(std::tuple const& v) { return lslboost::hash_detail::hash_tuple(v); } template inline std::size_t hash_value(std::tuple const& v) { return lslboost::hash_detail::hash_tuple(v); } template inline std::size_t hash_value(std::tuple const& v) { return lslboost::hash_detail::hash_tuple(v); } template inline std::size_t hash_value(std::tuple const& v) { return lslboost::hash_detail::hash_tuple(v); } template inline std::size_t hash_value(std::tuple const& v) { return lslboost::hash_detail::hash_tuple(v); } template inline std::size_t hash_value(std::tuple const& v) { return lslboost::hash_detail::hash_tuple(v); } template inline std::size_t hash_value(std::tuple const& v) { return lslboost::hash_detail::hash_tuple(v); } #endif #endif #if !defined(BOOST_NO_CXX11_SMART_PTR) template inline std::size_t hash_value(std::shared_ptr const& x) { return lslboost::hash_value(x.get()); } template inline std::size_t hash_value(std::unique_ptr const& x) { return lslboost::hash_value(x.get()); } #endif // // call_hash_impl // // On compilers without function template ordering, this deals with arrays. #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 lslboost; return hash_value(v); } }; }; template <> struct call_hash_impl { template struct inner { static std::size_t call(Array const& v) { const int size = sizeof(v) / sizeof(*v); return lslboost::hash_range(v, v + size); } }; }; template struct call_hash : public call_hash_impl::value> ::BOOST_NESTED_TEMPLATE inner { }; } #endif // BOOST_NO_FUNCTION_TEMPLATE_ORDERING // // lslboost::hash // #if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) template struct hash : lslboost::hash_detail::hash_base { #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 : lslboost::hash_detail::hash_base { std::size_t operator()(const T* val) const { return lslboost::hash_range(val, val+n); } }; #endif #else // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION // On compilers without partial specialization, lslboost::hash // has already been declared to deal with pointers, so just // need to supply the non-pointer version of hash_impl. namespace hash_detail { template struct hash_impl; template <> struct hash_impl { template struct inner : lslboost::hash_detail::hash_base { #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 }; }; } #endif // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION } #endif