Exemple #1
0
        public ClusteredList <T> GetRange(int index, int count)
        {
            if (index < 0)
            {
                ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
            }

            if (count < 0)
            {
                ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
            }

            if (_size - index < count)
            {
                ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen);
            }
#if DEBUG
            Contract.Ensures(Contract.Result <ClusteredList <T> >() != null);
            Contract.EndContractBlock();
#endif
            ClusteredList <T> list = new ClusteredList <T>(count);
            ClusteredArray <T> .Copy(_items, index, list._items, 0, count);

            list._size = count;
            return(list);
        }
Exemple #2
0
        // Removes a range of elements from this list.
        //
        public void RemoveRange(int index, int count)
        {
            if (index < 0)
            {
                ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
            }

            if (count < 0)
            {
                ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
            }

            if (_size - index < count)
            {
                ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen);
            }
#if DEBUG
            Contract.EndContractBlock();
#endif
            if (count > 0)
            {
                int i = _size;
                _size -= count;
                if (index < _size)
                {
                    ClusteredArray <T> .Copy(_items, index + count, _items, index, _size - index);
                }
                ClusteredArray <T> .Clear(_items, _size, count);

                _version++;
            }
        }
Exemple #3
0
        public object Clone()
        {
            ClusteredArray <T> newarray = new ClusteredArray <T>(_length);

            ClusteredArray <T> .Copy(this, 0, newarray, 0, _length);

            return(newarray);
        }
Exemple #4
0
        // Inserts the elements of the given collection at a given index. If
        // required, the capacity of the list is increased to twice the previous
        // capacity or the new size, whichever is larger.  Ranges may be added
        // to the end of the list by setting index to the List's size.
        //
        public void InsertRange(int index, IEnumerable <T> collection)
        {
            if (collection == null)
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.collection);
            }

            if ((uint)index > (uint)_size)
            {
                ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_Index);
            }
#if DEBUG
            Contract.EndContractBlock();
#endif
            ICollection <T> c = collection as ICollection <T>;
            if (c != null)
            {
                int count = c.Count;
                if (count > 0)
                {
                    EnsureCapacity(_size + count);
                    if (index < _size)
                    {
                        ClusteredArray <T> .Copy(_items, index, _items, index + count, _size - index);
                    }

                    // If we're inserting a List into itself, we want to be able to deal with that.
                    if (this == c)
                    {
                        // Copy first part of _items to insert location
                        ClusteredArray <T> .Copy(_items, 0, _items, index, index);

                        // Copy last part of _items back to inserted location
                        ClusteredArray <T> .Copy(_items, index + count, _items, index * 2, _size - index);
                    }
                    else
                    {
                        T[] itemsToInsert = new T[count];
                        c.CopyTo(itemsToInsert, 0);
                        _items.CopyFrom(itemsToInsert, 0, index, count);
                    }
                    _size += count;
                }
            }
            else
            {
                using (IEnumerator <T> en = collection.GetEnumerator())
                {
                    while (en.MoveNext())
                    {
                        Insert(index++, en.Current);
                    }
                }
            }
            _version++;
        }
Exemple #5
0
        // Removes the element at the given index. The size of the list is
        // decreased by one.
        //
        public void RemoveAt(int index)
        {
#if DEBUG
            Contract.EndContractBlock();
#endif
            _size--;
            if (index < _size)
            {
                ClusteredArray <T> .Copy(_items, index + 1, _items, index, _size - index);
            }
            _items[_size] = default(T);
            _version++;
        }
Exemple #6
0
        // Inserts an element into this list at a given index. The size of the list
        // is increased by one. If required, the capacity of the list is doubled
        // before inserting the new element.
        //
        public void Insert(int index, T item)
        {
            // Note that insertions at the end are legal.
            if ((uint)index > (uint)_size)
            {
                ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_ListInsert);
            }
#if DEBUG
            Contract.EndContractBlock();
#endif
            if (_size == _items.Length)
            {
                EnsureCapacity(_size + 1);
            }
            if (index < _size)
            {
                ClusteredArray <T> .Copy(_items, index, _items, index + 1, _size - index);
            }
            _items[index] = item;
            _size++;
            _version++;
        }
Exemple #7
0
        // PRIVATE Grows or shrinks the buffer to hold capacity objects. Capacity
        // must be >= _size.
        private void SetCapacity(int capacity)
        {
            ClusteredArray <T> newarray = new ClusteredArray <T>(capacity);

            if (_size > 0)
            {
                if (_head < _tail)
                {
                    ClusteredArray <T> .Copy(_array, _head, newarray, 0, _size);
                }
                else
                {
                    ClusteredArray <T> .Copy(_array, _head, newarray, 0, _array.Length - _head);

                    ClusteredArray <T> .Copy(_array, 0, newarray, _array.Length - _head, _tail);
                }
            }

            _array = newarray;
            _head  = 0;
            _tail  = (_size == capacity) ? 0 : _size;
            _version++;
        }