在SortedDictionary寻找最接近的数值 [英] Finding nearest value in a SortedDictionary

查看:127
本文介绍了在SortedDictionary寻找最接近的数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个SortedDictionary

I have a SortedDictionary

 SortedDictionary<int, CPUOptimizationObject> myDict;

现在我想找到上述十
的第一个值,我可以做这样的事情

Now I want to find the first value above X. I can do something like this

foreach (var iKey in MyDict.Keys)
{
   if (iKey >= thresholdKey)
   {
       foundKey = iKey;
       break;
   }
}



但是这不是明智的良好的性能。

任何更好的建议?结果
(是有,在集合类似的SortedDictionary二进制搜索的方法?)

but this isn't good performance wise.
Any better suggestion?
(is there a method for that in the collections something like Binary search for SortedDictionary ?)

推荐答案

虽然在理论上,发现比给定值大的最小的项目是能够有效地在二进制搜索树进行(这是一个 SortedDictionary的动作实现为) SortedDictionary 不公开的手段,为您执行该数据类型,如搜索。

While, in theory, finding the smallest item that is larger than a given value is an operation that can be performed efficiently on a Binary Search Tree (which is what a SortedDictionary is implemented as) SortedDictionary does not expose the means for you to perform such a search on that data type.

您需要使用,以便有效地执行这样的搜索不同的实现一个二进制搜索树,同时仍使用相同类型的数据结构。有没有合适的.NET类型;你需要使用第三方实现(其中有不少在那里)。

You would need to use a different implementation of a Binary Search Tree in order to efficiently perform such a search, while still using the same type of data structure. There are no suitable .NET types; you would need to use a 3rd party implementation (of which there are quite a few out there).

这篇关于在SortedDictionary寻找最接近的数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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