Pair.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #pragma once
  6. namespace JsUtil
  7. {
  8. template<class TFirst, class TSecond, template<class TValue> class Comparer = DefaultComparer>
  9. class Pair
  10. {
  11. private:
  12. TFirst first;
  13. TSecond second;
  14. #if DBG
  15. bool initialized;
  16. #endif
  17. public:
  18. Pair()
  19. #if DBG
  20. : initialized(false)
  21. #endif
  22. {
  23. Assert(!IsValid());
  24. }
  25. Pair(const TFirst &first, const TSecond &second)
  26. : first(first),
  27. second(second)
  28. #if DBG
  29. ,
  30. initialized(true)
  31. #endif
  32. {
  33. Assert(IsValid());
  34. }
  35. #if DBG
  36. private:
  37. bool IsValid() const
  38. {
  39. return initialized;
  40. }
  41. #endif
  42. public:
  43. const TFirst &First() const
  44. {
  45. Assert(IsValid());
  46. return first;
  47. }
  48. const TSecond &Second() const
  49. {
  50. Assert(IsValid());
  51. return second;
  52. }
  53. public:
  54. bool operator ==(const Pair &other) const
  55. {
  56. return Comparer<TFirst>::Equals(first, other.first) && Comparer<TSecond>::Equals(second, other.second);
  57. }
  58. operator hash_t() const
  59. {
  60. return Comparer<TFirst>::GetHashCode(first) + Comparer<TSecond>::GetHashCode(second);
  61. }
  62. };
  63. }