Grouping.cs 3.2 KB

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