CanonicalEquatableStruct.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /* Copyright 2010-present MongoDB Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. using System;
  16. namespace MongoDB.Shared
  17. {
  18. internal struct CanonicalEquatableStruct : IEquatable<CanonicalEquatableStruct>
  19. {
  20. // private fields
  21. private int _x;
  22. private int _y;
  23. // constructors
  24. /// <summary>
  25. /// Initializes a new instance of the <see cref="CanonicalEquatableStruct"/> struct.
  26. /// </summary>
  27. /// <param name="x">The x.</param>
  28. /// <param name="y">The y.</param>
  29. public CanonicalEquatableStruct(int x, int y)
  30. {
  31. _x = y;
  32. _y = y;
  33. }
  34. // public operators
  35. /// <summary>
  36. /// Determines whether two <see cref="CanonicalEquatableStruct"/> instances are equal.
  37. /// </summary>
  38. /// <param name="lhs">The LHS.</param>
  39. /// <param name="rhs">The RHS.</param>
  40. /// <returns>
  41. /// <c>true</c> if the left hand side is equal to the right hand side; otherwise, <c>false</c>.
  42. /// </returns>
  43. public static bool operator ==(CanonicalEquatableStruct lhs, CanonicalEquatableStruct rhs)
  44. {
  45. return lhs.Equals(rhs);
  46. }
  47. /// <summary>
  48. /// Determines whether two <see cref="CanonicalEquatableStruct"/> instances are not equal.
  49. /// </summary>
  50. /// <param name="lhs">The LHS.</param>
  51. /// <param name="rhs">The RHS.</param>
  52. /// <returns>
  53. /// <c>true</c> if the left hand side is not equal to the right hand side; otherwise, <c>false</c>.
  54. /// </returns>
  55. public static bool operator !=(CanonicalEquatableStruct lhs, CanonicalEquatableStruct rhs)
  56. {
  57. return !(lhs == rhs);
  58. }
  59. // public methods
  60. /// <summary>
  61. /// Determines whether the specified <see cref="CanonicalEquatableStruct" /> is equal to this instance.
  62. /// </summary>
  63. /// <param name="rhs">The <see cref="CanonicalEquatableStruct" /> to compare with this instance.</param>
  64. /// <returns>
  65. /// <c>true</c> if the specified <see cref="CanonicalEquatableStruct" /> is equal to this instance; otherwise, <c>false</c>.
  66. /// </returns>
  67. public bool Equals(CanonicalEquatableStruct rhs)
  68. {
  69. // actual work done here to avoid boxing
  70. // be sure x and y implement ==, otherwise use Equals
  71. return
  72. _x == rhs._x &&
  73. _y == rhs._y;
  74. }
  75. /// <summary>
  76. /// Determines whether the specified <see cref="System.Object" /> is equal to this instance.
  77. /// </summary>
  78. /// <param name="obj">The <see cref="System.Object" /> to compare with this instance.</param>
  79. /// <returns>
  80. /// <c>true</c> if the specified <see cref="System.Object" /> is equal to this instance; otherwise, <c>false</c>.
  81. /// </returns>
  82. public override bool Equals(object obj)
  83. {
  84. if (!(obj is CanonicalEquatableStruct)) { return false; } // handles obj == null correctly
  85. return Equals((CanonicalEquatableStruct)obj);
  86. }
  87. /// <summary>
  88. /// Returns a hash code for this instance.
  89. /// </summary>
  90. /// <returns>
  91. /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
  92. /// </returns>
  93. public override int GetHashCode()
  94. {
  95. return new Hasher()
  96. .Hash(_x)
  97. .Hash(_y)
  98. .GetHashCode();
  99. }
  100. }
  101. }