diff options
author | Jisi Liu <liujisi@google.com> | 2017-12-19 17:45:56 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-12-19 17:45:56 -0800 |
commit | d106399d39a3a5719f1e3d8b5997a4437eb83af3 (patch) | |
tree | 0b247a1d9a5fb782e432f1a17bb8eb90a4bdf24f /csharp/src/Google.Protobuf/Collections/MapField.cs | |
parent | 6c3c7f6d5d68433af01c5ca500978c2d69f15212 (diff) | |
parent | b308580bb1c3f87a062ac1448a3e0bdaab9d40cf (diff) | |
download | protobuf-d106399d39a3a5719f1e3d8b5997a4437eb83af3.tar.gz protobuf-d106399d39a3a5719f1e3d8b5997a4437eb83af3.tar.bz2 protobuf-d106399d39a3a5719f1e3d8b5997a4437eb83af3.zip |
Merge pull request #4072 from google/jieluo
Cherrypick for csharp, including:
Diffstat (limited to 'csharp/src/Google.Protobuf/Collections/MapField.cs')
-rw-r--r-- | csharp/src/Google.Protobuf/Collections/MapField.cs | 22 |
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; |