c++objective-ciphonehashmurmurhash

How to use MurmurHash 64 in Objective-C?


I need to Hash a NSString using murmurhash i am forced to do that because the other team is doing so, I need to use the 64-bit key length on x86 platform, have anyone implemented or used murmurhash in objective-C?

Murmurhash 64 is a .cpp file and i can't get it compiled in iOS SDK 4.3, also tried to rename it to .mm but the result still the same.

Any help or Guide would be greatly appreciated.

I will post the Compilation Error Messages if someone is interested.


Solution

  • For all those who will come to look for the correct answer, here is the deal:

    MurmurHash64.h class
    careful I have changed the typedef

    // MurmurHash, by Austin Appleby
    
    typedef unsigned long long uint64_t;
    
    uint64_t MurmurHash64B ( const void * key, int len, unsigned int seed );
    

    MurmurHash64.m class

    #include "MurmurHash64.h"
    // 64-bit hash for 32-bit platforms
    
    uint64_t MurmurHash64B ( const void * key, int len, unsigned int seed )
    {
        const unsigned int m = 0x5bd1e995;
        const int r = 24;
    
        unsigned int h1 = seed ^ len;
        unsigned int h2 = 0;
    
        const unsigned int * data = (const unsigned int *)key;
    
        while(len >= 8)
        {
           unsigned int k1 = *data++;
           k1 *= m; k1 ^= k1 >> r; k1 *= m;
           h1 *= m; h1 ^= k1;
           len -= 4;
    
           unsigned int k2 = *data++;
           k2 *= m; k2 ^= k2 >> r; k2 *= m;
           h2 *= m; h2 ^= k2;
           len -= 4;
        }
    
        if(len >= 4)
        {
           unsigned int k1 = *data++;
           k1 *= m; k1 ^= k1 >> r; k1 *= m;
           h1 *= m; h1 ^= k1;
           len -= 4;
        }
    
        switch(len)
        {
            case 3: h2 ^= ((unsigned char*)data)[2] << 16;
            case 2: h2 ^= ((unsigned char*)data)[1] << 8;
            case 1: h2 ^= ((unsigned char*)data)[0];
            h2 *= m;
        };
    
        h1 ^= h2 >> 18; h1 *= m;
        h2 ^= h1 >> 22; h2 *= m;
        h1 ^= h2 >> 17; h1 *= m;
        h2 ^= h1 >> 19; h2 *= m;
    
        uint64_t h = h1;
    
        h = (h << 32) | h2;
    
        return h;
    }