Compare commits

...

3 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
Peter Dimov
c28d0b813b Directly cast to size_t all integrals no wider than size_t 2022-06-26 01:25:45 +03:00
2 changed files with 41 additions and 23 deletions

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 ;

View File

@@ -26,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>
@@ -38,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
@@ -77,11 +83,12 @@ 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 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, std::size_t size_t_bits, std::size_t type_bits> struct hash_integral_impl<T, false, size_t_bits, type_bits>
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 )
{
@@ -89,7 +96,24 @@ namespace boost
}
};
template<class T> struct hash_integral_impl<T, true, 32, 64>
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 )
{
typedef typename boost::make_unsigned<T>::type U;
if( v >= 0 )
{
return hash_integral_impl<U>::fn( static_cast<U>( v ) );
}
else
{
return ~hash_integral_impl<U>::fn( static_cast<U>( ~static_cast<U>( v ) ) );
}
}
};
template<class T> struct hash_integral_impl<T, true, true, 32, 64>
{
static std::size_t fn( T v )
{
@@ -102,7 +126,7 @@ namespace boost
}
};
template<class T> struct hash_integral_impl<T, true, 32, 128>
template<class T> struct hash_integral_impl<T, true, true, 32, 128>
{
static std::size_t fn( T v )
{
@@ -117,7 +141,7 @@ namespace boost
}
};
template<class T> struct hash_integral_impl<T, true, 64, 128>
template<class T> struct hash_integral_impl<T, true, true, 64, 128>
{
static std::size_t fn( T v )
{
@@ -133,35 +157,29 @@ namespace boost
} // namespace hash_detail
template <typename T>
typename boost::enable_if_<boost::conjunction<boost::is_integral<T>, boost::is_unsigned<T> >::value, std::size_t>::type
typename boost::enable_if_<boost::is_integral<T>::value, std::size_t>::type
hash_value( T v )
{
return hash_detail::hash_integral_impl<T>::fn( v );
}
template <typename T>
typename boost::enable_if_<boost::conjunction<boost::is_integral<T>, boost::is_signed<T> >::value, std::size_t>::type
hash_value( T v )
{
typedef typename boost::make_unsigned<T>::type U;
if( v >= 0 )
{
return hash_value( static_cast<U>( v ) );
}
else
{
return ~hash_value( static_cast<U>( ~static_cast<U>( v ) ) );
}
}
// enumeration types
template <typename T>
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