MutableLookup.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. namespace InABox.Core
  6. {
  7. public interface IMutableLookup<TKey, TElement> : ILookup<TKey, TElement>
  8. {
  9. void Add(TKey key, TElement element);
  10. void AddRange(TKey key, IEnumerable<TElement> elements);
  11. void Remove(TKey key);
  12. }
  13. public class MutableLookup<TKey, TElement> : IMutableLookup<TKey, TElement>
  14. {
  15. private readonly Dictionary<TKey, List<TElement>> _data = new Dictionary<TKey, List<TElement>>();
  16. public MutableLookup()
  17. {
  18. }
  19. public MutableLookup(ILookup<TKey, TElement> source)
  20. {
  21. foreach (var grouping in source)
  22. foreach (var element in grouping)
  23. Add(grouping.Key, element);
  24. }
  25. public IEnumerable<TElement> All => (from key in _data.Keys
  26. select _data[key])
  27. .SelectMany(list => list);
  28. public int Count => All.Count();
  29. public IEnumerable<TElement> this[TKey key]
  30. {
  31. get
  32. {
  33. List<TElement> result;
  34. if (_data.TryGetValue(key, out result))
  35. return result;
  36. return Array.Empty<TElement>();
  37. }
  38. }
  39. public bool Contains(TKey key)
  40. {
  41. return _data.ContainsKey(key);
  42. }
  43. public void Add(TKey key, TElement element)
  44. {
  45. if (!_data.TryGetValue(key, out var list))
  46. {
  47. list = new List<TElement>();
  48. _data.Add(key, list);
  49. }
  50. if (!list.Contains(element))
  51. list.Add(element);
  52. }
  53. public void AddRange(TKey key, IEnumerable<TElement> elements)
  54. {
  55. if (!_data.TryGetValue(key, out var list))
  56. {
  57. list = new List<TElement>();
  58. _data.Add(key, list);
  59. }
  60. list.AddRange(elements.Where(x => list.Contains(x)));
  61. }
  62. public void Remove(TKey key)
  63. {
  64. _data.Remove(key);
  65. }
  66. public IEnumerator<IGrouping<TKey, TElement>> GetEnumerator()
  67. {
  68. return GetGroupings().GetEnumerator();
  69. }
  70. IEnumerator IEnumerable.GetEnumerator()
  71. {
  72. return (GetGroupings() as IEnumerable).GetEnumerator();
  73. }
  74. private IEnumerable<IGrouping<TKey, TElement>> GetGroupings()
  75. {
  76. return from key in _data.Keys
  77. select new LookupDictionaryGrouping<TKey, TElement>
  78. {
  79. Key = key,
  80. Elements = _data[key]
  81. } as IGrouping<TKey, TElement>;
  82. }
  83. }
  84. public class LookupDictionaryGrouping<TKey, TElement> : IGrouping<TKey, TElement>
  85. {
  86. public IEnumerable<TElement> Elements { get; set; }
  87. public TKey Key { get; set; }
  88. public IEnumerator<TElement> GetEnumerator()
  89. {
  90. return Elements.GetEnumerator();
  91. }
  92. IEnumerator IEnumerable.GetEnumerator()
  93. {
  94. return (Elements as IEnumerable).GetEnumerator();
  95. }
  96. }
  97. }