File indexing completed on 2025-01-18 09:30:01
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #ifndef BOOST_COMPUTE_FUNCTIONAL_HASH_HPP
0012 #define BOOST_COMPUTE_FUNCTIONAL_HASH_HPP
0013
0014 #include <boost/compute/function.hpp>
0015 #include <boost/compute/types/fundamental.hpp>
0016
0017 namespace boost {
0018 namespace compute {
0019 namespace detail {
0020
0021 template<class Key>
0022 std::string make_hash_function_name()
0023 {
0024 return std::string("boost_hash_") + type_name<Key>();
0025 }
0026
0027 template<class Key>
0028 inline std::string make_hash_function_source()
0029 {
0030 std::stringstream source;
0031 source << "inline ulong " << make_hash_function_name<Key>()
0032 << "(const " << type_name<Key>() << " x)\n"
0033 << "{\n"
0034
0035
0036 << " ulong a = as_uint(x);\n"
0037 << " a = (a ^ 61) ^ (a >> 16);\n"
0038 << " a = a + (a << 3);\n"
0039 << " a = a ^ (a >> 4);\n"
0040 << " a = a * 0x27d4eb2d;\n"
0041 << " a = a ^ (a >> 15);\n"
0042 << " return a;\n"
0043 << "}\n";
0044 return source.str();
0045 }
0046
0047 template<class Key>
0048 struct hash_impl
0049 {
0050 typedef Key argument_type;
0051 typedef ulong_ result_type;
0052
0053 hash_impl()
0054 : m_function("")
0055 {
0056 m_function = make_function_from_source<result_type(argument_type)>(
0057 make_hash_function_name<argument_type>(),
0058 make_hash_function_source<argument_type>()
0059 );
0060 }
0061
0062 template<class Arg>
0063 invoked_function<result_type, boost::tuple<Arg> >
0064 operator()(const Arg &arg) const
0065 {
0066 return m_function(arg);
0067 }
0068
0069 function<result_type(argument_type)> m_function;
0070 };
0071
0072 }
0073
0074
0075
0076
0077 template<class Key> struct hash;
0078
0079
0080 template<> struct hash<int_> : detail::hash_impl<int_> { };
0081
0082
0083 template<> struct hash<uint_> : detail::hash_impl<uint_> { };
0084
0085
0086 template<> struct hash<float_> : detail::hash_impl<float_> { };
0087
0088 }
0089 }
0090
0091 #endif