ranges_mismatch.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #ifndef _LIBCPP___ALGORITHM_RANGES_MISMATCH_H
  9. #define _LIBCPP___ALGORITHM_RANGES_MISMATCH_H
  10. #include <__algorithm/in_in_result.h>
  11. #include <__algorithm/mismatch.h>
  12. #include <__algorithm/unwrap_range.h>
  13. #include <__config>
  14. #include <__functional/identity.h>
  15. #include <__functional/invoke.h>
  16. #include <__functional/ranges_operations.h>
  17. #include <__iterator/concepts.h>
  18. #include <__iterator/indirectly_comparable.h>
  19. #include <__ranges/access.h>
  20. #include <__ranges/concepts.h>
  21. #include <__ranges/dangling.h>
  22. #include <__utility/move.h>
  23. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  24. # pragma GCC system_header
  25. #endif
  26. _LIBCPP_PUSH_MACROS
  27. #include <__undef_macros>
  28. _LIBCPP_BEGIN_NAMESPACE_STD
  29. #if _LIBCPP_STD_VER >= 20
  30. namespace ranges {
  31. template <class _I1, class _I2>
  32. using mismatch_result = in_in_result<_I1, _I2>;
  33. namespace __mismatch {
  34. struct __fn {
  35. template <class _I1, class _S1, class _I2, class _S2, class _Pred, class _Proj1, class _Proj2>
  36. static _LIBCPP_HIDE_FROM_ABI constexpr mismatch_result<_I1, _I2>
  37. __go(_I1 __first1, _S1 __last1, _I2 __first2, _S2 __last2, _Pred& __pred, _Proj1& __proj1, _Proj2& __proj2) {
  38. if constexpr (forward_iterator<_I1> && forward_iterator<_I2>) {
  39. auto __range1 = std::__unwrap_range(__first1, __last1);
  40. auto __range2 = std::__unwrap_range(__first2, __last2);
  41. auto __res =
  42. std::__mismatch(__range1.first, __range1.second, __range2.first, __range2.second, __pred, __proj1, __proj2);
  43. return {std::__rewrap_range<_S1>(__first1, __res.first), std::__rewrap_range<_S2>(__first2, __res.second)};
  44. } else {
  45. auto __res = std::__mismatch(
  46. std::move(__first1), std::move(__last1), std::move(__first2), std::move(__last2), __pred, __proj1, __proj2);
  47. return {std::move(__res.first), std::move(__res.second)};
  48. }
  49. }
  50. template <input_iterator _I1,
  51. sentinel_for<_I1> _S1,
  52. input_iterator _I2,
  53. sentinel_for<_I2> _S2,
  54. class _Pred = ranges::equal_to,
  55. class _Proj1 = identity,
  56. class _Proj2 = identity>
  57. requires indirectly_comparable<_I1, _I2, _Pred, _Proj1, _Proj2>
  58. _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr mismatch_result<_I1, _I2> operator()(
  59. _I1 __first1, _S1 __last1, _I2 __first2, _S2 __last2, _Pred __pred = {}, _Proj1 __proj1 = {}, _Proj2 __proj2 = {})
  60. const {
  61. return __go(std::move(__first1), __last1, std::move(__first2), __last2, __pred, __proj1, __proj2);
  62. }
  63. template <input_range _R1,
  64. input_range _R2,
  65. class _Pred = ranges::equal_to,
  66. class _Proj1 = identity,
  67. class _Proj2 = identity>
  68. requires indirectly_comparable<iterator_t<_R1>, iterator_t<_R2>, _Pred, _Proj1, _Proj2>
  69. _LIBCPP_NODISCARD_EXT
  70. _LIBCPP_HIDE_FROM_ABI constexpr mismatch_result<borrowed_iterator_t<_R1>, borrowed_iterator_t<_R2>>
  71. operator()(_R1&& __r1, _R2&& __r2, _Pred __pred = {}, _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const {
  72. return __go(
  73. ranges::begin(__r1), ranges::end(__r1), ranges::begin(__r2), ranges::end(__r2), __pred, __proj1, __proj2);
  74. }
  75. };
  76. } // namespace __mismatch
  77. inline namespace __cpo {
  78. constexpr inline auto mismatch = __mismatch::__fn{};
  79. } // namespace __cpo
  80. } // namespace ranges
  81. #endif // _LIBCPP_STD_VER >= 20
  82. _LIBCPP_END_NAMESPACE_STD
  83. _LIBCPP_POP_MACROS
  84. #endif // _LIBCPP___ALGORITHM_RANGES_MISMATCH_H