Dictionary<string, Dictionary<string, ... >...> nestedDictionary;
Above Dictionary
has a one-to-many
relationship at each level from top to bottom. Adding an item is pretty easy since we have the leaf object and we start from bottom, creating dictionaries and adding each to the relevant parent...
My problem is when I want to find an item at the inner Dictionaries. There are two options:
- Nested
foreach
and find the item then snapshot all the loops at the moment we found the item and exit all loops. Then we know item pedigree is string1->string2->...->stringN. Problems with this solution is A) Performance B) Thread-safety (since I want to remove the item, the parent if it has no child and it's parent if it has no child...) - Creating a reverse look-up dictionary
and indexing added items. Something
like a
Tuple
for all outer dictionaries. Then add the item as key and all the outer parents asTuple
members. Problem: A) Redundancy B) Keeping synchronized reverse look-upDictionary
with mainDictionary
.
Any idea for a fast and thread-safe solution?