Grouping.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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.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. internal int _count;
  15. internal TElement[] _elements;
  16. internal int _hashCode;
  17. internal Grouping<TKey, TElement> _hashNext;
  18. internal TKey _key;
  19. internal Grouping<TKey, TElement>? _next;
  20. public Grouping(TKey key, int hashCode, TElement[] elements, Grouping<TKey, TElement> hashNext)
  21. {
  22. _key = key;
  23. _hashCode = hashCode;
  24. _elements = elements;
  25. _hashNext = hashNext;
  26. }
  27. IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
  28. public IEnumerator<TElement> GetEnumerator()
  29. {
  30. for (var i = 0; i < _count; i++)
  31. {
  32. yield return _elements[i];
  33. }
  34. }
  35. // DDB195907: implement IGrouping<>.Key implicitly
  36. // so that WPF binding works on this property.
  37. public TKey Key => _key;
  38. int ICollection<TElement>.Count => _count;
  39. bool ICollection<TElement>.IsReadOnly => true;
  40. void ICollection<TElement>.Add(TElement item) => throw Error.NotSupported();
  41. void ICollection<TElement>.Clear() => throw Error.NotSupported();
  42. bool ICollection<TElement>.Contains(TElement item) => Array.IndexOf(_elements, item, 0, _count) >= 0;
  43. void ICollection<TElement>.CopyTo(TElement[] array, int arrayIndex) => Array.Copy(_elements, 0, array, arrayIndex, _count);
  44. bool ICollection<TElement>.Remove(TElement item) => throw Error.NotSupported();
  45. int IList<TElement>.IndexOf(TElement item) => Array.IndexOf(_elements, item, 0, _count);
  46. void IList<TElement>.Insert(int index, TElement item) => throw Error.NotSupported();
  47. void IList<TElement>.RemoveAt(int index) => throw Error.NotSupported();
  48. TElement IList<TElement>.this[int index]
  49. {
  50. get
  51. {
  52. if (index < 0 || index >= _count)
  53. {
  54. throw Error.ArgumentOutOfRange(nameof(index));
  55. }
  56. return _elements[index];
  57. }
  58. set => throw Error.NotSupported();
  59. }
  60. internal void Add(TElement element)
  61. {
  62. if (_elements.Length == _count)
  63. {
  64. Array.Resize(ref _elements, checked(_count*2));
  65. }
  66. _elements[_count] = element;
  67. _count++;
  68. }
  69. internal void Trim()
  70. {
  71. if (_elements.Length != _count)
  72. {
  73. Array.Resize(ref _elements, _count);
  74. }
  75. }
  76. IAsyncEnumerator<TElement> IAsyncEnumerable<TElement>.GetAsyncEnumerator(CancellationToken cancellationToken)
  77. {
  78. cancellationToken.ThrowIfCancellationRequested(); // NB: [LDM-2018-11-28] Equivalent to async iterator behavior.
  79. return this.ToAsyncEnumerable().GetAsyncEnumerator(cancellationToken);
  80. }
  81. }
  82. }