Ejemplo n.º 1
0
        public void Add(TSource item)
        {
            if (!_bufferOwner.IsValid)
            {
                _bufferOwner = new BufferOwner <TSource>(MinimumCapacity);
            }
            if (_itemsCount >= _bufferOwner.Buffer.Length)
            {
                _bufferOwner.GrowDoubleSize();
            }

            _bufferOwner.Buffer[_itemsCount++] = item;
        }
Ejemplo n.º 2
0
        public ListPool(IEnumerable <TSource> source)
        {
            if (source is ICollection <TSource> collection)
            {
                _bufferOwner = new BufferOwner <TSource>(collection.Count);
                _itemsCount  = collection.Count;

                collection.CopyTo(_bufferOwner.Buffer, 0);
            }
            else
            {
                _bufferOwner = new BufferOwner <TSource>(MinimumCapacity);
                _itemsCount  = 0;

                using IEnumerator <TSource> enumerator = source.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    Add(enumerator.Current);
                }
            }
        }
Ejemplo n.º 3
0
        public void Insert(int index, TSource item)
        {
            if (index < 0 || index > _itemsCount)
            {
                throw new ArgumentOutOfRangeException(nameof(index));
            }
            if (!_bufferOwner.IsValid)
            {
                _bufferOwner = new BufferOwner <TSource>(MinimumCapacity);
            }
            if (index >= _bufferOwner.Buffer.Length)
            {
                _bufferOwner.GrowDoubleSize();
            }
            if (index < _itemsCount)
            {
                Array.Copy(_bufferOwner.Buffer, index, _bufferOwner.Buffer, index + 1, _itemsCount - index);
            }

            _bufferOwner.Buffer[index] = item;
            _itemsCount++;
        }
Ejemplo n.º 4
0
 public ListPool(int length)
 {
     _bufferOwner = new BufferOwner <TSource>(length < MinimumCapacity ? MinimumCapacity : length);
     _itemsCount  = 0;
 }