Added murmur_hash3_2()

This commit is contained in:
Davis King 2012-11-27 20:45:35 -05:00
parent 484e8a3d73
commit 49f2d6f9df
3 changed files with 74 additions and 0 deletions

View File

@ -218,6 +218,43 @@ namespace dlib
return h1; return h1;
} }
// ----------------------------------------------------------------------------------------
inline uint32 murmur_hash3_2 (
const uint32 v1,
const uint32 v2
)
{
uint32 h1 = v2;
uint32 c1 = 0xcc9e2d51;
uint32 c2 = 0x1b873593;
//----------
// body
uint32 k1 = v1;
k1 *= c1;
k1 = DLIB_ROTL32(k1,15);
k1 *= c2;
h1 ^= k1;
h1 = DLIB_ROTL32(h1,13);
h1 = h1*5+0xe6546b64;
//----------
// finalization
h1 ^= 4; // =^ by length in bytes
h1 = murmur_fmix(h1);
return h1;
}
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
inline std::pair<uint64,uint64> murmur_hash3_128bit ( inline std::pair<uint64,uint64> murmur_hash3_128bit (

View File

@ -29,6 +29,21 @@ namespace dlib
See: http://code.google.com/p/smhasher/ See: http://code.google.com/p/smhasher/
!*/ !*/
// ----------------------------------------------------------------------------------------
inline uint32 murmur_hash3_2 (
const uint32 v1,
const uint32 v2
);
/*!
ensures
- returns a 32bit hash of the two integers given to this function.
- This function is machine architecture agnostic and should always give the same
hash value when presented with the same inputs.
- This hashing algorithm is Austin Appleby's excellent MurmurHash3_x86_32.
See: http://code.google.com/p/smhasher/
!*/
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
std::pair<uint64,uint64> murmur_hash3_128bit ( std::pair<uint64,uint64> murmur_hash3_128bit (

View File

@ -162,6 +162,27 @@ namespace
} }
} }
void test_murmur_hash_64_2()
{
byte_orderer bo;
dlib::rand rnd;
for (int i = 0; i < 100; ++i)
{
uint32 val = rnd.get_random_32bit_number();
const uint32 seed = rnd.get_random_32bit_number();
bo.host_to_little(val);
uint32 temp1, temp2;
// Make sure the 2 integer version of murmur hash does the same thing
// as the memory block version.
temp1 = murmur_hash3(&val, sizeof(val), seed);
temp2 = murmur_hash3_2(val, seed);
DLIB_TEST(temp1 == temp2);
}
}
class test_hash : public tester class test_hash : public tester
{ {
public: public:
@ -240,6 +261,7 @@ namespace
test_murmur_hash_128_4(); test_murmur_hash_128_4();
test_murmur_hash_128_3(); test_murmur_hash_128_3();
test_murmur_hash_64_2();
} }
} a; } a;