ProtobufEqualityComparers.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. using System;
  33. using System.Collections.Generic;
  34. namespace Google.Protobuf.Collections
  35. {
  36. /// <summary>
  37. /// Provides a central place to implement equality comparisons, primarily for bitwise float/double equality.
  38. /// </summary>
  39. public static class ProtobufEqualityComparers
  40. {
  41. /// <summary>
  42. /// Returns an equality comparer for <typeparamref name="T"/> suitable for Protobuf equality comparisons.
  43. /// This is usually just the default equality comparer for the type, but floating point numbers are compared
  44. /// bitwise.
  45. /// </summary>
  46. /// <typeparam name="T">The type of equality comparer to return.</typeparam>
  47. /// <returns>The equality comparer.</returns>
  48. public static EqualityComparer<T> GetEqualityComparer<T>()
  49. {
  50. return typeof(T) == typeof(double) ? (EqualityComparer<T>) (object) BitwiseDoubleEqualityComparer
  51. : typeof(T) == typeof(float) ? (EqualityComparer<T>) (object) BitwiseSingleEqualityComparer
  52. : typeof(T) == typeof(double?) ? (EqualityComparer<T>) (object) BitwiseNullableDoubleEqualityComparer
  53. : typeof(T) == typeof(float?) ? (EqualityComparer<T>) (object) BitwiseNullableSingleEqualityComparer
  54. : EqualityComparer<T>.Default;
  55. }
  56. /// <summary>
  57. /// Returns an equality comparer suitable for comparing 64-bit floating point values, by bitwise comparison.
  58. /// (NaN values are considered equal, but only when they have the same representation.)
  59. /// </summary>
  60. public static EqualityComparer<double> BitwiseDoubleEqualityComparer { get; } = new BitwiseDoubleEqualityComparerImpl();
  61. /// <summary>
  62. /// Returns an equality comparer suitable for comparing 32-bit floating point values, by bitwise comparison.
  63. /// (NaN values are considered equal, but only when they have the same representation.)
  64. /// </summary>
  65. public static EqualityComparer<float> BitwiseSingleEqualityComparer { get; } = new BitwiseSingleEqualityComparerImpl();
  66. /// <summary>
  67. /// Returns an equality comparer suitable for comparing nullable 64-bit floating point values, by bitwise comparison.
  68. /// (NaN values are considered equal, but only when they have the same representation.)
  69. /// </summary>
  70. public static EqualityComparer<double?> BitwiseNullableDoubleEqualityComparer { get; } = new BitwiseNullableDoubleEqualityComparerImpl();
  71. /// <summary>
  72. /// Returns an equality comparer suitable for comparing nullable 32-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<float?> BitwiseNullableSingleEqualityComparer { get; } = new BitwiseNullableSingleEqualityComparerImpl();
  76. private class BitwiseDoubleEqualityComparerImpl : EqualityComparer<double>
  77. {
  78. public override bool Equals(double x, double y) =>
  79. BitConverter.DoubleToInt64Bits(x) == BitConverter.DoubleToInt64Bits(y);
  80. public override int GetHashCode(double obj) =>
  81. BitConverter.DoubleToInt64Bits(obj).GetHashCode();
  82. }
  83. private class BitwiseSingleEqualityComparerImpl : EqualityComparer<float>
  84. {
  85. // Just promote values to double and use BitConverter.DoubleToInt64Bits,
  86. // as there's no BitConverter.SingleToInt32Bits, unfortunately.
  87. public override bool Equals(float x, float y) =>
  88. BitConverter.DoubleToInt64Bits(x) == BitConverter.DoubleToInt64Bits(y);
  89. public override int GetHashCode(float obj) =>
  90. BitConverter.DoubleToInt64Bits(obj).GetHashCode();
  91. }
  92. private class BitwiseNullableDoubleEqualityComparerImpl : EqualityComparer<double?>
  93. {
  94. public override bool Equals(double? x, double? y) =>
  95. x == null && y == null ? true
  96. : x == null || y == null ? false
  97. : BitwiseDoubleEqualityComparer.Equals(x.Value, y.Value);
  98. // The hash code for null is just a constant which is at least *unlikely* to be used
  99. // elsewhere. (Compared with 0, say.)
  100. public override int GetHashCode(double? obj) =>
  101. obj == null ? 293864 : BitwiseDoubleEqualityComparer.GetHashCode(obj.Value);
  102. }
  103. private class BitwiseNullableSingleEqualityComparerImpl : EqualityComparer<float?>
  104. {
  105. public override bool Equals(float? x, float? y) =>
  106. x == null && y == null ? true
  107. : x == null || y == null ? false
  108. : BitwiseSingleEqualityComparer.Equals(x.Value, y.Value);
  109. // The hash code for null is just a constant which is at least *unlikely* to be used
  110. // elsewhere. (Compared with 0, say.)
  111. public override int GetHashCode(float? obj) =>
  112. obj == null ? 293864 : BitwiseSingleEqualityComparer.GetHashCode(obj.Value);
  113. }
  114. }
  115. }