xref: /freebsd-src/contrib/llvm-project/libcxx/include/__algorithm/max_element.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
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_MAX_ELEMENT_H
10fe6060f1SDimitry Andric #define _LIBCPP___ALGORITHM_MAX_ELEMENT_H
11fe6060f1SDimitry Andric 
12fe6060f1SDimitry Andric #include <__algorithm/comp.h>
135e801ac6SDimitry Andric #include <__algorithm/comp_ref_type.h>
1404eeddc0SDimitry Andric #include <__config>
15fe6060f1SDimitry Andric #include <__iterator/iterator_traits.h>
16fe6060f1SDimitry Andric 
17fe6060f1SDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
18fe6060f1SDimitry Andric #  pragma GCC system_header
19fe6060f1SDimitry Andric #endif
20fe6060f1SDimitry Andric 
21fe6060f1SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
22fe6060f1SDimitry Andric 
235e801ac6SDimitry Andric template <class _Compare, class _ForwardIterator>
24bdd1243dSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _ForwardIterator
25cb14a3feSDimitry Andric __max_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp) {
26cb14a3feSDimitry Andric   static_assert(
27cb14a3feSDimitry Andric       __has_forward_iterator_category<_ForwardIterator>::value, "std::max_element requires a ForwardIterator");
28cb14a3feSDimitry Andric   if (__first != __last) {
29fe6060f1SDimitry Andric     _ForwardIterator __i = __first;
30fe6060f1SDimitry Andric     while (++__i != __last)
31fe6060f1SDimitry Andric       if (__comp(*__first, *__i))
32fe6060f1SDimitry Andric         __first = __i;
33fe6060f1SDimitry Andric   }
34fe6060f1SDimitry Andric   return __first;
35fe6060f1SDimitry Andric }
36fe6060f1SDimitry Andric 
375e801ac6SDimitry Andric template <class _ForwardIterator, class _Compare>
38*0fca6ea1SDimitry Andric _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _ForwardIterator
39cb14a3feSDimitry Andric max_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp) {
405f757f3fSDimitry Andric   return std::__max_element<__comp_ref_type<_Compare> >(__first, __last, __comp);
415e801ac6SDimitry Andric }
425e801ac6SDimitry Andric 
43fe6060f1SDimitry Andric template <class _ForwardIterator>
44*0fca6ea1SDimitry Andric _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _ForwardIterator
45cb14a3feSDimitry Andric max_element(_ForwardIterator __first, _ForwardIterator __last) {
465f757f3fSDimitry Andric   return std::max_element(__first, __last, __less<>());
47fe6060f1SDimitry Andric }
48fe6060f1SDimitry Andric 
49fe6060f1SDimitry Andric _LIBCPP_END_NAMESPACE_STD
50fe6060f1SDimitry Andric 
51fe6060f1SDimitry Andric #endif // _LIBCPP___ALGORITHM_MAX_ELEMENT_H
52