Grouping.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT 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.Threading;
  7. // Note: The type here has to be internal as System.Linq has its own public copy we're not using.
  8. namespace System.Linq.Internal
  9. {
  10. /// Adapted from System.Linq.Grouping from .NET Framework
  11. /// Source: https://github.com/dotnet/corefx/blob/b90532bc97b07234a7d18073819d019645285f1c/src/System.Linq/src/System/Linq/Grouping.cs#L64
  12. internal class Grouping<TKey, TElement> : IGrouping<TKey, TElement>, IList<TElement>, IAsyncGrouping<TKey, TElement>
  13. {
  14. #pragma warning disable IDE1006 // Naming Styles
  15. internal int _count;
  16. internal TElement[] _elements;
  17. internal int _hashCode;
  18. internal Grouping<TKey, TElement> _hashNext;
  19. internal TKey _key;
  20. internal Grouping<TKey, TElement>? _next;
  21. #pragma warning restore IDE1006 // Naming Styles
  22. public Grouping(TKey key, int hashCode, TElement[] elements, Grouping<TKey, TElement> hashNext)
  23. {
  24. _key = key;
  25. _hashCode = hashCode;
  26. _elements = elements;
  27. _hashNext = hashNext;
  28. }
  29. IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
  30. public IEnumerator<TElement> GetEnumerator()
  31. {
  32. for (var i = 0; i < _count; i++)
  33. {
  34. yield return _elements[i];
  35. }
  36. }
  37. // DDB195907: implement IGrouping<>.Key implicitly
  38. // so that WPF binding works on this property.
  39. public TKey Key => _key;
  40. int ICollection<TElement>.Count => _count;
  41. bool ICollection<TElement>.IsReadOnly => true;
  42. void ICollection<TElement>.Add(TElement item) => throw Error.NotSupported();
  43. void ICollection<TElement>.Clear() => throw Error.NotSupported();
  44. bool ICollection<TElement>.Contains(TElement item) => Array.IndexOf(_elements, item, 0, _count) >= 0;
  45. void ICollection<TElement>.CopyTo(TElement[] array, int arrayIndex) => Array.Copy(_elements, 0, array, arrayIndex, _count);
  46. bool ICollection<TElement>.Remove(TElement item) => throw Error.NotSupported();
  47. int IList<TElement>.IndexOf(TElement item) => Array.IndexOf(_elements, item, 0, _count);
  48. void IList<TElement>.Insert(int index, TElement item) => throw Error.NotSupported();
  49. void IList<TElement>.RemoveAt(int index) => throw Error.NotSupported();
  50. TElement IList<TElement>.this[int index]
  51. {
  52. get
  53. {
  54. if (index < 0 || index >= _count)
  55. {
  56. throw Error.ArgumentOutOfRange(nameof(index));
  57. }
  58. return _elements[index];
  59. }
  60. set => throw Error.NotSupported();
  61. }
  62. internal void Add(TElement element)
  63. {
  64. if (_elements.Length == _count)
  65. {
  66. Array.Resize(ref _elements, checked(_count*2));
  67. }
  68. _elements[_count] = element;
  69. _count++;
  70. }
  71. internal void Trim()
  72. {
  73. if (_elements.Length != _count)
  74. {
  75. Array.Resize(ref _elements, _count);
  76. }
  77. }
  78. IAsyncEnumerator<TElement> IAsyncEnumerable<TElement>.GetAsyncEnumerator(CancellationToken cancellationToken)
  79. {
  80. cancellationToken.ThrowIfCancellationRequested(); // NB: [LDM-2018-11-28] Equivalent to async iterator behavior.
  81. return this.ToAsyncEnumerable().GetAsyncEnumerator(cancellationToken);
  82. }
  83. }
  84. }