gpt4 book ai didi

c# - .net 中的 HashTable/Dictionary 实现选择了哪种类型的冲突解决方案?

转载 作者:太空狗 更新时间:2023-10-29 18:08:43 24 4
gpt4 key购买 nike

我们知道有 2 种经典的冲突解决策略:分离链接和开放寻址。

我想知道在 .net 中为 HashTable/Dictionary 选择了哪一个。

还是使用了其他策略?

最佳答案

这一切都在 MSDN 上的这篇论文中进行了描述:An Extensive Examination of Data Structures Using C# 2.0

...collision resolution technique called rehasing, which is the technique used by the .NET Framework's Hashtable class. In the final section, we'll look at the Dictionary class, which uses a collision resolution technique knows as chaining. ....

... Rehasing works as follows: there is a set of hash different functions, H1 ... Hn, and when inserting or retrieving an item from the hash table, initially the H1 hash function is used. If this leads to a collision, H2 is tried instead, and onwards up to Hn if needed. The previous section showed only one hash function, which is the initial hash function (H1). The other hash functions are very similar to this function, only differentiating by a multiplicative factor. In general, the hash function Hk is defined as:

 Hk(key) = [GetHash(key) + k * (1 + (((GetHash(key) >> 5) + 1) %  (hashsize – 1)))] % hashsize

The Dictionary class differs from the Hashtable class in more ways than one. In addition to being strongly-typed, the Dictionary also employs a different collision resolution strategy than the Hashtable class, using a technique referred to as chaining. Recall that with probing, in the event of a collision another slot in the list of buckets is tried. (With rehashing, the hash is recomputed, and that new slot is tried.) With chaining, however, a secondary data structure is utilized to hold any collisions. Specifically, each slot in the Dictionary has an array of elements that map to that bucket. In the event of a collision, the colliding element is prepended to the bucket's list.

记住只有第一句话是我自己的:-)

关于c# - .net 中的 HashTable/Dictionary 实现选择了哪种类型的冲突解决方案?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7444765/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com