1fe6060f1SDimitry Andric //===----------------------------------------------------------------------===//
2fe6060f1SDimitry Andric //
3fe6060f1SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4fe6060f1SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5fe6060f1SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6fe6060f1SDimitry Andric //
7fe6060f1SDimitry Andric //===----------------------------------------------------------------------===//
8fe6060f1SDimitry Andric
9fe6060f1SDimitry Andric #ifndef _LIBCPP___ALGORITHM_MINMAX_ELEMENT_H
10fe6060f1SDimitry Andric #define _LIBCPP___ALGORITHM_MINMAX_ELEMENT_H
11fe6060f1SDimitry Andric
12fe6060f1SDimitry Andric #include <__algorithm/comp.h>
1304eeddc0SDimitry Andric #include <__config>
1481ad6265SDimitry Andric #include <__functional/identity.h>
1506c3fb27SDimitry Andric #include <__functional/invoke.h>
16fe6060f1SDimitry Andric #include <__iterator/iterator_traits.h>
1706c3fb27SDimitry Andric #include <__type_traits/is_callable.h>
1881ad6265SDimitry Andric #include <__utility/pair.h>
19fe6060f1SDimitry Andric
20fe6060f1SDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
21fe6060f1SDimitry Andric # pragma GCC system_header
22fe6060f1SDimitry Andric #endif
23fe6060f1SDimitry Andric
24fe6060f1SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
25fe6060f1SDimitry Andric
2681ad6265SDimitry Andric template <class _Comp, class _Proj>
2781ad6265SDimitry Andric class _MinmaxElementLessFunc {
28753f127fSDimitry Andric _Comp& __comp_;
29753f127fSDimitry Andric _Proj& __proj_;
3081ad6265SDimitry Andric
3181ad6265SDimitry Andric public:
_MinmaxElementLessFunc(_Comp & __comp,_Proj & __proj)32cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _MinmaxElementLessFunc(_Comp& __comp, _Proj& __proj)
33cb14a3feSDimitry Andric : __comp_(__comp), __proj_(__proj) {}
3481ad6265SDimitry Andric
3581ad6265SDimitry Andric template <class _Iter>
operator()36cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool operator()(_Iter& __it1, _Iter& __it2) {
37753f127fSDimitry Andric return std::__invoke(__comp_, std::__invoke(__proj_, *__it1), std::__invoke(__proj_, *__it2));
3881ad6265SDimitry Andric }
3981ad6265SDimitry Andric };
4081ad6265SDimitry Andric
4181ad6265SDimitry Andric template <class _Iter, class _Sent, class _Proj, class _Comp>
42cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_Iter, _Iter>
__minmax_element_impl(_Iter __first,_Sent __last,_Comp & __comp,_Proj & __proj)43cb14a3feSDimitry Andric __minmax_element_impl(_Iter __first, _Sent __last, _Comp& __comp, _Proj& __proj) {
4481ad6265SDimitry Andric auto __less = _MinmaxElementLessFunc<_Comp, _Proj>(__comp, __proj);
4581ad6265SDimitry Andric
4681ad6265SDimitry Andric pair<_Iter, _Iter> __result(__first, __first);
4781ad6265SDimitry Andric if (__first == __last || ++__first == __last)
4881ad6265SDimitry Andric return __result;
4981ad6265SDimitry Andric
5081ad6265SDimitry Andric if (__less(__first, __result.first))
51fe6060f1SDimitry Andric __result.first = __first;
52fe6060f1SDimitry Andric else
53fe6060f1SDimitry Andric __result.second = __first;
5481ad6265SDimitry Andric
5581ad6265SDimitry Andric while (++__first != __last) {
5681ad6265SDimitry Andric _Iter __i = __first;
5781ad6265SDimitry Andric if (++__first == __last) {
5881ad6265SDimitry Andric if (__less(__i, __result.first))
59fe6060f1SDimitry Andric __result.first = __i;
6081ad6265SDimitry Andric else if (!__less(__i, __result.second))
61fe6060f1SDimitry Andric __result.second = __i;
62fe6060f1SDimitry Andric return __result;
63fe6060f1SDimitry Andric }
64fe6060f1SDimitry Andric
6581ad6265SDimitry Andric if (__less(__first, __i)) {
6681ad6265SDimitry Andric if (__less(__first, __result.first))
6781ad6265SDimitry Andric __result.first = __first;
6881ad6265SDimitry Andric if (!__less(__i, __result.second))
6981ad6265SDimitry Andric __result.second = __i;
7081ad6265SDimitry Andric } else {
7181ad6265SDimitry Andric if (__less(__i, __result.first))
7281ad6265SDimitry Andric __result.first = __i;
7381ad6265SDimitry Andric if (!__less(__first, __result.second))
7481ad6265SDimitry Andric __result.second = __first;
7581ad6265SDimitry Andric }
7681ad6265SDimitry Andric }
7781ad6265SDimitry Andric
7881ad6265SDimitry Andric return __result;
7981ad6265SDimitry Andric }
8081ad6265SDimitry Andric
8181ad6265SDimitry Andric template <class _ForwardIterator, class _Compare>
82*0fca6ea1SDimitry Andric _LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_ForwardIterator, _ForwardIterator>
minmax_element(_ForwardIterator __first,_ForwardIterator __last,_Compare __comp)8381ad6265SDimitry Andric minmax_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp) {
84cb14a3feSDimitry Andric static_assert(
85cb14a3feSDimitry Andric __has_forward_iterator_category<_ForwardIterator>::value, "std::minmax_element requires a ForwardIterator");
86cb14a3feSDimitry Andric static_assert(
87cb14a3feSDimitry Andric __is_callable<_Compare, decltype(*__first), decltype(*__first)>::value, "The comparator has to be callable");
8881ad6265SDimitry Andric auto __proj = __identity();
8981ad6265SDimitry Andric return std::__minmax_element_impl(__first, __last, __comp, __proj);
9081ad6265SDimitry Andric }
9181ad6265SDimitry Andric
9281ad6265SDimitry Andric template <class _ForwardIterator>
93*0fca6ea1SDimitry Andric _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_ForwardIterator, _ForwardIterator>
minmax_element(_ForwardIterator __first,_ForwardIterator __last)94cb14a3feSDimitry Andric minmax_element(_ForwardIterator __first, _ForwardIterator __last) {
9506c3fb27SDimitry Andric return std::minmax_element(__first, __last, __less<>());
96fe6060f1SDimitry Andric }
97fe6060f1SDimitry Andric
98fe6060f1SDimitry Andric _LIBCPP_END_NAMESPACE_STD
99fe6060f1SDimitry Andric
100fe6060f1SDimitry Andric #endif // _LIBCPP___ALGORITHM_MINMAX_ELEMENT_H
101