mismatch.hpp 6.7 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_MISMATCH_HPP
  14. #define RANGES_V3_ALGORITHM_MISMATCH_HPP
  15. #include <utility>
  16. #include <meta/meta.hpp>
  17. #include <range/v3/range_fwd.hpp>
  18. #include <range/v3/algorithm/result_types.hpp>
  19. #include <range/v3/functional/comparisons.hpp>
  20. #include <range/v3/functional/identity.hpp>
  21. #include <range/v3/functional/invoke.hpp>
  22. #include <range/v3/iterator/concepts.hpp>
  23. #include <range/v3/iterator/traits.hpp>
  24. #include <range/v3/range/access.hpp>
  25. #include <range/v3/range/concepts.hpp>
  26. #include <range/v3/range/dangling.hpp>
  27. #include <range/v3/range/traits.hpp>
  28. #include <range/v3/utility/static_const.hpp>
  29. #include <range/v3/detail/prologue.hpp>
  30. namespace ranges
  31. {
  32. /// \addtogroup group-algorithms
  33. /// @{
  34. template<typename I1, typename I2>
  35. using mismatch_result = detail::in1_in2_result<I1, I2>;
  36. RANGES_FUNC_BEGIN(mismatch)
  37. /// \brief function template \c mismatch
  38. template(typename I1,
  39. typename S1,
  40. typename I2,
  41. typename C = equal_to,
  42. typename P1 = identity,
  43. typename P2 = identity)(
  44. requires input_iterator<I1> AND sentinel_for<S1, I1> AND
  45. input_iterator<I2> AND
  46. indirect_relation<C, projected<I1, P1>, projected<I2, P2>>)
  47. RANGES_DEPRECATED(
  48. "Use the variant of ranges::mismatch that takes an upper bound for "
  49. "both sequences")
  50. mismatch_result<I1, I2> RANGES_FUNC(mismatch)(I1 begin1,
  51. S1 end1,
  52. I2 begin2,
  53. C pred = C{},
  54. P1 proj1 = P1{},
  55. P2 proj2 = P2{}) //
  56. {
  57. for(; begin1 != end1; ++begin1, ++begin2)
  58. if(!invoke(pred, invoke(proj1, *begin1), invoke(proj2, *begin2)))
  59. break;
  60. return {begin1, begin2};
  61. }
  62. /// \overload
  63. template(typename I1,
  64. typename S1,
  65. typename I2,
  66. typename S2,
  67. typename C = equal_to,
  68. typename P1 = identity,
  69. typename P2 = identity)(
  70. requires input_iterator<I1> AND sentinel_for<S1, I1> AND
  71. input_iterator<I2> AND sentinel_for<S2, I2> AND
  72. indirect_relation<C, projected<I1, P1>, projected<I2, P2>>)
  73. constexpr mismatch_result<I1, I2> RANGES_FUNC(mismatch)(I1 begin1,
  74. S1 end1,
  75. I2 begin2,
  76. S2 end2,
  77. C pred = C{},
  78. P1 proj1 = P1{},
  79. P2 proj2 = P2{}) //
  80. {
  81. for(; begin1 != end1 && begin2 != end2; ++begin1, ++begin2)
  82. if(!invoke(pred, invoke(proj1, *begin1), invoke(proj2, *begin2)))
  83. break;
  84. return {begin1, begin2};
  85. }
  86. /// \overload
  87. template(typename Rng1,
  88. typename I2Ref,
  89. typename C = equal_to,
  90. typename P1 = identity,
  91. typename P2 = identity)( //s
  92. requires input_range<Rng1> AND input_iterator<uncvref_t<I2Ref>> AND
  93. indirect_relation<C,
  94. projected<iterator_t<Rng1>, P1>,
  95. projected<uncvref_t<I2Ref>, P2>>)
  96. RANGES_DEPRECATED(
  97. "Use the variant of ranges::mismatch that takes an upper bound for "
  98. "both sequences")
  99. mismatch_result<borrowed_iterator_t<Rng1>, uncvref_t<I2Ref>>
  100. RANGES_FUNC(mismatch)(Rng1 && rng1,
  101. I2Ref && begin2,
  102. C pred = C{}, // see below [*]
  103. P1 proj1 = P1{},
  104. P2 proj2 = P2{}) //
  105. {
  106. RANGES_DIAGNOSTIC_PUSH
  107. RANGES_DIAGNOSTIC_IGNORE_DEPRECATED_DECLARATIONS
  108. return (*this)(begin(rng1),
  109. end(rng1),
  110. static_cast<uncvref_t<I2Ref> &&>(begin2),
  111. std::move(pred),
  112. std::move(proj1),
  113. std::move(proj2));
  114. RANGES_DIAGNOSTIC_POP
  115. }
  116. /// \overload
  117. template(typename Rng1,
  118. typename Rng2,
  119. typename C = equal_to,
  120. typename P1 = identity,
  121. typename P2 = identity)(
  122. requires input_range<Rng1> AND input_range<Rng2> AND
  123. indirect_relation<C,
  124. projected<iterator_t<Rng1>, P1>,
  125. projected<iterator_t<Rng2>, P2>>)
  126. constexpr mismatch_result<borrowed_iterator_t<Rng1>, borrowed_iterator_t<Rng2>> //
  127. RANGES_FUNC(mismatch)(Rng1 && rng1,
  128. Rng2 && rng2,
  129. C pred = C{},
  130. P1 proj1 = P1{},
  131. P2 proj2 = P2{}) //
  132. {
  133. return (*this)(begin(rng1),
  134. end(rng1),
  135. begin(rng2),
  136. end(rng2),
  137. std::move(pred),
  138. std::move(proj1),
  139. std::move(proj2));
  140. }
  141. RANGES_FUNC_END(mismatch)
  142. namespace cpp20
  143. {
  144. using ranges::mismatch;
  145. using ranges::mismatch_result;
  146. } // namespace cpp20
  147. // [*] In this case, the 'begin2' iterator is taken by universal reference. Why? So
  148. // that we can properly distinguish this case:
  149. // int x[] = {1,2,3,4};
  150. // int y[] = {1,2,3,4};
  151. // mismatch(x, y);
  152. // Had 'begin2' been taken by value as is customary, this call could match as either
  153. // two ranges, or a range and an iterator, where the iterator is the array, decayed
  154. // to a pointer. Yuk!
  155. /// @}
  156. } // namespace ranges
  157. #include <range/v3/detail/epilogue.hpp>
  158. #endif