什么哈希算法.net使用？小丑怎么样？

7 个答案:

HASHING算法是用于确定HashTable中项目的哈希码的算法。

HASHTABLE算法（我认为是这个人所问的）是HashTable在给定哈希码时用来组织其元素的算法。

Java恰好使用chained哈希表算法。

``````     /*
Implementation Notes:
The generic Dictionary was copied from Hashtable's source - any bug
fixes here probably need to be made to the generic Dictionary as well.
This Hashtable uses double hashing.  There are hashsize buckets in the
table, and each bucket can contain 0 or 1 element.  We a bit to mark
whether there's been a collision when we inserted multiple elements
(ie, an inserted item was hashed at least a second time and we probed
this bucket, but it was already in use).  Using the collision bit, we
can terminate lookups & removes for elements that aren't in the hash
table more quickly.  We steal the most significant bit from the hash code
to store the collision bit.

Our hash function is of the following form:

h(key, n) = h1(key) + n*h2(key)

where n is the number of times we've hit a collided bucket and rehashed
(on this particular lookup).  Here are our hash functions:

h1(key) = GetHash(key);  // default implementation calls key.GetHashCode();
h2(key) = 1 + (((h1(key) >> 5) + 1) % (hashsize - 1));

The h1 can return any number.  h2 must return a number between 1 and
hashsize - 1 that is relatively prime to hashsize (not a problem if
hashsize is prime).  (Knuth's Art of Computer Programming, Vol. 3, p. 528-9)
If this is true, then we are guaranteed to visit every bucket in exactly
hashsize probes, since the least common multiple of hashsize and h2(key)
will be hashsize * h2(key).  (This is the first number where adding h2 to
h1 mod hashsize will be 0 and we will search the same bucket twice).

We previously used a different h2(key, n) that was not constant.  That is a
horrifically bad idea, unless you can prove that series will never produce
any identical numbers that overlap when you mod them by hashsize, for all
subranges from i to i+hashsize, for all i.  It's not worth investigating,
since there was no clear benefit from using that hash function, and it was
broken.

For efficiency reasons, we've implemented this by storing h1 and h2 in a
temporary, and setting a variable called seed equal to h1.  We do a probe,
and if we collided, we simply add h2 to seed each time through the loop.

A good test for h2() is to subclass Hashtable, provide your own implementation
of GetHash() that returns a constant, then add many items to the hash table.
Make sure Count equals the number of items you inserted.

Note that when we remove an item from the hash table, we set the key
equal to buckets, if there was a collision in this bucket.  Otherwise
we'd either wipe out the collision bit, or we'd still have an item in
the hash table.

--
*/
``````

.NET `Dictionary<T>`类使用`IEqualityComparer<T>`计算密钥的哈希码，并在密钥之间执行比较以进行哈希查找。 如果在构造`IEqualityComparer<T>`实例时没有提供`Dictionary<T>`（它是构造函数的可选参数），它将为您创建一个默认实例，它使用`object.GetHashCode`和{{默认情况下为1}}方法。