ranges_mismatch.h 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 <__config>
  12. #include <__functional/identity.h>
  13. #include <__functional/invoke.h>
  14. #include <__functional/ranges_operations.h>
  15. #include <__iterator/concepts.h>
  16. #include <__iterator/indirectly_comparable.h>
  17. #include <__ranges/access.h>
  18. #include <__ranges/concepts.h>
  19. #include <__ranges/dangling.h>
  20. #include <__utility/move.h>
  21. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  22. # pragma GCC system_header
  23. #endif
  24. _LIBCPP_PUSH_MACROS
  25. #include <__undef_macros>
  26. _LIBCPP_BEGIN_NAMESPACE_STD
  27. #if _LIBCPP_STD_VER >= 20
  28. namespace ranges {
  29. template <class _I1, class _I2>
  30. using mismatch_result = in_in_result<_I1, _I2>;
  31. namespace __mismatch {
  32. struct __fn {
  33. template <class _I1, class _S1, class _I2, class _S2, class _Pred, class _Proj1, class _Proj2>
  34. static _LIBCPP_HIDE_FROM_ABI constexpr mismatch_result<_I1, _I2>
  35. __go(_I1 __first1, _S1 __last1, _I2 __first2, _S2 __last2, _Pred& __pred, _Proj1& __proj1, _Proj2& __proj2) {
  36. while (__first1 != __last1 && __first2 != __last2) {
  37. if (!std::invoke(__pred, std::invoke(__proj1, *__first1), std::invoke(__proj2, *__first2)))
  38. break;
  39. ++__first1;
  40. ++__first2;
  41. }
  42. return {std::move(__first1), std::move(__first2)};
  43. }
  44. template <input_iterator _I1,
  45. sentinel_for<_I1> _S1,
  46. input_iterator _I2,
  47. sentinel_for<_I2> _S2,
  48. class _Pred = ranges::equal_to,
  49. class _Proj1 = identity,
  50. class _Proj2 = identity>
  51. requires indirectly_comparable<_I1, _I2, _Pred, _Proj1, _Proj2>
  52. _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr mismatch_result<_I1, _I2> operator()(
  53. _I1 __first1, _S1 __last1, _I2 __first2, _S2 __last2, _Pred __pred = {}, _Proj1 __proj1 = {}, _Proj2 __proj2 = {})
  54. const {
  55. return __go(std::move(__first1), __last1, std::move(__first2), __last2, __pred, __proj1, __proj2);
  56. }
  57. template <input_range _R1,
  58. input_range _R2,
  59. class _Pred = ranges::equal_to,
  60. class _Proj1 = identity,
  61. class _Proj2 = identity>
  62. requires indirectly_comparable<iterator_t<_R1>, iterator_t<_R2>, _Pred, _Proj1, _Proj2>
  63. _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr mismatch_result<borrowed_iterator_t<_R1>,
  64. borrowed_iterator_t<_R2>>
  65. operator()(_R1&& __r1, _R2&& __r2, _Pred __pred = {}, _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const {
  66. return __go(
  67. ranges::begin(__r1), ranges::end(__r1), ranges::begin(__r2), ranges::end(__r2), __pred, __proj1, __proj2);
  68. }
  69. };
  70. } // namespace __mismatch
  71. inline namespace __cpo {
  72. constexpr inline auto mismatch = __mismatch::__fn{};
  73. } // namespace __cpo
  74. } // namespace ranges
  75. #endif // _LIBCPP_STD_VER >= 20
  76. _LIBCPP_END_NAMESPACE_STD
  77. _LIBCPP_POP_MACROS
  78. #endif // _LIBCPP___ALGORITHM_RANGES_MISMATCH_H