C# Generic MultiDictionary
Note this is a fairly basic implementation as it dosn't implement any of the expected interfaces but it does provide the functionality of a generic type safe multi map.
class MultiDictionary<Key, Value>
{
private System.Collections.Generic.Dictionary<Key, System.Collections.Generic.List<Value>> m_collection = new System.Collections.Generic.Dictionary<Key, System.Collections.Generic.List<Value>>();
public void Clear()
{
m_collection.Clear();
}
public System.Collections.Generic.List<Value> this[Key id]
{
get
{
System.Collections.Generic.List<Value> l;
if (m_collection.TryGetValue(id, out l))
{
return l;
}
else
{
return new System.Collections.Generic.List<Value>();
}
}
}
public void Add(Key id, Value node)
{
System.Collections.Generic.List<Value> l;
if (m_collection.TryGetValue(id, out l))
{
l.Add(node);
}
else
{
l = new System.Collections.Generic.List<Value>();
l.Add(node);
m_collection[id] = l;
}
}
public void Remove(Key id, Value node)
{
System.Collections.Generic.List<Value> l;
if (m_collection.TryGetValue(id, out l))
{
l.Remove(node);
}
}
public bool ContainsKey(Key id)
{
return m_collection.ContainsKey(id);
}
};