1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef _LIBCPP___ALGORITHM_MINMAX_ELEMENT_H
10 #define _LIBCPP___ALGORITHM_MINMAX_ELEMENT_H
12 #include <__algorithm/comp.h>
14 #include <__functional/identity.h>
15 #include <__functional/invoke.h>
16 #include <__iterator/iterator_traits.h>
17 #include <__type_traits/is_callable.h>
18 #include <__utility/pair.h>
20 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
21 # pragma GCC system_header
24 _LIBCPP_BEGIN_NAMESPACE_STD
26 template <class _Comp
, class _Proj
>
27 class _MinmaxElementLessFunc
{
32 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
33 _MinmaxElementLessFunc(_Comp
& __comp
, _Proj
& __proj
) : __comp_(__comp
), __proj_(__proj
) {}
35 template <class _Iter
>
36 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
37 bool operator()(_Iter
& __it1
, _Iter
& __it2
) {
38 return std::__invoke(__comp_
, std::__invoke(__proj_
, *__it1
), std::__invoke(__proj_
, *__it2
));
42 template <class _Iter
, class _Sent
, class _Proj
, class _Comp
>
43 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
44 pair
<_Iter
, _Iter
> __minmax_element_impl(_Iter __first
, _Sent __last
, _Comp
& __comp
, _Proj
& __proj
) {
45 auto __less
= _MinmaxElementLessFunc
<_Comp
, _Proj
>(__comp
, __proj
);
47 pair
<_Iter
, _Iter
> __result(__first
, __first
);
48 if (__first
== __last
|| ++__first
== __last
)
51 if (__less(__first
, __result
.first
))
52 __result
.first
= __first
;
54 __result
.second
= __first
;
56 while (++__first
!= __last
) {
58 if (++__first
== __last
) {
59 if (__less(__i
, __result
.first
))
61 else if (!__less(__i
, __result
.second
))
62 __result
.second
= __i
;
66 if (__less(__first
, __i
)) {
67 if (__less(__first
, __result
.first
))
68 __result
.first
= __first
;
69 if (!__less(__i
, __result
.second
))
70 __result
.second
= __i
;
72 if (__less(__i
, __result
.first
))
74 if (!__less(__first
, __result
.second
))
75 __result
.second
= __first
;
82 template <class _ForwardIterator
, class _Compare
>
83 _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
84 pair
<_ForwardIterator
, _ForwardIterator
>
85 minmax_element(_ForwardIterator __first
, _ForwardIterator __last
, _Compare __comp
) {
86 static_assert(__has_forward_iterator_category
<_ForwardIterator
>::value
,
87 "std::minmax_element requires a ForwardIterator");
88 static_assert(__is_callable
<_Compare
, decltype(*__first
), decltype(*__first
)>::value
,
89 "The comparator has to be callable");
90 auto __proj
= __identity();
91 return std::__minmax_element_impl(__first
, __last
, __comp
, __proj
);
94 template <class _ForwardIterator
>
95 _LIBCPP_NODISCARD_EXT
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
96 pair
<_ForwardIterator
, _ForwardIterator
> minmax_element(_ForwardIterator __first
, _ForwardIterator __last
) {
97 return std::minmax_element(__first
, __last
, __less
<>());
100 _LIBCPP_END_NAMESPACE_STD
102 #endif // _LIBCPP___ALGORITHM_MINMAX_ELEMENT_H