ranges_minmax.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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_RANGES_MINMAX_H
  9. #define _LIBCPP___ALGORITHM_RANGES_MINMAX_H
  10. #include <__algorithm/min_max_result.h>
  11. #include <__algorithm/minmax_element.h>
  12. #include <__assert>
  13. #include <__concepts/copyable.h>
  14. #include <__concepts/same_as.h>
  15. #include <__config>
  16. #include <__functional/identity.h>
  17. #include <__functional/invoke.h>
  18. #include <__functional/ranges_operations.h>
  19. #include <__iterator/concepts.h>
  20. #include <__iterator/next.h>
  21. #include <__iterator/projected.h>
  22. #include <__ranges/access.h>
  23. #include <__ranges/concepts.h>
  24. #include <__type_traits/is_reference.h>
  25. #include <__type_traits/remove_cvref.h>
  26. #include <__utility/forward.h>
  27. #include <__utility/move.h>
  28. #include <__utility/pair.h>
  29. #include <initializer_list>
  30. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  31. # pragma GCC system_header
  32. #endif
  33. #if _LIBCPP_STD_VER >= 20
  34. _LIBCPP_PUSH_MACROS
  35. # include <__undef_macros>
  36. _LIBCPP_BEGIN_NAMESPACE_STD
  37. namespace ranges {
  38. template <class _T1>
  39. using minmax_result = min_max_result<_T1>;
  40. namespace __minmax {
  41. struct __fn {
  42. template <class _Type,
  43. class _Proj = identity,
  44. indirect_strict_weak_order<projected<const _Type*, _Proj>> _Comp = ranges::less>
  45. _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr ranges::minmax_result<const _Type&>
  46. operator()(_LIBCPP_LIFETIMEBOUND const _Type& __a,
  47. _LIBCPP_LIFETIMEBOUND const _Type& __b,
  48. _Comp __comp = {},
  49. _Proj __proj = {}) const {
  50. if (std::invoke(__comp, std::invoke(__proj, __b), std::invoke(__proj, __a)))
  51. return {__b, __a};
  52. return {__a, __b};
  53. }
  54. template <copyable _Type,
  55. class _Proj = identity,
  56. indirect_strict_weak_order<projected<const _Type*, _Proj>> _Comp = ranges::less>
  57. _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr ranges::minmax_result<_Type>
  58. operator()(initializer_list<_Type> __il, _Comp __comp = {}, _Proj __proj = {}) const {
  59. _LIBCPP_ASSERT_UNCATEGORIZED(__il.begin() != __il.end(), "initializer_list has to contain at least one element");
  60. auto __iters = std::__minmax_element_impl(__il.begin(), __il.end(), __comp, __proj);
  61. return ranges::minmax_result<_Type>{*__iters.first, *__iters.second};
  62. }
  63. template <input_range _Range,
  64. class _Proj = identity,
  65. indirect_strict_weak_order<projected<iterator_t<_Range>, _Proj>> _Comp = ranges::less>
  66. requires indirectly_copyable_storable<iterator_t<_Range>, range_value_t<_Range>*>
  67. _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr ranges::minmax_result<range_value_t<_Range>>
  68. operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const {
  69. auto __first = ranges::begin(__r);
  70. auto __last = ranges::end(__r);
  71. using _ValueT = range_value_t<_Range>;
  72. _LIBCPP_ASSERT_UNCATEGORIZED(__first != __last, "range has to contain at least one element");
  73. if constexpr (forward_range<_Range>) {
  74. // Special-case the one element case. Avoid repeatedly initializing objects from the result of an iterator
  75. // dereference when doing so might not be idempotent. The `if constexpr` avoids the extra branch in cases where
  76. // it's not needed.
  77. if constexpr (!same_as<remove_cvref_t<range_reference_t<_Range>>, _ValueT> ||
  78. is_rvalue_reference_v<range_reference_t<_Range>>) {
  79. if (ranges::next(__first) == __last) {
  80. // During initialization, members are allowed to refer to already initialized members
  81. // (see http://eel.is/c++draft/dcl.init.aggr#6)
  82. minmax_result<_ValueT> __result = {*__first, __result.min};
  83. return __result;
  84. }
  85. }
  86. auto __result = std::__minmax_element_impl(__first, __last, __comp, __proj);
  87. return {*__result.first, *__result.second};
  88. } else {
  89. // input_iterators can't be copied, so the implementation for input_iterators has to store
  90. // the values instead of a pointer to the correct values
  91. auto __less = [&](auto&& __a, auto&& __b) -> bool {
  92. return std::invoke(__comp,
  93. std::invoke(__proj, std::forward<decltype(__a)>(__a)),
  94. std::invoke(__proj, std::forward<decltype(__b)>(__b)));
  95. };
  96. // During initialization, members are allowed to refer to already initialized members
  97. // (see http://eel.is/c++draft/dcl.init.aggr#6)
  98. ranges::minmax_result<_ValueT> __result = {*__first, __result.min};
  99. if (__first == __last || ++__first == __last)
  100. return __result;
  101. if (__less(*__first, __result.min))
  102. __result.min = *__first;
  103. else
  104. __result.max = *__first;
  105. while (++__first != __last) {
  106. _ValueT __i = *__first;
  107. if (++__first == __last) {
  108. if (__less(__i, __result.min))
  109. __result.min = __i;
  110. else if (!__less(__i, __result.max))
  111. __result.max = __i;
  112. return __result;
  113. }
  114. if (__less(*__first, __i)) {
  115. if (__less(*__first, __result.min))
  116. __result.min = *__first;
  117. if (!__less(__i, __result.max))
  118. __result.max = std::move(__i);
  119. } else {
  120. if (__less(__i, __result.min))
  121. __result.min = std::move(__i);
  122. if (!__less(*__first, __result.max))
  123. __result.max = *__first;
  124. }
  125. }
  126. return __result;
  127. }
  128. }
  129. };
  130. } // namespace __minmax
  131. inline namespace __cpo {
  132. inline constexpr auto minmax = __minmax::__fn{};
  133. } // namespace __cpo
  134. } // namespace ranges
  135. _LIBCPP_END_NAMESPACE_STD
  136. _LIBCPP_POP_MACROS
  137. #endif // _LIBCPP_STD_VER >= 20
  138. #endif // _LIBCPP___ALGORITHM_RANGES_MINMAX_H