Add hashing support

This commit is contained in:
Peter Dimov
2020-01-11 04:51:30 +02:00
parent 58b4a21deb
commit fece11142c
3 changed files with 154 additions and 0 deletions

View File

@ -18,6 +18,7 @@
#endif
#include <boost/config.hpp>
#include <boost/detail/workaround.hpp>
#include <boost/cstdint.hpp>
#include <cstddef>
#include <type_traits>
#include <exception>
@ -1785,9 +1786,87 @@ void swap( variant<T...> & v, variant<T...> & w )
v.swap( w );
}
// hashing support
namespace detail
{
template<class V> struct hash_value_L
{
V const & v;
template<class I> std::size_t operator()( I ) const
{
boost::ulong_long_type hv = ( boost::ulong_long_type( 0xCBF29CE4 ) << 32 ) + 0x84222325;
boost::ulong_long_type const prime = ( boost::ulong_long_type( 0x00000100 ) << 32 ) + 0x000001B3;
// index
hv ^= I::value;
hv *= prime;
// value
auto const & t = unsafe_get<I::value>( v );
hv ^= std::hash<remove_cv_ref_t<decltype(t)>>()( t );
hv *= prime;
return static_cast<std::size_t>( hv );
}
};
} // namespace detail
std::size_t hash_value( monostate const & )
{
return 0xA7EE4757u;
}
template<class... T> std::size_t hash_value( variant<T...> const & v )
{
return mp11::mp_with_index<sizeof...(T)>( v.index(), detail::hash_value_L< variant<T...> >{ v } );
}
namespace detail
{
template<class T> using is_hash_enabled = std::is_default_constructible< std::hash<typename std::remove_const<T>::type> >;
template<class V, bool E = mp11::mp_all_of<V, is_hash_enabled>::value> struct std_hash_impl;
template<class V> struct std_hash_impl<V, false>
{
std_hash_impl() = delete;
std_hash_impl( std_hash_impl const& ) = delete;
std_hash_impl& operator=( std_hash_impl const& ) = delete;
};
template<class V> struct std_hash_impl<V, true>
{
std::size_t operator()( V const & v ) const
{
return hash_value( v );
}
};
} // namespace detail
} // namespace variant2
} // namespace boost
template<class... T> struct std::hash< boost::variant2::variant<T...> >: public boost::variant2::detail::std_hash_impl< boost::variant2::variant<T...> >
{
};
template<> struct std::hash< boost::variant2::monostate >
{
std::size_t operator()( boost::variant2::monostate const & v ) const
{
return hash_value( v );
}
};
#if defined(_MSC_VER) && _MSC_VER < 1910
# pragma warning( pop )
#endif

View File

@ -105,3 +105,5 @@ run variant_get_by_type.cpp throw_exception.cpp : : : $(NX) : variant_get_by_typ
compile variant_get_by_type_cx.cpp : $(NX) : variant_get_by_type_cx_nx ;
run variant_subset.cpp throw_exception.cpp : : : $(NX) : variant_subset_nx ;
run variant_hash.cpp ;

73
test/variant_hash.cpp Normal file
View File

@ -0,0 +1,73 @@
// Copyright 2020 Peter Dimov.
// Distributed under the Boost Software License, Version 1.0.
// http://www.boost.org/LICENSE_1_0.txt
#if defined(_MSC_VER)
# pragma warning( disable: 4244 ) // conversion from int to float, possible loss of data
#endif
#include <boost/variant2/variant.hpp>
#include <boost/core/lightweight_test.hpp>
#include <boost/core/lightweight_test_trait.hpp>
#include <boost/container_hash/hash.hpp>
#include <boost/config/workaround.hpp>
using namespace boost::variant2;
template<template<class...> class Hash, class T1, class T2, class T3> void test()
{
variant<T1, T2, T3> v1( in_place_index_t<0>{} );
std::size_t h1 = Hash<decltype(v1)>()( v1 );
variant<T1, T2, T3> v2( in_place_index_t<1>{} );
std::size_t h2 = Hash<decltype(v2)>()( v2 );
variant<T1, T2, T3> v3( in_place_index_t<2>{} );
std::size_t h3 = Hash<decltype(v3)>()( v3 );
BOOST_TEST_NE( h1, h2 );
BOOST_TEST_NE( h1, h3 );
BOOST_TEST_NE( h2, h3 );
}
template<template<class...> class Hash, class T> void test2()
{
variant<T> v1( 0 );
std::size_t h1 = Hash<decltype(v1)>()( v1 );
variant<T> v2( 1 );
std::size_t h2 = Hash<decltype(v2)>()( v2 );
variant<T> v3( 2 );
std::size_t h3 = Hash<decltype(v3)>()( v3 );
BOOST_TEST_NE( h1, h2 );
BOOST_TEST_NE( h1, h3 );
BOOST_TEST_NE( h2, h3 );
}
struct X {};
int main()
{
test<std::hash, monostate, monostate, monostate>();
test<std::hash, int, int, float>();
test<boost::hash, monostate, monostate, monostate>();
test<boost::hash, int, int, float>();
test2<std::hash, int>();
test2<std::hash, float>();
test2<boost::hash, int>();
test2<boost::hash, float>();
#if !BOOST_WORKAROUND(BOOST_MSVC, < 1910)
BOOST_TEST_TRAIT_FALSE(( detail::is_hash_enabled<X> ));
#endif
return boost::report_errors();
}