什么集合对象适合值的固定顺序?

发布于 2024-10-12 14:27:51 字数 1214 浏览 1 评论 0原文

场景:我正在跟踪多个性能计数器,并且有一个与 DataSnapshot[] 相关的 CounterDescription[]...,其中 CounterDescription[n] 描述了 DataSnapshot[n] 中加载的数据。

我想在 C# 中公开一个易于使用的 API,以便轻松高效地扩展数组。

简化的示例(它变得更加复杂)

CounterDescription[0] = Humidity;
DataSnapshot[0] = .9;
CounterDescription[1] = Temp;
DataSnapshot[1] = 63;

请注意,我的意图是将许多数据快照与 DateTime 引用相关联,并使用数据的偏移量来引用其含义。这被确定为在后端存储数据的最有效方式,现在已反映为以下结构:

  public class myDataObject { 
    [DataMember]
    public SortedDictionary<DateTime, float[]> Pages { get; set; }

    /// <summary>
    /// An array that identifies what each position in the array is supposed to be
    /// </summary>
    [DataMember]
    public CounterDescription[] Counters { get; set; }
   }

如何使用 myDataObject?

我会经常搜索计数器通过字符串名称,并使用其位置来确定将保存特定值的偏移量。我可以使用自行开发的扩展方法来枚举对象,或者在保证顺序的情况下利用框架。

另外,当添加新传感器时,我需要扩展每个数组:(float[]CounterDescription[] ),但无论已存在的数据都必须保留在该数组中相对偏移量。我不希望该对象的序列化版本将 Temp(偏移量 1)与 Humidity(偏移量 0)混淆。

哪些 .NET 对象支持这种固定排序、扩展和枚举(以及可选的按字符串搜索)?我的猜测是使用这些对象之一......

Array[] , LinkedList<t>, and List<t>

Scenario: I am tracking several performance counters and have a CounterDescription[] correlate to DataSnapshot[]... where CounterDescription[n] describes the data loaded within DataSnapshot[n].

I want to expose an easy to use API within C# that will allow for the easy and efficient expansion of the arrays.

Simplified example (it gets more complex)

CounterDescription[0] = Humidity;
DataSnapshot[0] = .9;
CounterDescription[1] = Temp;
DataSnapshot[1] = 63;

Note how my intent is to correlate many Datasnapshots with a DateTime reference, and using the offset of the data to refer to its meaning. This was determined to be the most efficient way to store the data on the back-end, and has now reflected itself into the following structure:

  public class myDataObject { 
    [DataMember]
    public SortedDictionary<DateTime, float[]> Pages { get; set; }

    /// <summary>
    /// An array that identifies what each position in the array is supposed to be
    /// </summary>
    [DataMember]
    public CounterDescription[] Counters { get; set; }
   }

How will myDataObject be used?:

I will frequently search for a counter by string name, and use its' position to determine what offset a particular value will be saved. I can use an homegrown extension method to enumerate the object, or leverage the framework if ordering is guaranteed.

Also, I will need to expand each of these arrays as new sensors are added: (float[] and CounterDescription[] ), but whatever data already exists must stay in that relative offset. I don't want the serialized version of this object to confuse Temp (offset 1) with Humidity (offset 0)

Which .NET objects support this fixed ordering, expansion, and enumeration (and optional searching by string)? My guess is to use one of these objects...

Array[] , LinkedList<t>, and List<t>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

憧憬巴黎街头的黎明 2024-10-19 14:27:51

使用 Dictionary 以便每个名称 (string) 映射到一个值 (double):

var counters = new Dictionary<string, double>();

counters["Humidity"] = 0.9;
counters["Temp"] = 63;

并使用获取的服务并设置计数器值:

[OperationContract]
public double GetCounter(string name)
{
     return Counters[name];
}

[OperationContract]
public void SetCounter(string name, double value)
{
     Counters[name] = value;
}

您可以以相同的方式使用 CounterDescription 和/或 DataSnapshot 类,但请确保用作键的类(可能是 >CounterDescription) 使用正确的实现覆盖 Object.Equals()Object.GetHashCode()

Use a Dictionary<string, double> so that each name (string) maps to a value (double):

var counters = new Dictionary<string, double>();

counters["Humidity"] = 0.9;
counters["Temp"] = 63;

And use a service that gets and sets the counter values:

[OperationContract]
public double GetCounter(string name)
{
     return Counters[name];
}

[OperationContract]
public void SetCounter(string name, double value)
{
     Counters[name] = value;
}

You can use your CounterDescription and/or DataSnapshot classes in the same way, but make sure that the class you use as the key (probably CounterDescription) overrides Object.Equals() and Object.GetHashCode() with a proper implementation.

吃素的狼 2024-10-19 14:27:51

如果 CounterDescription 是一个字符串或一个 enum,那么 Dictionary 似乎很合适:

Dictionary<string, double> Counters = new Dictionary<string, double>();
// Then initialize it ...
Counters.Add("Humidity", 0);
Counters.Add("Temp", 0);
// To update:
Counters["Humidity"] = 0.9;
// To query
double humidity = Counters["Humidity"];

您可以使用 做同样的事情>enum 作为键,而不是字符串。

如果您的 CounterDescription 类型是一个复杂对象,您仍然可以将其用作键,但您需要实现 IComparable 或提供比较函数。

If CounterDescription is a string or an enum, then Dictionary seems like a good fit:

Dictionary<string, double> Counters = new Dictionary<string, double>();
// Then initialize it ...
Counters.Add("Humidity", 0);
Counters.Add("Temp", 0);
// To update:
Counters["Humidity"] = 0.9;
// To query
double humidity = Counters["Humidity"];

You can do the same sort of thing with a enum for the key, rather than a string.

If your CounterDescription type is a complex object, you can still use it as a key, but you'll need to implement IComparable or provide a comparison function.

情释 2024-10-19 14:27:51

任何列表 (IList) 都有有序值。我总是假设 IEnumerables 下面没有严格的顺序;虽然他们通常会这样做,但你不能保证。我同意其他人的观点,即字典(或其他一些 IDictionary)很合适。

Any list (IList) has ordered values. I always assume that mere IEnumerables have no strict order underneath; although they usually do, you can't guarantee it. I agree with the others that a Dictionary (or some other IDictionary) is a good fit.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文