| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 | // Licensed to the .NET Foundation under one or more agreements.// The .NET Foundation licenses this file to you under the Apache 2.0 License.// See the LICENSE file in the project root for more information. using System.Collections;using System.Collections.Generic;namespace System.Linq.Internal{    /// Adapted from System.Linq.Grouping from .NET Framework    /// Source: https://github.com/dotnet/corefx/blob/b90532bc97b07234a7d18073819d019645285f1c/src/System.Linq/src/System/Linq/Grouping.cs#L64    internal class Grouping<TKey, TElement> : IGrouping<TKey, TElement>, IList<TElement>    {        internal TKey _key;        internal int _hashCode;        internal TElement[] _elements;        internal int _count;        internal Grouping<TKey, TElement> _hashNext;        internal Grouping<TKey, TElement> _next;        internal Grouping()        {        }        internal void Add(TElement element)        {            if (_elements.Length == _count)            {                Array.Resize(ref _elements, checked(_count * 2));            }            _elements[_count] = element;            _count++;        }        internal void Trim()        {            if (_elements.Length != _count)            {                Array.Resize(ref _elements, _count);            }        }        public IEnumerator<TElement> GetEnumerator()        {            for (int i = 0; i < _count; i++)            {                yield return _elements[i];            }        }        IEnumerator IEnumerable.GetEnumerator()        {            return GetEnumerator();        }        // DDB195907: implement IGrouping<>.Key implicitly        // so that WPF binding works on this property.        public TKey Key        {            get { return _key; }        }        int ICollection<TElement>.Count        {            get { return _count; }        }        bool ICollection<TElement>.IsReadOnly        {            get { return true; }        }        void ICollection<TElement>.Add(TElement item)        {            throw new NotSupportedException(Strings.NOT_SUPPORTED);        }        void ICollection<TElement>.Clear()        {            throw new NotSupportedException(Strings.NOT_SUPPORTED);        }        bool ICollection<TElement>.Contains(TElement item)        {            return Array.IndexOf(_elements, item, 0, _count) >= 0;        }        void ICollection<TElement>.CopyTo(TElement[] array, int arrayIndex)        {            Array.Copy(_elements, 0, array, arrayIndex, _count);        }        bool ICollection<TElement>.Remove(TElement item)        {            throw new NotSupportedException(Strings.NOT_SUPPORTED);        }        int IList<TElement>.IndexOf(TElement item)        {            return Array.IndexOf(_elements, item, 0, _count);        }        void IList<TElement>.Insert(int index, TElement item)        {            throw new NotSupportedException(Strings.NOT_SUPPORTED);        }        void IList<TElement>.RemoveAt(int index)        {            throw new NotSupportedException(Strings.NOT_SUPPORTED);        }        TElement IList<TElement>.this[int index]        {            get            {                if (index < 0 || index >= _count)                {                    throw new ArgumentOutOfRangeException(nameof(index));                }                return _elements[index];            }            set            {                throw new NotSupportedException(Strings.NOT_SUPPORTED);            }        }    }}
 |