Lookup.cs 8.7 KB

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