comparisons.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /// \file
  2. // Range v3 library
  3. //
  4. // Copyright Eric Niebler 2013-present
  5. // Copyright Casey Carter 2016
  6. //
  7. // Use, modification and distribution is subject to the
  8. // Boost Software License, Version 1.0. (See accompanying
  9. // file LICENSE_1_0.txt or copy at
  10. // http://www.boost.org/LICENSE_1_0.txt)
  11. //
  12. // Project home: https://github.com/ericniebler/range-v3
  13. //
  14. #ifndef RANGES_V3_FUNCTIONAL_COMPARISONS_HPP
  15. #define RANGES_V3_FUNCTIONAL_COMPARISONS_HPP
  16. #include <concepts/concepts.hpp>
  17. #include <range/v3/range_fwd.hpp>
  18. #include <range/v3/detail/prologue.hpp>
  19. namespace ranges
  20. {
  21. /// \addtogroup group-functional
  22. /// @{
  23. struct equal_to
  24. {
  25. template(typename T, typename U)(
  26. requires equality_comparable_with<T, U>)
  27. constexpr bool operator()(T && t, U && u) const
  28. {
  29. return (T &&) t == (U &&) u;
  30. }
  31. using is_transparent = void;
  32. };
  33. struct not_equal_to
  34. {
  35. template(typename T, typename U)(
  36. requires equality_comparable_with<T, U>)
  37. constexpr bool operator()(T && t, U && u) const
  38. {
  39. return !equal_to{}((T &&) t, (U &&) u);
  40. }
  41. using is_transparent = void;
  42. };
  43. struct less
  44. {
  45. template(typename T, typename U)(
  46. requires totally_ordered_with<T, U>)
  47. constexpr bool operator()(T && t, U && u) const
  48. {
  49. return (T &&) t < (U &&) u;
  50. }
  51. using is_transparent = void;
  52. };
  53. struct less_equal
  54. {
  55. template(typename T, typename U)(
  56. requires totally_ordered_with<T, U>)
  57. constexpr bool operator()(T && t, U && u) const
  58. {
  59. return !less{}((U &&) u, (T &&) t);
  60. }
  61. using is_transparent = void;
  62. };
  63. struct greater_equal
  64. {
  65. template(typename T, typename U)(
  66. requires totally_ordered_with<T, U>)
  67. constexpr bool operator()(T && t, U && u) const
  68. {
  69. return !less{}((T &&) t, (U &&) u);
  70. }
  71. using is_transparent = void;
  72. };
  73. struct greater
  74. {
  75. template(typename T, typename U)(
  76. requires totally_ordered_with<T, U>)
  77. constexpr bool operator()(T && t, U && u) const
  78. {
  79. return less{}((U &&) u, (T &&) t);
  80. }
  81. using is_transparent = void;
  82. };
  83. using ordered_less RANGES_DEPRECATED(
  84. "Repace uses of ranges::ordered_less with ranges::less") = less;
  85. #if __cplusplus > 201703L && __has_include(<compare>) && \
  86. defined(__cpp_concepts) && defined(__cpp_impl_three_way_comparison)
  87. struct compare_three_way
  88. {
  89. template(typename T, typename U)(
  90. requires three_way_comparable_with<T, U>)
  91. constexpr auto operator()(T && t, U && u) const
  92. -> decltype((T &&) t <=> (U &&) u)
  93. {
  94. return (T &&) t <=> (U &&) u;
  95. }
  96. using is_transparent = void;
  97. };
  98. #endif // __cplusplus
  99. namespace cpp20
  100. {
  101. using ranges::equal_to;
  102. using ranges::greater;
  103. using ranges::greater_equal;
  104. using ranges::less;
  105. using ranges::less_equal;
  106. using ranges::not_equal_to;
  107. } // namespace cpp20
  108. /// @}
  109. } // namespace ranges
  110. #include <range/v3/detail/epilogue.hpp>
  111. #endif