turbonss/cxxmph/seeded_hash.h

90 lines
3.1 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>
2011-05-24 03:18:24 +03:00
#include <tr1/unordered_map> // for std::tr1::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; };
2011-05-24 03:18:24 +03:00
// Use Murmur2 instead for all types defined in std::tr1::hash, plus
2010-11-05 08:40:15 +02:00
// std::string which is commonly extended.
2011-05-24 03:18:24 +03:00
template <> struct seeded_hash<std::tr1::hash<char*> >
2010-11-05 08:40:15 +02:00
{ typedef seeded_hash_function<Murmur2StringPiece> hash_function; };
2011-05-24 03:18:24 +03:00
template <> struct seeded_hash<std::tr1::hash<const char*> >
2010-11-05 08:40:15 +02:00
{ typedef seeded_hash_function<Murmur2StringPiece> hash_function; };
2011-05-24 03:18:24 +03:00
template <> struct seeded_hash<std::tr1::hash<std::string> >
2010-11-05 08:40:15 +02:00
{ typedef seeded_hash_function<Murmur2StringPiece> hash_function; };
2011-06-13 08:16:19 +03:00
template <> struct seeded_hash<std::tr1::hash<cxxmph::StringPiece> >
{ typedef seeded_hash_function<Murmur2StringPiece> hash_function; };
2010-11-05 08:40:15 +02:00
2011-05-24 03:18:24 +03:00
template <> struct seeded_hash<std::tr1::hash<char> >
2010-11-05 08:40:15 +02:00
{ typedef seeded_hash_function<Murmur2> hash_function; };
2011-05-24 03:18:24 +03:00
template <> struct seeded_hash<std::tr1::hash<unsigned char> >
2010-11-05 08:40:15 +02:00
{ typedef seeded_hash_function<Murmur2> hash_function; };
2011-05-24 03:18:24 +03:00
template <> struct seeded_hash<std::tr1::hash<short> >
2010-11-05 08:40:15 +02:00
{ typedef seeded_hash_function<Murmur2> hash_function; };
2011-05-24 03:18:24 +03:00
template <> struct seeded_hash<std::tr1::hash<unsigned short> >
2010-11-05 08:40:15 +02:00
{ typedef seeded_hash_function<Murmur2> hash_function; };
2011-05-24 03:18:24 +03:00
template <> struct seeded_hash<std::tr1::hash<int> >
2010-11-05 08:40:15 +02:00
{ typedef seeded_hash_function<Murmur2> hash_function; };
2011-05-24 03:18:24 +03:00
template <> struct seeded_hash<std::tr1::hash<unsigned int> >
2010-11-05 08:40:15 +02:00
{ typedef seeded_hash_function<Murmur2> hash_function; };
2011-05-24 03:18:24 +03:00
template <> struct seeded_hash<std::tr1::hash<long> >
2010-11-05 08:40:15 +02:00
{ typedef seeded_hash_function<Murmur2> hash_function; };
2011-05-24 03:18:24 +03:00
template <> struct seeded_hash<std::tr1::hash<unsigned long> >
2010-11-08 22:19:44 +02:00
{ typedef seeded_hash_function<Murmur2> hash_function; };
2011-05-24 03:18:24 +03:00
template <> struct seeded_hash<std::tr1::hash<long long> >
2010-11-08 22:19:44 +02:00
{ typedef seeded_hash_function<Murmur2> hash_function; };
2011-05-24 03:18:24 +03:00
template <> struct seeded_hash<std::tr1::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__