brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.3 KiB · dc0c3a8 Raw
101 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_MINMAX_ELEMENT_H10#define _LIBCPP___ALGORITHM_MINMAX_ELEMENT_H11 12#include <__algorithm/comp.h>13#include <__config>14#include <__functional/identity.h>15#include <__iterator/iterator_traits.h>16#include <__type_traits/invoke.h>17#include <__type_traits/is_callable.h>18#include <__utility/pair.h>19 20#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)21#  pragma GCC system_header22#endif23 24_LIBCPP_BEGIN_NAMESPACE_STD25 26template <class _Comp, class _Proj>27class _MinmaxElementLessFunc {28  _Comp& __comp_;29  _Proj& __proj_;30 31public:32  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _MinmaxElementLessFunc(_Comp& __comp, _Proj& __proj)33      : __comp_(__comp), __proj_(__proj) {}34 35  template <class _Iter>36  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool operator()(_Iter& __it1, _Iter& __it2) {37    return std::__invoke(__comp_, std::__invoke(__proj_, *__it1), std::__invoke(__proj_, *__it2));38  }39};40 41template <class _Iter, class _Sent, class _Proj, class _Comp>42_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_Iter, _Iter>43__minmax_element_impl(_Iter __first, _Sent __last, _Comp& __comp, _Proj& __proj) {44  auto __less = _MinmaxElementLessFunc<_Comp, _Proj>(__comp, __proj);45 46  pair<_Iter, _Iter> __result(__first, __first);47  if (__first == __last || ++__first == __last)48    return __result;49 50  if (__less(__first, __result.first))51    __result.first = __first;52  else53    __result.second = __first;54 55  while (++__first != __last) {56    _Iter __i = __first;57    if (++__first == __last) {58      if (__less(__i, __result.first))59        __result.first = __i;60      else if (!__less(__i, __result.second))61        __result.second = __i;62      return __result;63    }64 65    if (__less(__first, __i)) {66      if (__less(__first, __result.first))67        __result.first = __first;68      if (!__less(__i, __result.second))69        __result.second = __i;70    } else {71      if (__less(__i, __result.first))72        __result.first = __i;73      if (!__less(__first, __result.second))74        __result.second = __first;75    }76  }77 78  return __result;79}80 81template <class _ForwardIterator, class _Compare>82[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_ForwardIterator, _ForwardIterator>83minmax_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp) {84  static_assert(85      __has_forward_iterator_category<_ForwardIterator>::value, "std::minmax_element requires a ForwardIterator");86  static_assert(87      __is_callable<_Compare&, decltype(*__first), decltype(*__first)>::value, "The comparator has to be callable");88  auto __proj = __identity();89  return std::__minmax_element_impl(__first, __last, __comp, __proj);90}91 92template <class _ForwardIterator>93[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_ForwardIterator, _ForwardIterator>94minmax_element(_ForwardIterator __first, _ForwardIterator __last) {95  return std::minmax_element(__first, __last, __less<>());96}97 98_LIBCPP_END_NAMESPACE_STD99 100#endif // _LIBCPP___ALGORITHM_MINMAX_ELEMENT_H101