Boost logo

Boost-Commit :

From: daniel_james_at_[hidden]
Date: 2007-08-27 14:16:55


Author: danieljames
Date: 2007-08-27 14:16:54 EDT (Mon, 27 Aug 2007)
New Revision: 39014
URL: http://svn.boost.org/trac/boost/changeset/39014

Log:
Add proper support for long longs and unsigned long longs.
Text files modified:
   trunk/boost/functional/hash/hash.hpp | 65 +++++++++++++++++++++++++++++----------
   trunk/libs/functional/hash/doc/ref.xml | 50 ++++++++++++++++++++++++++++++
   trunk/libs/functional/hash/test/hash_number_test.cpp | 12 ++++++
   3 files changed, 109 insertions(+), 18 deletions(-)

Modified: trunk/boost/functional/hash/hash.hpp
==============================================================================
--- trunk/boost/functional/hash/hash.hpp (original)
+++ trunk/boost/functional/hash/hash.hpp 2007-08-27 14:16:54 EDT (Mon, 27 Aug 2007)
@@ -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 @@
     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 @@
 
     // 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 @@
     }
 #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 @@
 #endif // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
 }
 
-#if defined(BOOST_MSVC)
-# pragma warning(pop)
-#endif
-
 #endif
 

Modified: trunk/libs/functional/hash/doc/ref.xml
==============================================================================
--- trunk/libs/functional/hash/doc/ref.xml (original)
+++ trunk/libs/functional/hash/doc/ref.xml 2007-08-27 14:16:54 EDT (Mon, 27 Aug 2007)
@@ -264,6 +264,42 @@
       <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>
           <template-arg>float</template-arg>
         </specialization>
         <method name="operator()" cv="const">
@@ -536,6 +572,16 @@
 
         <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>
         </signature>
 
@@ -700,6 +746,10 @@
                   <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) &lt;= std::numeric_limits&lt;std::size_t&gt;::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>
                 </row>

Modified: trunk/libs/functional/hash/test/hash_number_test.cpp
==============================================================================
--- trunk/libs/functional/hash/test/hash_number_test.cpp (original)
+++ trunk/libs/functional/hash/test/hash_number_test.cpp 2007-08-27 14:16:54 EDT (Mon, 27 Aug 2007)
@@ -48,7 +48,8 @@
 
     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 @@
     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 @@
     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)


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk