Data structure for data with multiple keys?

Is there a commonly used data structure for multi-key data? e.g. (key1, key2, ..., keyN) -> value. I used dictionaries of dictionaries (in C #) and then wrote my own wrapper on top of this so that the syntax looks a little better. but it seems that I still have to write a wrapper for each N-dictionary, where N is the number of keys, since I have to define the nested dictionary structure in the code.

Assuming I'm using C #, is there a data structure that encapsulates this usage better and can contain an arbitrary number of keys with a hash table-style search function? I can't just combine all the keys into one unique key, because I need to do something like

foreach key2 in data[key1]
    foreach key3 in data[key1][key2]
        foreach key4 in data[key1][key2][key3]
+3
source share
1 answer

No, it is not.

Without violating type security, I think there are two solutions.

  • Dictionaries of dictionaries - Dictionary<T1, Dictionary<T2, TRes>>
  • Dictionaries of tuples Dictionary<Tuple3<T1, T2, T3>, Res>. Please note: unlike F #, where you could write Map<T1 * T2 * T3, Res>- C # does not have a built-in tuple - you will have to implement this separately as a general class or structure.

But regarding your sample code, the only alternatives are fuzzy dictionaries (dictionaries from dictionaries).

+2
source

Source: https://habr.com/ru/post/1711347/


All Articles