Created
May 6, 2012 22:49
-
-
Save DDuarte/2624878 to your computer and use it in GitHub Desktop.
ConcurrentMultiDictionary - C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// By Nayd | |
public class ConcurrentMultiDictionary<TKey, TValue> : ConcurrentDictionary<TKey, List<TValue>> | |
{ | |
public bool TryAdd(TKey key, TValue value) | |
{ | |
List<TValue> container; | |
if (!TryGetValue(key, out container)) | |
{ | |
container = new List<TValue>(); | |
if (!TryAdd(key, container)) | |
return false; | |
} | |
container.Add(value); | |
return true; | |
} | |
public bool ContainsValue(TKey key, TValue value) | |
{ | |
List<TValue> container; | |
return TryGetValue(key, out container) && container.Contains(value); | |
} | |
public List<TValue> GetValues(TKey key) | |
{ | |
List<TValue> container; | |
return TryGetValue(key, out container) ? container : new List<TValue>(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment