forked from boostorg/functional
in hash and seperate out some of the detail headers. Merged revisions 53159-53161,53167-53169,53175,53185,53205,53247-53248,53254 via svnmerge from https://svn.boost.org/svn/boost/trunk ........ r53159 | danieljames | 2009-05-21 22:21:11 +0100 (Thu, 21 May 2009) | 1 line Move the hash limits workaround into its own file. ........ r53160 | danieljames | 2009-05-21 22:21:44 +0100 (Thu, 21 May 2009) | 1 line Move the two different hash float implementation into their own header. ........ r53161 | danieljames | 2009-05-21 22:22:04 +0100 (Thu, 21 May 2009) | 1 line Try to automatically detect which float functions are available. ........ r53167 | danieljames | 2009-05-22 07:00:56 +0100 (Fri, 22 May 2009) | 1 line Fix a typo. ........ r53168 | danieljames | 2009-05-22 07:01:19 +0100 (Fri, 22 May 2009) | 3 lines Spell out exactly which functions can be used with which types. I was hitting some ambiguity errors when the function was for the wrong type. ........ r53169 | danieljames | 2009-05-22 07:01:35 +0100 (Fri, 22 May 2009) | 1 line Some STLport fixes for hash. ........ r53175 | danieljames | 2009-05-22 14:35:56 +0100 (Fri, 22 May 2009) | 2 lines Rename struct to avoid using 'type::'type' which confuses some compilers. ........ r53185 | danieljames | 2009-05-22 20:00:35 +0100 (Fri, 22 May 2009) | 1 line Explicitly qualify 'none' to avoid confusion with boost::none. ........ r53205 | danieljames | 2009-05-23 16:21:38 +0100 (Sat, 23 May 2009) | 4 lines Try to deal with macros for frexpl and ldexpl. The error message for msvc-9.0~wm5~stlport5.2 suggests that frexpl and ldexpl are macros. ........ r53247 | danieljames | 2009-05-25 14:45:16 +0100 (Mon, 25 May 2009) | 4 lines Check for float functions with less templates. The only template mechanism now used is full specialization, so this should hopefully be more portable to compilers we don't test. ........ r53248 | danieljames | 2009-05-25 15:27:00 +0100 (Mon, 25 May 2009) | 1 line Fix a couple of clumsy errors in the last commit. ........ r53254 | danieljames | 2009-05-25 20:44:52 +0100 (Mon, 25 May 2009) | 1 line Hash change log. ........ [SVN r53361]
94 lines
2.8 KiB
C++
94 lines
2.8 KiB
C++
|
|
// Copyright 2005-2009 Daniel James.
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
// A general purpose hash function for non-zero floating point values.
|
|
|
|
#if !defined(BOOST_FUNCTIONAL_HASH_DETAIL_HASH_FLOAT_GENERIC_HEADER)
|
|
#define BOOST_FUNCTIONAL_HASH_DETAIL_HASH_FLOAT_GENERIC_HEADER
|
|
|
|
#include <boost/functional/hash/detail/float_functions.hpp>
|
|
#include <boost/integer/static_log2.hpp>
|
|
#include <boost/functional/hash/detail/limits.hpp>
|
|
|
|
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
|
|
# pragma once
|
|
#endif
|
|
|
|
#if defined(BOOST_MSVC)
|
|
#pragma warning(push)
|
|
#if BOOST_MSVC >= 1400
|
|
#pragma warning(disable:6294) // Ill-defined for-loop: initial condition does
|
|
// not satisfy test. Loop body not executed
|
|
#endif
|
|
#endif
|
|
|
|
namespace boost
|
|
{
|
|
namespace hash_detail
|
|
{
|
|
inline void hash_float_combine(std::size_t& seed, std::size_t value)
|
|
{
|
|
seed ^= value + (seed<<6) + (seed>>2);
|
|
}
|
|
|
|
template <class T>
|
|
inline std::size_t float_hash_impl2(T v)
|
|
{
|
|
boost::hash_detail::call_frexp<T> frexp;
|
|
boost::hash_detail::call_ldexp<T> ldexp;
|
|
|
|
int exp = 0;
|
|
|
|
v = frexp(v, &exp);
|
|
|
|
// A postive value is easier to hash, so combine the
|
|
// sign with the exponent and use the absolute value.
|
|
if(v < 0) {
|
|
v = -v;
|
|
exp += limits<T>::max_exponent -
|
|
limits<T>::min_exponent;
|
|
}
|
|
|
|
// The result of frexp is always between 0.5 and 1, so its
|
|
// top bit will always be 1. Subtract by 0.5 to remove that.
|
|
v -= T(0.5);
|
|
v = ldexp(v, limits<std::size_t>::digits + 1);
|
|
std::size_t seed = static_cast<std::size_t>(v);
|
|
v -= seed;
|
|
|
|
// ceiling(digits(T) * log2(radix(T))/ digits(size_t)) - 1;
|
|
std::size_t const length
|
|
= (limits<T>::digits *
|
|
boost::static_log2<limits<T>::radix>::value - 1)
|
|
/ limits<std::size_t>::digits;
|
|
|
|
for(std::size_t i = 0; i != length; ++i)
|
|
{
|
|
v = ldexp(v, limits<std::size_t>::digits);
|
|
std::size_t part = static_cast<std::size_t>(v);
|
|
v -= part;
|
|
hash_float_combine(seed, part);
|
|
}
|
|
|
|
hash_float_combine(seed, exp);
|
|
|
|
return seed;
|
|
};
|
|
|
|
template <class T>
|
|
inline std::size_t float_hash_impl(T v)
|
|
{
|
|
typedef BOOST_DEDUCED_TYPENAME select_hash_type<T>::type type;
|
|
return float_hash_impl2(static_cast<type>(v));
|
|
}
|
|
}
|
|
}
|
|
|
|
#if defined(BOOST_MSVC)
|
|
#pragma warning(pop)
|
|
#endif
|
|
|
|
#endif
|