Esempio n. 1
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.ThrowArgumentOutOfRange_IndexException();
            }
            Contract.EndContractBlock();

            ICollection <T> c = collection as ICollection <T>;

            if (c != null)       // if collection is ICollection<T>
            {
                int count = c.Count;
                if (count > 0)
                {
                    EnsureCapacity(_size + count);
                    if (index < _size)
                    {
                        Array.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
                        Array.Copy(_items, 0, _items, index, index);
                        // Copy last part of _items back to inserted location
                        Array.Copy(_items, index + count, _items, index * 2, _size - index);
                    }
                    else
                    {
                        c.CopyTo(_items, index);
                    }
                    _size += count;
                }
            }
            else
            {
                using (IEnumerator <T> en = collection.GetEnumerator()) {
                    while (en.MoveNext())
                    {
                        Insert(index++, en.Current);
                    }
                }
            }
            _version++;
        }
Esempio n. 2
0
 public void RemoveAt(int index)
 {
     if ((uint)index >= (uint)this._size)
     {
         ThrowHelper.ThrowArgumentOutOfRange_IndexException();
     }
     this._size = this._size - 1;
     if (index < this._size)
     {
         Array.Copy((Array)this._items, index + 1, (Array)this._items, index, this._size - index);
     }
     this._items[this._size] = default(T);
     this._version           = this._version + 1;
 }
Esempio n. 3
0
        // Returns the index of the first occurrence of a given value in a range of
        // this list. The list is searched forwards, starting at index
        // index and upto count number of elements. The
        // elements of the list are compared to the given value using the
        // Object.Equals method.
        //
        // This method uses the Array.IndexOf method to perform the
        // search.
        //
        public int IndexOf(T item, int index, int count)
        {
            if (index > _size)
            {
                ThrowHelper.ThrowArgumentOutOfRange_IndexException();
            }

            if (count < 0 || index > _size - count)
            {
                ThrowHelper.ThrowCountArgumentOutOfRange_ArgumentOutOfRange_Count();
            }

            return(Array.IndexOf(_items, item, index, count));
        }
Esempio n. 4
0
 // Removes the element at the given index. The size of the list is
 // decreased by one.
 //
 public void RemoveAt(int index)
 {
     if ((uint)index >= (uint)_size)
     {
         ThrowHelper.ThrowArgumentOutOfRange_IndexException();
     }
     Contract.EndContractBlock();
     _size--;
     if (index < _size)
     {
         Array.Copy(_items, index + 1, _items, index, _size - index);
     }
     _items[_size] = default(T);
     _version++;
 }
Esempio n. 5
0
        // Returns the index of the first occurrence of a given value in a range of
        // this list. The list is searched forwards, starting at index
        // index and upto count number of elements. The
        // elements of the list are compared to the given value using the
        // Object.Equals method.
        //
        // This method uses the Array.IndexOf method to perform the
        // search.
        //
        public int IndexOf(T item, int index, int count)
        {
            if (index > _size)
            {
                ThrowHelper.ThrowArgumentOutOfRange_IndexException();
            }

            if (count < 0 || index > _size - count)
            {
                ThrowHelper.ThrowCountArgumentOutOfRange_ArgumentOutOfRange_Count();
            }
            Contract.Ensures(Contract.Result <int>() >= -1);
            Contract.Ensures(Contract.Result <int>() < Count);
            Contract.EndContractBlock();

            return(Array.IndexOf(_items, item, index, count));
        }
Esempio n. 6
0
 // Removes the element at the given index. The size of the list is
 // decreased by one.
 public void RemoveAt(int index)
 {
     if ((uint)index >= (uint)_size)
     {
         ThrowHelper.ThrowArgumentOutOfRange_IndexException();
     }
     _size--;
     if (index < _size)
     {
         Array.Copy(_items, index + 1, _items, index, _size - index);
     }
     if (RuntimeHelpers.IsReferenceOrContainsReferences <T>())
     {
         _items[_size] = default(T);
     }
     _version++;
 }
Esempio n. 7
0
 public void RemoveAt(int index)
 {
     if ((uint)index >= (uint)this._size)
     {
         ThrowHelper.ThrowArgumentOutOfRange_IndexException();
     }
     --this._size;
     if (index < this._size)
     {
         Array.Copy((Array)this._items, index + 1, (Array)this._items, index, this._size - index);
     }
     if (RuntimeHelpers.IsReferenceOrContainsReferences <T>())
     {
         this._items[this._size] = default(T);
     }
     ++this._version;
 }
Esempio n. 8
0
 public T this[int index]
 {
     [MethodImpl(MethodImplOptions.AggressiveInlining)] get
     {
         if ((uint)index >= (uint)this._size)
         {
             ThrowHelper.ThrowArgumentOutOfRange_IndexException();
         }
         return(Array.UnsafeLoad <T>(this._items, index));
     }
     set
     {
         if ((uint)index >= (uint)this._size)
         {
             ThrowHelper.ThrowArgumentOutOfRange_IndexException();
         }
         this._items[index] = value;
         this._version      = this._version + 1;
     }
 }
Esempio n. 9
0
 public T this[int index]
 {
     get
     {
         if ((uint)index >= (uint)this._size)
         {
             ThrowHelper.ThrowArgumentOutOfRange_IndexException();
         }
         return(this._items[index]);
     }
     set
     {
         if ((uint)index >= (uint)this._size)
         {
             ThrowHelper.ThrowArgumentOutOfRange_IndexException();
         }
         this._items[index] = value;
         ++this._version;
     }
 }
Esempio n. 10
0
 public void InsertRange(int index, IEnumerable <T> collection)
 {
     if (collection == null)
     {
         ThrowHelper.ThrowArgumentNullException(ExceptionArgument.collection);
     }
     if ((uint)index > (uint)this._size)
     {
         ThrowHelper.ThrowArgumentOutOfRange_IndexException();
     }
     if (collection is ICollection <T> objs)
     {
         int count = objs.Count;
         if (count > 0)
         {
             this.EnsureCapacity(this._size + count);
             if (index < this._size)
             {
                 Array.Copy((Array)this._items, index, (Array)this._items, index + count, this._size - index);
             }
             if (this == objs)
             {
                 Array.Copy((Array)this._items, 0, (Array)this._items, index, index);
                 Array.Copy((Array)this._items, index + count, (Array)this._items, index * 2, this._size - index);
             }
             else
             {
                 objs.CopyTo(this._items, index);
             }
             this._size += count;
         }
     }
     else
     {
         foreach (T obj in collection)
         {
             this.Insert(index++, obj);
         }
     }
     ++this._version;
 }
Esempio n. 11
0
        // Sets or Gets the element at the given index.
        //
        public T this[int index] {
            get {
                // Following trick can reduce the range check by one
                if ((uint)index >= (uint)_size)
                {
                    ThrowHelper.ThrowArgumentOutOfRange_IndexException();
                }
                Contract.EndContractBlock();
                return(_items[index]);
            }

            set {
                if ((uint)index >= (uint)_size)
                {
                    ThrowHelper.ThrowArgumentOutOfRange_IndexException();
                }
                Contract.EndContractBlock();
                _items[index] = value;
                _version++;
            }
        }