简体   繁体   English

一组(x,y)点的C#数据结构

[英]C# Data Structure for a set of (x,y) points

I'm not familiar with all the collections available in C# but I'd like to implement a class to store a mathematical relation or function, ie a set of pairs (x,y). 我对C#中可用的所有集合都不熟悉,但是我想实现一个用于存储数学关系或函数的类,即一组对(x,y)。 Presumably it would include a list of tuples or some other built collection from .NET, but I'm not sure what's best. 大概它会包含一个元组列表或.NET的其他一些内置集合,但是我不确定什么是最好的。 Some potentially relevant facts: 一些潜在的相关事实:

  1. up a million pairs 高达一百万双
  2. frequently would want to lookup which y goes with a particular x 通常会想查找与特定x对应的y
  3. x would be a double type for all cases I know of now 对于我现在知道的所有情况,x都是双精度类型
  4. may want to interpolate y's to nonexistant x's 可能想将y内插到不存在的x上
  5. will want to extract a subset of the relation including all pairs with x in a certain range 将要提取关系的子集,包括在一定范围内带有x的所有对
  6. will want to iterate through pairs in order of xs sometimes seems like it should be sorted by xs based on above things? 是否会想按xs的顺序对进行迭代,有时似乎应该根据上述内容按xs进行排序?

The SortedSet<T> seems like the right tool for this task. SortedSet<T>似乎是完成此任务的正确工具。

We can define an IComparable element type as follows. 我们可以如下定义一个IComparable元素类型。

struct FunctionPoint : IComparable<FunctionPoint>
{
    public double X, Y;
    public FunctionPoint(double x)
    {
        this.X = x;
        this.Y = 0;
    }
    public FunctionPoint(double x, double y)
    {
        this.X = x;
        this.Y = y;
    }
    public int CompareTo(FunctionPoint other)
    {
        return X.CompareTo(other.X);
    }
}

And then use it in a SortedSet<FunctionPoint> as follows. 然后按如下所示在SortedSet<FunctionPoint>使用它。

var function = new SortedSet<FunctionPoint>();
  1. up a million pairs 高达一百万双
for (int i = 0; i < 100000; i++)
{
    var x = 2 * Math.PI * i / 1000000;
    var y = Math.Sin(x);
    function.Add(new FunctionPoint { X = x, Y = y });
}
  1. frequently would want to lookup which y goes with a particular x 通常会想查找与特定x对应的y
var view = function.GetViewBetween(new FunctionPoint(x), new FunctionPoint(x));
if (view.Count > 0)
{
    var y = view.Min;
}
  1. may want to interpolate y's to nonexistant x's 可能想将y内插到不存在的x上
var left = function.GetViewBetween(new FunctionPoint(double.NegativeInfinity), new FunctionPoint(x)).Max;
var right = function.GetViewBetween(new FunctionPoint(x), new FunctionPoint(double.PositiveInfinity)).Min;
var y = LinearInterpolate(left, right, x);
  1. will want to extract a subset of the relation including all pairs with x in a certain range 将要提取关系的子集,包括在一定范围内带有x的所有对
var view = function.GetViewBetween(new FunctionPoint(a), new FunctionPoint(b));
  1. will want to iterate through pairs in order of xs sometimes seems like it should be sorted by xs based on above things? 是否会想按xs的顺序对进行迭代,有时似乎应该根据上述内容按xs进行排序?
foreach (var point in function)
{
}

Maybe a System.Collections.Generic.SortedList<double, double> is what you need? 也许您需要一个System.Collections.Generic.SortedList<double, double>
SortedList 排序列表

If you constantly remove and add items, a SortedDictionary might perform better. 如果您不断删除和添加项目,则SortedDictionary的性能可能会更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM