Add proper support for long longs and unsigned long longs.

[SVN r39014]
This commit is contained in:
Daniel James
2007-08-27 18:16:54 +00:00
parent 59bc46847c
commit fe5fa48972
3 changed files with 109 additions and 18 deletions

View File

@@ -261,6 +261,42 @@ file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
</method> </method>
</struct-specialization> </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"> <struct-specialization name="hash">
<template></template> <template></template>
<specialization> <specialization>
@@ -534,6 +570,16 @@ for(; first != last; ++first)
<parameter name="val"><paramtype>unsigned long</paramtype></parameter> <parameter name="val"><paramtype>unsigned long</paramtype></parameter>
</signature> </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> <signature>
<type>std::size_t</type> <type>std::size_t</type>
<parameter name="val"><paramtype>float</paramtype></parameter> <parameter name="val"><paramtype>float</paramtype></parameter>
@@ -699,6 +745,10 @@ for(; first != last; ++first)
</entry> </entry>
<entry><code>val</code></entry> <entry><code>val</code></entry>
</row> </row>
<row>
<entry><code>long long</code>, <code>unsigned long long</code></entry>
<entry><code>val</code> when <code>abs(val) &lt;= std::numeric_limits&lt;std::size_t&gt;::max()</code>.</entry>
</row>
<row> <row>
<entry><code>float</code>, <code>double</code>, <code>long double</code></entry> <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> <entry>An unspecified value, except that equal arguments shall yield the same result.</entry>

View File

@@ -48,7 +48,8 @@ void numeric_test(T*)
if (limits::is_integer) 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(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(10)) == (std::size_t)T(10u));
BOOST_TEST(HASH_NAMESPACE::hash_value(T(25)) == (std::size_t)T(25u)); 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); \ numeric_test((type*) 0); \
limits_test((type*) 0); \ limits_test((type*) 0); \
poor_quality_tests((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() int main()
{ {
@@ -128,6 +133,11 @@ int main()
NUMERIC_TEST(long, hash_long) NUMERIC_TEST(long, hash_long)
NUMERIC_TEST(unsigned long, ulong) 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(float, float)
NUMERIC_TEST(double, double) NUMERIC_TEST(double, double)
NUMERIC_TEST(long double, ldouble) NUMERIC_TEST(long double, ldouble)

View File

@@ -28,11 +28,6 @@
#include <boost/type_traits/is_const.hpp> #include <boost/type_traits/is_const.hpp>
#endif #endif
#if defined(BOOST_MSVC)
# pragma warning(push)
# pragma warning(disable:4267)
#endif
namespace boost namespace boost
{ {
std::size_t hash_value(bool); std::size_t hash_value(bool);
@@ -50,11 +45,7 @@ namespace boost
std::size_t hash_value(wchar_t); std::size_t hash_value(wchar_t);
#endif #endif
#if defined(BOOST_HAS_LONG_LONG) && defined(_M_X64) && defined(_WIN64) #if defined(BOOST_HAS_LONG_LONG)
// 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).
std::size_t hash_value(long long); std::size_t hash_value(long long);
std::size_t hash_value(unsigned long long); std::size_t hash_value(unsigned long long);
#endif #endif
@@ -99,6 +90,50 @@ namespace boost
// Implementation // 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) inline std::size_t hash_value(bool v)
{ {
return static_cast<std::size_t>(v); return static_cast<std::size_t>(v);
@@ -155,15 +190,15 @@ namespace boost
} }
#endif #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) 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) inline std::size_t hash_value(unsigned long long v)
{ {
return v; return hash_detail::hash_value_unsigned(v);
} }
#endif #endif
@@ -635,9 +670,5 @@ namespace boost
#endif // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION #endif // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
} }
#if defined(BOOST_MSVC)
# pragma warning(pop)
#endif
#endif #endif