我需要为线性插值编写一些代码,并且我试图找出一种最有效的方法来搜索一个SortedList<K, V>用于围绕我的目标键的上下键的键.
SortedList<int, double> xyTable = new SortedList<int, double>()
{
{1, 10}, {2, 20}, {3, 30}, {4,40}
};
double targetX = 3.5;
Run Code Online (Sandbox Code Playgroud)
搜索列表并确定3.5介于3和4之间的最有效方法是什么?我有一个适用于整数的方法/作弊(暂时将目标密钥插入列表然后找到索引)但我想我会问专业人员所以我可以生成高质量的代码.
谢谢.
鉴于人口稠密SortedList<DateTime, double>。对于给定的低和高DateTime时间间隔,我想获取所有键(或其索引范围,应该是一个封闭的int间隔(缺少我什么?))。
注意:不需要低值和高值实际上在SortedList中。
如果没有更好的主意而又没有SortedList的人怎么做,这是我想做的事,我发现SortedList可能更合适:
我有使用的Java代码SortedMap.tailMap。在我的移植代码中,我有SortedMap= Dictionary<IComparable, value>。我需要一种在C#中复制/模仿tailMap的方法。
我已经想到了以下内容:
myDictionary.Where(p => p.Key.CompareTo(value) >= 0).ToDictionary
这将返回Dictionary,而我需要SortedDictionary返回。我可以SortedDictionary从中创建一个Dictionary,但是我觉得应该已经有一种更好的,更优雅的方法来做到这一点。
另一个想法是做类似的事情
var newSD = new SortedDictionary<k,v>();
foreach (var p in oldDictionary.Where(p => p.Key.CompareTo(value) >= 0))
newSD.Add(p.Key, p.Value);
Run Code Online (Sandbox Code Playgroud)
那应该行得通,我不确定在构建列表时按排序顺序添加值将如何影响插入的时间。
还有其他想法吗?
我有一个Dictionary对大多数操作我只需要检索的钥匙的一个条目,但对于一些操作,我需要与一定范围的密钥相关的条目工作.我这样做的方法是使用GetKeys和FindAll我感兴趣的范围匹配,但是想知道是否有人能提出更好的方法.
我以前没有使用过 LINQ,但我知道它的效率有多高。
我创建了一个带有对象的列表,您可以在下面看到:
public sealed class Item
{
public long Start { private set; get; }
public long End { private set; get; }
public Item(string start, string end)
{
this.Start = Convert.ToInt64(start);
this.End = Convert.ToInt64(end);
}
}
Run Code Online (Sandbox Code Playgroud)
这将填充DataSet包含大约 200k 个项目的 s。
现在,我想在属性“开始”和“结束”之间选择最佳的单个项目。
this.ItemList.Add(new Item(100000, 100002));
this.ItemList.Add(new Item(100003, 100006));
this.ItemList.Add(new Item(100007, 100012));
this.ItemList.Add(new Item(100013, 100026));
this.ItemList.Add(new Item(100027, 100065));
Run Code Online (Sandbox Code Playgroud)
从另一个工具中,我得到了值:100009
如何new Item(100007, 100012)使用 LINQ 取回对象?有没有人有什么建议?
c# ×5
.net ×1
dictionary ×1
generics ×1
java ×1
linq ×1
performance ×1
search ×1
sortedlist ×1