minmax_element.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. #ifndef _LIBCPP___ALGORITHM_MINMAX_ELEMENT_H
  9. #define _LIBCPP___ALGORITHM_MINMAX_ELEMENT_H
  10. #include <__algorithm/comp.h>
  11. #include <__config>
  12. #include <__functional/identity.h>
  13. #include <__functional/invoke.h>
  14. #include <__iterator/iterator_traits.h>
  15. #include <__type_traits/is_callable.h>
  16. #include <__utility/pair.h>
  17. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  18. # pragma GCC system_header
  19. #endif
  20. _LIBCPP_BEGIN_NAMESPACE_STD
  21. template <class _Comp, class _Proj>
  22. class _MinmaxElementLessFunc {
  23. _Comp& __comp_;
  24. _Proj& __proj_;
  25. public:
  26. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _MinmaxElementLessFunc(_Comp& __comp, _Proj& __proj)
  27. : __comp_(__comp), __proj_(__proj) {}
  28. template <class _Iter>
  29. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool operator()(_Iter& __it1, _Iter& __it2) {
  30. return std::__invoke(__comp_, std::__invoke(__proj_, *__it1), std::__invoke(__proj_, *__it2));
  31. }
  32. };
  33. template <class _Iter, class _Sent, class _Proj, class _Comp>
  34. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_Iter, _Iter>
  35. __minmax_element_impl(_Iter __first, _Sent __last, _Comp& __comp, _Proj& __proj) {
  36. auto __less = _MinmaxElementLessFunc<_Comp, _Proj>(__comp, __proj);
  37. pair<_Iter, _Iter> __result(__first, __first);
  38. if (__first == __last || ++__first == __last)
  39. return __result;
  40. if (__less(__first, __result.first))
  41. __result.first = __first;
  42. else
  43. __result.second = __first;
  44. while (++__first != __last) {
  45. _Iter __i = __first;
  46. if (++__first == __last) {
  47. if (__less(__i, __result.first))
  48. __result.first = __i;
  49. else if (!__less(__i, __result.second))
  50. __result.second = __i;
  51. return __result;
  52. }
  53. if (__less(__first, __i)) {
  54. if (__less(__first, __result.first))
  55. __result.first = __first;
  56. if (!__less(__i, __result.second))
  57. __result.second = __i;
  58. } else {
  59. if (__less(__i, __result.first))
  60. __result.first = __i;
  61. if (!__less(__first, __result.second))
  62. __result.second = __first;
  63. }
  64. }
  65. return __result;
  66. }
  67. template <class _ForwardIterator, class _Compare>
  68. _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_ForwardIterator, _ForwardIterator>
  69. minmax_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp) {
  70. static_assert(
  71. __has_forward_iterator_category<_ForwardIterator>::value, "std::minmax_element requires a ForwardIterator");
  72. static_assert(
  73. __is_callable<_Compare, decltype(*__first), decltype(*__first)>::value, "The comparator has to be callable");
  74. auto __proj = __identity();
  75. return std::__minmax_element_impl(__first, __last, __comp, __proj);
  76. }
  77. template <class _ForwardIterator>
  78. _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
  79. pair<_ForwardIterator, _ForwardIterator>
  80. minmax_element(_ForwardIterator __first, _ForwardIterator __last) {
  81. return std::minmax_element(__first, __last, __less<>());
  82. }
  83. _LIBCPP_END_NAMESPACE_STD
  84. #endif // _LIBCPP___ALGORITHM_MINMAX_ELEMENT_H