Triple`3.cs 4.8 KB

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