1//===----------------------------------------------------------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef _LIBCPP___ALGORITHM_MINMAX_ELEMENT_H
10#define _LIBCPP___ALGORITHM_MINMAX_ELEMENT_H
11
12#include <__algorithm/comp.h>
13#include <__config>
14#include <__functional/identity.h>
15#include <__iterator/iterator_traits.h>
16#include <__type_traits/invoke.h>
17#include <__type_traits/is_callable.h>
18#include <__utility/pair.h>
19
20#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
21# pragma GCC system_header
22#endif
23
24_LIBCPP_BEGIN_NAMESPACE_STD
25
26template <class _Comp, class _Proj>
27class _MinmaxElementLessFunc {
28 _Comp& __comp_;
29 _Proj& __proj_;
30
31public:
32 _LIBCPP_CONSTEXPR _MinmaxElementLessFunc(_Comp& __comp, _Proj& __proj) : __comp_(__comp), __proj_(__proj) {}
33
34 template <class _Iter>
35 _LIBCPP_CONSTEXPR_SINCE_CXX14 bool operator()(_Iter& __it1, _Iter& __it2) {
36 return std::__invoke(__comp_, std::__invoke(__proj_, *__it1), std::__invoke(__proj_, *__it2));
37 }
38};
39
40template <class _Iter, class _Sent, class _Proj, class _Comp>
41_LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_Iter, _Iter>
42__minmax_element_impl(_Iter __first, _Sent __last, _Comp& __comp, _Proj& __proj) {
43 auto __less = _MinmaxElementLessFunc<_Comp, _Proj>(__comp, __proj);
44
45 pair<_Iter, _Iter> __result(__first, __first);
46 if (__first == __last || ++__first == __last)
47 return __result;
48
49 if (__less(__first, __result.first))
50 __result.first = __first;
51 else
52 __result.second = __first;
53
54 while (++__first != __last) {
55 _Iter __i = __first;
56 if (++__first == __last) {
57 if (__less(__i, __result.first))
58 __result.first = __i;
59 else if (!__less(__i, __result.second))
60 __result.second = __i;
61 return __result;
62 }
63
64 if (__less(__first, __i)) {
65 if (__less(__first, __result.first))
66 __result.first = __first;
67 if (!__less(__i, __result.second))
68 __result.second = __i;
69 } else {
70 if (__less(__i, __result.first))
71 __result.first = __i;
72 if (!__less(__first, __result.second))
73 __result.second = __first;
74 }
75 }
76
77 return __result;
78}
79
80template <class _ForwardIterator, class _Compare>
81[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_ForwardIterator, _ForwardIterator>
82minmax_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp) {
83 static_assert(
84 __has_forward_iterator_category<_ForwardIterator>::value, "std::minmax_element requires a ForwardIterator");
85 static_assert(
86 __is_callable<_Compare&, decltype(*__first), decltype(*__first)>::value, "The comparator has to be callable");
87 auto __proj = __identity();
88 return std::__minmax_element_impl(__first, __last, __comp, __proj);
89}
90
91template <class _ForwardIterator>
92[[__nodiscard__]] inline _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_ForwardIterator, _ForwardIterator>
93minmax_element(_ForwardIterator __first, _ForwardIterator __last) {
94 return std::minmax_element(__first, __last, __less<>());
95}
96
97_LIBCPP_END_NAMESPACE_STD
98
99#endif // _LIBCPP___ALGORITHM_MINMAX_ELEMENT_H
100