How to implement a multi-index dictionary?

前端 未结 11 1425
孤独总比滥情好
孤独总比滥情好 2020-12-14 00:32

Basically I want something like Dictionary, but not (as I\'ve seen here in other question) with the keys in AND, but in OR. To better explain: I

相关标签:
11条回答
  • 2020-12-14 01:05

    I have written such a dictionary and posted it on my blog. It will give you a nice API like this:

    DoubleKeyDictionary<int, string, string> books = new DoubleKeyDictionary<string, string, string>();
    bookListEx.Add(1, “21/12/2009″, “Lord of the Rings - Fellowship of the Ring”); 
    

    You can also do "Equals" on two dictionaries and for-each over it.

    Please note that there are at least one bug in the code (as discovered in the comments) and no unit tests etc. When (yeah!) I get some time I'll update the code with unit tests...

    0 讨论(0)
  • 2020-12-14 01:08

    Did you consider holding two dictionaries, one for each key? Adding an item would add it to both.

    Removal means removing from both dictionaries too, and requires both keys. To remove with just one key, the item would have to store both keys. If it doesn't already hold both keys, you could wrap it in a container object that holds both keys and the item.

    0 讨论(0)
  • 2020-12-14 01:08

    Check out this article on CodeProject: http://www.codeproject.com/KB/recipes/multikey-dictionary.aspx

    This is clearly the right way to do a multi-key dictionary ;)

    0 讨论(0)
  • 2020-12-14 01:11

    So you want a multi-index dictionary, supports lookups based on any key, and supports extensions to multiple keys?

    Maybe you're thinking of the wrong data structure, try a KD-Tree instead. An immutable KD-tree would satisfy the thread-safety requirement.

    KD-trees have some major advantages over the naive Dictionary{Key1, Dictionary{Key2, Value}} approach, namely that you can search for all fields based on Key2 without knowing Key1. Additionally, KD-trees allow you to search for keys which are near some other key. For example, dating sites classify people into dozens of groups (smoker/non-smoker, gender, religion, lifestyle, age, height), then return nearest neighbors based on your query.

    Here's a C# and Java implementation:

    http://home.wlu.edu/~levys/software/kd/ (broken link, archived at https://web.archive.org/web/20190609084214/http://home.wlu.edu/~levys/software/kd/)

    0 讨论(0)
  • 2020-12-14 01:11

    You could just use a regular dictionary for this and insert the value twice, once for each key. To delete you remove both keys.

    Upsides:

    • Can search for both keys with one lookup.
    • No new code needed.
    • Scales to any number of keys.

    Downsides:

    • Lose type safety if keys are of different types.
    • Can't iterate over (key1, key2, value) tuples.
    • Values appear twice so size() is doubled.
    0 讨论(0)
  • 2020-12-14 01:18

    Create simple class to store Tkey1, TKey2, TValue, make List of them and use LINQ to query such structure would be an option.

    0 讨论(0)
提交回复
热议问题