Han*_*ank 10 .net c# boolean hashcode
我有一个不可变类,其唯一的字段是bool[](在运行时确定的大小).
bool[]
如何计算此类的良好哈希码?通常我会调用GetHashCode()每个字段,并将它们与这些运算符中的一个结合起来:+ | &但是因为唯一可能的哈希码是0for false和1for true,所以这并不能让我到处都是.我的实现只需要使用bools,并且必须适用于任意大小的数组.
GetHashCode()
+ | &
0
false
1
true
bools
(可能并不重要,但我在C#/ .NET编码.)
jas*_*son 10
假设你bool[]的名字是bools:
unchecked { int hash = 17; for(int index = 0; index < bools.Length; index++) { hash = hash * 23 + bools[index].GetHashCode(); } return hash; }
归档时间:
14 年,5 月 前
查看次数:
1882 次
最近记录:
12 年 前