minmax_element.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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
  27. _MinmaxElementLessFunc(_Comp& __comp, _Proj& __proj) : __comp_(__comp), __proj_(__proj) {}
  28. template <class _Iter>
  29. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
  30. bool operator()(_Iter& __it1, _Iter& __it2) {
  31. return std::__invoke(__comp_, std::__invoke(__proj_, *__it1), std::__invoke(__proj_, *__it2));
  32. }
  33. };
  34. template <class _Iter, class _Sent, class _Proj, class _Comp>
  35. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
  36. pair<_Iter, _Iter> __minmax_element_impl(_Iter __first, _Sent __last, _Comp& __comp, _Proj& __proj) {
  37. auto __less = _MinmaxElementLessFunc<_Comp, _Proj>(__comp, __proj);
  38. pair<_Iter, _Iter> __result(__first, __first);
  39. if (__first == __last || ++__first == __last)
  40. return __result;
  41. if (__less(__first, __result.first))
  42. __result.first = __first;
  43. else
  44. __result.second = __first;
  45. while (++__first != __last) {
  46. _Iter __i = __first;
  47. if (++__first == __last) {
  48. if (__less(__i, __result.first))
  49. __result.first = __i;
  50. else if (!__less(__i, __result.second))
  51. __result.second = __i;
  52. return __result;
  53. }
  54. if (__less(__first, __i)) {
  55. if (__less(__first, __result.first))
  56. __result.first = __first;
  57. if (!__less(__i, __result.second))
  58. __result.second = __i;
  59. } else {
  60. if (__less(__i, __result.first))
  61. __result.first = __i;
  62. if (!__less(__first, __result.second))
  63. __result.second = __first;
  64. }
  65. }
  66. return __result;
  67. }
  68. template <class _ForwardIterator, class _Compare>
  69. _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
  70. pair<_ForwardIterator, _ForwardIterator>
  71. minmax_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp) {
  72. static_assert(__has_forward_iterator_category<_ForwardIterator>::value,
  73. "std::minmax_element requires a ForwardIterator");
  74. static_assert(__is_callable<_Compare, decltype(*__first), decltype(*__first)>::value,
  75. "The comparator has to be callable");
  76. auto __proj = __identity();
  77. return std::__minmax_element_impl(__first, __last, __comp, __proj);
  78. }
  79. template <class _ForwardIterator>
  80. _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
  81. pair<_ForwardIterator, _ForwardIterator> minmax_element(_ForwardIterator __first, _ForwardIterator __last) {
  82. return std::minmax_element(__first, __last, __less<>());
  83. }
  84. _LIBCPP_END_NAMESPACE_STD
  85. #endif // _LIBCPP___ALGORITHM_MINMAX_ELEMENT_H