From f08204e29aa3e17567b084a411c465c2d861b236 Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Sun, 29 May 2022 21:58:19 +0300 Subject: [PATCH] Revert hash_value for std::complex --- include/boost/container_hash/hash.hpp | 13 ++++++------- test/hash_complex_test.cpp | 18 ++++++++---------- 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/include/boost/container_hash/hash.hpp b/include/boost/container_hash/hash.hpp index 8fd7cae..7cb2d01 100644 --- a/include/boost/container_hash/hash.hpp +++ b/include/boost/container_hash/hash.hpp @@ -86,8 +86,8 @@ namespace boost { static std::size_t fn( T v ) { - // The bias makes negative numbers that fit into a ssize_t hash to themselves - // E.g. hash_value( -4LL ) == (size_t)-4 + // The bias makes negative numbers that fit into a ssize_t hash to themselves + // E.g. hash_value( -4LL ) == (size_t)-4 std::size_t const bias = (std::numeric_limits::max)() / 4; @@ -252,12 +252,11 @@ namespace boost template std::size_t hash_value( std::complex const& v ) { - std::size_t seed = 0; + std::size_t re = boost::hash()( v.real() ); + std::size_t im = boost::hash()( v.imag() ); - boost::hash_combine( seed, v.real() ); - boost::hash_combine( seed, v.imag() ); - - return seed; + im ^= re + ( im << 6 ) + ( im >> 2 ); + return im; } // pair diff --git a/test/hash_complex_test.cpp b/test/hash_complex_test.cpp index 2dbad10..2435ab4 100644 --- a/test/hash_complex_test.cpp +++ b/test/hash_complex_test.cpp @@ -53,11 +53,11 @@ void generic_complex_tests(std::complex v) BOOST_HASH_TEST_NAMESPACE::hash real_hasher; T real = v.real(); - // T imag = v.imag(); + T imag = v.imag(); - // BOOST_TEST(real_hasher(real) == complex_hasher(std::complex(real))); + BOOST_TEST(real_hasher(real) == complex_hasher(std::complex(real))); - if(real_hasher(real) == complex_hasher(v)) { + if(imag != 0 && real_hasher(real) == complex_hasher(v)) { std::ostringstream os; os<<"real_hasher("< void complex_grid_test() +template void complex_grid_test( short N ) { - short const N = 16; - std::set hashes; for( short i = 0; i < N; ++i ) @@ -122,10 +120,10 @@ int main() complex_integral_tests((unsigned int*) 0); complex_integral_tests((unsigned long*) 0); - complex_grid_test(); - complex_grid_test(); - complex_grid_test(); - complex_grid_test(); + complex_grid_test( 16 ); + complex_grid_test( 7 ); + complex_grid_test( 16 ); + complex_grid_test( 16 ); return boost::report_errors(); }