Skip to content

Commit 28f7ed6

Browse files
committed
optimize minmax_element
1 parent 1c9fe8c commit 28f7ed6

File tree

1 file changed

+49
-3
lines changed

1 file changed

+49
-3
lines changed

libcxx/include/__algorithm/minmax_element.h

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <__iterator/iterator_traits.h>
1616
#include <__type_traits/invoke.h>
1717
#include <__type_traits/is_callable.h>
18+
#include <__type_traits/is_integral.h>
1819
#include <__utility/pair.h>
1920

2021
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
@@ -38,9 +39,10 @@ class _MinmaxElementLessFunc {
3839
}
3940
};
4041

41-
template <class _Iter, class _Sent, class _Proj, class _Comp>
42-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_Iter, _Iter>
43-
__minmax_element_impl(_Iter __first, _Sent __last, _Comp& __comp, _Proj& __proj) {
42+
template<class _Iter, class _Sent, class _Proj, class _Comp>
43+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_Iter, _Iter>
44+
__minmax_element_loop(_Iter __first, _Sent __last, _Comp& __comp, _Proj& __proj) {
45+
__builtin_printf("Debug: __minmax_element_impl called, %d\n", __LINE__); // 不需要 iostream
4446
auto __less = _MinmaxElementLessFunc<_Comp, _Proj>(__comp, __proj);
4547

4648
pair<_Iter, _Iter> __result(__first, __first);
@@ -78,6 +80,50 @@ __minmax_element_impl(_Iter __first, _Sent __last, _Comp& __comp, _Proj& __proj)
7880
return __result;
7981
}
8082

83+
84+
// template<class _Tp>
85+
// typename std::iterator_traits<_Iter>::value_type
86+
// __minmax_element_vectorized(_Tp __first, _Tp __last) {
87+
88+
// }
89+
90+
91+
template <class _Iter, class _Proj, class _Comp,
92+
__enable_if_t<is_integral_v<typename std::iterator_traits<_Iter>::value_type>
93+
&& __is_identity<_Proj>::value && __desugars_to_v<__less_tag, _Comp, _Iter, _Iter>,
94+
int> = 0
95+
>
96+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_Iter, _Iter>
97+
__minmax_element_impl(_Iter __first, _Iter __last, _Comp& __comp, _Proj& __proj) {
98+
if (__libcpp_is_constant_evaluated()) {
99+
return __minmax_element_loop(__first, __last, __comp, __proj);
100+
} else {
101+
102+
}
103+
}
104+
105+
template <class _Iter, class _Proj, class _Comp,
106+
__enable_if_t<!is_integral_v<typename std::iterator_traits<_Iter>::value_type>
107+
&& __can_map_to_integer_v<typename std::iterator_traits<_Iter>::value_type>
108+
&& __libcpp_is_trivially_equality_comparable<typename std::iterator_traits<_Iter>::value_type, typename std::iterator_traits<_Iter>::value_type>::value
109+
&& __is_identity<_Proj>::value && __desugars_to_v<__less_tag, _Comp, _Iter, _Iter>,
110+
int> = 0
111+
>
112+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_Iter, _Iter>
113+
__minmax_element_impl(_Iter __first, _Iter __last, _Comp& __comp, _Proj& __proj) {
114+
if (__libcpp_is_constant_evaluated()) {
115+
return __minmax_element_loop(__first, __last, __comp, __proj);
116+
} else {
117+
118+
}
119+
}
120+
121+
template <class _Iter, class _Sent, class _Proj, class _Comp>
122+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_Iter, _Iter>
123+
__minmax_element_impl(_Iter __first, _Sent __last, _Comp& __comp, _Proj& __proj) {
124+
return std::__minmax_element_loop(__first, __last, __comp, __proj);
125+
}
126+
81127
template <class _ForwardIterator, class _Compare>
82128
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_ForwardIterator, _ForwardIterator>
83129
minmax_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp) {

0 commit comments

Comments
 (0)