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 <__iterator/iterator_traits.h>
16 #include <__utility/pair.h>
17 #include <type_traits>
19 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
20 # pragma GCC system_header
23 _LIBCPP_BEGIN_NAMESPACE_STD
25 template <class _Comp, class _Proj>
26 class _MinmaxElementLessFunc {
31 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
32 _MinmaxElementLessFunc(_Comp& __comp, _Proj& __proj) : __comp_(__comp), __proj_(__proj) {}
34 template <class _Iter>
35 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11
36 bool operator()(_Iter& __it1, _Iter& __it2) {
37 return std::__invoke(__comp_, std::__invoke(__proj_, *__it1), std::__invoke(__proj_, *__it2));
41 template <class _Iter, class _Sent, class _Proj, class _Comp>
42 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11
43 pair<_Iter, _Iter> __minmax_element_impl(_Iter __first, _Sent __last, _Comp& __comp, _Proj& __proj) {
44 auto __less = _MinmaxElementLessFunc<_Comp, _Proj>(__comp, __proj);
46 pair<_Iter, _Iter> __result(__first, __first);
47 if (__first == __last || ++__first == __last)
50 if (__less(__first, __result.first))
51 __result.first = __first;
53 __result.second = __first;
55 while (++__first != __last) {
57 if (++__first == __last) {
58 if (__less(__i, __result.first))
60 else if (!__less(__i, __result.second))
61 __result.second = __i;
65 if (__less(__first, __i)) {
66 if (__less(__first, __result.first))
67 __result.first = __first;
68 if (!__less(__i, __result.second))
69 __result.second = __i;
71 if (__less(__i, __result.first))
73 if (!__less(__first, __result.second))
74 __result.second = __first;
81 template <class _ForwardIterator, class _Compare>
82 _LIBCPP_NODISCARD_EXT _LIBCPP_CONSTEXPR_AFTER_CXX11
83 pair<_ForwardIterator, _ForwardIterator>
84 minmax_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp) {
85 static_assert(__is_cpp17_forward_iterator<_ForwardIterator>::value,
86 "std::minmax_element requires a ForwardIterator");
87 static_assert(__is_callable<_Compare, decltype(*__first), decltype(*__first)>::value,
88 "The comparator has to be callable");
89 auto __proj = __identity();
90 return std::__minmax_element_impl(__first, __last, __comp, __proj);
93 template <class _ForwardIterator>
94 _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11
95 pair<_ForwardIterator, _ForwardIterator> minmax_element(_ForwardIterator __first, _ForwardIterator __last) {
96 return std::minmax_element(__first, __last, __less<typename iterator_traits<_ForwardIterator>::value_type>());
99 _LIBCPP_END_NAMESPACE_STD
101 #endif // _LIBCPP___ALGORITHM_MINMAX_ELEMENT_H