Esempio n. 1
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);
     }
     Contract.EndContractBlock();
     if (_size == _items.Length)
     {
         EnsureCapacity(_size + 1);
     }
     if (index < _size)
     {
         Array.Copy(_items, index, _items, index + 1, _size - index);
     }
     _items[index] = item;
     _size++;
     _version++;
 }
Esempio n. 2
0
        // Reverses the elements in a range of this list. Following a call to this
        // method, an element in the range given by index and count
        // which was previously located at index i will now be located at
        // index index + (index + count - i - 1).
        //
        // This method uses the Array.Reverse method to reverse the
        // elements.
        //
        public void Reverse(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);
            }
            Contract.EndContractBlock();
            Array.Reverse(_items, index, count);
            _version++;
        }
Esempio n. 3
0
 public T this[int index]
 {
     get
     {
         if (index >= this._size)
         {
             ThrowHelper.ThrowArgumentOutOfRangeException();
         }
         return(this._items[index]);
     }
     set
     {
         if (index >= this._size)
         {
             ThrowHelper.ThrowArgumentOutOfRangeException();
         }
         this._items[index] = value;
         this._version++;
     }
 }
Esempio n. 4
0
        public int FindLastIndex(int startIndex, int count, Predicate <T> match)
        {
            if (match == null)
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match);
            }

            if (_size == 0)
            {
                // Special case for 0 length List
                if (startIndex != -1)
                {
                    ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.startIndex, ExceptionResource.ArgumentOutOfRange_Index);
                }
            }
            else
            {
                // Make sure we're not out of range
                if ((uint)startIndex >= (uint)_size)
                {
                    ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.startIndex, ExceptionResource.ArgumentOutOfRange_Index);
                }
            }

            // 2nd have of this also catches when startIndex == MAXINT, so MAXINT - 0 + 1 == -1, which is < 0.
            if (count < 0 || startIndex - count + 1 < 0)
            {
                ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_Count);
            }

            int endIndex = startIndex - count;

            for (int i = startIndex; i > endIndex; i--)
            {
                if (match(_items[i]))
                {
                    return(i);
                }
            }
            return(-1);
        }
Esempio n. 5
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.ThrowArgumentOutOfRangeException();
                }
                Contract.EndContractBlock();
                return(_items[index]);
            }

            set {
                if ((uint)index >= (uint)_size)
                {
                    ThrowHelper.ThrowArgumentOutOfRangeException();
                }
                Contract.EndContractBlock();
                _items[index] = value;
                _version++;
            }
        }
Esempio n. 6
0
        // Sorts the elements in a section of this list. The sort compares the
        // elements to each other using the given IComparer interface. If
        // comparer is null, the elements are compared to each other using
        // the IComparable interface, which in that case must be implemented by all
        // elements of the list.
        //
        // This method uses the Array.Sort method to sort the elements.
        //
        public void Sort(int index, int count, IComparer <T> comparer)
        {
            if (index < 0)
            {
                ThrowHelper.ThrowIndexArgumentOutOfRange_NeedNonNegNumException();
            }

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

            if (_size - index < count)
            {
                ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen);
            }
            Contract.EndContractBlock();

            Array.Sort <T>(_items, index, count, comparer);
            _version++;
        }
Esempio n. 7
0
 public void RemoveRange(int index, int count)
 {
     if ((index < 0) || (count < 0))
     {
         ThrowHelper.ThrowArgumentOutOfRangeException((index < 0) ? ExceptionArgument.index : ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
     }
     if ((this._size - index) < count)
     {
         ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen);
     }
     if (count > 0)
     {
         this._size -= count;
         if (index < this._size)
         {
             Array.Copy(this._items, index + count, this._items, index, this._size - index);
         }
         Array.Clear(this._items, this._size, count);
         this._version++;
     }
 }
Esempio n. 8
0
        // Copies the values in this SortedList to an array.
        void ICollection <KeyValuePair <TKey, TValue> > .CopyTo(KeyValuePair <TKey, TValue>[] array, int arrayIndex)
        {
            if (array == null)
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
            }

            if (arrayIndex < 0 || arrayIndex > array.Length)
            {
                ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.arrayIndex, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
            }

            if (array.Length - arrayIndex < Count)
            {
                ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_ArrayPlusOffTooSmall);
            }

            for (int i = 0; i < Count; i++)
            {
                KeyValuePair <TKey, TValue> entry = new KeyValuePair <TKey, TValue>(keys[i], values[i]);
                array[arrayIndex + i] = entry;
            }
        }
