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

Commit

Permalink
ref returns
Browse files Browse the repository at this point in the history
  • Loading branch information
benaadams committed Sep 18, 2017
1 parent be119b6 commit f8129a1
Showing 1 changed file with 30 additions and 28 deletions.
58 changes: 30 additions & 28 deletions src/mscorlib/src/System/Collections/Generic/Dictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ private struct Entry
public TValue value; // Value of entry
}

private static Entry s_nullEntry;

private int[] _buckets;
private Entry[] _entries;
private int _count;
Expand Down Expand Up @@ -196,8 +198,11 @@ public TValue this[TKey key]
{
get
{
int i = FindEntry(key);
if (i >= 0) return _entries[i].value;
ref Entry entry = ref FindEntry(key, out bool found);
if (found)
{
return entry.value;
}
ThrowHelper.ThrowKeyNotFoundException();
return default(TValue);
}
Expand All @@ -221,18 +226,14 @@ void ICollection<KeyValuePair<TKey, TValue>>.Add(KeyValuePair<TKey, TValue> keyV

bool ICollection<KeyValuePair<TKey, TValue>>.Contains(KeyValuePair<TKey, TValue> keyValuePair)
{
int i = FindEntry(keyValuePair.Key);
if (i >= 0 && EqualityComparer<TValue>.Default.Equals(_entries[i].value, keyValuePair.Value))
{
return true;
}
return false;
ref Entry entry = ref FindEntry(keyValuePair.Key, out bool found);
return found && EqualityComparer<TValue>.Default.Equals(entry.value, keyValuePair.Value);
}

bool ICollection<KeyValuePair<TKey, TValue>>.Remove(KeyValuePair<TKey, TValue> keyValuePair)
{
int i = FindEntry(keyValuePair.Key);
if (i >= 0 && EqualityComparer<TValue>.Default.Equals(_entries[i].value, keyValuePair.Value))
ref Entry entry = ref FindEntry(keyValuePair.Key, out bool found);
if (found && EqualityComparer<TValue>.Default.Equals(entry.value, keyValuePair.Value))
{
Remove(keyValuePair.Key);
return true;
Expand All @@ -258,7 +259,11 @@ public void Clear()
}
}

public bool ContainsKey(TKey key) => FindEntry(key) >= 0;
public bool ContainsKey(TKey key)
{
FindEntry(key, out bool found);
return found;
}

public bool ContainsValue(TValue value)
{
Expand Down Expand Up @@ -331,30 +336,31 @@ public virtual void GetObjectData(SerializationInfo info, StreamingContext conte
}
}

private int FindEntry(TKey key)
private ref Entry FindEntry(TKey key, out bool found)
{
if (key == null)
{
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
}

int result = -1;
found = true;
int[] buckets = _buckets;
if (buckets != null)
{
int hashCode = KeyHashCode(key);
Entry[] entries = _entries;
for (int i = buckets[hashCode % buckets.Length]; i >= 0; i = entries[i].next)
{
if (entries[i].hashCode == hashCode && KeyEquals(entries[i].key, key))
ref Entry entry = ref entries[i];
if (entry.hashCode == hashCode && KeyEquals(entry.key, key))
{
result = i;
break;
return ref entry;
}
}
}

return result;
found = false;
return ref s_nullEntry;
}

private int[] Initialize(int capacity)
Expand Down Expand Up @@ -658,15 +664,11 @@ public bool Remove(TKey key, out TValue value)

public bool TryGetValue(TKey key, out TValue value)
{
int i = FindEntry(key);
if (i >= 0)
{
value = _entries[i].value;
return true;
}
value = default(TValue);
return false;
ref Entry entry = ref FindEntry(key, out bool found);
value = found ? entry.value : default(TValue);
return found;
}

public bool TryAdd(TKey key, TValue value) => TryInsert(key, value, InsertionBehavior.None);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down Expand Up @@ -874,10 +876,10 @@ object IDictionary.this[object key]
{
if (IsCompatibleKey(key))
{
int i = FindEntry((TKey)key);
if (i >= 0)
ref Entry entry = ref FindEntry((TKey)key, out bool found);
if (found)
{
return _entries[i].value;
return entry.value;
}
}
return null;
Expand Down

0 comments on commit f8129a1

Please sign in to comment.