1

i am in search for a good Hash function which i can use in Hash table implementation. The thing is that i want to give both strings and integers as parameters(keys) in my hash function.

i have a txt file with ~500 data and every one of them consists of integers and strings(max 15 chars). So, the thing that i want to do is to pick one of these ints/strings and use it as a key for my hash function in order to put my data in the "right" bucket.

Is there any good function to do this?

Thank you :)

Joe
  • 41,484
  • 20
  • 104
  • 125
matrix
  • 161
  • 2
  • 12

3 Answers3

0

Use the Integer value if that's present & reasonably well distributed, then hash the String if it's not. Integer hashcode is much cheaper to compute than String.

The algorithm has to be repeatable, obviously.

Thomas W
  • 13,940
  • 4
  • 58
  • 76
0

Your question is somewhat vague. It's unclear if your data set has 500 columns and you are trying to figure out which column to use for hashing, or if it has 500 items which you want to hash.

If you are looking for a decent general purpose hash that will produce well-distributed hash values, you may want to check out the Jenkins hash functions which have variants for strings and integers. But, to be frank, if your dataset has 500 fixed items you may want to look at a perfect hash function generator, like GNU gperf or even alternative data structures depending on your data.

Nik Bougalis
  • 10,495
  • 1
  • 21
  • 37
0

Since you want to hash using two keys, I presume the distribution improves using two keys.

For string hashing, I have had good results with PJW algorithm. Just google for "PJW Hash String". One variation here To augment the hash with an integer, see here

Community
  • 1
  • 1
Sudhee
  • 704
  • 6
  • 12