1
Fork 0
turbonss/cxxmph/seeded_hash.h

88 lines
2.9 KiB
C
Raw Normal View History

2011-05-16 02:47:42 +03:00
#ifndef __CXXMPH_SEEDED_HASH_H__
#define __CXXMPH_SEEDED_HASH_H__
2011-02-14 00:40:26 +02:00
#include <stdint.h> // for uint32_t and friends
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"
namespace cxxmph {
template <class HashFcn>
struct seeded_hash_function {
template <class Key>
2011-02-14 00:40:26 +02:00
uint32_t operator()(const Key& k, uint32_t seed) const {
2010-11-05 08:40:15 +02:00
return HashFcn()(k) ^ seed;
}
};
struct Murmur2 {
template<class Key>
2011-02-14 00:40:26 +02:00
uint32_t operator()(const Key& k) const {
2010-11-05 08:40:15 +02:00
return MurmurHash2(k, sizeof(Key), 1 /* seed */);
}
};
struct Murmur2StringPiece {
template <class Key>
2011-02-14 00:40:26 +02:00
uint32_t operator()(const Key& k) const {
2010-11-05 08:40:15 +02:00
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>
2011-02-14 00:40:26 +02:00
uint32_t operator()(const Key& k, uint32_t 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>
2011-02-14 00:40:26 +02:00
uint32_t operator()(const Key& k, uint32_t seed) const {
2010-11-05 08:40:15 +02:00
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
}
};
2011-05-16 02:47:42 +03:00
template <class HashFcn> struct seeded_hash
2010-11-05 08:40:15 +02:00
{ 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.
2011-05-16 02:47:42 +03:00
template <> struct seeded_hash<std::hash<char*> >
2010-11-05 08:40:15 +02:00
{ typedef seeded_hash_function<Murmur2StringPiece> hash_function; };
2011-05-16 02:47:42 +03:00
template <> struct seeded_hash<std::hash<const char*> >
2010-11-05 08:40:15 +02:00
{ typedef seeded_hash_function<Murmur2StringPiece> hash_function; };
2011-05-16 02:47:42 +03:00
template <> struct seeded_hash<std::hash<std::string> >
2010-11-05 08:40:15 +02:00
{ typedef seeded_hash_function<Murmur2StringPiece> hash_function; };
2011-05-16 02:47:42 +03:00
template <> struct seeded_hash<std::hash<char> >
2010-11-05 08:40:15 +02:00
{ typedef seeded_hash_function<Murmur2> hash_function; };
2011-05-16 02:47:42 +03:00
template <> struct seeded_hash<std::hash<unsigned char> >
2010-11-05 08:40:15 +02:00
{ typedef seeded_hash_function<Murmur2> hash_function; };
2011-05-16 02:47:42 +03:00
template <> struct seeded_hash<std::hash<short> >
2010-11-05 08:40:15 +02:00
{ typedef seeded_hash_function<Murmur2> hash_function; };
2011-05-16 02:47:42 +03:00
template <> struct seeded_hash<std::hash<unsigned short> >
2010-11-05 08:40:15 +02:00
{ typedef seeded_hash_function<Murmur2> hash_function; };
2011-05-16 02:47:42 +03:00
template <> struct seeded_hash<std::hash<int> >
2010-11-05 08:40:15 +02:00
{ typedef seeded_hash_function<Murmur2> hash_function; };
2011-05-16 02:47:42 +03:00
template <> struct seeded_hash<std::hash<unsigned int> >
2010-11-05 08:40:15 +02:00
{ typedef seeded_hash_function<Murmur2> hash_function; };
2011-05-16 02:47:42 +03:00
template <> struct seeded_hash<std::hash<long> >
2010-11-05 08:40:15 +02:00
{ typedef seeded_hash_function<Murmur2> hash_function; };
2011-05-16 02:47:42 +03:00
template <> struct seeded_hash<std::hash<unsigned long> >
2010-11-08 22:19:44 +02:00
{ typedef seeded_hash_function<Murmur2> hash_function; };
2011-05-16 02:47:42 +03:00
template <> struct seeded_hash<std::hash<long long> >
2010-11-08 22:19:44 +02:00
{ typedef seeded_hash_function<Murmur2> hash_function; };
2011-05-16 02:47:42 +03:00
template <> struct seeded_hash<std::hash<unsigned long long> >
2010-11-05 08:40:15 +02:00
{ typedef seeded_hash_function<Murmur2> hash_function; };
} // namespace cxxmph
2011-05-16 02:47:42 +03:00
#endif // __CXXMPH_SEEDED_HASH_H__