1 #region Copyright notice and license 2 // Protocol Buffers - Google's data interchange format 3 // Copyright 2017 Google Inc. All rights reserved. 4 // https://developers.google.com/protocol-buffers/ 5 // 6 // Redistribution and use in source and binary forms, with or without 7 // modification, are permitted provided that the following conditions are 8 // met: 9 // 10 // * Redistributions of source code must retain the above copyright 11 // notice, this list of conditions and the following disclaimer. 12 // * Redistributions in binary form must reproduce the above 13 // copyright notice, this list of conditions and the following disclaimer 14 // in the documentation and/or other materials provided with the 15 // distribution. 16 // * Neither the name of Google Inc. nor the names of its 17 // contributors may be used to endorse or promote products derived from 18 // this software without specific prior written permission. 19 // 20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 #endregion 32 33 using System; 34 using System.Collections.Generic; 35 36 namespace Google.Protobuf.Collections 37 { 38 /// <summary> 39 /// Provides a central place to implement equality comparisons, primarily for bitwise float/double equality. 40 /// </summary> 41 public static class ProtobufEqualityComparers 42 { 43 /// <summary> 44 /// Returns an equality comparer for <typeparamref name="T"/> suitable for Protobuf equality comparisons. 45 /// This is usually just the default equality comparer for the type, but floating point numbers are compared 46 /// bitwise. 47 /// </summary> 48 /// <typeparam name="T">The type of equality comparer to return.</typeparam> 49 /// <returns>The equality comparer.</returns> GetEqualityComparer()50 public static EqualityComparer<T> GetEqualityComparer<T>() 51 { 52 return typeof(T) == typeof(double) ? (EqualityComparer<T>) (object) BitwiseDoubleEqualityComparer 53 : typeof(T) == typeof(float) ? (EqualityComparer<T>) (object) BitwiseSingleEqualityComparer 54 : typeof(T) == typeof(double?) ? (EqualityComparer<T>) (object) BitwiseNullableDoubleEqualityComparer 55 : typeof(T) == typeof(float?) ? (EqualityComparer<T>) (object) BitwiseNullableSingleEqualityComparer 56 : EqualityComparer<T>.Default; 57 } 58 59 /// <summary> 60 /// Returns an equality comparer suitable for comparing 64-bit floating point values, by bitwise comparison. 61 /// (NaN values are considered equal, but only when they have the same representation.) 62 /// </summary> 63 public static EqualityComparer<double> BitwiseDoubleEqualityComparer { get; } = new BitwiseDoubleEqualityComparerImpl(); 64 65 /// <summary> 66 /// Returns an equality comparer suitable for comparing 32-bit floating point values, by bitwise comparison. 67 /// (NaN values are considered equal, but only when they have the same representation.) 68 /// </summary> 69 public static EqualityComparer<float> BitwiseSingleEqualityComparer { get; } = new BitwiseSingleEqualityComparerImpl(); 70 71 /// <summary> 72 /// Returns an equality comparer suitable for comparing nullable 64-bit floating point values, by bitwise comparison. 73 /// (NaN values are considered equal, but only when they have the same representation.) 74 /// </summary> 75 public static EqualityComparer<double?> BitwiseNullableDoubleEqualityComparer { get; } = new BitwiseNullableDoubleEqualityComparerImpl(); 76 77 /// <summary> 78 /// Returns an equality comparer suitable for comparing nullable 32-bit floating point values, by bitwise comparison. 79 /// (NaN values are considered equal, but only when they have the same representation.) 80 /// </summary> 81 public static EqualityComparer<float?> BitwiseNullableSingleEqualityComparer { get; } = new BitwiseNullableSingleEqualityComparerImpl(); 82 83 private class BitwiseDoubleEqualityComparerImpl : EqualityComparer<double> 84 { Equals(double x, double y)85 public override bool Equals(double x, double y) => 86 BitConverter.DoubleToInt64Bits(x) == BitConverter.DoubleToInt64Bits(y); 87 GetHashCode(double obj)88 public override int GetHashCode(double obj) => 89 BitConverter.DoubleToInt64Bits(obj).GetHashCode(); 90 } 91 92 private class BitwiseSingleEqualityComparerImpl : EqualityComparer<float> 93 { 94 // Just promote values to double and use BitConverter.DoubleToInt64Bits, 95 // as there's no BitConverter.SingleToInt32Bits, unfortunately. 96 Equals(float x, float y)97 public override bool Equals(float x, float y) => 98 BitConverter.DoubleToInt64Bits(x) == BitConverter.DoubleToInt64Bits(y); 99 GetHashCode(float obj)100 public override int GetHashCode(float obj) => 101 BitConverter.DoubleToInt64Bits(obj).GetHashCode(); 102 } 103 104 private class BitwiseNullableDoubleEqualityComparerImpl : EqualityComparer<double?> 105 { Equals(double? x, double? y)106 public override bool Equals(double? x, double? y) => 107 x == null && y == null ? true 108 : x == null || y == null ? false 109 : BitwiseDoubleEqualityComparer.Equals(x.Value, y.Value); 110 111 // The hash code for null is just a constant which is at least *unlikely* to be used 112 // elsewhere. (Compared with 0, say.) GetHashCode(double? obj)113 public override int GetHashCode(double? obj) => 114 obj == null ? 293864 : BitwiseDoubleEqualityComparer.GetHashCode(obj.Value); 115 } 116 117 private class BitwiseNullableSingleEqualityComparerImpl : EqualityComparer<float?> 118 { Equals(float? x, float? y)119 public override bool Equals(float? x, float? y) => 120 x == null && y == null ? true 121 : x == null || y == null ? false 122 : BitwiseSingleEqualityComparer.Equals(x.Value, y.Value); 123 124 // The hash code for null is just a constant which is at least *unlikely* to be used 125 // elsewhere. (Compared with 0, say.) GetHashCode(float? obj)126 public override int GetHashCode(float? obj) => 127 obj == null ? 293864 : BitwiseSingleEqualityComparer.GetHashCode(obj.Value); 128 } 129 } 130 } 131