ranges_max.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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_MAX_H
  9. #define _LIBCPP___ALGORITHM_RANGES_MAX_H
  10. #include <__algorithm/ranges_min_element.h>
  11. #include <__assert>
  12. #include <__concepts/copyable.h>
  13. #include <__config>
  14. #include <__functional/identity.h>
  15. #include <__functional/invoke.h>
  16. #include <__functional/ranges_operations.h>
  17. #include <__iterator/concepts.h>
  18. #include <__iterator/projected.h>
  19. #include <__ranges/access.h>
  20. #include <__ranges/concepts.h>
  21. #include <__type_traits/is_trivially_copyable.h>
  22. #include <__utility/move.h>
  23. #include <initializer_list>
  24. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  25. # pragma GCC system_header
  26. #endif
  27. #if _LIBCPP_STD_VER >= 20
  28. _LIBCPP_PUSH_MACROS
  29. # include <__undef_macros>
  30. _LIBCPP_BEGIN_NAMESPACE_STD
  31. namespace ranges {
  32. namespace __max {
  33. struct __fn {
  34. template <class _Tp,
  35. class _Proj = identity,
  36. indirect_strict_weak_order<projected<const _Tp*, _Proj>> _Comp = ranges::less>
  37. _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr const _Tp&
  38. operator()(_LIBCPP_LIFETIMEBOUND const _Tp& __a,
  39. _LIBCPP_LIFETIMEBOUND const _Tp& __b,
  40. _Comp __comp = {},
  41. _Proj __proj = {}) const {
  42. return std::invoke(__comp, std::invoke(__proj, __a), std::invoke(__proj, __b)) ? __b : __a;
  43. }
  44. template <copyable _Tp,
  45. class _Proj = identity,
  46. indirect_strict_weak_order<projected<const _Tp*, _Proj>> _Comp = ranges::less>
  47. _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr _Tp
  48. operator()(initializer_list<_Tp> __il, _Comp __comp = {}, _Proj __proj = {}) const {
  49. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
  50. __il.begin() != __il.end(), "initializer_list must contain at least one element");
  51. auto __comp_lhs_rhs_swapped = [&](auto&& __lhs, auto&& __rhs) -> bool { return std::invoke(__comp, __rhs, __lhs); };
  52. return *ranges::__min_element_impl(__il.begin(), __il.end(), __comp_lhs_rhs_swapped, __proj);
  53. }
  54. template <input_range _Rp,
  55. class _Proj = identity,
  56. indirect_strict_weak_order<projected<iterator_t<_Rp>, _Proj>> _Comp = ranges::less>
  57. requires indirectly_copyable_storable<iterator_t<_Rp>, range_value_t<_Rp>*>
  58. _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr range_value_t<_Rp>
  59. operator()(_Rp&& __r, _Comp __comp = {}, _Proj __proj = {}) const {
  60. auto __first = ranges::begin(__r);
  61. auto __last = ranges::end(__r);
  62. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__first != __last, "range must contain at least one element");
  63. if constexpr (forward_range<_Rp> && !__is_cheap_to_copy<range_value_t<_Rp>>) {
  64. auto __comp_lhs_rhs_swapped = [&](auto&& __lhs, auto&& __rhs) -> bool {
  65. return std::invoke(__comp, __rhs, __lhs);
  66. };
  67. return *ranges::__min_element_impl(std::move(__first), std::move(__last), __comp_lhs_rhs_swapped, __proj);
  68. } else {
  69. range_value_t<_Rp> __result = *__first;
  70. while (++__first != __last) {
  71. if (std::invoke(__comp, std::invoke(__proj, __result), std::invoke(__proj, *__first)))
  72. __result = *__first;
  73. }
  74. return __result;
  75. }
  76. }
  77. };
  78. } // namespace __max
  79. inline namespace __cpo {
  80. inline constexpr auto max = __max::__fn{};
  81. } // namespace __cpo
  82. } // namespace ranges
  83. _LIBCPP_END_NAMESPACE_STD
  84. _LIBCPP_POP_MACROS
  85. #endif // _LIBCPP_STD_VER >= 20
  86. #endif // _LIBCPP___ALGORITHM_RANGES_MAX_H