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 <__algorithm/comp.h> 13 #include <__algorithm/comp_ref_type.h> 14 #include <__config> 15 #include <__iterator/iterator_traits.h> 16 #include <__type_traits/is_callable.h> 17 18 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 19 # pragma GCC system_header 20 #endif 21 22 _LIBCPP_BEGIN_NAMESPACE_STD 23 24 template <class _Compare, class _ForwardIterator> 25 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _ForwardIterator 26 __max_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp) { 27 static_assert( 28 __has_forward_iterator_category<_ForwardIterator>::value, "std::max_element requires a ForwardIterator"); 29 if (__first != __last) { 30 _ForwardIterator __i = __first; 31 while (++__i != __last) 32 if (__comp(*__first, *__i)) 33 __first = __i; 34 } 35 return __first; 36 } 37 38 template <class _ForwardIterator, class _Compare> 39 [[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _ForwardIterator 40 max_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp) { 41 static_assert( 42 __is_callable<_Compare&, decltype(*__first), decltype(*__first)>::value, "The comparator has to be callable"); 43 return std::__max_element<__comp_ref_type<_Compare> >(__first, __last, __comp); 44 } 45 46 template <class _ForwardIterator> 47 [[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _ForwardIterator 48 max_element(_ForwardIterator __first, _ForwardIterator __last) { 49 return std::max_element(__first, __last, __less<>()); 50 } 51 52 _LIBCPP_END_NAMESPACE_STD 53 54 #endif // _LIBCPP___ALGORITHM_MAX_ELEMENT_H 55