Two reversed dictionaries or one dictionary with key and value lookup?
https://softwareengineering.stackexchange.com/questions/386741
-
20-02-2021 - |
Pergunta
I have situation where in the Repository class I have a dictionary:
Dictionary<TableName, Dictionary<EntityColumnName, SourceColumnName> map1 = new Dictionary<TableName, Dictionary<EntityColumnName, SourceColumnName> ()
with methods for manipulating data (Get,Save,Add etc).
Now there is a need to search by value from the inner map, not only by key (in this example SourceColumnName
).
My question is what would be best approach here? I want to add another dictionary but with reversed data:
Dictionary<TableName, Dictionary<SourceColumnName, EntityColumnName> map2 = new Dictionary<TableName, Dictionary<SourceColumnName, EntityColumnName> ()
But the problem is memory usage, is that a waste of memory? But if I implement lookup by value, I'll lose the main advantage of dictionary.
The data stored in the dictionary is around 20 * 5k data in the inner map. Any suggestion how to design / refactor this class would be appreciated.
EDIT1: Micro optimisation post didn't resolved my problem. I work on project where optimisation is very important.
Solução
The type you're looking for is generally called a 'bidirectional dictionary'; I found a C# implementation in this Stack Overflow answer, but you're on the right track; you'll need two dictionaries, so memory usage will be higher. Whether this is worth the increase in performance you can only find out by testing and measuring.