From 0d2266decbae74fd3f075108b597c660e7666198 Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Sun, 30 Oct 2022 02:47:10 +0300 Subject: [PATCH] Specialize boost::unordered::hash_is_avalanching for hash and hash --- include/boost/container_hash/hash.hpp | 30 +++++++++++++- test/Jamfile.v2 | 3 ++ test/hash_is_avalanching_test.cpp | 52 ++++++++++++++++++++++++ test/hash_is_avalanching_test2.cpp | 57 +++++++++++++++++++++++++++ 4 files changed, 141 insertions(+), 1 deletion(-) create mode 100644 test/hash_is_avalanching_test.cpp create mode 100644 test/hash_is_avalanching_test2.cpp diff --git a/include/boost/container_hash/hash.hpp b/include/boost/container_hash/hash.hpp index a00132b..a1afe7c 100644 --- a/include/boost/container_hash/hash.hpp +++ b/include/boost/container_hash/hash.hpp @@ -63,6 +63,10 @@ #include #endif +#if !defined(BOOST_NO_CXX17_HDR_STRING_VIEW) +# include +#endif + namespace boost { @@ -629,6 +633,30 @@ namespace boost }; #endif -} + + // boost::unordered::hash_is_avalanching + + namespace unordered + { + template struct hash_is_avalanching; + template struct hash_is_avalanching< boost::hash< std::basic_string > >: boost::is_integral {}; + + // boost::is_integral is false, but should be true (https://github.com/boostorg/type_traits/issues/175) +#if defined(__cpp_char8_t) && __cpp_char8_t >= 201811L + template<> struct hash_is_avalanching< boost::hash< std::u8string > >: boost::true_type {}; +#endif + +#if !defined(BOOST_NO_CXX17_HDR_STRING_VIEW) + + template struct hash_is_avalanching< boost::hash< std::basic_string_view > >: boost::is_integral {}; + +#if defined(__cpp_char8_t) && __cpp_char8_t >= 201811L + template<> struct hash_is_avalanching< boost::hash< std::u8string_view > >: boost::true_type {}; +#endif + +#endif + } // namespace unordered + +} // namespace boost #endif // #ifndef BOOST_FUNCTIONAL_HASH_HASH_HPP diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index f0a2cf1..b5985cc 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -112,3 +112,6 @@ run is_described_class_test3.cpp run described_class_test.cpp : : : extra ; + +run hash_is_avalanching_test.cpp ; +run hash_is_avalanching_test2.cpp ; diff --git a/test/hash_is_avalanching_test.cpp b/test/hash_is_avalanching_test.cpp new file mode 100644 index 0000000..7787299 --- /dev/null +++ b/test/hash_is_avalanching_test.cpp @@ -0,0 +1,52 @@ +// Copyright 2022 Peter Dimov. +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +#include +#include +#include +#include + +#if defined(BOOST_NO_CXX11_HDR_TYPE_TRAITS) + +BOOST_PRAGMA_MESSAGE( "Test skipped, BOOST_NO_CXX11_HDR_TYPE_TRAITS is defined" ) +int main() {} + +#else + +#include +#include + +enum my_char { min = 0, max = 255 }; + +int main() +{ + using boost::unordered::hash_is_avalanching; + + BOOST_TEST_TRAIT_TRUE(( hash_is_avalanching< boost::hash > )); + BOOST_TEST_TRAIT_TRUE(( hash_is_avalanching< boost::hash > )); + +#if !defined(BOOST_NO_CXX11_CHAR16_T) + + BOOST_TEST_TRAIT_TRUE(( hash_is_avalanching< boost::hash > )); + +#endif + +#if !defined(BOOST_NO_CXX11_CHAR32_T) + + BOOST_TEST_TRAIT_TRUE(( hash_is_avalanching< boost::hash > )); + +#endif + +#if defined(__cpp_char8_t) && __cpp_char8_t >= 201811L + + BOOST_TEST_TRAIT_TRUE(( hash_is_avalanching< boost::hash > )); + +#endif + + BOOST_TEST_TRAIT_FALSE(( hash_is_avalanching< boost::hash > > )); + + return boost::report_errors(); +} + +#endif diff --git a/test/hash_is_avalanching_test2.cpp b/test/hash_is_avalanching_test2.cpp new file mode 100644 index 0000000..2158341 --- /dev/null +++ b/test/hash_is_avalanching_test2.cpp @@ -0,0 +1,57 @@ +// Copyright 2022 Peter Dimov. +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +#include +#include +#include +#include + +#if defined(BOOST_NO_CXX11_HDR_TYPE_TRAITS) + +BOOST_PRAGMA_MESSAGE( "Test skipped, BOOST_NO_CXX11_HDR_TYPE_TRAITS is defined" ) +int main() {} + +#elif defined(BOOST_NO_CXX17_HDR_STRING_VIEW) + +BOOST_PRAGMA_MESSAGE( "Test skipped, BOOST_NO_CXX17_HDR_STRING_VIEW is defined" ) +int main() {} + +#else + +#include +#include + +enum my_char { min = 0, max = 255 }; + +int main() +{ + using boost::unordered::hash_is_avalanching; + + BOOST_TEST_TRAIT_TRUE(( hash_is_avalanching< boost::hash > )); + BOOST_TEST_TRAIT_TRUE(( hash_is_avalanching< boost::hash > )); + +#if !defined(BOOST_NO_CXX11_CHAR16_T) + + BOOST_TEST_TRAIT_TRUE(( hash_is_avalanching< boost::hash > )); + +#endif + +#if !defined(BOOST_NO_CXX11_CHAR32_T) + + BOOST_TEST_TRAIT_TRUE(( hash_is_avalanching< boost::hash > )); + +#endif + +#if defined(__cpp_char8_t) && __cpp_char8_t >= 201811L + + BOOST_TEST_TRAIT_TRUE(( hash_is_avalanching< boost::hash > )); + +#endif + + BOOST_TEST_TRAIT_FALSE(( hash_is_avalanching< boost::hash > > )); + + return boost::report_errors(); +} + +#endif