Grouping.cs 3.4 KB

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