MultiMap implementation

I am writing a simple IDictionary abstraction in C # that wraps a Dictionary <K, ICollection <V>. Basically, it maps multiple values ​​to one key. I am unable to decide whether to delete the key and its empty list when the last item in the list of values ​​is removed, or leave it (so as not to instantiate a new collection if the key is reused) and run the Count key values ​​checks when determining if the key exists.

+1


source to share


4 answers


I would remove the collections so that your MultiMap has consistent behavior. If I were to use your MultiMap, I would be very surprised (and unhappy) to find that the missing key behaves differently depending on whether the key was previously in the MultiMap or not.

Does Clear () delete the collection?



You can also create an unintended memory leak if you don't delete the collections. A developer can add many elements and then remove them. Memory usage (after GC) should return to the same amount as before adding these items.

I would not worry about the cost of creating Collections. I will be worried about the contract you create for your MultiMap. If, after profiling your application, you find this is the case, you can modify or create a custom MultiMap for this behavior. Don't fall into the trap of premature optimization.

+4


source


In .NET 3.5 there ILookup<TKey,TValue>

and Lookup<TKey,TValue>

that act as a multi-card. The inline implementation ( Lookup<TKey,TValue>

) is immutable, but I wrote EditableLookup<TKey,TValue>

in miscutil .



In this version; yes - I remove the key if the last item (with that key) is removed. This makes it easier to find keys (i.e. .Keys

, etc.).

+2


source


Why not treat the key as present even if all values ​​have been removed and provide an explicit API to remove the key?

0


source


It depends on your usage pattern. If you are going to add and remove many elements, then these empty collections will use memory. My guess is that you won't be able to save a lot of time by saving collections. As always, if it's important to your job, you should measure, not guess, which path is best.

If you really think creating these collections is expensive, then instead of creating new ones all the time, put the unused ones on the list and reuse them when new keys are added to your hash file. I think it might be flies. You should probably keep the list of unused collections less than half the size of the main hashmap (again, measure how the ratio affects performance).

0


source







All Articles