MultiDictionary.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using System.Collections.Generic;
  2. namespace ET
  3. {
  4. public class MultiDictionary<T, M, N>: Dictionary<T, Dictionary<M, N>>
  5. {
  6. public bool TryGetDic(T t, out Dictionary<M, N> k)
  7. {
  8. return this.TryGetValue(t, out k);
  9. }
  10. public bool TryGetValue(T t, M m, out N n)
  11. {
  12. n = default;
  13. if (!this.TryGetValue(t, out Dictionary<M, N> dic))
  14. {
  15. return false;
  16. }
  17. return dic.TryGetValue(m, out n);
  18. }
  19. public void Add(T t, M m, N n)
  20. {
  21. Dictionary<M, N> kSet;
  22. this.TryGetValue(t, out kSet);
  23. if (kSet == null)
  24. {
  25. kSet = new Dictionary<M, N>();
  26. this[t] = kSet;
  27. }
  28. kSet.Add(m, n);
  29. }
  30. public bool Remove(T t, M m)
  31. {
  32. this.TryGetValue(t, out Dictionary<M, N> dic);
  33. if (dic == null || !dic.Remove(m))
  34. {
  35. return false;
  36. }
  37. if (dic.Count == 0)
  38. {
  39. this.Remove(t);
  40. }
  41. return true;
  42. }
  43. public bool ContainSubKey(T t, M m)
  44. {
  45. this.TryGetValue(t, out Dictionary<M, N> dic);
  46. if (dic == null)
  47. {
  48. return false;
  49. }
  50. return dic.ContainsKey(m);
  51. }
  52. public bool ContainValue(T t, M m, N n)
  53. {
  54. this.TryGetValue(t, out Dictionary<M, N> dic);
  55. if (dic == null)
  56. {
  57. return false;
  58. }
  59. if (!dic.ContainsKey(m))
  60. {
  61. return false;
  62. }
  63. return dic.ContainsValue(n);
  64. }
  65. }
  66. }