字典如何内部维护? [英] How the Dictionary is internally maintained?

查看:120
本文介绍了字典如何内部维护?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我说

Dictionary<int,string>

相当于两个不同的数组,例如:

is it equivalent to two different arrays such as:

int[] keys =new int[] { 1, 2, 3 };
string[] values=new string[]{"val1","val2","val3"};


推荐答案

这不算太远。看看Reflector中的源代码,它似乎使用了三个内部集合:

That's not too far off. Looking at the source code in Reflector, it seems three internal collections are used:

private Entry<TKey, TValue>[] entries;
private KeyCollection<TKey, TValue> keys;
private ValueCollection<TKey, TValue> values;

请注意,还有一个 int []桶变量来跟踪哈希码所需的碰撞。

Note that there is also a int[] buckets variable to keep track of the buckets required in the case of hash-code collisions.

这些变量的目的应该是相当不言自明的。这不是特别令人惊讶,无论如何,由于字典类是已知和记录的(理想情况下,每个桶中有一个项目) O(1) 查找时间。

These variables' purposes should all be fairly self-explanatory. This is not particularly surprising, anyway, since the Dictionary class is known and documented to provide (ideally, with one item per bucket) O(1) lookup time.

这篇关于字典如何内部维护?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