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

Commit

Permalink
ContainsValue
Browse files Browse the repository at this point in the history
  • Loading branch information
benaadams committed Oct 13, 2019
1 parent 413931e commit 17d666c
Showing 1 changed file with 21 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -237,21 +237,31 @@ public bool ContainsKey(TKey key)
public bool ContainsValue(TValue value)
{
Entry[]? entries = _entries;
int count = _count;
bool result = false;
if (value == null)
{
for (int i = 0; i < _count; i++)
for (int i = 0; i < count; i++)
{
if (entries![i].next >= -1 && entries[i].value == null) return true;
if (entries![i].next >= -1 && entries[i].value == null)
{
result = true;
break;
}
}
}
else
{
if (default(TValue)! != null) // TODO-NULLABLE: default(T) == null warning (https://github.com/dotnet/roslyn/issues/34757)
{
// ValueType: Devirtualize with EqualityComparer<TValue>.Default intrinsic
for (int i = 0; i < _count; i++)
for (int i = 0; i < count; i++)
{
if (entries![i].next >= -1 && EqualityComparer<TValue>.Default.Equals(entries[i].value, value)) return true;
if (entries![i].next >= -1 && EqualityComparer<TValue>.Default.Equals(entries[i].value, value))
{
result = true;
break;
}
}
}
else
Expand All @@ -260,13 +270,17 @@ public bool ContainsValue(TValue value)
// https://github.com/dotnet/coreclr/issues/17273
// So cache in a local rather than get EqualityComparer per loop iteration
EqualityComparer<TValue> defaultComparer = EqualityComparer<TValue>.Default;
for (int i = 0; i < _count; i++)
for (int i = 0; i < count; i++)
{
if (entries![i].next >= -1 && defaultComparer.Equals(entries[i].value, value)) return true;
if (entries![i].next >= -1 && defaultComparer.Equals(entries[i].value, value))
{
result = true;
break;
}
}
}
}
return false;
return result;
}

private void CopyTo(KeyValuePair<TKey, TValue>[] array, int index)
Expand Down

0 comments on commit 17d666c

Please sign in to comment.