如何快速是KeyCollection操作由Dictionary.Keys返回? (。净) [英] How fast is operation on KeyCollection returned by Dictionary.Keys ? (.NET)
问题描述
的IDictionary< TK,电视>
定义方法 IDictionary.ContainsKey(在TK)
和财产 <$ C (类型为的ICollection
)$ C> IDictionary.Keys 。
我感兴趣的是这种方法和属性的 <$ C $(渐进)的复杂性C>词典 实施 的IDictionary&LT; TK,电视&GT;
考虑定义
的IDictionary&LT; INT,字符串&GT;字典=新字典&LT; INT,字符串&GT;();
INT K =新的随机()下()。
和考虑,我们已经添加到字典字典
N
唯一 KeyValuePair </ code>秒。
什么是预期呼叫 dict.ContainsKey(k)的(渐近)的复杂性
?我希望它在 O(1)
,但我没有找到它的 Dictionary.ContainsKey
。
通话的期望是什么(渐近)的复杂性 dict.Keys.Contains(K)
?我希望它在 O(1)
,但我没有找到它的的文档 Dictionary.Keys
也不在的 Dictionary.KeyCollection
的文档。如果是在 O(1)
我不明白,为什么类型 IDictionary.Keys
属性的ICollection
,而不是的ISet
(类似于Java为例)。
由于的IDictionary&LT; K,V&GT;
只是一个接口,而不是实现,那么它不牛逼提供任何的性能保证。
有关内置词典&LT; K,V&GT;
键入时,的 的containsKey
的方法应该是O(1):
借助 Keys.Contains
方法实际上调用父字典的的containsKey
的方法,这样也应该是O(1):
(两个引号是取自备注的相关文档页面的部分。)的
IDictionary<TK, TV>
defines method IDictionary.ContainsKey(in TK)
and property IDictionary.Keys
(of type ICollection
).
I'm interested in (asymptotic) complexity of this method and property in Dictionary
implementation of IDictionary<TK, TV>
.
Consider definitions
IDictionary<int, string> dict = new Dictionary<int, string>();
int k = new Random().Next();
and consider that we have added to the dictionary dict
n
unique KeyValuePair
s.
What is expected (asymptotic) complexity of a call dict.ContainsKey(k)
? I hope it's in O(1)
but I did not find it in documentation of Dictionary.ContainsKey
.
What is expected (asymptotic) complexity of call dict.Keys.Contains(k)
? I hope it's in O(1)
but I did not find it in documentation of Dictionary.Keys
nor in documentation of Dictionary.KeyCollection
. If it is in O(1)
I don't understand why type of IDictionary.Keys
property is ICollection
and not ISet
(like in Java for example).
Since IDictionary<K,V>
is only an interface, not an implementation, then it doesn't provide any performance guarantees.
For the built-in Dictionary<K,V>
type, the ContainsKey
method should be O(1):
The Keys.Contains
method actually calls the parent dictionary's ContainsKey
method, so that should also be O(1):
(Both quotes are taken from the "Remarks" section of the relevant documentation pages.)
这篇关于如何快速是KeyCollection操作由Dictionary.Keys返回? (。净)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!