0

My hash function is as follows:

unsigned int Game::xorHash(const string &s)
{
    unsigned int h = 0;

    for (unsigned int i = 0; i < s.length(); i++ )
        h ^= s.c_str()[i];

    return h;
}

I am trying to distribute roughly 160,000 strings into a table that contains around 3-10 strings per. I'm pretty lost.

The above implementation is very top heavy. My assignment requires that I have at least 500 buckets, but any number above that will suffice.

Does anyone have anyone suggestions/direction? It would be greatly appreciated.

rearden
  • 135
  • 1
  • 1
  • 11
  • How about using something that is already heavily optimised, like `std::hash` or even `std::unordered_map`? –  Nov 09 '13 at 21:59
  • I have to write my own. – rearden Nov 09 '13 at 22:26
  • You might consider a function that spreads the bits a little better. Google "string hashing function" for a bunch of different ways you might do that. This other StackOverflow question has a few ways too: http://stackoverflow.com/questions/8317508/hash-function-for-a-string – Joe Z Nov 09 '13 at 22:32
  • Do you really need to implement your own hash function or just a table? Without the proper background (and even with, usually), rolling your own hash function is a bad idea (you will likely have many collisions) – RageD Nov 10 '13 at 02:29
  • I have to implement my own. Already made the table and got graded on that, now I have to do the function. – rearden Nov 10 '13 at 15:06

0 Answers0