Triple`3.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace SmartCoalApplication.Base
  7. {
  8. [Serializable]
  9. public struct Triple<T, U, V>
  10. {
  11. private T first;
  12. private U second;
  13. private V third;
  14. public T First
  15. {
  16. get
  17. {
  18. return this.first;
  19. }
  20. }
  21. public U Second
  22. {
  23. get
  24. {
  25. return this.second;
  26. }
  27. }
  28. public V Third
  29. {
  30. get
  31. {
  32. return this.third;
  33. }
  34. }
  35. public override int GetHashCode()
  36. {
  37. int firstHash;
  38. int secondHash;
  39. int thirdHash;
  40. if (object.ReferenceEquals(this.first, null))
  41. {
  42. firstHash = 0;
  43. }
  44. else
  45. {
  46. firstHash = this.first.GetHashCode();
  47. }
  48. if (object.ReferenceEquals(this.second, null))
  49. {
  50. secondHash = 0;
  51. }
  52. else
  53. {
  54. secondHash = this.second.GetHashCode();
  55. }
  56. if (object.ReferenceEquals(this.third, null))
  57. {
  58. thirdHash = 0;
  59. }
  60. else
  61. {
  62. thirdHash = this.third.GetHashCode();
  63. }
  64. return firstHash ^ secondHash ^ thirdHash;
  65. }
  66. public override bool Equals(object obj)
  67. {
  68. return ((obj != null) && (obj is Triple<T, U, V>) && (this == (Triple<T, U, V>)obj));
  69. }
  70. public static bool operator ==(Triple<T, U, V> lhs, Triple<T, U, V> rhs)
  71. {
  72. bool firstEqual;
  73. bool secondEqual;
  74. bool thirdEqual;
  75. if (object.ReferenceEquals(lhs.First, null) && object.ReferenceEquals(rhs.First, null))
  76. {
  77. firstEqual = true;
  78. }
  79. else if (object.ReferenceEquals(lhs.First, null) || object.ReferenceEquals(rhs.First, null))
  80. {
  81. firstEqual = false;
  82. }
  83. else
  84. {
  85. firstEqual = lhs.First.Equals(rhs.First);
  86. }
  87. if (object.ReferenceEquals(lhs.Second, null) && object.ReferenceEquals(rhs.Second, null))
  88. {
  89. secondEqual = true;
  90. }
  91. else if (object.ReferenceEquals(lhs.Second, null) || object.ReferenceEquals(rhs.Second, null))
  92. {
  93. secondEqual = false;
  94. }
  95. else
  96. {
  97. secondEqual = lhs.Second.Equals(rhs.Second);
  98. }
  99. if (object.ReferenceEquals(lhs.Third, null) && object.ReferenceEquals(rhs.Third, null))
  100. {
  101. thirdEqual = true;
  102. }
  103. else if (object.ReferenceEquals(lhs.Third, null) || object.ReferenceEquals(rhs.Third, null))
  104. {
  105. thirdEqual = false;
  106. }
  107. else
  108. {
  109. thirdEqual = lhs.Third.Equals(rhs.Third);
  110. }
  111. return firstEqual && secondEqual && thirdEqual;
  112. }
  113. public static bool operator !=(Triple<T, U, V> lhs, Triple<T, U, V> rhs)
  114. {
  115. return !(lhs == rhs);
  116. }
  117. public Triple(T first, U second, V third)
  118. {
  119. this.first = first;
  120. this.second = second;
  121. this.third = third;
  122. }
  123. private sealed class TripleComparer
  124. : IEqualityComparer<Triple<T, U, V>>
  125. {
  126. private IEqualityComparer<T> tComparer;
  127. private IEqualityComparer<U> uComparer;
  128. private IEqualityComparer<V> vComparer;
  129. public TripleComparer(IEqualityComparer<T> tComparer, IEqualityComparer<U> uComparer, IEqualityComparer<V> vComparer)
  130. {
  131. this.tComparer = tComparer;
  132. this.uComparer = uComparer;
  133. this.vComparer = vComparer;
  134. }
  135. public bool Equals(Triple<T, U, V> x, Triple<T, U, V> y)
  136. {
  137. return this.tComparer.Equals(x.First, y.First) && this.uComparer.Equals(x.Second, y.Second) && this.vComparer.Equals(x.Third, y.Third);
  138. }
  139. public int GetHashCode(Triple<T, U, V> obj)
  140. {
  141. return this.tComparer.GetHashCode(obj.First) ^ this.uComparer.GetHashCode(obj.Second) ^ this.vComparer.GetHashCode(obj.Third);
  142. }
  143. }
  144. public static IEqualityComparer<Triple<T, U, V>> CreateComparer(
  145. IEqualityComparer<T> tComparer,
  146. IEqualityComparer<U> uComparer,
  147. IEqualityComparer<V> vComparer)
  148. {
  149. return new TripleComparer(
  150. tComparer ?? EqualityComparer<T>.Default,
  151. uComparer ?? EqualityComparer<U>.Default,
  152. vComparer ?? EqualityComparer<V>.Default);
  153. }
  154. }
  155. }