Lookup.cs 8.9 KB

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