Esempio n. 9
0
        public Net40List <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);
            }

            Net40List <T> list = new Net40List <T>(count);

            Array.Copy(_items, index, list._items, 0, count);
            list._size = count;
            return(list);
        }
Esempio n. 10
0
        // Reverses the elements in a range of this list. Following a call to this
        // method, an element in the range given by index and count
        // which was previously located at index i will now be located at
        // index index + (index + count - i - 1).
        //
        public void Reverse(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);
            }
            Contract.EndContractBlock();

            // The non-generic Array.Reverse is not used because it does not perform
            // well for non-primitive value types.
            // If/when a generic Array.Reverse<T> becomes available, the below code
            // can be deleted and replaced with a call to Array.Reverse<T>.
            int i = index;
            int j = index + count - 1;

            T[] array = _items;
            while (i < j)
            {
                T temp = array[i];
                array[i] = array[j];
                array[j] = temp;
                i++;
                j--;
            }

            _version++;
        }
Esempio n. 11
0
        // Removes a range of elements from this list.
        //
        public void RemoveRange(int index, int count)
        {
            if (index < 0 || count < 0)
            {
                ThrowHelper.ThrowArgumentOutOfRangeException((index < 0 ? ExceptionArgument.index : ExceptionArgument.count), ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
            }

            if (_size - index < count)
            {
                ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen);
            }

            if (count > 0)
            {
                int i = _size;
                _size -= count;
                if (index < _size)
                {
                    Array.Copy(_items, index + count, _items, index, _size - index);
                }
                Array.Clear(_items, _size, count);
                _version++;
            }
        }
Esempio n. 12
0
 public int LastIndexOf(T item, int index, int count)
 {
     if (this.Count != 0 && index < 0)
     {
         ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
     }
     if (this.Count != 0 && count < 0)
     {
         ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
     }
     if (this._size == 0)
     {
         return(-1);
     }
     if (index >= this._size)
     {
         ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_BiggerThanCollection);
     }
     if (count > index + 1)
     {
         ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_BiggerThanCollection);
     }
     return(Array.LastIndexOf <T>(this._items, item, index, count));
 }
Esempio n. 13
0
 void ICollection.CopyTo(Array array, int index)
 {
     if (array == null)
     {
         ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
     }
     if (array.Rank != 1)
     {
         ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_RankMultiDimNotSupported);
     }
     if (array.GetLowerBound(0) != 0)
     {
         ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_NonZeroLowerBound);
     }
     if ((index < 0) || (index > array.Length))
     {
         ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
     }
     if ((array.Length - index) < this.Count)
     {
         ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_ArrayPlusOffTooSmall);
     }
     KeyValuePair <TKey, TValue>[] pairArray = array as KeyValuePair <TKey, TValue>[];
     if (pairArray != null)
     {
         this.CopyTo(pairArray, index);
     }
     else if (array is DictionaryEntry[])
     {
         DictionaryEntry[]      entryArray = array as DictionaryEntry[];
         Entry <TKey, TValue>[] entries    = this.entries;
         for (int i = 0; i < this.count; i++)
         {
             if (entries[i].hashCode >= 0)
             {
                 entryArray[index++] = new DictionaryEntry(entries[i].key, entries[i].value);
             }
         }
     }
     else
     {
         object[] objArray = array as object[];
         if (objArray == null)
         {
             ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidArrayType);
         }
         try
         {
             int count = this.count;
             Entry <TKey, TValue>[] entryArray3 = this.entries;
             for (int j = 0; j < count; j++)
             {
                 if (entryArray3[j].hashCode >= 0)
                 {
                     objArray[index++] = new KeyValuePair <TKey, TValue>(entryArray3[j].key, entryArray3[j].value);
                 }
             }
         }
         catch (ArrayTypeMismatchException)
         {
             ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidArrayType);
         }
     }
 }