Hash functions for strings
-
It is common to want to use string-valued keys in hash tables
-
What is a good hash function for strings?
-
The basic approach is to use the characters in the string to compute an integer, and then take the integer mod the size of the table
-
How to compute an integer from a string?
-
You could just take the last two 16-bit chars of the string and form a 32-bit int
-
But then all strings ending in the same 2 chars would hash to the same location; this could be very bad
-
It would be better to have the hash function depend on all the chars in the string
-
There is no recognized single "best" hash function for strings. Let’s look at some possibile ones
CONTENTS PREVIOUS NEXT