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++; } }
// 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++; } }
public bool MoveNext() { if (version != dictionary.version) { ThrowHelper.ThrowInvalidOperationException_InvalidOperation_EnumFailedVersion(); } while ((uint)index < (uint)dictionary.count) { if (dictionary.entries[index].hashCode >= 0) { currentValue = dictionary.entries[index].value; index++; return(true); } index++; } index = dictionary.count + 1; currentValue = default(TValue); return(false); }
// 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.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.Sort <T>(_items, index, count, comparer); _version++; }
// 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.ThrowIndexArgumentOutOfRange_NeedNonNegNumException(); } 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++; }
object IDictionary.this[object key] { get { if (IsCompatibleKey(key)) { int i = FindEntry((TKey)key); if (i >= 0) { return(entries[i].value); } } return(null); } set { if (key == null) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key); } ThrowHelper.IfNullAndNullsAreIllegalThenThrow <TValue>(value, ExceptionArgument.value); try { TKey tempKey = (TKey)key; try { this[tempKey] = (TValue)value; } catch (InvalidCastException) { ThrowHelper.ThrowWrongValueTypeArgumentException(value, typeof(TValue)); } } catch (InvalidCastException) { ThrowHelper.ThrowWrongKeyTypeArgumentException(key, typeof(TKey)); } } }
void IDictionary.Add(object key, object value) { if (key == null) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key); } ThrowHelper.IfNullAndNullsAreIllegalThenThrow <TValue>(value, ExceptionArgument.value); try { TKey tempKey = (TKey)key; try { Add(tempKey, (TValue)value); } catch (InvalidCastException) { ThrowHelper.ThrowWrongValueTypeArgumentException(value, typeof(TValue)); } } catch (InvalidCastException) { ThrowHelper.ThrowWrongKeyTypeArgumentException(key, typeof(TKey)); } }
// Constructs a List, copying the contents of the given collection. The // size and capacity of the new list will both be equal to the size of the // given collection. // public List(IEnumerable <T> collection) { if (collection == null) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.collection); } Contract.EndContractBlock(); ICollection <T> c = collection as ICollection <T>; if (c != null) { int count = c.Count; if (count == 0) { _items = _emptyArray; } else { _items = new T[count]; c.CopyTo(_items, 0); _size = count; } } else { _size = 0; _items = _emptyArray; // This enumerable could be empty. Let Add allocate a new array, if needed. // Note it will also go to _defaultCapacity first, not 1, then 2, etc. using (IEnumerator <T> en = collection.GetEnumerator()) { while (en.MoveNext()) { Add(en.Current); } } } }
public virtual void GetObjectData(SerializationInfo info, StreamingContext context) { if (info == null) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.info); } info.AddValue(VersionName, version); #if FEATURE_RANDOMIZED_STRING_HASHING info.AddValue(ComparerName, HashHelpers.GetEqualityComparerForSerialization(comparer), typeof(IEqualityComparer <TKey>)); #else info.AddValue(ComparerName, comparer, typeof(IEqualityComparer <TKey>)); #endif info.AddValue(HashSizeName, buckets == null ? 0 : buckets.Length); //This is the length of the bucket array. if (buckets != null) { KeyValuePair <TKey, TValue>[] array = new KeyValuePair <TKey, TValue> [Count]; CopyTo(array, 0); info.AddValue(KeyValuePairsName, array, typeof(KeyValuePair <TKey, TValue>[])); } }
public virtual void OnDeserialization(object sender) { if (this.m_siInfo != null) { int num = this.m_siInfo.GetInt32("Version"); int num2 = this.m_siInfo.GetInt32("HashSize"); this.comparer = (IEqualityComparer <TKey>) this.m_siInfo.GetValue("Comparer", typeof(IEqualityComparer <TKey>)); if (num2 != 0) { this.buckets = new int[num2]; for (int i = 0; i < this.buckets.Length; i++) { this.buckets[i] = -1; } this.entries = new Entry <TKey, TValue> [num2]; this.freeList = -1; KeyValuePair <TKey, TValue>[] pairArray = (KeyValuePair <TKey, TValue>[]) this.m_siInfo.GetValue("KeyValuePairs", typeof(KeyValuePair <TKey, TValue>[])); if (pairArray == null) { ThrowHelper.ThrowSerializationException(ExceptionResource.Serialization_MissingKeyValuePairs); } for (int j = 0; j < pairArray.Length; j++) { if (pairArray[j].Key == null) { ThrowHelper.ThrowSerializationException(ExceptionResource.Serialization_NullKey); } this.Insert(pairArray[j].Key, pairArray[j].Value, true); } } else { this.buckets = null; } this.version = num; this.m_siInfo = null; } }
public bool Remove(TKey key) { if (key == null) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key); } if (this.buckets != null) { int num = this.comparer.GetHashCode(key) & 0x7fffffff; int index = num % this.buckets.Length; int num3 = -1; for (int i = this.buckets[index]; i >= 0; i = this.entries[i].next) { if ((this.entries[i].hashCode == num) && this.comparer.Equals(this.entries[i].key, key)) { if (num3 < 0) { this.buckets[index] = this.entries[i].next; } else { this.entries[num3].next = this.entries[i].next; } this.entries[i].hashCode = -1; this.entries[i].next = this.freeList; this.entries[i].key = default(TKey); this.entries[i].value = default(TValue); this.freeList = i; this.freeCount++; this.version++; return(true); } num3 = i; } } return(false); }
public bool Remove(TKey key) { if (key == null) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key); } if (buckets != null) { int hashCode = comparer.GetHashCode(key) & 0x7FFFFFFF; int bucket = hashCode % buckets.Length; int last = -1; for (int i = buckets[bucket]; i >= 0; last = i, i = entries[i].next) { if (entries[i].hashCode == hashCode && comparer.Equals(entries[i].key, key)) { if (last < 0) { buckets[bucket] = entries[i].next; } else { entries[last].next = entries[i].next; } entries[i].hashCode = -1; entries[i].next = freeList; entries[i].key = default(TKey); entries[i].value = default(TValue); freeList = i; freeCount++; version++; return(true); } } } return(false); }
// 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++; } }
public void ForEach(Action <T> action) { if (action == null) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match); } Contract.EndContractBlock(); int version = _version; for (int i = 0; i < _size; i++) { if (version != _version && BinaryCompatibility.TargetsAtLeast_Desktop_V4_5) { break; } action(_items[i]); } if (version != _version && BinaryCompatibility.TargetsAtLeast_Desktop_V4_5) { ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumFailedVersion); } }
public void ForEach(Action <T> action) { if (action == null) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.action); } Contract.EndContractBlock(); int version = _version; for (int i = 0; i < _size; i++) { if (version != _version) { break; } action(_items[i]); } if (version != _version) { ThrowHelper.ThrowInvalidOperationException_InvalidOperation_EnumFailedVersion(); } }
public int RemoveAll(Predicate <T> match) { if (match == null) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match); } int index = 0; while ((index < this._size) && !match(this._items[index])) { index++; } if (index >= this._size) { return(0); } int num2 = index + 1; while (num2 < this._size) { while ((num2 < this._size) && match(this._items[num2])) { num2++; } if (num2 < this._size) { this._items[index++] = this._items[num2++]; } } Array.Clear(this._items, index, this._size - index); int num3 = this._size - index; this._size = index; this._version++; return(num3); }
public bool MoveNext() { if (version != dictionary.version) { ThrowHelper.ThrowInvalidOperationException_InvalidOperation_EnumFailedVersion(); } // Use unsigned comparison since we set index to dictionary.count+1 when the enumeration ends. // dictionary.count+1 could be negative if dictionary.count is Int32.MaxValue while ((uint)index < (uint)dictionary.count) { if (dictionary.entries[index].hashCode >= 0) { current = new KeyValuePair <TKey, TValue>(dictionary.entries[index].key, dictionary.entries[index].value); index++; return(true); } index++; } index = dictionary.count + 1; current = new KeyValuePair <TKey, TValue>(); return(false); }
bool ICollection <TKey> .Remove(TKey item) { ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_KeyCollectionSet); return(false); }
void ICollection <TKey> .Clear() { ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_KeyCollectionSet); }
void ICollection <TKey> .Add(TKey item) { ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_KeyCollectionSet); }
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); } } }
private void Insert(TKey key, TValue value, bool add) { if (key == null) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key); } if (buckets == null) { Initialize(0); } if (this.isSimpleKey) { if (this.simpleBuckets.HasOwnProperty(key)) { if (add) { ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_AddingDuplicate); } entries[GetBucket(this.simpleBuckets, key)].value = value; version++; return; } int simpleIndex; if (freeCount > 0) { simpleIndex = freeList; freeList = entries[simpleIndex].next; freeCount--; } else { if (count == entries.Length) { Resize(); } simpleIndex = count; count++; } entries[simpleIndex].hashCode = 1; entries[simpleIndex].next = -1; entries[simpleIndex].key = key; entries[simpleIndex].value = value; SetBucket(simpleBuckets, key, simpleIndex); version++; return; } int hashCode = comparer.GetHashCode(key) & 0x7FFFFFFF; int targetBucket = hashCode % buckets.Length; for (int i = buckets[targetBucket]; i >= 0; i = entries[i].next) { if (entries[i].hashCode == hashCode && comparer.Equals(entries[i].key, key)) { if (add) { ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_AddingDuplicate); } entries[i].value = value; version++; return; } } int index; if (freeCount > 0) { index = freeList; freeList = entries[index].next; freeCount--; } else { if (count == entries.Length) { Resize(); targetBucket = hashCode % buckets.Length; } index = count; count++; } entries[index].hashCode = hashCode; entries[index].next = buckets[targetBucket]; entries[index].key = key; entries[index].value = value; buckets[targetBucket] = index; version++; }
int IEqualityComparer.GetHashCode(object obj) { if (obj == null) return 0; if (obj is T) return GetHashCode((T)obj); ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidArgumentForComparison); return 0; }
bool ICollection <TValue> .Remove(TValue item) { ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ValueCollectionSet); return(false); }
void ICollection <TValue> .Clear() { ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ValueCollectionSet); }
private void Insert(TKey key, TValue value, bool add) { if (key == null) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key); } if (buckets == null) { Initialize(0); } int hashCode = comparer.GetHashCode(key) & 0x7FFFFFFF; int targetBucket = hashCode % buckets.Length; #if FEATURE_RANDOMIZED_STRING_HASHING int collisionCount = 0; #endif for (int i = buckets[targetBucket]; i >= 0; i = entries[i].next) { if (entries[i].hashCode == hashCode && comparer.Equals(entries[i].key, key)) { if (add) { ThrowHelper.ThrowAddingDuplicateWithKeyArgumentException(key); } entries[i].value = value; version++; return; } #if FEATURE_RANDOMIZED_STRING_HASHING collisionCount++; #endif } int index; if (freeCount > 0) { index = freeList; freeList = entries[index].next; freeCount--; } else { if (count == entries.Length) { Resize(); targetBucket = hashCode % buckets.Length; } index = count; count++; } entries[index].hashCode = hashCode; entries[index].next = buckets[targetBucket]; entries[index].key = key; entries[index].value = value; buckets[targetBucket] = index; version++; #if FEATURE_RANDOMIZED_STRING_HASHING // In case we hit the collision threshold we'll need to switch to the comparer which is using randomized string hashing // in this case will be EqualityComparer<string>.Default. // Note, randomized string hashing is turned on by default on coreclr so EqualityComparer<string>.Default will // be using randomized string hashing if (collisionCount > HashHelpers.HashCollisionThreshold && comparer == NonRandomizedStringEqualityComparer.Default) { comparer = (IEqualityComparer <TKey>)EqualityComparer <string> .Default; Resize(entries.Length, true); } #endif }