2010-10-25 05:12:47 +03:00
|
|
|
#ifndef __CXXMPH_RANDOMLY_SEEDED_HASH__
|
|
|
|
#define __CXXMPH_RANDOMLY_SEEDED_HASH__
|
|
|
|
|
|
|
|
// Helper to create randomly seeded hash functions out of existing hash
|
|
|
|
// functions that take a seed as a parameter.
|
|
|
|
|
|
|
|
#include <cstdlib>
|
|
|
|
|
|
|
|
#include "../src/cmph_types.h"
|
|
|
|
#include "MurmurHash2.h"
|
2010-10-28 03:17:09 +03:00
|
|
|
#include "stringpiece.h"
|
2010-10-25 05:12:47 +03:00
|
|
|
|
|
|
|
namespace cxxmph {
|
|
|
|
|
2010-10-28 03:17:09 +03:00
|
|
|
template <class HashFun>
|
|
|
|
struct RandomlySeededHashFunction { };
|
|
|
|
|
|
|
|
class Murmur2StringPiece { };
|
|
|
|
class Murmur2Pod { };
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct RandomlySeededHashFunction<Murmur2StringPiece> {
|
2010-10-25 05:12:47 +03:00
|
|
|
RandomlySeededHashFunction() : seed(random()) { }
|
2010-10-28 03:17:09 +03:00
|
|
|
cmph_uint32 operator()(const StringPiece& key) const {
|
2010-10-25 05:12:47 +03:00
|
|
|
return MurmurHash2(key.data(), key.length(), seed);
|
|
|
|
}
|
|
|
|
cmph_uint32 seed;
|
|
|
|
};
|
|
|
|
|
2010-10-28 03:17:09 +03:00
|
|
|
template<>
|
|
|
|
struct RandomlySeededHashFunction<Murmur2Pod> {
|
|
|
|
RandomlySeededHashFunction() : seed(random()) { }
|
|
|
|
template<class Key>
|
|
|
|
cmph_uint32 operator()(const Key& key) const {
|
|
|
|
return MurmurHash2(&key, sizeof(key), seed);
|
|
|
|
}
|
|
|
|
cmph_uint32 seed;
|
|
|
|
};
|
|
|
|
|
2010-10-25 05:12:47 +03:00
|
|
|
} // namespace cxxmph
|
|
|
|
|
|
|
|
#endif // __CXXMPH_RANDOMLY_SEEDED_HASH__
|