Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-03 08:13:08

0001 //===----------------------------------------------------------------------===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef _LIBCPP___ALGORITHM_MAX_ELEMENT_H
0010 #define _LIBCPP___ALGORITHM_MAX_ELEMENT_H
0011 
0012 #include <__algorithm/comp.h>
0013 #include <__algorithm/comp_ref_type.h>
0014 #include <__config>
0015 #include <__iterator/iterator_traits.h>
0016 #include <__type_traits/is_callable.h>
0017 
0018 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0019 #  pragma GCC system_header
0020 #endif
0021 
0022 _LIBCPP_BEGIN_NAMESPACE_STD
0023 
0024 template <class _Compare, class _ForwardIterator>
0025 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _ForwardIterator
0026 __max_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp) {
0027   static_assert(
0028       __has_forward_iterator_category<_ForwardIterator>::value, "std::max_element requires a ForwardIterator");
0029   if (__first != __last) {
0030     _ForwardIterator __i = __first;
0031     while (++__i != __last)
0032       if (__comp(*__first, *__i))
0033         __first = __i;
0034   }
0035   return __first;
0036 }
0037 
0038 template <class _ForwardIterator, class _Compare>
0039 [[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _ForwardIterator
0040 max_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp) {
0041   static_assert(
0042       __is_callable<_Compare&, decltype(*__first), decltype(*__first)>::value, "The comparator has to be callable");
0043   return std::__max_element<__comp_ref_type<_Compare> >(__first, __last, __comp);
0044 }
0045 
0046 template <class _ForwardIterator>
0047 [[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _ForwardIterator
0048 max_element(_ForwardIterator __first, _ForwardIterator __last) {
0049   return std::max_element(__first, __last, __less<>());
0050 }
0051 
0052 _LIBCPP_END_NAMESPACE_STD
0053 
0054 #endif // _LIBCPP___ALGORITHM_MAX_ELEMENT_H