Grouping.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. namespace System.Linq.Internal
  7. {
  8. /// Adapted from System.Linq.Grouping from .NET Framework
  9. /// Source: https://github.com/dotnet/corefx/blob/b90532bc97b07234a7d18073819d019645285f1c/src/System.Linq/src/System/Linq/Grouping.cs#L64
  10. internal class Grouping<TKey, TElement> : IGrouping<TKey, TElement>, IList<TElement>
  11. {
  12. internal TKey _key;
  13. internal int _hashCode;
  14. internal TElement[] _elements;
  15. internal int _count;
  16. internal Grouping<TKey, TElement> _hashNext;
  17. internal Grouping<TKey, TElement> _next;
  18. internal Grouping()
  19. {
  20. }
  21. internal void Add(TElement element)
  22. {
  23. if (_elements.Length == _count)
  24. {
  25. Array.Resize(ref _elements, checked(_count * 2));
  26. }
  27. _elements[_count] = element;
  28. _count++;
  29. }
  30. internal void Trim()
  31. {
  32. if (_elements.Length != _count)
  33. {
  34. Array.Resize(ref _elements, _count);
  35. }
  36. }
  37. public IEnumerator<TElement> GetEnumerator()
  38. {
  39. for (int i = 0; i < _count; i++)
  40. {
  41. yield return _elements[i];
  42. }
  43. }
  44. IEnumerator IEnumerable.GetEnumerator()
  45. {
  46. return GetEnumerator();
  47. }
  48. // DDB195907: implement IGrouping<>.Key implicitly
  49. // so that WPF binding works on this property.
  50. public TKey Key
  51. {
  52. get { return _key; }
  53. }
  54. int ICollection<TElement>.Count
  55. {
  56. get { return _count; }
  57. }
  58. bool ICollection<TElement>.IsReadOnly
  59. {
  60. get { return true; }
  61. }
  62. void ICollection<TElement>.Add(TElement item)
  63. {
  64. throw new NotSupportedException(Strings.NOT_SUPPORTED);
  65. }
  66. void ICollection<TElement>.Clear()
  67. {
  68. throw new NotSupportedException(Strings.NOT_SUPPORTED);
  69. }
  70. bool ICollection<TElement>.Contains(TElement item)
  71. {
  72. return Array.IndexOf(_elements, item, 0, _count) >= 0;
  73. }
  74. void ICollection<TElement>.CopyTo(TElement[] array, int arrayIndex)
  75. {
  76. Array.Copy(_elements, 0, array, arrayIndex, _count);
  77. }
  78. bool ICollection<TElement>.Remove(TElement item)
  79. {
  80. throw new NotSupportedException(Strings.NOT_SUPPORTED);
  81. }
  82. int IList<TElement>.IndexOf(TElement item)
  83. {
  84. return Array.IndexOf(_elements, item, 0, _count);
  85. }
  86. void IList<TElement>.Insert(int index, TElement item)
  87. {
  88. throw new NotSupportedException(Strings.NOT_SUPPORTED);
  89. }
  90. void IList<TElement>.RemoveAt(int index)
  91. {
  92. throw new NotSupportedException(Strings.NOT_SUPPORTED);
  93. }
  94. TElement IList<TElement>.this[int index]
  95. {
  96. get
  97. {
  98. if (index < 0 || index >= _count)
  99. {
  100. throw new ArgumentOutOfRangeException(nameof(index));
  101. }
  102. return _elements[index];
  103. }
  104. set
  105. {
  106. throw new NotSupportedException(Strings.NOT_SUPPORTED);
  107. }
  108. }
  109. }
  110. }