Pair.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #pragma once
  4. #include "../Container/Hash.h"
  5. namespace Urho3D
  6. {
  7. /// %Pair template class.
  8. template <class T, class U> class Pair
  9. {
  10. public:
  11. /// Construct undefined.
  12. Pair() = default;
  13. /// Construct with values.
  14. Pair(const T& first, const U& second) :
  15. first_(first),
  16. second_(second)
  17. {
  18. }
  19. /// Test for equality with another pair.
  20. bool operator ==(const Pair<T, U>& rhs) const { return first_ == rhs.first_ && second_ == rhs.second_; }
  21. /// Test for inequality with another pair.
  22. bool operator !=(const Pair<T, U>& rhs) const { return first_ != rhs.first_ || second_ != rhs.second_; }
  23. /// Test for less than with another pair.
  24. bool operator <(const Pair<T, U>& rhs) const
  25. {
  26. if (first_ < rhs.first_)
  27. return true;
  28. if (first_ != rhs.first_)
  29. return false;
  30. return second_ < rhs.second_;
  31. }
  32. /// Test for greater than with another pair.
  33. bool operator >(const Pair<T, U>& rhs) const
  34. {
  35. if (first_ > rhs.first_)
  36. return true;
  37. if (first_ != rhs.first_)
  38. return false;
  39. return second_ > rhs.second_;
  40. }
  41. /// Return hash value for HashSet & HashMap.
  42. hash32 ToHash() const { return (MakeHash(first_) & 0xffff) | (MakeHash(second_) << 16); }
  43. /// First value.
  44. T first_;
  45. /// Second value.
  46. U second_;
  47. };
  48. /// Construct a pair.
  49. template <class T, class U> Pair<T, U> MakePair(const T& first, const U& second)
  50. {
  51. return Pair<T, U>(first, second);
  52. }
  53. template <class T> T begin(Urho3D::Pair<T, T>& range) { return range.first_; }
  54. template <class T> T end(Urho3D::Pair<T, T>& range) { return range.second_; }
  55. template <class T> T begin(const Urho3D::Pair<T, T>& range) { return range.first_; }
  56. template <class T> T end(const Urho3D::Pair<T, T>& range) { return range.second_; }
  57. }