我有一个类,它的内部是一个整数只是一个数组。一旦构建了数组永远不会改变。我想pre-计算好的哈希code,使这个类可以非常有效地使用作为一个字典的关键。数组的长度小于约30个项目,以及整数是在一般-1000和1000之间。
I have a class that internally is just an array of integers. Once constructed the array never changes. I'd like to pre-compute a good hashcode so that this class can be very efficiently used as a key in a Dictionary. The length of the array is less than about 30 items, and the integers are between -1000 and 1000 in general.
不是很聪明,但也足够满足大多数实际目的:
Not very clever, but sufficient for most practical purposes:
编辑:由于亨克Holterman的感谢的意见,改变了的
int hc=array.Length;
for(int i=0;i<array.Length;++i)
{
hc=unchecked(hc*314159 +array[i]);
}
return hc;
如果你需要一些更复杂的,看这里。
If you need something more sophisticated, look here.