diff options
Diffstat (limited to 'src/mscorlib/src/System/Collections/Generic/Dictionary.cs')
-rw-r--r-- | src/mscorlib/src/System/Collections/Generic/Dictionary.cs | 101 |
1 files changed, 52 insertions, 49 deletions
diff --git a/src/mscorlib/src/System/Collections/Generic/Dictionary.cs b/src/mscorlib/src/System/Collections/Generic/Dictionary.cs index 9cbfff5a57..c2b2da9ad2 100644 --- a/src/mscorlib/src/System/Collections/Generic/Dictionary.cs +++ b/src/mscorlib/src/System/Collections/Generic/Dictionary.cs @@ -91,12 +91,12 @@ namespace System.Collections.Generic { if (capacity > 0) Initialize(capacity); this.comparer = comparer ?? EqualityComparer<TKey>.Default; -#if FEATURE_RANDOMIZED_STRING_HASHING && FEATURE_CORECLR +#if FEATURE_RANDOMIZED_STRING_HASHING if (HashHelpers.s_UseRandomizedStringHashing && comparer == EqualityComparer<string>.Default) { this.comparer = (IEqualityComparer<TKey>) NonRandomizedStringEqualityComparer.Default; } -#endif // FEATURE_RANDOMIZED_STRING_HASHING && FEATURE_CORECLR +#endif // FEATURE_RANDOMIZED_STRING_HASHING } public Dictionary(IDictionary<TKey,TValue> dictionary): this(dictionary, null) {} @@ -129,6 +129,21 @@ namespace System.Collections.Generic { } } + public Dictionary(IEnumerable<KeyValuePair<TKey, TValue>> collection): + this(collection, null) { } + + public Dictionary(IEnumerable<KeyValuePair<TKey, TValue>> collection, IEqualityComparer<TKey> comparer): + this((collection as ICollection<KeyValuePair<TKey, TValue>>)?.Count ?? 0, comparer) + { + if (collection == null) { + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.collection); + } + + foreach (KeyValuePair<TKey, TValue> pair in collection) { + Add(pair.Key, pair.Value); + } + } + protected Dictionary(SerializationInfo info, StreamingContext context) { //We can't do anything with the keys and values until the entire graph has been deserialized //and we have a resonable estimate that GetHashCode is not going to fail. For the time being, @@ -263,7 +278,7 @@ namespace System.Collections.Generic { } if (index < 0 || index > array.Length ) { - ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum); + ThrowHelper.ThrowIndexArgumentOutOfRange_NeedNonNegNumException(); } if (array.Length - index < Count) { @@ -287,7 +302,6 @@ namespace System.Collections.Generic { return new Enumerator(this, Enumerator.KeyValuePair); } - [System.Security.SecurityCritical] // auto-generated_required public virtual void GetObjectData(SerializationInfo info, StreamingContext context) { if (info==null) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.info); @@ -347,11 +361,7 @@ namespace System.Collections.Generic { for (int i = buckets[targetBucket]; i >= 0; i = entries[i].next) { if (entries[i].hashCode == hashCode && comparer.Equals(entries[i].key, key)) { if (add) { -#if FEATURE_CORECLR ThrowHelper.ThrowAddingDuplicateWithKeyArgumentException(key); -#else - ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_AddingDuplicate); -#endif } entries[i].value = value; version++; @@ -386,8 +396,6 @@ namespace System.Collections.Generic { version++; #if FEATURE_RANDOMIZED_STRING_HASHING - -#if FEATURE_CORECLR // 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 @@ -398,14 +406,6 @@ namespace System.Collections.Generic { comparer = (IEqualityComparer<TKey>) EqualityComparer<string>.Default; Resize(entries.Length, true); } -#else - if(collisionCount > HashHelpers.HashCollisionThreshold && HashHelpers.IsWellKnownEqualityComparer(comparer)) - { - comparer = (IEqualityComparer<TKey>) HashHelpers.GetRandomizedEqualityComparer(comparer); - Resize(entries.Length, true); - } -#endif // FEATURE_CORECLR - #endif } @@ -459,7 +459,7 @@ namespace System.Collections.Generic { } private void Resize(int newSize, bool forceNewHashCodes) { - Contract.Assert(newSize >= entries.Length); + Debug.Assert(newSize >= entries.Length); int[] newBuckets = new int[newSize]; for (int i = 0; i < newBuckets.Length; i++) newBuckets[i] = -1; Entry[] newEntries = new Entry[newSize]; @@ -523,16 +523,19 @@ namespace System.Collections.Generic { return false; } - // This is a convenience method for the internal callers that were converted from using Hashtable. - // Many were combining key doesn't exist and key exists but null value (for non-value types) checks. - // This allows them to continue getting that behavior with minimal code delta. This is basically - // TryGetValue without the out param - internal TValue GetValueOrDefault(TKey key) { + // Method similar to TryGetValue that returns the value instead of putting it in an out param. + public TValue GetValueOrDefault(TKey key) => GetValueOrDefault(key, default(TValue)); + + // Method similar to TryGetValue that returns the value instead of putting it in an out param. If the entry + // doesn't exist, returns the defaultValue instead. + public TValue GetValueOrDefault(TKey key, TValue defaultValue) + { int i = FindEntry(key); - if (i >= 0) { + if (i >= 0) + { return entries[i].value; } - return default(TValue); + return defaultValue; } bool ICollection<KeyValuePair<TKey,TValue>>.IsReadOnly { @@ -557,7 +560,7 @@ namespace System.Collections.Generic { } if (index < 0 || index > array.Length) { - ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum); + ThrowHelper.ThrowIndexArgumentOutOfRange_NeedNonNegNumException(); } if (array.Length - index < Count) { @@ -580,7 +583,7 @@ namespace System.Collections.Generic { else { object[] objects = array as object[]; if (objects == null) { - ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidArrayType); + ThrowHelper.ThrowArgumentException_Argument_InvalidArrayType(); } try { @@ -593,7 +596,7 @@ namespace System.Collections.Generic { } } catch(ArrayTypeMismatchException) { - ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidArrayType); + ThrowHelper.ThrowArgumentException_Argument_InvalidArrayType(); } } } @@ -733,7 +736,7 @@ namespace System.Collections.Generic { public bool MoveNext() { if (version != dictionary.version) { - ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumFailedVersion); + ThrowHelper.ThrowInvalidOperationException_InvalidOperation_EnumFailedVersion(); } // Use unsigned comparison since we set index to dictionary.count+1 when the enumeration ends. @@ -762,7 +765,7 @@ namespace System.Collections.Generic { object IEnumerator.Current { get { if( index == 0 || (index == dictionary.count + 1)) { - ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumOpCantHappen); + ThrowHelper.ThrowInvalidOperationException_InvalidOperation_EnumOpCantHappen(); } if (getEnumeratorRetType == DictEntry) { @@ -775,7 +778,7 @@ namespace System.Collections.Generic { void IEnumerator.Reset() { if (version != dictionary.version) { - ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumFailedVersion); + ThrowHelper.ThrowInvalidOperationException_InvalidOperation_EnumFailedVersion(); } index = 0; @@ -785,7 +788,7 @@ namespace System.Collections.Generic { DictionaryEntry IDictionaryEnumerator.Entry { get { if( index == 0 || (index == dictionary.count + 1)) { - ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumOpCantHappen); + ThrowHelper.ThrowInvalidOperationException_InvalidOperation_EnumOpCantHappen(); } return new DictionaryEntry(current.Key, current.Value); @@ -795,7 +798,7 @@ namespace System.Collections.Generic { object IDictionaryEnumerator.Key { get { if( index == 0 || (index == dictionary.count + 1)) { - ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumOpCantHappen); + ThrowHelper.ThrowInvalidOperationException_InvalidOperation_EnumOpCantHappen(); } return current.Key; @@ -805,7 +808,7 @@ namespace System.Collections.Generic { object IDictionaryEnumerator.Value { get { if( index == 0 || (index == dictionary.count + 1)) { - ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumOpCantHappen); + ThrowHelper.ThrowInvalidOperationException_InvalidOperation_EnumOpCantHappen(); } return current.Value; @@ -837,7 +840,7 @@ namespace System.Collections.Generic { } if (index < 0 || index > array.Length) { - ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum); + ThrowHelper.ThrowIndexArgumentOutOfRange_NeedNonNegNumException(); } if (array.Length - index < dictionary.Count) { @@ -898,7 +901,7 @@ namespace System.Collections.Generic { } if (index < 0 || index > array.Length) { - ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum); + ThrowHelper.ThrowIndexArgumentOutOfRange_NeedNonNegNumException(); } if (array.Length - index < dictionary.Count) { @@ -912,7 +915,7 @@ namespace System.Collections.Generic { else { object[] objects = array as object[]; if (objects == null) { - ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidArrayType); + ThrowHelper.ThrowArgumentException_Argument_InvalidArrayType(); } int count = dictionary.count; @@ -923,7 +926,7 @@ namespace System.Collections.Generic { } } catch(ArrayTypeMismatchException) { - ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidArrayType); + ThrowHelper.ThrowArgumentException_Argument_InvalidArrayType(); } } } @@ -956,7 +959,7 @@ namespace System.Collections.Generic { public bool MoveNext() { if (version != dictionary.version) { - ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumFailedVersion); + ThrowHelper.ThrowInvalidOperationException_InvalidOperation_EnumFailedVersion(); } while ((uint)index < (uint)dictionary.count) { @@ -982,7 +985,7 @@ namespace System.Collections.Generic { Object System.Collections.IEnumerator.Current { get { if( index == 0 || (index == dictionary.count + 1)) { - ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumOpCantHappen); + ThrowHelper.ThrowInvalidOperationException_InvalidOperation_EnumOpCantHappen(); } return currentKey; @@ -991,7 +994,7 @@ namespace System.Collections.Generic { void System.Collections.IEnumerator.Reset() { if (version != dictionary.version) { - ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumFailedVersion); + ThrowHelper.ThrowInvalidOperationException_InvalidOperation_EnumFailedVersion(); } index = 0; @@ -1024,7 +1027,7 @@ namespace System.Collections.Generic { } if (index < 0 || index > array.Length) { - ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum); + ThrowHelper.ThrowIndexArgumentOutOfRange_NeedNonNegNumException(); } if (array.Length - index < dictionary.Count) { @@ -1085,7 +1088,7 @@ namespace System.Collections.Generic { } if (index < 0 || index > array.Length) { - ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum); + ThrowHelper.ThrowIndexArgumentOutOfRange_NeedNonNegNumException(); } if (array.Length - index < dictionary.Count) @@ -1098,7 +1101,7 @@ namespace System.Collections.Generic { else { object[] objects = array as object[]; if (objects == null) { - ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidArrayType); + ThrowHelper.ThrowArgumentException_Argument_InvalidArrayType(); } int count = dictionary.count; @@ -1109,7 +1112,7 @@ namespace System.Collections.Generic { } } catch(ArrayTypeMismatchException) { - ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidArrayType); + ThrowHelper.ThrowArgumentException_Argument_InvalidArrayType(); } } } @@ -1142,7 +1145,7 @@ namespace System.Collections.Generic { public bool MoveNext() { if (version != dictionary.version) { - ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumFailedVersion); + ThrowHelper.ThrowInvalidOperationException_InvalidOperation_EnumFailedVersion(); } while ((uint)index < (uint)dictionary.count) { @@ -1167,7 +1170,7 @@ namespace System.Collections.Generic { Object System.Collections.IEnumerator.Current { get { if( index == 0 || (index == dictionary.count + 1)) { - ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumOpCantHappen); + ThrowHelper.ThrowInvalidOperationException_InvalidOperation_EnumOpCantHappen(); } return currentValue; @@ -1176,7 +1179,7 @@ namespace System.Collections.Generic { void System.Collections.IEnumerator.Reset() { if (version != dictionary.version) { - ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumFailedVersion); + ThrowHelper.ThrowInvalidOperationException_InvalidOperation_EnumFailedVersion(); } index = 0; currentValue = default(TValue); |