1
Fork 0
turbonss/cxxmph/cmph_hash_function.h

82 lines
3.0 KiB
C
Raw Normal View History

2010-11-05 08:40:15 +02:00
#include <cstdlib>
2010-11-08 22:19:44 +02:00
#include <unordered_map> // for std::hash
2010-11-05 08:40:15 +02:00
#include "MurmurHash2.h"
#include "stringpiece.h"
#include "cmph_types.h"
namespace cxxmph {
template <class HashFcn>
struct seeded_hash_function {
template <class Key>
cmph_uint32 operator()(const Key& k, cmph_uint32 seed) const {
return HashFcn()(k) ^ seed;
}
};
struct Murmur2 {
template<class Key>
cmph_uint32 operator()(const Key& k) const {
return MurmurHash2(k, sizeof(Key), 1 /* seed */);
}
};
struct Murmur2StringPiece {
template <class Key>
cmph_uint32 operator()(const Key& k) const {
StringPiece s(k);
2010-11-05 08:48:53 +02:00
return MurmurHash2(s.data(), s.length(), 1 /* seed */);
2010-11-05 08:40:15 +02:00
}
};
template <>
struct seeded_hash_function<Murmur2> {
template <class Key>
cmph_uint32 operator()(const Key& k, cmph_uint32 seed) const {
2010-11-08 22:19:44 +02:00
return MurmurHash2(reinterpret_cast<const void*>(&k), sizeof(Key), seed);
2010-11-05 08:40:15 +02:00
}
};
template <>
struct seeded_hash_function<Murmur2StringPiece> {
template <class Key>
cmph_uint32 operator()(const Key& k, cmph_uint32 seed) const {
StringPiece s(k);
2010-11-05 08:48:53 +02:00
return MurmurHash2(s.data(), s.length(), seed);
2010-11-05 08:40:15 +02:00
}
};
template <class HashFcn> struct OptimizedSeededHashFunction
{ typedef seeded_hash_function<HashFcn> hash_function; };
2010-11-08 22:19:44 +02:00
// Use Murmur2 instead for all types defined in std::hash, plus
2010-11-05 08:40:15 +02:00
// std::string which is commonly extended.
2010-11-08 22:19:44 +02:00
template <> struct OptimizedSeededHashFunction<std::hash<char*> >
2010-11-05 08:40:15 +02:00
{ typedef seeded_hash_function<Murmur2StringPiece> hash_function; };
2010-11-08 22:19:44 +02:00
template <> struct OptimizedSeededHashFunction<std::hash<const char*> >
2010-11-05 08:40:15 +02:00
{ typedef seeded_hash_function<Murmur2StringPiece> hash_function; };
2010-11-08 22:19:44 +02:00
template <> struct OptimizedSeededHashFunction<std::hash<std::string> >
2010-11-05 08:40:15 +02:00
{ typedef seeded_hash_function<Murmur2StringPiece> hash_function; };
2010-11-08 22:19:44 +02:00
template <> struct OptimizedSeededHashFunction<std::hash<char> >
2010-11-05 08:40:15 +02:00
{ typedef seeded_hash_function<Murmur2> hash_function; };
2010-11-08 22:19:44 +02:00
template <> struct OptimizedSeededHashFunction<std::hash<unsigned char> >
2010-11-05 08:40:15 +02:00
{ typedef seeded_hash_function<Murmur2> hash_function; };
2010-11-08 22:19:44 +02:00
template <> struct OptimizedSeededHashFunction<std::hash<short> >
2010-11-05 08:40:15 +02:00
{ typedef seeded_hash_function<Murmur2> hash_function; };
2010-11-08 22:19:44 +02:00
template <> struct OptimizedSeededHashFunction<std::hash<unsigned short> >
2010-11-05 08:40:15 +02:00
{ typedef seeded_hash_function<Murmur2> hash_function; };
2010-11-08 22:19:44 +02:00
template <> struct OptimizedSeededHashFunction<std::hash<int> >
2010-11-05 08:40:15 +02:00
{ typedef seeded_hash_function<Murmur2> hash_function; };
2010-11-08 22:19:44 +02:00
template <> struct OptimizedSeededHashFunction<std::hash<unsigned int> >
2010-11-05 08:40:15 +02:00
{ typedef seeded_hash_function<Murmur2> hash_function; };
2010-11-08 22:19:44 +02:00
template <> struct OptimizedSeededHashFunction<std::hash<long> >
2010-11-05 08:40:15 +02:00
{ typedef seeded_hash_function<Murmur2> hash_function; };
2010-11-08 22:19:44 +02:00
template <> struct OptimizedSeededHashFunction<std::hash<unsigned long> >
{ typedef seeded_hash_function<Murmur2> hash_function; };
template <> struct OptimizedSeededHashFunction<std::hash<long long> >
{ typedef seeded_hash_function<Murmur2> hash_function; };
template <> struct OptimizedSeededHashFunction<std::hash<unsigned long long> >
2010-11-05 08:40:15 +02:00
{ typedef seeded_hash_function<Murmur2> hash_function; };
} // namespace cxxmph