Lookup.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.Linq;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. namespace System.Linq.Internal
  9. {
  10. internal class Lookup<TKey, TElement> : ILookup<TKey, TElement>
  11. {
  12. private readonly IEqualityComparer<TKey> _comparer;
  13. private Grouping<TKey, TElement>[] _groupings;
  14. private Grouping<TKey, TElement> _lastGrouping;
  15. private int _count;
  16. internal static Lookup<TKey, TElement> Create<TSource>(IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey> comparer)
  17. {
  18. Debug.Assert(source != null);
  19. Debug.Assert(keySelector != null);
  20. Debug.Assert(elementSelector != null);
  21. Lookup<TKey, TElement> lookup = new Lookup<TKey, TElement>(comparer);
  22. foreach (TSource item in source)
  23. {
  24. lookup.GetGrouping(keySelector(item), create: true).Add(elementSelector(item));
  25. }
  26. return lookup;
  27. }
  28. internal static Lookup<TKey, TElement> Create(IEnumerable<TElement> source, Func<TElement, TKey> keySelector, IEqualityComparer<TKey> comparer)
  29. {
  30. Debug.Assert(source != null);
  31. Debug.Assert(keySelector != null);
  32. Lookup<TKey, TElement> lookup = new Lookup<TKey, TElement>(comparer);
  33. foreach (TElement item in source)
  34. {
  35. lookup.GetGrouping(keySelector(item), create: true).Add(item);
  36. }
  37. return lookup;
  38. }
  39. internal static Lookup<TKey, TElement> CreateForJoin(IEnumerable<TElement> source, Func<TElement, TKey> keySelector, IEqualityComparer<TKey> comparer)
  40. {
  41. Lookup<TKey, TElement> lookup = new Lookup<TKey, TElement>(comparer);
  42. foreach (TElement item in source)
  43. {
  44. TKey key = keySelector(item);
  45. if (key != null)
  46. {
  47. lookup.GetGrouping(key, create: true).Add(item);
  48. }
  49. }
  50. return lookup;
  51. }
  52. internal static async Task<Lookup<TKey, TElement>> CreateForJoinAsync(IAsyncEnumerable<TElement> source, Func<TElement, TKey> keySelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
  53. {
  54. Lookup<TKey, TElement> lookup = new Lookup<TKey, TElement>(comparer);
  55. using (var enu = source.GetEnumerator())
  56. {
  57. while (await enu.MoveNext(cancellationToken)
  58. .ConfigureAwait(false))
  59. {
  60. TKey key = keySelector(enu.Current);
  61. if (key != null)
  62. {
  63. lookup.GetGrouping(key, create: true).Add(enu.Current);
  64. }
  65. }
  66. }
  67. return lookup;
  68. }
  69. private Lookup(IEqualityComparer<TKey> comparer)
  70. {
  71. _comparer = comparer ?? EqualityComparer<TKey>.Default;
  72. _groupings = new Grouping<TKey, TElement>[7];
  73. }
  74. public int Count
  75. {
  76. get { return _count; }
  77. }
  78. public IEnumerable<TElement> this[TKey key]
  79. {
  80. get
  81. {
  82. Grouping<TKey, TElement> grouping = GetGrouping(key, create: false);
  83. if (grouping != null)
  84. {
  85. return grouping;
  86. }
  87. #if NO_ARRAY_EMPTY
  88. return EmptyArray<TElement>.Value;
  89. #else
  90. return Array.Empty<TElement>();
  91. #endif
  92. }
  93. }
  94. public bool Contains(TKey key)
  95. {
  96. return GetGrouping(key, create: false) != null;
  97. }
  98. public IEnumerator<IGrouping<TKey, TElement>> GetEnumerator()
  99. {
  100. Grouping<TKey, TElement> g = _lastGrouping;
  101. if (g != null)
  102. {
  103. do
  104. {
  105. g = g._next;
  106. yield return g;
  107. }
  108. while (g != _lastGrouping);
  109. }
  110. }
  111. internal TResult[] ToArray<TResult>(Func<TKey, IEnumerable<TElement>, TResult> resultSelector)
  112. {
  113. TResult[] array = new TResult[_count];
  114. int index = 0;
  115. Grouping<TKey, TElement> g = _lastGrouping;
  116. if (g != null)
  117. {
  118. do
  119. {
  120. g = g._next;
  121. g.Trim();
  122. array[index] = resultSelector(g._key, g._elements);
  123. ++index;
  124. }
  125. while (g != _lastGrouping);
  126. }
  127. return array;
  128. }
  129. internal List<TResult> ToList<TResult>(Func<TKey, IEnumerable<TElement>, TResult> resultSelector)
  130. {
  131. List<TResult> list = new List<TResult>(_count);
  132. Grouping<TKey, TElement> g = _lastGrouping;
  133. if (g != null)
  134. {
  135. do
  136. {
  137. g = g._next;
  138. g.Trim();
  139. list.Add(resultSelector(g._key, g._elements));
  140. }
  141. while (g != _lastGrouping);
  142. }
  143. return list;
  144. }
  145. public IEnumerable<TResult> ApplyResultSelector<TResult>(Func<TKey, IEnumerable<TElement>, TResult> resultSelector)
  146. {
  147. Grouping<TKey, TElement> g = _lastGrouping;
  148. if (g != null)
  149. {
  150. do
  151. {
  152. g = g._next;
  153. g.Trim();
  154. yield return resultSelector(g._key, g._elements);
  155. }
  156. while (g != _lastGrouping);
  157. }
  158. }
  159. IEnumerator IEnumerable.GetEnumerator()
  160. {
  161. return GetEnumerator();
  162. }
  163. internal int InternalGetHashCode(TKey key)
  164. {
  165. // Handle comparer implementations that throw when passed null
  166. return (key == null) ? 0 : _comparer.GetHashCode(key) & 0x7FFFFFFF;
  167. }
  168. internal Grouping<TKey, TElement> GetGrouping(TKey key, bool create)
  169. {
  170. int hashCode = InternalGetHashCode(key);
  171. for (Grouping<TKey, TElement> g = _groupings[hashCode % _groupings.Length]; g != null; g = g._hashNext)
  172. {
  173. if (g._hashCode == hashCode && _comparer.Equals(g._key, key))
  174. {
  175. return g;
  176. }
  177. }
  178. if (create)
  179. {
  180. if (_count == _groupings.Length)
  181. {
  182. Resize();
  183. }
  184. int index = hashCode % _groupings.Length;
  185. Grouping<TKey, TElement> g = new Grouping<TKey, TElement>();
  186. g._key = key;
  187. g._hashCode = hashCode;
  188. g._elements = new TElement[1];
  189. g._hashNext = _groupings[index];
  190. _groupings[index] = g;
  191. if (_lastGrouping == null)
  192. {
  193. g._next = g;
  194. }
  195. else
  196. {
  197. g._next = _lastGrouping._next;
  198. _lastGrouping._next = g;
  199. }
  200. _lastGrouping = g;
  201. _count++;
  202. return g;
  203. }
  204. return null;
  205. }
  206. private void Resize()
  207. {
  208. int newSize = checked((_count * 2) + 1);
  209. Grouping<TKey, TElement>[] newGroupings = new Grouping<TKey, TElement>[newSize];
  210. Grouping<TKey, TElement> g = _lastGrouping;
  211. do
  212. {
  213. g = g._next;
  214. int index = g._hashCode % newSize;
  215. g._hashNext = newGroupings[index];
  216. newGroupings[index] = g;
  217. }
  218. while (g != _lastGrouping);
  219. _groupings = newGroupings;
  220. }
  221. }
  222. }