Compare commits

..

2 Commits

Author SHA1 Message Date
Peter Dimov 80485fb963 Use the underlying type for hashing enums 2022-07-03 20:29:28 +03:00
Peter Dimov a3cac265b1 Disable examples/template.cpp for msvc-8.0 2022-07-03 20:28:28 +03:00
2 changed files with 41 additions and 20 deletions
+1 -1
View File
@@ -6,4 +6,4 @@
run books.cpp ;
run point.cpp ;
run portable.cpp ;
run template.cpp ;
run template.cpp : : : <toolset>msvc-8.0:<build>no ;
+40 -19
View File
@@ -1,6 +1,5 @@
// Copyright 2005-2014 Daniel James.
// Copyright 2021 Peter Dimov.
// Copyright 2022 Glen Joseph Fernandes (glenjofe@gmail.com)
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
@@ -27,7 +26,9 @@
#include <boost/type_traits/is_floating_point.hpp>
#include <boost/type_traits/is_signed.hpp>
#include <boost/type_traits/is_unsigned.hpp>
#include <boost/type_traits/make_signed.hpp>
#include <boost/type_traits/make_unsigned.hpp>
#include <boost/type_traits/conditional.hpp>
#include <boost/type_traits/enable_if.hpp>
#include <boost/type_traits/conjunction.hpp>
#include <boost/cstdint.hpp>
@@ -39,6 +40,10 @@
#include <climits>
#include <cstring>
#if !defined(BOOST_NO_CXX11_HDR_TYPE_TRAITS)
# include <type_traits>
#endif
#if !defined(BOOST_NO_CXX11_SMART_PTR)
# include <memory>
#endif
@@ -79,10 +84,11 @@ namespace boost
template<class T,
bool bigger_than_size_t = (sizeof(T) > sizeof(std::size_t)),
bool is_unsigned = boost::is_unsigned<T>::value,
std::size_t blocks = sizeof(T) / sizeof(std::size_t)>
std::size_t size_t_bits = sizeof(std::size_t) * CHAR_BIT,
std::size_t type_bits = sizeof(T) * CHAR_BIT>
struct hash_integral_impl;
template<class T, bool is_unsigned, std::size_t blocks> struct hash_integral_impl<T, false, is_unsigned, blocks>
template<class T, bool is_unsigned, std::size_t size_t_bits, std::size_t type_bits> struct hash_integral_impl<T, false, is_unsigned, size_t_bits, type_bits>
{
static std::size_t fn( T v )
{
@@ -90,7 +96,7 @@ namespace boost
}
};
template<class T, std::size_t blocks> struct hash_integral_impl<T, true, false, blocks>
template<class T, std::size_t size_t_bits, std::size_t type_bits> struct hash_integral_impl<T, true, false, size_t_bits, type_bits>
{
static std::size_t fn( T v )
{
@@ -107,36 +113,41 @@ namespace boost
}
};
template<class T> struct hash_integral_impl<T, true, true, 2>
template<class T> struct hash_integral_impl<T, true, true, 32, 64>
{
static std::size_t fn( T v )
{
enum {
M = sizeof(std::size_t) * CHAR_BIT
};
std::size_t seed = 0;
seed ^= static_cast<std::size_t>( v >> M ) + ( seed << 6 ) + ( seed >> 2 );
seed ^= static_cast<std::size_t>( v >> 32 ) + ( seed << 6 ) + ( seed >> 2 );
seed ^= static_cast<std::size_t>( v ) + ( seed << 6 ) + ( seed >> 2 );
return seed;
}
};
template<class T> struct hash_integral_impl<T, true, true, 4>
template<class T> struct hash_integral_impl<T, true, true, 32, 128>
{
static std::size_t fn( T v )
{
enum {
M1 = sizeof(std::size_t) * CHAR_BIT,
M2 = M1 + M1,
M3 = M2 + M1
};
std::size_t seed = 0;
seed ^= static_cast<std::size_t>( v >> M3 ) + ( seed << 6 ) + ( seed >> 2 );
seed ^= static_cast<std::size_t>( v >> M2 ) + ( seed << 6 ) + ( seed >> 2 );
seed ^= static_cast<std::size_t>( v >> M1 ) + ( seed << 6 ) + ( seed >> 2 );
seed ^= static_cast<std::size_t>( v >> 96 ) + ( seed << 6 ) + ( seed >> 2 );
seed ^= static_cast<std::size_t>( v >> 64 ) + ( seed << 6 ) + ( seed >> 2 );
seed ^= static_cast<std::size_t>( v >> 32 ) + ( seed << 6 ) + ( seed >> 2 );
seed ^= static_cast<std::size_t>( v ) + ( seed << 6 ) + ( seed >> 2 );
return seed;
}
};
template<class T> struct hash_integral_impl<T, true, true, 64, 128>
{
static std::size_t fn( T v )
{
std::size_t seed = 0;
seed ^= static_cast<std::size_t>( v >> 64 ) + ( seed << 6 ) + ( seed >> 2 );
seed ^= static_cast<std::size_t>( v ) + ( seed << 6 ) + ( seed >> 2 );
return seed;
@@ -158,7 +169,17 @@ namespace boost
typename boost::enable_if_<boost::is_enum<T>::value, std::size_t>::type
hash_value( T v )
{
return static_cast<std::size_t>( v );
#if !defined(BOOST_NO_CXX11_HDR_TYPE_TRAITS)
typedef typename std::underlying_type<T>::type U;
#else
typedef typename boost::conditional< boost::is_signed<T>::value, boost::make_signed<T>, boost::make_unsigned<T> >::type::type U;
#endif
return boost::hash_value( static_cast<U>( v ) );
}
// floating point types