File indexing completed on 2026-05-03 08:13:08
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #ifndef _LIBCPP___ALGORITHM_MISMATCH_H
0011 #define _LIBCPP___ALGORITHM_MISMATCH_H
0012
0013 #include <__algorithm/comp.h>
0014 #include <__algorithm/min.h>
0015 #include <__algorithm/simd_utils.h>
0016 #include <__algorithm/unwrap_iter.h>
0017 #include <__config>
0018 #include <__cstddef/size_t.h>
0019 #include <__functional/identity.h>
0020 #include <__iterator/aliasing_iterator.h>
0021 #include <__iterator/iterator_traits.h>
0022 #include <__type_traits/desugars_to.h>
0023 #include <__type_traits/enable_if.h>
0024 #include <__type_traits/invoke.h>
0025 #include <__type_traits/is_constant_evaluated.h>
0026 #include <__type_traits/is_equality_comparable.h>
0027 #include <__type_traits/is_integral.h>
0028 #include <__utility/move.h>
0029 #include <__utility/pair.h>
0030
0031 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0032 # pragma GCC system_header
0033 #endif
0034
0035 _LIBCPP_PUSH_MACROS
0036 #include <__undef_macros>
0037
0038 _LIBCPP_BEGIN_NAMESPACE_STD
0039
0040 template <class _Iter1, class _Sent1, class _Iter2, class _Pred, class _Proj1, class _Proj2>
0041 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pair<_Iter1, _Iter2>
0042 __mismatch_loop(_Iter1 __first1, _Sent1 __last1, _Iter2 __first2, _Pred& __pred, _Proj1& __proj1, _Proj2& __proj2) {
0043 while (__first1 != __last1) {
0044 if (!std::__invoke(__pred, std::__invoke(__proj1, *__first1), std::__invoke(__proj2, *__first2)))
0045 break;
0046 ++__first1;
0047 ++__first2;
0048 }
0049 return std::make_pair(std::move(__first1), std::move(__first2));
0050 }
0051
0052 template <class _Iter1, class _Sent1, class _Iter2, class _Pred, class _Proj1, class _Proj2>
0053 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pair<_Iter1, _Iter2>
0054 __mismatch(_Iter1 __first1, _Sent1 __last1, _Iter2 __first2, _Pred& __pred, _Proj1& __proj1, _Proj2& __proj2) {
0055 return std::__mismatch_loop(__first1, __last1, __first2, __pred, __proj1, __proj2);
0056 }
0057
0058 #if _LIBCPP_VECTORIZE_ALGORITHMS
0059
0060 template <class _Iter>
0061 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pair<_Iter, _Iter>
0062 __mismatch_vectorized(_Iter __first1, _Iter __last1, _Iter __first2) {
0063 using __value_type = __iter_value_type<_Iter>;
0064 constexpr size_t __unroll_count = 4;
0065 constexpr size_t __vec_size = __native_vector_size<__value_type>;
0066 using __vec = __simd_vector<__value_type, __vec_size>;
0067
0068 if (!__libcpp_is_constant_evaluated()) {
0069 auto __orig_first1 = __first1;
0070 auto __last2 = __first2 + (__last1 - __first1);
0071 while (static_cast<size_t>(__last1 - __first1) >= __unroll_count * __vec_size) [[__unlikely__]] {
0072 __vec __lhs[__unroll_count];
0073 __vec __rhs[__unroll_count];
0074
0075 for (size_t __i = 0; __i != __unroll_count; ++__i) {
0076 __lhs[__i] = std::__load_vector<__vec>(__first1 + __i * __vec_size);
0077 __rhs[__i] = std::__load_vector<__vec>(__first2 + __i * __vec_size);
0078 }
0079
0080 for (size_t __i = 0; __i != __unroll_count; ++__i) {
0081 if (auto __cmp_res = __lhs[__i] == __rhs[__i]; !std::__all_of(__cmp_res)) {
0082 auto __offset = __i * __vec_size + std::__find_first_not_set(__cmp_res);
0083 return {__first1 + __offset, __first2 + __offset};
0084 }
0085 }
0086
0087 __first1 += __unroll_count * __vec_size;
0088 __first2 += __unroll_count * __vec_size;
0089 }
0090
0091
0092 while (static_cast<size_t>(__last1 - __first1) >= __vec_size) {
0093 if (auto __cmp_res = std::__load_vector<__vec>(__first1) == std::__load_vector<__vec>(__first2);
0094 !std::__all_of(__cmp_res)) {
0095 auto __offset = std::__find_first_not_set(__cmp_res);
0096 return {__first1 + __offset, __first2 + __offset};
0097 }
0098 __first1 += __vec_size;
0099 __first2 += __vec_size;
0100 }
0101
0102 if (__last1 - __first1 == 0)
0103 return {__first1, __first2};
0104
0105
0106
0107 if (static_cast<size_t>(__first1 - __orig_first1) >= __vec_size) {
0108 __first1 = __last1 - __vec_size;
0109 __first2 = __last2 - __vec_size;
0110 auto __offset =
0111 std::__find_first_not_set(std::__load_vector<__vec>(__first1) == std::__load_vector<__vec>(__first2));
0112 return {__first1 + __offset, __first2 + __offset};
0113 }
0114 }
0115
0116 __equal_to __pred;
0117 __identity __proj;
0118 return std::__mismatch_loop(__first1, __last1, __first2, __pred, __proj, __proj);
0119 }
0120
0121 template <class _Tp,
0122 class _Pred,
0123 class _Proj1,
0124 class _Proj2,
0125 __enable_if_t<is_integral<_Tp>::value && __desugars_to_v<__equal_tag, _Pred, _Tp, _Tp> &&
0126 __is_identity<_Proj1>::value && __is_identity<_Proj2>::value,
0127 int> = 0>
0128 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pair<_Tp*, _Tp*>
0129 __mismatch(_Tp* __first1, _Tp* __last1, _Tp* __first2, _Pred&, _Proj1&, _Proj2&) {
0130 return std::__mismatch_vectorized(__first1, __last1, __first2);
0131 }
0132
0133 template <class _Tp,
0134 class _Pred,
0135 class _Proj1,
0136 class _Proj2,
0137 __enable_if_t<!is_integral<_Tp>::value && __desugars_to_v<__equal_tag, _Pred, _Tp, _Tp> &&
0138 __is_identity<_Proj1>::value && __is_identity<_Proj2>::value &&
0139 __can_map_to_integer_v<_Tp> && __libcpp_is_trivially_equality_comparable<_Tp, _Tp>::value,
0140 int> = 0>
0141 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pair<_Tp*, _Tp*>
0142 __mismatch(_Tp* __first1, _Tp* __last1, _Tp* __first2, _Pred& __pred, _Proj1& __proj1, _Proj2& __proj2) {
0143 if (__libcpp_is_constant_evaluated()) {
0144 return std::__mismatch_loop(__first1, __last1, __first2, __pred, __proj1, __proj2);
0145 } else {
0146 using _Iter = __aliasing_iterator<_Tp*, __get_as_integer_type_t<_Tp>>;
0147 auto __ret = std::__mismatch_vectorized(_Iter(__first1), _Iter(__last1), _Iter(__first2));
0148 return {__ret.first.__base(), __ret.second.__base()};
0149 }
0150 }
0151 #endif
0152
0153 template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
0154 [[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pair<_InputIterator1, _InputIterator2>
0155 mismatch(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _BinaryPredicate __pred) {
0156 __identity __proj;
0157 auto __res = std::__mismatch(
0158 std::__unwrap_iter(__first1), std::__unwrap_iter(__last1), std::__unwrap_iter(__first2), __pred, __proj, __proj);
0159 return std::make_pair(std::__rewrap_iter(__first1, __res.first), std::__rewrap_iter(__first2, __res.second));
0160 }
0161
0162 template <class _InputIterator1, class _InputIterator2>
0163 [[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pair<_InputIterator1, _InputIterator2>
0164 mismatch(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2) {
0165 return std::mismatch(__first1, __last1, __first2, __equal_to());
0166 }
0167
0168 #if _LIBCPP_STD_VER >= 14
0169 template <class _Iter1, class _Sent1, class _Iter2, class _Sent2, class _Pred, class _Proj1, class _Proj2>
0170 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pair<_Iter1, _Iter2> __mismatch(
0171 _Iter1 __first1, _Sent1 __last1, _Iter2 __first2, _Sent2 __last2, _Pred& __pred, _Proj1& __proj1, _Proj2& __proj2) {
0172 while (__first1 != __last1 && __first2 != __last2) {
0173 if (!std::__invoke(__pred, std::__invoke(__proj1, *__first1), std::__invoke(__proj2, *__first2)))
0174 break;
0175 ++__first1;
0176 ++__first2;
0177 }
0178 return {std::move(__first1), std::move(__first2)};
0179 }
0180
0181 template <class _Tp, class _Pred, class _Proj1, class _Proj2>
0182 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pair<_Tp*, _Tp*>
0183 __mismatch(_Tp* __first1, _Tp* __last1, _Tp* __first2, _Tp* __last2, _Pred& __pred, _Proj1& __proj1, _Proj2& __proj2) {
0184 auto __len = std::min(__last1 - __first1, __last2 - __first2);
0185 return std::__mismatch(__first1, __first1 + __len, __first2, __pred, __proj1, __proj2);
0186 }
0187
0188 template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
0189 [[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pair<_InputIterator1, _InputIterator2>
0190 mismatch(_InputIterator1 __first1,
0191 _InputIterator1 __last1,
0192 _InputIterator2 __first2,
0193 _InputIterator2 __last2,
0194 _BinaryPredicate __pred) {
0195 __identity __proj;
0196 auto __res = std::__mismatch(
0197 std::__unwrap_iter(__first1),
0198 std::__unwrap_iter(__last1),
0199 std::__unwrap_iter(__first2),
0200 std::__unwrap_iter(__last2),
0201 __pred,
0202 __proj,
0203 __proj);
0204 return {std::__rewrap_iter(__first1, __res.first), std::__rewrap_iter(__first2, __res.second)};
0205 }
0206
0207 template <class _InputIterator1, class _InputIterator2>
0208 [[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pair<_InputIterator1, _InputIterator2>
0209 mismatch(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2) {
0210 return std::mismatch(__first1, __last1, __first2, __last2, __equal_to());
0211 }
0212 #endif
0213
0214 _LIBCPP_END_NAMESPACE_STD
0215
0216 _LIBCPP_POP_MACROS
0217
0218 #endif