From a0a6fa061678629599db8fbd4da9a4a516f393b6 Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Sun, 17 Oct 2021 02:03:50 +0300 Subject: [PATCH] Move std::complex support to hash.hpp --- include/boost/container_hash/extensions.hpp | 13 --------- include/boost/container_hash/hash.hpp | 14 ++++++++++ test/hash_complex_test.cpp | 31 ++++++++++++++++++--- 3 files changed, 41 insertions(+), 17 deletions(-) diff --git a/include/boost/container_hash/extensions.hpp b/include/boost/container_hash/extensions.hpp index fa4d4f8..3636060 100644 --- a/include/boost/container_hash/extensions.hpp +++ b/include/boost/container_hash/extensions.hpp @@ -21,7 +21,6 @@ #include #include #include -#include #if !defined(BOOST_NO_CXX11_HDR_TUPLE) # include @@ -36,9 +35,6 @@ namespace boost template std::size_t hash_value(std::pair const&); - template - std::size_t hash_value(std::complex const&); - template std::size_t hash_value(std::pair const& v) { @@ -48,15 +44,6 @@ namespace boost return seed; } - 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; - } - #if !defined(BOOST_NO_CXX11_HDR_TUPLE) namespace hash_detail { template diff --git a/include/boost/container_hash/hash.hpp b/include/boost/container_hash/hash.hpp index ae89a05..d9e62cf 100644 --- a/include/boost/container_hash/hash.hpp +++ b/include/boost/container_hash/hash.hpp @@ -30,6 +30,7 @@ #include #include #include +#include #include #if !defined(BOOST_NO_CXX11_HDR_TYPEINDEX) @@ -153,6 +154,19 @@ namespace boost return boost::hash_range( x, x + N ); } + // complex + + template + std::size_t hash_value( std::complex const& v ) + { + std::size_t seed = 0; + + boost::hash_combine( seed, v.real() ); + boost::hash_combine( seed, v.imag() ); + + return seed; + } + // ranges (list, set, deque...) template diff --git a/test/hash_complex_test.cpp b/test/hash_complex_test.cpp index 5afd7ac..2dbad10 100644 --- a/test/hash_complex_test.cpp +++ b/test/hash_complex_test.cpp @@ -39,9 +39,10 @@ int main() {} #pragma GCC diagnostic ignored "-Wfloat-equal" #endif +#include #include #include -#include +#include template void generic_complex_tests(std::complex v) @@ -52,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(imag != 0 && real_hasher(real) == complex_hasher(v)) { + if(real_hasher(real) == complex_hasher(v)) { std::ostringstream os; os<<"real_hasher("< void complex_grid_test() +{ + short const N = 16; + + std::set hashes; + + for( short i = 0; i < N; ++i ) + { + for( short j = 0; j < N; ++j ) + { + hashes.insert( boost::hash< std::complex >()( std::complex( i, j ) ) ); + } + } + + BOOST_TEST_EQ( hashes.size(), N * N ); +} + int main() { // I've comments out the short and unsigned short tests @@ -104,6 +122,11 @@ 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(); + return boost::report_errors(); }