Best data structure for storing an object indexed by a 3-tuple for quick retrieval for each dimension and low memory profile?

I want to store objects indexed using 3 tuples (String, String, DateTime). Lets name these identifiers, categories, day

Any object in the data structure is guaranteed to be unique with the help of 3 tuples (no duplicates)

The data structure should support quick answers to questions such as:
- What are all the unique identifiers?
- What are the categories for the identifier "xyz"?
- What are the days when id = "xyz" and category "mycategory"?

Removal is also possible. It would be great to maintain a low memory profile.

As a base level, I use Dictionary <string, Dictionary <string, Dictionary <DateTime, object β†’ β†’> </p> Theoretically, this should give me an O (1) search, but I'm not familiar with internal dictionaries and generally feel like mine The solution is not optimal.

I know that there is probably not a single correct answer here, and I could provide numerous usage data, but maybe someone can just give me some ideas for the game?

<b> Change
Only an equality search is performed (ie Identiifer = "xyz"). I do not use inequalities (more, less, etc.)

+4
source share
2 answers

It depends on the relative numbers of values ​​in each column, their distribution and the distribution of queries, so there is no better answer.

Your dictionaries are great for searching in one dimension, but you will have to search linearly if you need a combination of functions.

If space wasn’t a problem, you might have a level 3 index (trees or hash tables) so that you first retrieve elements by 1 size and then use the dictionary in this node to find all elements along the second dimension with a value for dimension 1 , then use the dictionary in node to find all elements with all 3 values.

It is also important if you want to answer queries using inequalities. In this case, a tree is better than a dictionary because it is ordered.

+1
source

since you added the .NET 4.0 tag, I assume you are in .NET 4.0, so why not Dictionary<Tuple<T1,T2,T3>,object>

0
source

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


All Articles