简体   繁体   English

仅使用每个键从具有多个键的字典中检索值

[英]Retrieve Values from Dictionary with Multiple Keys Using Only Each Key

I have this situation where I am using a dictionary to store entities. 在这种情况下,我使用字典来存储实体。 Each entity has two ID's that I want to index by when retrieving values. 每个实体都有两个ID,在检索值时我希望以此索引。 One is generated as the object is created (I'll call this ID) and the other is read from an external source (I'll call this Handle). 一个是在创建对象时生成的(我称这个ID),另一个是从外部源读取的(我称这个Handle)。 I've played around with ToLookup() and that is very, very slow. 我玩过ToLookup(),这非常非常慢。 I've created a custom key object to contain both identifiers, but then it seems that basic use of a dictionary means you have to know both to retrieve the associated value. 我创建了一个包含两个标识符的自定义键对象,但是似乎字典的基本用法意味着您必须知道两者才能检索关联的值。 I need a very fast solution if I only know one or the other. 如果我只知道一个或另一个,我需要一个非常快速的解决方案。 Any help would be appreciated. 任何帮助,将不胜感激。 I also tried creating a class that would internally contain two dictionaries, but then I guess the pointer references add to the memory footprint, so I'm not sure this is the right approach either. 我还尝试创建一个内部包含两个字典的类,但是随后我猜想指针引用会增加内存占用,因此我不确定这是否是正确的方法。 One of the reasons I'm somewhat concerned about the memory footprint is because I'm compiling this as a dll so that it can be loaded into a third-party system. 我有点担心内存占用的原因之一是因为我将其编译为dll,以便可以将其加载到第三方系统中。 I don't have any control over the amount of memory to be used, so the more I use, the less I can do. 我对要使用的内存量没有任何控制,所以我使用的内存越多,我可以做的事就越少。 If this isn't clear enough I can provide some oversimplified examples. 如果还不清楚,我可以提供一些过于简化的示例。 Just let me know which to provide. 请让我知道要提供的内容。

The only way that you're going to be able to efficiently look up a value given just one of two possible types of keys is to have two dictionaries, and that is inherently going to require twice as much memory (approximately) as a single lookup. 仅给出两种可能类型的键中的一种,就能够有效地查找值的唯一方法是拥有两个字典,而这本质上将需要单次查找两倍的内存(大约) 。 There's no real way around that. 没有真正的解决办法。

If using that much memory isn't an option then you have to resort to far less efficient searching algorithms, such as a linear search. 如果无法使用那么多的内存,则必须诉诸效率低得多的搜索算法,例如线性搜索。

The choice is yours, execution speed or memory. 选择是您自己的,执行速度还是内存。 You have to pick one. 你必须选一个。

Well, I think I've come up with the best compromise I think I could have come up with considering. 好吧,我想我已经想出了最好的折衷方案,我想我可以考虑一下。 I ended up using the main dictionary with an internal sub-dictionary to be used as a lookup. 我最终将主字典与内部子字典一起使用来进行查找。 The sub-dictionary stores the ID and is indexed by the Handle. 子词典存储ID,并由Handle索引。 I can then create a custom indexer for the Handle, use the lookup dictionary to find the ID, then call the base indexer to retrieve the actual value. 然后,我可以为Handle创建自定义索引器,使用查找字典查找ID,然后调用基本索引器以检索实际值。 This makes it a little lighter-weight than duplicating dictionaries. 这比复制字典要轻一些。 If anyone else is interested (or has other comments to either improve or show me a better direction), here's a very simplified example: 如果其他人有兴趣(或有其他意见可以改善或为我提供更好的指导),下面是一个非常简化的示例:

public class MultiKeyDictionary : Dictionary<EntityID, string>
{
    private Dictionary<string, EntityID> _handleLookup = new Dictionary<string, EntityID>();
    public Dictionary<string, EntityID> HandleLookup
    {
        get { return _handleLookup; }
        set { _handleLookup = value; }
    }

    public string this[string handle]
    {
        get
        {
            EntityID id = this.HandleLookup[handle];
            return base[id];
        }
    }

    public void Add(EntityID id, string handle, string value)
    {
        base.Add(id, value);
        this.HandleLookup.Add(handle, id);
    }
}

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

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