diff options
author | Jie Luo <jieluo@google.com> | 2017-12-19 11:54:11 -0800 |
---|---|---|
committer | Jie Luo <jieluo@google.com> | 2017-12-19 15:38:02 -0800 |
commit | b308580bb1c3f87a062ac1448a3e0bdaab9d40cf (patch) | |
tree | ea211f27b3deb05a9ba5461079288020a11b5c8d /csharp/src/Google.Protobuf/Collections/MapField.cs | |
parent | 3a06fe1fc9590b534f403114a0668befba926947 (diff) | |
download | protobuf-b308580bb1c3f87a062ac1448a3e0bdaab9d40cf.tar.gz protobuf-b308580bb1c3f87a062ac1448a3e0bdaab9d40cf.tar.bz2 protobuf-b308580bb1c3f87a062ac1448a3e0bdaab9d40cf.zip |
Cherrypick for csharp, including:
Add preserve UnknownFields
Compare floating point values bitwise
Add auto-generated header to C# generated files
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; |