forked from boostorg/container_hash
Add proper support for long longs and unsigned long longs.
[SVN r39014]
This commit is contained in:
50
doc/ref.xml
50
doc/ref.xml
@@ -261,6 +261,42 @@ file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
</method>
|
||||
</struct-specialization>
|
||||
|
||||
<struct-specialization name="hash">
|
||||
<template></template>
|
||||
<specialization>
|
||||
<template-arg>long long</template-arg>
|
||||
</specialization>
|
||||
<method name="operator()" cv="const">
|
||||
<type>std::size_t</type>
|
||||
<parameter name="val">
|
||||
<paramtype>long long</paramtype>
|
||||
</parameter>
|
||||
<returns>
|
||||
<para>Unspecified in TR1, except that equal arguments yield the same result.</para>
|
||||
<para><functionname>hash_value</functionname>(val) in Boost.</para>
|
||||
</returns>
|
||||
<throws><para>Doesn't throw</para></throws>
|
||||
</method>
|
||||
</struct-specialization>
|
||||
|
||||
<struct-specialization name="hash">
|
||||
<template></template>
|
||||
<specialization>
|
||||
<template-arg>unsigned long long</template-arg>
|
||||
</specialization>
|
||||
<method name="operator()" cv="const">
|
||||
<type>std::size_t</type>
|
||||
<parameter name="val">
|
||||
<paramtype>unsigned long long</paramtype>
|
||||
</parameter>
|
||||
<returns>
|
||||
<para>Unspecified in TR1, except that equal arguments yield the same result.</para>
|
||||
<para><functionname>hash_value</functionname>(val) in Boost.</para>
|
||||
</returns>
|
||||
<throws><para>Doesn't throw</para></throws>
|
||||
</method>
|
||||
</struct-specialization>
|
||||
|
||||
<struct-specialization name="hash">
|
||||
<template></template>
|
||||
<specialization>
|
||||
@@ -534,6 +570,16 @@ for(; first != last; ++first)
|
||||
<parameter name="val"><paramtype>unsigned long</paramtype></parameter>
|
||||
</signature>
|
||||
|
||||
<signature>
|
||||
<type>std::size_t</type>
|
||||
<parameter name="val"><paramtype>long long</paramtype></parameter>
|
||||
</signature>
|
||||
|
||||
<signature>
|
||||
<type>std::size_t</type>
|
||||
<parameter name="val"><paramtype>unsigned long long</paramtype></parameter>
|
||||
</signature>
|
||||
|
||||
<signature>
|
||||
<type>std::size_t</type>
|
||||
<parameter name="val"><paramtype>float</paramtype></parameter>
|
||||
@@ -699,6 +745,10 @@ for(; first != last; ++first)
|
||||
</entry>
|
||||
<entry><code>val</code></entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry><code>long long</code>, <code>unsigned long long</code></entry>
|
||||
<entry><code>val</code> when <code>abs(val) <= std::numeric_limits<std::size_t>::max()</code>.</entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry><code>float</code>, <code>double</code>, <code>long double</code></entry>
|
||||
<entry>An unspecified value, except that equal arguments shall yield the same result.</entry>
|
||||
|
@@ -28,11 +28,6 @@
|
||||
#include <boost/type_traits/is_const.hpp>
|
||||
#endif
|
||||
|
||||
#if defined(BOOST_MSVC)
|
||||
# pragma warning(push)
|
||||
# pragma warning(disable:4267)
|
||||
#endif
|
||||
|
||||
namespace boost
|
||||
{
|
||||
std::size_t hash_value(bool);
|
||||
@@ -50,11 +45,7 @@ namespace boost
|
||||
std::size_t hash_value(wchar_t);
|
||||
#endif
|
||||
|
||||
#if defined(BOOST_HAS_LONG_LONG) && defined(_M_X64) && defined(_WIN64)
|
||||
// On 64-bit windows std::size_t is a typedef for unsigned long long, which
|
||||
// isn't due to be supported until Boost 1.35. So add support here.
|
||||
// (Technically, Boost.Hash isn't actually documented as supporting
|
||||
// std::size_t. But it would be pretty silly not to).
|
||||
#if defined(BOOST_HAS_LONG_LONG)
|
||||
std::size_t hash_value(long long);
|
||||
std::size_t hash_value(unsigned long long);
|
||||
#endif
|
||||
@@ -99,6 +90,50 @@ namespace boost
|
||||
|
||||
// Implementation
|
||||
|
||||
namespace hash_detail
|
||||
{
|
||||
template <class T>
|
||||
inline std::size_t hash_value_signed(T val)
|
||||
{
|
||||
const int size_t_bits = std::numeric_limits<std::size_t>::digits;
|
||||
// ceiling(std::numeric_limits<T>::digits / size_t_bits) - 1
|
||||
const int length = (std::numeric_limits<T>::digits - 1)
|
||||
/ size_t_bits;
|
||||
|
||||
std::size_t seed = 0;
|
||||
T positive = val < 0 ? -1 - val : val;
|
||||
|
||||
// Hopefully, this loop can be unrolled.
|
||||
for(unsigned int i = length * size_t_bits; i > 0; i -= size_t_bits)
|
||||
{
|
||||
seed ^= (std::size_t) (positive >> i) + (seed<<6) + (seed>>2);
|
||||
}
|
||||
seed ^= (std::size_t) val + (seed<<6) + (seed>>2);
|
||||
|
||||
return seed;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
inline std::size_t hash_value_unsigned(T val)
|
||||
{
|
||||
const int size_t_bits = std::numeric_limits<std::size_t>::digits;
|
||||
// ceiling(std::numeric_limits<T>::digits / size_t_bits) - 1
|
||||
const int length = (std::numeric_limits<T>::digits - 1)
|
||||
/ size_t_bits;
|
||||
|
||||
std::size_t seed = 0;
|
||||
|
||||
// Hopefully, this loop can be unrolled.
|
||||
for(unsigned int i = length * size_t_bits; i > 0; i -= size_t_bits)
|
||||
{
|
||||
seed ^= (std::size_t) (val >> i) + (seed<<6) + (seed>>2);
|
||||
}
|
||||
seed ^= (std::size_t) val + (seed<<6) + (seed>>2);
|
||||
|
||||
return seed;
|
||||
}
|
||||
}
|
||||
|
||||
inline std::size_t hash_value(bool v)
|
||||
{
|
||||
return static_cast<std::size_t>(v);
|
||||
@@ -155,15 +190,15 @@ namespace boost
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(BOOST_HAS_LONG_LONG) && defined(_M_X64) && defined(_WIN64)
|
||||
#if defined(BOOST_HAS_LONG_LONG)
|
||||
inline std::size_t hash_value(long long v)
|
||||
{
|
||||
return v;
|
||||
return hash_detail::hash_value_signed(v);
|
||||
}
|
||||
|
||||
inline std::size_t hash_value(unsigned long long v)
|
||||
{
|
||||
return v;
|
||||
return hash_detail::hash_value_unsigned(v);
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -635,9 +670,5 @@ namespace boost
|
||||
#endif // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
|
||||
}
|
||||
|
||||
#if defined(BOOST_MSVC)
|
||||
# pragma warning(pop)
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
|
@@ -48,7 +48,8 @@ void numeric_test(T*)
|
||||
|
||||
if (limits::is_integer)
|
||||
{
|
||||
BOOST_TEST(HASH_NAMESPACE::hash_value(T(-5)) == (std::size_t)T(-5));
|
||||
if(limits::is_signed || limits::digits <= std::numeric_limits<std::size_t>::digits)
|
||||
BOOST_TEST(HASH_NAMESPACE::hash_value(T(-5)) == (std::size_t)T(-5));
|
||||
BOOST_TEST(HASH_NAMESPACE::hash_value(T(0)) == (std::size_t)T(0u));
|
||||
BOOST_TEST(HASH_NAMESPACE::hash_value(T(10)) == (std::size_t)T(10u));
|
||||
BOOST_TEST(HASH_NAMESPACE::hash_value(T(25)) == (std::size_t)T(25u));
|
||||
@@ -111,6 +112,10 @@ void poor_quality_tests(T*)
|
||||
numeric_test((type*) 0); \
|
||||
limits_test((type*) 0); \
|
||||
poor_quality_tests((type*) 0);
|
||||
#define NUMERIC_TEST_NO_LIMITS(type, name) \
|
||||
std::cerr<<"Testing: " BOOST_STRINGIZE(name) "\n"; \
|
||||
numeric_test((type*) 0); \
|
||||
poor_quality_tests((type*) 0);
|
||||
|
||||
int main()
|
||||
{
|
||||
@@ -128,6 +133,11 @@ int main()
|
||||
NUMERIC_TEST(long, hash_long)
|
||||
NUMERIC_TEST(unsigned long, ulong)
|
||||
|
||||
#if defined(BOOST_HAS_LONG_LONG)
|
||||
NUMERIC_TEST_NO_LIMITS(long long, hash_longlong)
|
||||
NUMERIC_TEST_NO_LIMITS(unsigned long long, ulonglong)
|
||||
#endif
|
||||
|
||||
NUMERIC_TEST(float, float)
|
||||
NUMERIC_TEST(double, double)
|
||||
NUMERIC_TEST(long double, ldouble)
|
||||
|
Reference in New Issue
Block a user