I have a problem where I need a .NET dictionary that supports multiple items per key. In the past I’ve used the STL multimap in my C++ programs. How does the design of a multimap differ from a dictionary of lists i.e. performance, size, etc. (excluding generics vs. templates)?
Share
multimap.count:O(log n + m)wherenis number of keys andmis number of items associated with a given key.For a
Dictionary<TKey, List<TValue>>the equivalent functionality would be:And safer is to say
This is an
O(1)operation because lookup isO(1)1 andList<T>.CountisO(1).multimap.find:O(log n)wherenis number of keysFor a
Dictionary<TKey, List<TValue>>the equivalent functionality would be:And safer is to say
This is
O(1). See the previous remark on lookup by key in aDictionary<TKey, TValue>.multimap.insert:O(log n)wherenis the number of keys.For a
Dictionary<TKey, List<TValue>>the equivalent functionality would be:This is usually
O(1)but can beO(n)when the capacity of the dictionary must be increased to accomodate the new element.multimap.remove: There are three overloads of this method; I will only consider the one that accepts a key and removes all occurrences of that key from the multimap. This is anO(log n + m)operation where therenkeys andmobjects associate with a given key.For a
Dictionary<TKey, List<TValue>>the equivalent functionality would be:From the documentation: “This method approaches an O(1) operation.” Same comment applies.
1: From the documentation: “Retrieving a value by using its key is very fast, close to
O(1).” Why the documentation is vague on this point is confusing to me. Either an operation isO(1)or it isn’t. There is no such thing as “close” toO(1).