Grouping.cs 3.3 KB

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