0

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.

4

2 回答 2

1

为了弄清楚“如何”,我们首先需要回答几个问题:
a)您应该能够容纳多少物品?
b) 哈希表的大小是多少?
c) 有多少种组合<string X int>(因为字符串的大小固定,很容易计算)?

一旦你弄清楚了 - 就会更容易找到一个可以最大限度地减少冲突的散列函数,例如,可能存在使用 Hs(string) 就足够好的情况!

于 2013-09-26T03:12:33.677 回答
0

其中任何一个都行得通。我的默认值是 Hs(string) XOR Hs(int) 但 plus 也可以。它只需要可能不会发生碰撞,尽管 Hs(string) XOR Hs(int) 或 Hs(string) + Hs(int) 会更快,但它们可能都不会发生碰撞。

于 2013-09-26T03:12:25.593 回答