ranges_minmax.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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_VALID_ELEMENT_ACCESS(
  60. __il.begin() != __il.end(), "initializer_list has to contain at least one element");
  61. auto __iters = std::__minmax_element_impl(__il.begin(), __il.end(), __comp, __proj);
  62. return ranges::minmax_result<_Type>{*__iters.first, *__iters.second};
  63. }
  64. template <input_range _Range,
  65. class _Proj = identity,
  66. indirect_strict_weak_order<projected<iterator_t<_Range>, _Proj>> _Comp = ranges::less>
  67. requires indirectly_copyable_storable<iterator_t<_Range>, range_value_t<_Range>*>
  68. _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr ranges::minmax_result<range_value_t<_Range>>
  69. operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const {
  70. auto __first = ranges::begin(__r);
  71. auto __last = ranges::end(__r);
  72. using _ValueT = range_value_t<_Range>;
  73. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__first != __last, "range has to contain at least one element");
  74. if constexpr (forward_range<_Range>) {
  75. // Special-case the one element case. Avoid repeatedly initializing objects from the result of an iterator
  76. // dereference when doing so might not be idempotent. The `if constexpr` avoids the extra branch in cases where
  77. // it's not needed.
  78. if constexpr (!same_as<remove_cvref_t<range_reference_t<_Range>>, _ValueT> ||
  79. is_rvalue_reference_v<range_reference_t<_Range>>) {
  80. if (ranges::next(__first) == __last) {
  81. // During initialization, members are allowed to refer to already initialized members
  82. // (see http://eel.is/c++draft/dcl.init.aggr#6)
  83. minmax_result<_ValueT> __result = {*__first, __result.min};
  84. return __result;
  85. }
  86. }
  87. auto __result = std::__minmax_element_impl(__first, __last, __comp, __proj);
  88. return {*__result.first, *__result.second};
  89. } else {
  90. // input_iterators can't be copied, so the implementation for input_iterators has to store
  91. // the values instead of a pointer to the correct values
  92. auto __less = [&](auto&& __a, auto&& __b) -> bool {
  93. return std::invoke(__comp,
  94. std::invoke(__proj, std::forward<decltype(__a)>(__a)),
  95. std::invoke(__proj, std::forward<decltype(__b)>(__b)));
  96. };
  97. // During initialization, members are allowed to refer to already initialized members
  98. // (see http://eel.is/c++draft/dcl.init.aggr#6)
  99. ranges::minmax_result<_ValueT> __result = {*__first, __result.min};
  100. if (__first == __last || ++__first == __last)
  101. return __result;
  102. if (__less(*__first, __result.min))
  103. __result.min = *__first;
  104. else
  105. __result.max = *__first;
  106. while (++__first != __last) {
  107. _ValueT __i = *__first;
  108. if (++__first == __last) {
  109. if (__less(__i, __result.min))
  110. __result.min = __i;
  111. else if (!__less(__i, __result.max))
  112. __result.max = __i;
  113. return __result;
  114. }
  115. if (__less(*__first, __i)) {
  116. if (__less(*__first, __result.min))
  117. __result.min = *__first;
  118. if (!__less(__i, __result.max))
  119. __result.max = std::move(__i);
  120. } else {
  121. if (__less(__i, __result.min))
  122. __result.min = std::move(__i);
  123. if (!__less(*__first, __result.max))
  124. __result.max = *__first;
  125. }
  126. }
  127. return __result;
  128. }
  129. }
  130. };
  131. } // namespace __minmax
  132. inline namespace __cpo {
  133. inline constexpr auto minmax = __minmax::__fn{};
  134. } // namespace __cpo
  135. } // namespace ranges
  136. _LIBCPP_END_NAMESPACE_STD
  137. _LIBCPP_POP_MACROS
  138. #endif // _LIBCPP_STD_VER >= 20
  139. #endif // _LIBCPP___ALGORITHM_RANGES_MINMAX_H