tags:

views:

177

answers:

1

Hi all, I am implementing google's dense hash map in my C++ code. I want to use MurmurHash2 (http://murmurhash.googlepages.com/) as a hash function. But here's the problem. I have tried a lot but cant seem to get the hash function to work. The example shows the use of a default hash function (hash< const char * >).

dense_hash_map < const char * , int, hash < const char*>, eqstr> months;

I would like to replace hash< const char * > by

unsigned int MurmurHash2 ( const char * key, int len, unsigned int seed)

Can any one plz suggest what to do?

thanks!

+2  A: 

Clearly you have a mismatch between the signature that the dense_hash_map requires from its hashing function, and the signature that MurmurHash2 provides. You'll have to perform your own "impedence matching" in a function or functor of your own that implements the required signature and internally uses the provided one. This, however, requires the ability to determine the len corresponding to a given const char *, and there's no obvious answer to that. Do you plan to store in your hash map only "null terminated arrays of characters", as in old-fashioned C "pseudo-strings", so that strlen will suffice? or, what else...?

Maybe using std::string (or some other unambiguous C++ type depending on exactly what you're trying to achieve!) instead of those dubious const char *s would be a better start!

Alex Martelli
Hey thanks for the reply. yes strlen would suffice. I can try using std::string also. But assuming that we know the 'len', can you tell me how do I perform "impedence matching". I mean, I just want to be able to use the hash function. I am not able to create a hash_map that would use MurmurHash2 as the hash function. Thanks!
aneesh
You need to write your own function (which takes only one parameter) and internally calls MurmurHash2 with its three parameters, and use that function of yours as "the hash function" for `hash_map`. You will never be able to use a three-parameters function as "the hash function" itself: `hash_map` wants a hash function with ONE parameter, so you have to write your own respecting that constraint and use it instead. Why is that a problem?!
Alex Martelli