这个内存查找表的最佳数据结构是什么?

发布于 2024-07-15 05:01:08 字数 389 浏览 8 评论 0原文

我需要将查找表存储为我的一个类中的实例成员。 该表将在对象构造时初始化。 每个“行”将有 3 个“列”:

StringKey (e.g., "car")
EnumKey (e.g., LookupKeys.Car)
Value (e.g, "Ths is a car.")

我想选择能够通过 StringKey EnumKey 进行查找时产生最佳性能的数据结构。

对于同一个字典值有两个键有点尴尬。 我以前从未遇到过这种情况,所以我想知道这种事情的规范是什么。

我可以制作一个键/值/值结构而不是键/键/值,但我想知道这会产生什么类型的性能影响。

难道我的想法全错了?

I need to store a lookup table as an instance member in one of my classes. The table will be initialized when the object is constructed. Each "row" will have 3 "columns":

StringKey (e.g., "car")
EnumKey (e.g., LookupKeys.Car)
Value (e.g, "Ths is a car.")

I want to pick the data structure that will yield the best performance for doing lookups either by the StringKey or the EnumKey.

It's kind of awkward having 2 keys for the same dictionary value. I've never encountered this before, so I'm wondering what the norm is for this type of thing.

I could make a Key/Value/Value structure instead of Key/Key/Value, but I'm wondering what type of performance impact that would have.

Am I thinking about this all wrong?

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

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

发布评论

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

评论(5

一世旳自豪 2024-07-22 05:01:08

嗯……“错误”是一种严厉的表达方式。 我认为,因为最常见的字典是“单键到值”,并且为此付出了很多努力来提供有效的数据结构(映射),所以通常最好只使用其中两个,在以下情况下共享值的内存:完全有可能。

Well ... "Wrong" is a harsh way of putting it. I think that because the most common dictionary is "single key to value", and a lot of effort goes into providing efficient data structures for that (maps), it's often best to just use two of those, sharing the memory for the values if at all possible.

找个人就嫁了吧 2024-07-22 05:01:08

你有两个哈希图。

  • 一个从 StringKey 到值。

  • 一个从 EnumKey 到值。

您不必复制所有 Value 实例,这些对象可以在两个哈希图之间共享。

如果有很多项目,您可能需要使用两个树形图而不是两个哈希图。 但基本原则(“分享价值观”)适用于这两种结构。 一组带有两个映射的值。

You have two hashmaps.

  • One from StringKey to value.

  • One from EnumKey to value.

You do not have to duplicate all the Value instances, those objects can be shared between the two hashmaps.

If it's a LOT of items, you might want to use two treemaps instead of two hashmaps. But the essential principle ("Share the Values") applies to both structures. One set of Values with two maps.

债姬 2024-07-22 05:01:08

是否真的有必要使用两种类型的密钥键入相同的结构? 您可能不需要自己重建复杂的数据结构。 您可以对查找表进行某种封装,这样如果内存不是问题,您实际上就有两个查找表。 您可以使用此封装结构来模拟能够使用任一类型的键从“相同”结构中提取值。

或者

如果有某种方法可以在枚举值和字符串键之间进行映射,那么您可以只使用一种类型的查找表来实现这一点。

Is it really necessary to key into the same structure with both types of key? You probably don't need to rebuild a complex data structure yourself. You could do some sort of encapsulation for the lookup table so that you really have two lookup tables if memory is not an issue. You could use this encapsulating structure to simulate being able to pull out the value from the "same" structure with either type of key.

OR

If there is some way to map between the enum value and the string key you could go that route with only having one type of lookup table.

吐个泡泡 2024-07-22 05:01:08

LINQ 的 ILookup(TKey, TElement) 接口可能会有所帮助。 假设你的字典是这样的:

Dictionary<carKey, carValue> cars;

你可以使用:

ILookUp<carValue, carKey> lookup = cars.ToLookup(x => x.Value, x => x.Key);

(...实际上我想我可能有点误读了这个问题 - 但 ILookUp 可能仍然符合要求,但键/值集可能需要是键和枚举.)

LINQ's ILookup(TKey, TElement) interface may help. Assuming your Dictionary is something like:

Dictionary<carKey, carValue> cars;

You could use:

ILookUp<carValue, carKey> lookup = cars.ToLookup(x => x.Value, x => x.Key);

(...actually I think I might have slightly misread the question - but an ILookUp might still fit the bill, but the key/value set might need to be the key and the enum.)

肥爪爪 2024-07-22 05:01:08

如果保证每个值都可以通过两种类型的键访问,则另一个想法是将一种类型的键转换为另一种类型的键。 例如:

public Value getValue(String key)
{
    dictionary.get(key); // normal way
}

public Value getValue(Enum enumKey)
{
    String realKey = toKey(enumKey);
    getValue(realKey); // use String key
}

您可以让 Enum 实现一个 toKey() 方法来返回其 String 键,或者可以使用另一个字典将 Enum 值映射到 String 对应项。

If every value is guaranteed to be accessible by both types of keys, another idea would be to convert one type of key to another. For example:

public Value getValue(String key)
{
    dictionary.get(key); // normal way
}

public Value getValue(Enum enumKey)
{
    String realKey = toKey(enumKey);
    getValue(realKey); // use String key
}

You could have your Enum implement a toKey() method that returns their String key, or maybe have another dictionary that maps Enum values to the String counterparts.

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