equal.hpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /// \file
  2. // Range v3 library
  3. //
  4. // Copyright Eric Niebler 2014-present
  5. //
  6. // Use, modification and distribution is subject to the
  7. // Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. //
  11. // Project home: https://github.com/ericniebler/range-v3
  12. //
  13. #ifndef RANGES_V3_ALGORITHM_EQUAL_HPP
  14. #define RANGES_V3_ALGORITHM_EQUAL_HPP
  15. #include <utility>
  16. #include <range/v3/range_fwd.hpp>
  17. #include <range/v3/functional/comparisons.hpp>
  18. #include <range/v3/functional/identity.hpp>
  19. #include <range/v3/functional/invoke.hpp>
  20. #include <range/v3/iterator/concepts.hpp>
  21. #include <range/v3/iterator/operations.hpp>
  22. #include <range/v3/iterator/traits.hpp>
  23. #include <range/v3/range/access.hpp>
  24. #include <range/v3/range/concepts.hpp>
  25. #include <range/v3/range/traits.hpp>
  26. #include <range/v3/utility/static_const.hpp>
  27. #include <range/v3/detail/prologue.hpp>
  28. namespace ranges
  29. {
  30. /// \addtogroup group-algorithms
  31. /// @{
  32. /// \cond
  33. namespace detail
  34. {
  35. template<typename I0, typename S0, typename I1, typename S1, typename C,
  36. typename P0, typename P1>
  37. constexpr bool equal_nocheck(I0 begin0, S0 end0, I1 begin1, S1 end1, C pred,
  38. P0 proj0, P1 proj1)
  39. {
  40. for(; begin0 != end0 && begin1 != end1; ++begin0, ++begin1)
  41. if(!invoke(pred, invoke(proj0, *begin0), invoke(proj1, *begin1)))
  42. return false;
  43. return begin0 == end0 && begin1 == end1;
  44. }
  45. } // namespace detail
  46. /// \endcond
  47. RANGES_FUNC_BEGIN(equal)
  48. /// \brief function template \c equal
  49. template(typename I0,
  50. typename S0,
  51. typename I1,
  52. typename C = equal_to,
  53. typename P0 = identity,
  54. typename P1 = identity)(
  55. requires input_iterator<I0> AND sentinel_for<S0, I0> AND
  56. input_iterator<I1> AND indirectly_comparable<I0, I1, C, P0, P1>)
  57. RANGES_DEPRECATED(
  58. "Use the variant of ranges::equal that takes an upper bound for "
  59. "both sequences")
  60. constexpr bool RANGES_FUNC(equal)(I0 begin0,
  61. S0 end0,
  62. I1 begin1,
  63. C pred = C{},
  64. P0 proj0 = P0{},
  65. P1 proj1 = P1{}) //
  66. {
  67. for(; begin0 != end0; ++begin0, ++begin1)
  68. if(!invoke(pred, invoke(proj0, *begin0), invoke(proj1, *begin1)))
  69. return false;
  70. return true;
  71. }
  72. /// \overload
  73. template(typename I0,
  74. typename S0,
  75. typename I1,
  76. typename S1,
  77. typename C = equal_to,
  78. typename P0 = identity,
  79. typename P1 = identity)(
  80. requires input_iterator<I0> AND sentinel_for<S0, I0> AND
  81. input_iterator<I1> AND sentinel_for<S1, I1> AND
  82. indirectly_comparable<I0, I1, C, P0, P1>)
  83. constexpr bool RANGES_FUNC(equal)(I0 begin0,
  84. S0 end0,
  85. I1 begin1,
  86. S1 end1,
  87. C pred = C{},
  88. P0 proj0 = P0{},
  89. P1 proj1 = P1{}) //
  90. {
  91. if(RANGES_CONSTEXPR_IF(sized_sentinel_for<S0, I0> &&
  92. sized_sentinel_for<S1, I1>))
  93. if(distance(begin0, end0) != distance(begin1, end1))
  94. return false;
  95. return detail::equal_nocheck(std::move(begin0),
  96. std::move(end0),
  97. std::move(begin1),
  98. std::move(end1),
  99. std::move(pred),
  100. std::move(proj0),
  101. std::move(proj1));
  102. }
  103. /// \overload
  104. template(typename Rng0,
  105. typename I1Ref,
  106. typename C = equal_to,
  107. typename P0 = identity,
  108. typename P1 = identity)(
  109. requires input_range<Rng0> AND input_iterator<uncvref_t<I1Ref>> AND
  110. indirectly_comparable<iterator_t<Rng0>, uncvref_t<I1Ref>, C, P0, P1>)
  111. RANGES_DEPRECATED(
  112. "Use the variant of ranges::equal that takes an upper bound for "
  113. "both sequences")
  114. constexpr bool RANGES_FUNC(equal)(Rng0 && rng0,
  115. I1Ref && begin1,
  116. C pred = C{},
  117. P0 proj0 = P0{},
  118. P1 proj1 = P1{}) //
  119. {
  120. RANGES_DIAGNOSTIC_PUSH
  121. RANGES_DIAGNOSTIC_IGNORE_DEPRECATED_DECLARATIONS
  122. return (*this)(begin(rng0),
  123. end(rng0),
  124. (I1Ref &&) begin1,
  125. std::move(pred),
  126. std::move(proj0),
  127. std::move(proj1));
  128. RANGES_DIAGNOSTIC_POP
  129. }
  130. /// \overload
  131. template(typename Rng0,
  132. typename Rng1,
  133. typename C = equal_to,
  134. typename P0 = identity,
  135. typename P1 = identity)(
  136. requires input_range<Rng0> AND input_range<Rng1> AND
  137. indirectly_comparable<iterator_t<Rng0>, iterator_t<Rng1>, C, P0, P1>)
  138. constexpr bool RANGES_FUNC(equal)(
  139. Rng0 && rng0, Rng1 && rng1, C pred = C{}, P0 proj0 = P0{}, P1 proj1 = P1{}) //
  140. {
  141. if(RANGES_CONSTEXPR_IF(sized_range<Rng0> && sized_range<Rng1>))
  142. if(distance(rng0) != distance(rng1))
  143. return false;
  144. return detail::equal_nocheck(begin(rng0),
  145. end(rng0),
  146. begin(rng1),
  147. end(rng1),
  148. std::move(pred),
  149. std::move(proj0),
  150. std::move(proj1));
  151. }
  152. RANGES_FUNC_END(equal)
  153. namespace cpp20
  154. {
  155. using ranges::equal;
  156. }
  157. /// @}
  158. } // namespace ranges
  159. #include <range/v3/detail/epilogue.hpp>
  160. #endif