Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.

Commit

Permalink
Dictionary - use EqualityComparer<TKey>.Default directly
Browse files Browse the repository at this point in the history
  • Loading branch information
benaadams committed Dec 7, 2017
1 parent a321506 commit 9876cdd
Showing 1 changed file with 20 additions and 23 deletions.
43 changes: 20 additions & 23 deletions src/mscorlib/shared/System/Collections/Generic/Dictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,14 @@ public Dictionary(int capacity, IEqualityComparer<TKey> comparer)
{
if (capacity < 0) ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.capacity);
if (capacity > 0) Initialize(capacity);
this.comparer = comparer ?? EqualityComparer<TKey>.Default;
if (comparer != EqualityComparer<TKey>.Default)
{
this.comparer = comparer;
}

if (this.comparer == EqualityComparer<string>.Default)
if (typeof(TKey) == typeof(string) && comparer == null)
{
// To start, move off default comparer for string which is randomised
this.comparer = (IEqualityComparer<TKey>)NonRandomizedStringEqualityComparer.Default;
}
}
Expand Down Expand Up @@ -139,13 +143,7 @@ protected Dictionary(SerializationInfo info, StreamingContext context)
HashHelpers.SerializationInfoTable.Add(this, info);
}

public IEqualityComparer<TKey> Comparer
{
get
{
return comparer;
}
}
public IEqualityComparer<TKey> Comparer => comparer ?? EqualityComparer<TKey>.Default;

public int Count
{
Expand Down Expand Up @@ -283,10 +281,9 @@ public bool ContainsValue(TValue value)
}
else
{
EqualityComparer<TValue> c = EqualityComparer<TValue>.Default;
for (int i = 0; i < count; i++)
{
if (entries[i].hashCode >= 0 && c.Equals(entries[i].value, value)) return true;
if (entries[i].hashCode >= 0 && (default(TValue) == null ? value.Equals(entries[i].value) : EqualityComparer<TValue>.Default.Equals(entries[i].value, value))) return true;
}
}
return false;
Expand Down Expand Up @@ -338,7 +335,7 @@ public virtual void GetObjectData(SerializationInfo info, StreamingContext conte
}

info.AddValue(VersionName, version);
info.AddValue(ComparerName, comparer, typeof(IEqualityComparer<TKey>));
info.AddValue(ComparerName, comparer ?? EqualityComparer<TKey>.Default, typeof(IEqualityComparer<TKey>));
info.AddValue(HashSizeName, buckets == null ? 0 : buckets.Length); // This is the length of the bucket array

if (buckets != null)
Expand All @@ -358,10 +355,10 @@ private int FindEntry(TKey key)

if (buckets != null)
{
int hashCode = comparer.GetHashCode(key) & 0x7FFFFFFF;
int hashCode = (comparer != null ? comparer.GetHashCode(key) : key.GetHashCode()) & 0x7FFFFFFF;
for (int i = buckets[hashCode % buckets.Length]; i >= 0; i = entries[i].next)
{
if (entries[i].hashCode == hashCode && comparer.Equals(entries[i].key, key)) return i;
if (entries[i].hashCode == hashCode && (comparer != null ? comparer.Equals(entries[i].key, key) : (default(TKey) == null ? key.Equals(entries[i].key) : EqualityComparer<TKey>.Default.Equals(entries[i].key, key)))) return i;
}
}
return -1;
Expand All @@ -384,13 +381,13 @@ private bool TryInsert(TKey key, TValue value, InsertionBehavior behavior)
}

if (buckets == null) Initialize(0);
int hashCode = comparer.GetHashCode(key) & 0x7FFFFFFF;
int hashCode = (comparer != null ? comparer.GetHashCode(key) : key.GetHashCode()) & 0x7FFFFFFF;
int targetBucket = hashCode % buckets.Length;
int collisionCount = 0;

for (int i = buckets[targetBucket]; i >= 0; i = entries[i].next)
{
if (entries[i].hashCode == hashCode && comparer.Equals(entries[i].key, key))
if (entries[i].hashCode == hashCode && (comparer != null ? comparer.Equals(entries[i].key, key) : (default(TKey) == null ? key.Equals(entries[i].key) : EqualityComparer<TKey>.Default.Equals(entries[i].key, key))))
{
if (behavior == InsertionBehavior.OverwriteExisting)
{
Expand Down Expand Up @@ -437,9 +434,9 @@ private bool TryInsert(TKey key, TValue value, InsertionBehavior behavior)
// If we hit the collision threshold we'll need to switch to the comparer which is using randomized string hashing
// i.e. EqualityComparer<string>.Default.

if (collisionCount > HashHelpers.HashCollisionThreshold && comparer is NonRandomizedStringEqualityComparer)
if (default(TKey) == null && collisionCount > HashHelpers.HashCollisionThreshold && comparer is NonRandomizedStringEqualityComparer)
{
comparer = (IEqualityComparer<TKey>)EqualityComparer<string>.Default;
comparer = null;
Resize(entries.Length, true);
}

Expand Down Expand Up @@ -514,7 +511,7 @@ private void Resize(int newSize, bool forceNewHashCodes)
{
if (newEntries[i].hashCode != -1)
{
newEntries[i].hashCode = (comparer.GetHashCode(newEntries[i].key) & 0x7FFFFFFF);
newEntries[i].hashCode = (comparer != null ? comparer.GetHashCode(newEntries[i].key) : newEntries[i].key.GetHashCode()) & 0x7FFFFFFF;
}
}
}
Expand Down Expand Up @@ -545,15 +542,15 @@ public bool Remove(TKey key)

if (buckets != null)
{
int hashCode = comparer.GetHashCode(key) & 0x7FFFFFFF;
int hashCode = (comparer != null ? comparer.GetHashCode(key) : key.GetHashCode()) & 0x7FFFFFFF;
int bucket = hashCode % buckets.Length;
int last = -1;
int i = buckets[bucket];
while (i >= 0)
{
ref Entry entry = ref entries[i];

if (entry.hashCode == hashCode && comparer.Equals(entry.key, key))
if (entry.hashCode == hashCode && (comparer != null ? comparer.Equals(entry.key, key) : (default(TKey) == null ? key.Equals(entry.key) : EqualityComparer<TKey>.Default.Equals(entry.key, key))))
{
if (last < 0)
{
Expand Down Expand Up @@ -599,15 +596,15 @@ public bool Remove(TKey key, out TValue value)

if (buckets != null)
{
int hashCode = comparer.GetHashCode(key) & 0x7FFFFFFF;
int hashCode = (comparer != null ? comparer.GetHashCode(key) : key.GetHashCode()) & 0x7FFFFFFF;
int bucket = hashCode % buckets.Length;
int last = -1;
int i = buckets[bucket];
while (i >= 0)
{
ref Entry entry = ref entries[i];

if (entry.hashCode == hashCode && comparer.Equals(entry.key, key))
if (entry.hashCode == hashCode && (comparer != null ? comparer.Equals(entry.key, key) : (default(TKey) == null ? key.Equals(entry.key) : EqualityComparer<TKey>.Default.Equals(entry.key, key))))
{
if (last < 0)
{
Expand Down

0 comments on commit 9876cdd

Please sign in to comment.