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_MIN_ELEMENT_H
10fe6060f1SDimitry Andric #define _LIBCPP___ALGORITHM_MIN_ELEMENT_H
11fe6060f1SDimitry Andric
12fe6060f1SDimitry Andric #include <__algorithm/comp.h>
135e801ac6SDimitry Andric #include <__algorithm/comp_ref_type.h>
1404eeddc0SDimitry Andric #include <__config>
15fcaf7f86SDimitry Andric #include <__functional/identity.h>
16fcaf7f86SDimitry Andric #include <__functional/invoke.h>
17fe6060f1SDimitry Andric #include <__iterator/iterator_traits.h>
18fcaf7f86SDimitry Andric #include <__type_traits/is_callable.h>
19fcaf7f86SDimitry Andric #include <__utility/move.h>
20fe6060f1SDimitry Andric
21fe6060f1SDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
22fe6060f1SDimitry Andric # pragma GCC system_header
23fe6060f1SDimitry Andric #endif
24fe6060f1SDimitry Andric
2506c3fb27SDimitry Andric _LIBCPP_PUSH_MACROS
2606c3fb27SDimitry Andric #include <__undef_macros>
2706c3fb27SDimitry Andric
28fe6060f1SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
29fe6060f1SDimitry Andric
30fcaf7f86SDimitry Andric template <class _Comp, class _Iter, class _Sent, class _Proj>
31cb14a3feSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Iter
__min_element(_Iter __first,_Sent __last,_Comp __comp,_Proj & __proj)32cb14a3feSDimitry Andric __min_element(_Iter __first, _Sent __last, _Comp __comp, _Proj& __proj) {
33fcaf7f86SDimitry Andric if (__first == __last)
34fe6060f1SDimitry Andric return __first;
35fcaf7f86SDimitry Andric
36fcaf7f86SDimitry Andric _Iter __i = __first;
37fcaf7f86SDimitry Andric while (++__i != __last)
38fcaf7f86SDimitry Andric if (std::__invoke(__comp, std::__invoke(__proj, *__i), std::__invoke(__proj, *__first)))
39fcaf7f86SDimitry Andric __first = __i;
40fcaf7f86SDimitry Andric
41fcaf7f86SDimitry Andric return __first;
42fcaf7f86SDimitry Andric }
43fcaf7f86SDimitry Andric
44fcaf7f86SDimitry Andric template <class _Comp, class _Iter, class _Sent>
__min_element(_Iter __first,_Sent __last,_Comp __comp)45cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Iter __min_element(_Iter __first, _Sent __last, _Comp __comp) {
46fcaf7f86SDimitry Andric auto __proj = __identity();
47fcaf7f86SDimitry Andric return std::__min_element<_Comp>(std::move(__first), std::move(__last), __comp, __proj);
48fe6060f1SDimitry Andric }
49fe6060f1SDimitry Andric
505e801ac6SDimitry Andric template <class _ForwardIterator, class _Compare>
51*0fca6ea1SDimitry Andric _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _ForwardIterator
min_element(_ForwardIterator __first,_ForwardIterator __last,_Compare __comp)52cb14a3feSDimitry Andric min_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp) {
53cb14a3feSDimitry Andric static_assert(
54cb14a3feSDimitry Andric __has_forward_iterator_category<_ForwardIterator>::value, "std::min_element requires a ForwardIterator");
55cb14a3feSDimitry Andric static_assert(
56cb14a3feSDimitry Andric __is_callable<_Compare, decltype(*__first), decltype(*__first)>::value, "The comparator has to be callable");
57fcaf7f86SDimitry Andric
58bdd1243dSDimitry Andric return std::__min_element<__comp_ref_type<_Compare> >(std::move(__first), std::move(__last), __comp);
595e801ac6SDimitry Andric }
605e801ac6SDimitry Andric
61fe6060f1SDimitry Andric template <class _ForwardIterator>
62*0fca6ea1SDimitry Andric _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _ForwardIterator
min_element(_ForwardIterator __first,_ForwardIterator __last)63cb14a3feSDimitry Andric min_element(_ForwardIterator __first, _ForwardIterator __last) {
645f757f3fSDimitry Andric return std::min_element(__first, __last, __less<>());
65fe6060f1SDimitry Andric }
66fe6060f1SDimitry Andric
67fe6060f1SDimitry Andric _LIBCPP_END_NAMESPACE_STD
68fe6060f1SDimitry Andric
6906c3fb27SDimitry Andric _LIBCPP_POP_MACROS
7006c3fb27SDimitry Andric
71fe6060f1SDimitry Andric #endif // _LIBCPP___ALGORITHM_MIN_ELEMENT_H
72