MultiDictionary.cs 868 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace MoonSharp.Interpreter.DataStructs
  6. {
  7. public class MultiDictionary<K, V>
  8. {
  9. Dictionary<K, List<V>> m_Map = new Dictionary<K, List<V>>();
  10. public void Add(K key, V value)
  11. {
  12. List<V> list;
  13. if (m_Map.TryGetValue(key, out list))
  14. {
  15. list.Add(value);
  16. }
  17. else
  18. {
  19. list = new List<V>();
  20. list.Add(value);
  21. m_Map.Add(key, list);
  22. }
  23. }
  24. public IEnumerable<V> Find(K key)
  25. {
  26. List<V> list;
  27. if (m_Map.TryGetValue(key, out list))
  28. return list;
  29. else
  30. return new V[0];
  31. }
  32. public bool ContainsKey(K key)
  33. {
  34. return m_Map.ContainsKey(key);
  35. }
  36. public IEnumerable<K> Keys
  37. {
  38. get { return m_Map.Keys; }
  39. }
  40. public void Clear()
  41. {
  42. m_Map.Clear();
  43. }
  44. public void Remove(K key)
  45. {
  46. m_Map.Remove(key);
  47. }
  48. }
  49. }