Back to home page

EIC code displayed by LXR

 
 

    


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

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___CXX03___ALGORITHM_MINMAX_ELEMENT_H
0010 #define _LIBCPP___CXX03___ALGORITHM_MINMAX_ELEMENT_H
0011 
0012 #include <__cxx03/__algorithm/comp.h>
0013 #include <__cxx03/__config>
0014 #include <__cxx03/__functional/identity.h>
0015 #include <__cxx03/__functional/invoke.h>
0016 #include <__cxx03/__iterator/iterator_traits.h>
0017 #include <__cxx03/__type_traits/is_callable.h>
0018 #include <__cxx03/__utility/pair.h>
0019 
0020 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0021 #  pragma GCC system_header
0022 #endif
0023 
0024 _LIBCPP_BEGIN_NAMESPACE_STD
0025 
0026 template <class _Comp, class _Proj>
0027 class _MinmaxElementLessFunc {
0028   _Comp& __comp_;
0029   _Proj& __proj_;
0030 
0031 public:
0032   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _MinmaxElementLessFunc(_Comp& __comp, _Proj& __proj)
0033       : __comp_(__comp), __proj_(__proj) {}
0034 
0035   template <class _Iter>
0036   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool operator()(_Iter& __it1, _Iter& __it2) {
0037     return std::__invoke(__comp_, std::__invoke(__proj_, *__it1), std::__invoke(__proj_, *__it2));
0038   }
0039 };
0040 
0041 template <class _Iter, class _Sent, class _Proj, class _Comp>
0042 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_Iter, _Iter>
0043 __minmax_element_impl(_Iter __first, _Sent __last, _Comp& __comp, _Proj& __proj) {
0044   auto __less = _MinmaxElementLessFunc<_Comp, _Proj>(__comp, __proj);
0045 
0046   pair<_Iter, _Iter> __result(__first, __first);
0047   if (__first == __last || ++__first == __last)
0048     return __result;
0049 
0050   if (__less(__first, __result.first))
0051     __result.first = __first;
0052   else
0053     __result.second = __first;
0054 
0055   while (++__first != __last) {
0056     _Iter __i = __first;
0057     if (++__first == __last) {
0058       if (__less(__i, __result.first))
0059         __result.first = __i;
0060       else if (!__less(__i, __result.second))
0061         __result.second = __i;
0062       return __result;
0063     }
0064 
0065     if (__less(__first, __i)) {
0066       if (__less(__first, __result.first))
0067         __result.first = __first;
0068       if (!__less(__i, __result.second))
0069         __result.second = __i;
0070     } else {
0071       if (__less(__i, __result.first))
0072         __result.first = __i;
0073       if (!__less(__first, __result.second))
0074         __result.second = __first;
0075     }
0076   }
0077 
0078   return __result;
0079 }
0080 
0081 template <class _ForwardIterator, class _Compare>
0082 _LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_ForwardIterator, _ForwardIterator>
0083 minmax_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp) {
0084   static_assert(
0085       __has_forward_iterator_category<_ForwardIterator>::value, "std::minmax_element requires a ForwardIterator");
0086   static_assert(
0087       __is_callable<_Compare, decltype(*__first), decltype(*__first)>::value, "The comparator has to be callable");
0088   auto __proj = __identity();
0089   return std::__minmax_element_impl(__first, __last, __comp, __proj);
0090 }
0091 
0092 template <class _ForwardIterator>
0093 _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_ForwardIterator, _ForwardIterator>
0094 minmax_element(_ForwardIterator __first, _ForwardIterator __last) {
0095   return std::minmax_element(__first, __last, __less<>());
0096 }
0097 
0098 _LIBCPP_END_NAMESPACE_STD
0099 
0100 #endif // _LIBCPP___CXX03___ALGORITHM_MINMAX_ELEMENT_H