I have a simple struct consists of a fixed size string and an integer. I need to use this struct as the key for a hash table. I have a hash function for sting, Hs(string), and a hash function for integer, Hi(int), I'm wondering if the hash function for this simple struct would just be H(struct) = Hs(string) + Hi(int)? Alternatively, I could encode the integer into a string and append it to the string, then just use the string hash function. Any suggestions? Thanks.
-
1What programming language?David-SkyMesh– David-SkyMesh2013-09-26 03:01:38 +00:00Commented Sep 26, 2013 at 3:01
-
c,c++. But I would be surprised if the answer is language specific.atsu– atsu2013-09-26 03:13:48 +00:00Commented Sep 26, 2013 at 3:13
-
some programing languages provide runtime support for hashing of classes/structs derived from built-ins (e.g: C#)David-SkyMesh– David-SkyMesh2013-09-26 03:15:14 +00:00Commented Sep 26, 2013 at 3:15
2 Answers
In order to figure the "how" we need to answer a few questions first:
a) how many items you should be able to accommodate ?
b) what's the size of the hash table ?
c) how many combinations of <string X int> are there (since the string has a fixed size it's easy to calculate) ?
Once you figure that out - it will be easier to find a hashing function that will minimize collisions, for example, there might be a case where using Hs(string) is good enough!
4 Comments
bucket the <string X int> is (when there is a collision).Either of those would work. My default would be Hs(string) XOR Hs(int) but plus is fine too. It just needs to probably not collide and both of those will probably not collide although Hs(string) XOR Hs(int) or Hs(string) + Hs(int) will be faster.
3 Comments
int = 32 byte (at least in Java it is) while String is (theoretically) limited only by the JVM - how do you XOR two items of different sizes ?