ProtobufEqualityComparers.cs 7.2 KB

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