From 49f2d6f9dffbe6b68ab3778b53ad7902683bd4e5 Mon Sep 17 00:00:00 2001 From: Davis King Date: Tue, 27 Nov 2012 20:45:35 -0500 Subject: [PATCH] Added murmur_hash3_2() --- dlib/general_hash/murmur_hash3.h | 37 +++++++++++++++++++++++ dlib/general_hash/murmur_hash3_abstract.h | 15 +++++++++ dlib/test/hash.cpp | 22 ++++++++++++++ 3 files changed, 74 insertions(+) diff --git a/dlib/general_hash/murmur_hash3.h b/dlib/general_hash/murmur_hash3.h index 353ac4fe2..ab4f77005 100644 --- a/dlib/general_hash/murmur_hash3.h +++ b/dlib/general_hash/murmur_hash3.h @@ -218,6 +218,43 @@ namespace dlib 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 murmur_hash3_128bit ( diff --git a/dlib/general_hash/murmur_hash3_abstract.h b/dlib/general_hash/murmur_hash3_abstract.h index 726d45eda..05fd7c42e 100644 --- a/dlib/general_hash/murmur_hash3_abstract.h +++ b/dlib/general_hash/murmur_hash3_abstract.h @@ -29,6 +29,21 @@ namespace dlib 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 murmur_hash3_128bit ( diff --git a/dlib/test/hash.cpp b/dlib/test/hash.cpp index 66ce70c49..4635e9825 100644 --- a/dlib/test/hash.cpp +++ b/dlib/test/hash.cpp @@ -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 { public: @@ -240,6 +261,7 @@ namespace test_murmur_hash_128_4(); test_murmur_hash_128_3(); + test_murmur_hash_64_2(); } } a;