55 lines · c
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-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef _LIBCPP___ALGORITHM_MAX_ELEMENT_H10#define _LIBCPP___ALGORITHM_MAX_ELEMENT_H11 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_header20#endif21 22_LIBCPP_BEGIN_NAMESPACE_STD23 24template <class _Compare, class _ForwardIterator>25inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _ForwardIterator26__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 38template <class _ForwardIterator, class _Compare>39[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _ForwardIterator40max_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 46template <class _ForwardIterator>47[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _ForwardIterator48max_element(_ForwardIterator __first, _ForwardIterator __last) {49 return std::max_element(__first, __last, __less<>());50}51 52_LIBCPP_END_NAMESPACE_STD53 54#endif // _LIBCPP___ALGORITHM_MAX_ELEMENT_H55