Pair.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #pragma once
  24. #include "Hash.h"
  25. /// %Pair template class.
  26. template <class T, class U> class Pair
  27. {
  28. public:
  29. /// Construct undefined.
  30. Pair()
  31. {
  32. }
  33. /// Construct with values.
  34. Pair(const T& first, const U& second) :
  35. first_(first),
  36. second_(second)
  37. {
  38. }
  39. /// Test for equality with another pair.
  40. bool operator == (const Pair<T, U>& rhs) const { return first_ == rhs.first_ && second_ == rhs.second_; }
  41. /// Test for inequality with another pair.
  42. bool operator != (const Pair<T, U>& rhs) const { return first_ != rhs.first_ || second_ != rhs.second_; }
  43. /// Test for less than with another pair.
  44. bool operator < (const Pair<T, U>& rhs) const
  45. {
  46. if (first_ < rhs.first_)
  47. return true;
  48. if (first_ != rhs.first_)
  49. return false;
  50. return second_ < rhs.second_;
  51. }
  52. /// Test for less than with another pair.
  53. bool operator > (const Pair<T, U>& rhs) const
  54. {
  55. if (first_ > rhs.first_)
  56. return true;
  57. if (first_ != rhs.first_)
  58. return false;
  59. return second_ > rhs.second_;
  60. }
  61. /// Return hash value for HashSet & HashMap.
  62. unsigned ToHash() const { return (MakeHash(first_) & 0xffff) | (MakeHash(second_) << 16); }
  63. /// First value.
  64. T first_;
  65. /// Second value.
  66. U second_;
  67. };
  68. /// Construct a pair.
  69. template <class T, class U> Pair<T, U> MakePair(const T& first, const U& second)
  70. {
  71. return Pair<T, U>(first, second);
  72. }