Grouping.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 it's 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)
  34. {
  35. throw new NotSupportedException(Strings.NOT_SUPPORTED);
  36. }
  37. void ICollection<TElement>.Clear()
  38. {
  39. throw new NotSupportedException(Strings.NOT_SUPPORTED);
  40. }
  41. bool ICollection<TElement>.Contains(TElement item) => Array.IndexOf(_elements, item, 0, _count) >= 0;
  42. void ICollection<TElement>.CopyTo(TElement[] array, int arrayIndex) => Array.Copy(_elements, 0, array, arrayIndex, _count);
  43. bool ICollection<TElement>.Remove(TElement item)
  44. {
  45. throw new NotSupportedException(Strings.NOT_SUPPORTED);
  46. }
  47. int IList<TElement>.IndexOf(TElement item) => Array.IndexOf(_elements, item, 0, _count);
  48. void IList<TElement>.Insert(int index, TElement item)
  49. {
  50. throw new NotSupportedException(Strings.NOT_SUPPORTED);
  51. }
  52. void IList<TElement>.RemoveAt(int index)
  53. {
  54. throw new NotSupportedException(Strings.NOT_SUPPORTED);
  55. }
  56. TElement IList<TElement>.this[int index]
  57. {
  58. get
  59. {
  60. if (index < 0 || index >= _count)
  61. {
  62. throw new ArgumentOutOfRangeException(nameof(index));
  63. }
  64. return _elements[index];
  65. }
  66. set { throw new NotSupportedException(Strings.NOT_SUPPORTED); }
  67. }
  68. internal void Add(TElement element)
  69. {
  70. if (_elements.Length == _count)
  71. {
  72. Array.Resize(ref _elements, checked(_count*2));
  73. }
  74. _elements[_count] = element;
  75. _count++;
  76. }
  77. internal void Trim()
  78. {
  79. if (_elements.Length != _count)
  80. {
  81. Array.Resize(ref _elements, _count);
  82. }
  83. }
  84. IAsyncEnumerator<TElement> IAsyncEnumerable<TElement>.GetAsyncEnumerator(CancellationToken cancellationToken) => this.ToAsyncEnumerable().GetAsyncEnumerator(cancellationToken);
  85. }
  86. }