aboutsummaryrefslogtreecommitdiffhomepage
path: root/csharp/src/Google.Protobuf/Collections/MapField.cs
diff options
context:
space:
mode:
Diffstat (limited to 'csharp/src/Google.Protobuf/Collections/MapField.cs')
-rw-r--r--csharp/src/Google.Protobuf/Collections/MapField.cs22
1 files changed, 11 insertions, 11 deletions
diff --git a/csharp/src/Google.Protobuf/Collections/MapField.cs b/csharp/src/Google.Protobuf/Collections/MapField.cs
index 8dac8e30..dbbcc148 100644
--- a/csharp/src/Google.Protobuf/Collections/MapField.cs
+++ b/csharp/src/Google.Protobuf/Collections/MapField.cs
@@ -71,9 +71,12 @@ namespace Google.Protobuf.Collections
, IReadOnlyDictionary<TKey, TValue>
#endif
{
+ private static readonly EqualityComparer<TValue> ValueEqualityComparer = ProtobufEqualityComparers.GetEqualityComparer<TValue>();
+ private static readonly EqualityComparer<TKey> KeyEqualityComparer = ProtobufEqualityComparers.GetEqualityComparer<TKey>();
+
// TODO: Don't create the map/list until we have an entry. (Assume many maps will be empty.)
private readonly Dictionary<TKey, LinkedListNode<KeyValuePair<TKey, TValue>>> map =
- new Dictionary<TKey, LinkedListNode<KeyValuePair<TKey, TValue>>>();
+ new Dictionary<TKey, LinkedListNode<KeyValuePair<TKey, TValue>>>(KeyEqualityComparer);
private readonly LinkedList<KeyValuePair<TKey, TValue>> list = new LinkedList<KeyValuePair<TKey, TValue>>();
/// <summary>
@@ -131,11 +134,8 @@ namespace Google.Protobuf.Collections
return map.ContainsKey(key);
}
- private bool ContainsValue(TValue value)
- {
- var comparer = EqualityComparer<TValue>.Default;
- return list.Any(pair => comparer.Equals(pair.Value, value));
- }
+ private bool ContainsValue(TValue value) =>
+ list.Any(pair => ValueEqualityComparer.Equals(pair.Value, value));
/// <summary>
/// Removes the entry identified by the given key from the map.
@@ -293,8 +293,7 @@ namespace Google.Protobuf.Collections
bool ICollection<KeyValuePair<TKey, TValue>>.Contains(KeyValuePair<TKey, TValue> item)
{
TValue value;
- return TryGetValue(item.Key, out value)
- && EqualityComparer<TValue>.Default.Equals(item.Value, value);
+ return TryGetValue(item.Key, out value) && ValueEqualityComparer.Equals(item.Value, value);
}
/// <summary>
@@ -363,11 +362,12 @@ namespace Google.Protobuf.Collections
/// </returns>
public override int GetHashCode()
{
- var valueComparer = EqualityComparer<TValue>.Default;
+ var keyComparer = KeyEqualityComparer;
+ var valueComparer = ValueEqualityComparer;
int hash = 0;
foreach (var pair in list)
{
- hash ^= pair.Key.GetHashCode() * 31 + valueComparer.GetHashCode(pair.Value);
+ hash ^= keyComparer.GetHashCode(pair.Key) * 31 + valueComparer.GetHashCode(pair.Value);
}
return hash;
}
@@ -394,7 +394,7 @@ namespace Google.Protobuf.Collections
{
return false;
}
- var valueComparer = EqualityComparer<TValue>.Default;
+ var valueComparer = ValueEqualityComparer;
foreach (var pair in this)
{
TValue value;