-
Notifications
You must be signed in to change notification settings - Fork 14.4k
[libc++] Optimizing is_permutation #129565
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
imdj
wants to merge
6
commits into
llvm:main
Choose a base branch
from
imdj:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1b7878a
[libc++] Tiny optimizations for is_permutation
imdj eaa8148
Adjust for readability and performance
imdj ef1a6d6
Fix typos in is_permutation
imdj 6652e66
Substitute second loop with mismatch
imdj dc1b930
Remove preemptive iterator advancement for better optimizations
imdj c65efbd
Merge branch 'llvm:main' into main
imdj File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,9 +11,13 @@ | |
#define _LIBCPP___ALGORITHM_IS_PERMUTATION_H | ||
|
||
#include <__algorithm/comp.h> | ||
#include <__algorithm/count_if.h> | ||
#include <__algorithm/find_if.h> | ||
#include <__algorithm/iterator_operations.h> | ||
#include <__algorithm/mismatch.h> | ||
#include <__config> | ||
#include <__functional/identity.h> | ||
#include <__functional/reference_wrapper.h> | ||
#include <__iterator/concepts.h> | ||
#include <__iterator/distance.h> | ||
#include <__iterator/iterator_traits.h> | ||
|
@@ -79,31 +83,30 @@ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool __is_permutation_impl( | |
_Proj1&& __proj1, | ||
_Proj2&& __proj2) { | ||
using _D1 = __iter_diff_t<_Iter1>; | ||
using _Ref1 = typename iterator_traits<_Iter1>::reference; | ||
using _Ref2 = typename iterator_traits<_Iter2>::reference; | ||
__identity __ident; | ||
|
||
for (auto __i = __first1; __i != __last1; ++__i) { | ||
// Have we already counted the number of *__i in [f1, l1)? | ||
auto __match = __first1; | ||
for (; __match != __i; ++__match) { | ||
if (std::__invoke(__pred, std::__invoke(__proj1, *__match), std::__invoke(__proj1, *__i))) | ||
break; | ||
} | ||
auto __match = std::find_if(__first1, __i, [&](_Ref1 __x) -> bool { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should probably hold on to the result of |
||
return std::__invoke(__pred, std::__invoke(__proj1, __x), std::__invoke(__proj1, *__i)); | ||
}); | ||
|
||
if (__match == __i) { | ||
// Count number of *__i in [f2, l2) | ||
_D1 __c2 = 0; | ||
for (auto __j = __first2; __j != __last2; ++__j) { | ||
if (std::__invoke(__pred, std::__invoke(__proj1, *__i), std::__invoke(__proj2, *__j))) | ||
++__c2; | ||
} | ||
auto __predicate2 = [&](_Ref2 __x) -> bool { | ||
return std::__invoke(__pred, std::__invoke(__proj1, *__i), std::__invoke(__proj2, __x)); | ||
}; | ||
_D1 __c2 = std::__count_if<_AlgPolicy>(__first2, __last2, __predicate2, __ident); | ||
ldionne marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (__c2 == 0) | ||
return false; | ||
|
||
// Count number of *__i in [__i, l1) (we can start with 1) | ||
_D1 __c1 = 1; | ||
for (auto __j = _IterOps<_AlgPolicy>::next(__i); __j != __last1; ++__j) { | ||
if (std::__invoke(__pred, std::__invoke(__proj1, *__i), std::__invoke(__proj1, *__j))) | ||
++__c1; | ||
} | ||
// Count number of *__i in [__i, l1) | ||
auto __predicate1 = [&](_Ref1 __x) -> bool { | ||
ldionne marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return std::__invoke(__pred, std::__invoke(__proj1, *__i), std::__invoke(__proj1, __x)); | ||
}; | ||
_D1 __c1 = std::__count_if<_AlgPolicy>(__i, __last1, __predicate1, __ident); | ||
ldionne marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (__c1 != __c2) | ||
return false; | ||
} | ||
|
@@ -116,11 +119,10 @@ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool __is_permutation_impl( | |
template <class _AlgPolicy, class _ForwardIterator1, class _Sentinel1, class _ForwardIterator2, class _BinaryPredicate> | ||
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool __is_permutation( | ||
_ForwardIterator1 __first1, _Sentinel1 __last1, _ForwardIterator2 __first2, _BinaryPredicate&& __pred) { | ||
// Shorten sequences as much as possible by lopping of any equal prefix. | ||
for (; __first1 != __last1; ++__first1, (void)++__first2) { | ||
if (!__pred(*__first1, *__first2)) | ||
break; | ||
} | ||
// Shorten sequences as much as possible by lopping off any equal prefix. | ||
auto __result = std::mismatch(__first1, __last1, __first2, std::ref(__pred)); | ||
imdj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
__first1 = __result.first; | ||
__first2 = __result.second; | ||
|
||
if (__first1 == __last1) | ||
return true; | ||
|
@@ -160,13 +162,11 @@ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool __is_permutation( | |
_Proj1&& __proj1, | ||
_Proj2&& __proj2, | ||
/*_ConstTimeDistance=*/false_type) { | ||
// Shorten sequences as much as possible by lopping of any equal prefix. | ||
while (__first1 != __last1 && __first2 != __last2) { | ||
if (!std::__invoke(__pred, std::__invoke(__proj1, *__first1), std::__invoke(__proj2, *__first2))) | ||
break; | ||
++__first1; | ||
++__first2; | ||
} | ||
// Shorten sequences as much as possible by lopping off any equal prefix. | ||
auto __result = std::__mismatch(__first1, __last1, __first2, __last2, __pred, __proj1, __proj2); | ||
|
||
__first1 = __result.first; | ||
__first2 = __result.second; | ||
|
||
if (__first1 == __last1) | ||
return __first2 == __last2; | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.