Skip to content

Commit 7bfebb7

Browse files
committed
Substitute second loop with mismatch
1 parent ef1a6d6 commit 7bfebb7

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

libcxx/include/__algorithm/is_permutation.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -164,12 +164,15 @@ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool __is_permutation(
164164
_Proj2&& __proj2,
165165
/*_ConstTimeDistance=*/false_type) {
166166
// Shorten sequences as much as possible by lopping off any equal prefix.
167-
while (__first1 != __last1 && __first2 != __last2) {
168-
if (!std::__invoke(__pred, std::__invoke(__proj1, *__first1), std::__invoke(__proj2, *__first2)))
169-
break;
170-
++__first1;
171-
++__first2;
172-
}
167+
using _Ref1 = typename iterator_traits<_Iter1>::reference;
168+
using _Ref2 = typename iterator_traits<_Iter2>::reference;
169+
170+
auto __result = std::mismatch(__first1, __last1, __first2, __last2, [&](_Ref1 __x, _Ref2 __y) -> bool {
171+
return !std::__invoke(__pred, std::__invoke(__proj1, __x), std::__invoke(__proj2, __y));
172+
});
173+
174+
__first1 = __result.first;
175+
__first2 = __result.second;
173176

174177
if (__first1 == __last1)
175178
return __first2 == __last2;

0 commit comments

Comments
 (0)