Pair.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using System;
  2. using System.Collections.Generic;
  3. namespace UniRx
  4. {
  5. // Pair is used for Observable.Pairwise
  6. [Serializable]
  7. public struct Pair<T> : IEquatable<Pair<T>>
  8. {
  9. readonly T previous;
  10. readonly T current;
  11. public T Previous
  12. {
  13. get { return previous; }
  14. }
  15. public T Current
  16. {
  17. get { return current; }
  18. }
  19. public Pair(T previous, T current)
  20. {
  21. this.previous = previous;
  22. this.current = current;
  23. }
  24. public override int GetHashCode()
  25. {
  26. var comparer = EqualityComparer<T>.Default;
  27. int h0;
  28. h0 = comparer.GetHashCode(previous);
  29. h0 = (h0 << 5) + h0 ^ comparer.GetHashCode(current);
  30. return h0;
  31. }
  32. public override bool Equals(object obj)
  33. {
  34. if (!(obj is Pair<T>)) return false;
  35. return Equals((Pair<T>)obj);
  36. }
  37. public bool Equals(Pair<T> other)
  38. {
  39. var comparer = EqualityComparer<T>.Default;
  40. return comparer.Equals(previous, other.Previous) &&
  41. comparer.Equals(current, other.Current);
  42. }
  43. public override string ToString()
  44. {
  45. return string.Format("({0}, {1})", previous, current);
  46. }
  47. }
  48. }