C# GetHashCode 的實現方式
在項目中,在使用哈希表時,有時會需要Override GetHashCode。這裡給出一種普遍的做法:
版本1:
實現一個helper,傳遞類型T,返回這個類型的hashcode。函數邏輯很直接,只是做了null check而已;如果obj不為空,則直接使用obj的hash code。
public class HashHelper
{
private int _seed = 17;
public int Hash(T obj)
{
// why 31?
// https://computinglife.wordpress.com/2008/11/20/why-do-hash-functions-use-prime-numbers/
// shortly, to reduce the conflict of hashing key's distrabution
return 31 * _seed + ((obj == null) ? -1 : obj.GetHashCode());
}
}
為什麼使用了magic number 31? 使用素數乘積可以相對增加唯一性,減少哈希鍵值分配時的沖突;而31則是為了編譯器優化的考慮(有效的轉換為i<<5-1)。大概搜了一下,這種實現方式來自JAVA中string 的hash code函數。這裡有詳細介紹:
https://computinglife.wordpress.com/2008/11/20/why-do-hash-functions-use-prime-numbers/
實現版本2:
可以擴展這個類成為流暢接口,它可以hash各種類型的,對於值類型來說,重載的意義在於減少裝箱;對於集合或泛型,則為了讓外部調用更自然,可讀性更強。
public class HashFluent
{
private int _seed = 17;
private int _hashContext;
public HashFluent Hash(T obj)
{
// why 31?
// https://computinglife.wordpress.com/2008/11/20/why-do-hash-functions-use-prime-numbers/
// shortly, to reduce the conflict of hashing key's distrabution
_hashContext = 31 * _seed + ((obj == null) ? -1 : obj.GetHashCode());
return this;
}
public HashFluent Hash(int? value)
{
_hashContext = 31 * _seed + ((value == null) ? -1 : value.GetHashCode());
return this;
}
public HashFluent Hash(IEnumerable sequence)
{
if (sequence == null)
{
_hashContext = 31 * _hashContext + -1;
}
else
{
foreach (var element in sequence)
{
_hashContext = 31 * _hashContext + ((element == null) ? -1 : element.GetHashCode());
}
}
return this;
}
public override int GetHashCode (){
return _hashContext;
}
// add more overridings here ..
// add value types overridings to avoid boxing which is important
}