xref: /freebsd/contrib/llvm-project/libcxx/include/__algorithm/max_element.h (revision ec0ea6efa1ad229d75c394c1a9b9cac33af2b1d3)
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_MAX_ELEMENT_H
10 #define _LIBCPP___ALGORITHM_MAX_ELEMENT_H
11 
12 #include <__config>
13 #include <__algorithm/comp.h>
14 #include <__iterator/iterator_traits.h>
15 
16 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
17 #pragma GCC system_header
18 #endif
19 
20 _LIBCPP_PUSH_MACROS
21 #include <__undef_macros>
22 
23 _LIBCPP_BEGIN_NAMESPACE_STD
24 
25 template <class _ForwardIterator, class _Compare>
26 _LIBCPP_NODISCARD_EXT inline
27 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
28 _ForwardIterator
29 max_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
30 {
31     static_assert(__is_cpp17_forward_iterator<_ForwardIterator>::value,
32         "std::max_element requires a ForwardIterator");
33     if (__first != __last)
34     {
35         _ForwardIterator __i = __first;
36         while (++__i != __last)
37             if (__comp(*__first, *__i))
38                 __first = __i;
39     }
40     return __first;
41 }
42 
43 
44 template <class _ForwardIterator>
45 _LIBCPP_NODISCARD_EXT inline
46 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
47 _ForwardIterator
48 max_element(_ForwardIterator __first, _ForwardIterator __last)
49 {
50     return _VSTD::max_element(__first, __last,
51               __less<typename iterator_traits<_ForwardIterator>::value_type>());
52 }
53 
54 _LIBCPP_END_NAMESPACE_STD
55 
56 _LIBCPP_POP_MACROS
57 
58 #endif // _LIBCPP___ALGORITHM_MAX_ELEMENT_H
59