ranges_max.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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_UNCATEGORIZED(__il.begin() != __il.end(), "initializer_list must contain at least one element");
  50. auto __comp_lhs_rhs_swapped = [&](auto&& __lhs, auto&& __rhs) { return std::invoke(__comp, __rhs, __lhs); };
  51. return *ranges::__min_element_impl(__il.begin(), __il.end(), __comp_lhs_rhs_swapped, __proj);
  52. }
  53. template <input_range _Rp,
  54. class _Proj = identity,
  55. indirect_strict_weak_order<projected<iterator_t<_Rp>, _Proj>> _Comp = ranges::less>
  56. requires indirectly_copyable_storable<iterator_t<_Rp>, range_value_t<_Rp>*>
  57. _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr range_value_t<_Rp>
  58. operator()(_Rp&& __r, _Comp __comp = {}, _Proj __proj = {}) const {
  59. auto __first = ranges::begin(__r);
  60. auto __last = ranges::end(__r);
  61. _LIBCPP_ASSERT_UNCATEGORIZED(__first != __last, "range must contain at least one element");
  62. if constexpr (forward_range<_Rp> && !__is_cheap_to_copy<range_value_t<_Rp>>) {
  63. auto __comp_lhs_rhs_swapped = [&](auto&& __lhs, auto&& __rhs) { return std::invoke(__comp, __rhs, __lhs); };
  64. return *ranges::__min_element_impl(std::move(__first), std::move(__last), __comp_lhs_rhs_swapped, __proj);
  65. } else {
  66. range_value_t<_Rp> __result = *__first;
  67. while (++__first != __last) {
  68. if (std::invoke(__comp, std::invoke(__proj, __result), std::invoke(__proj, *__first)))
  69. __result = *__first;
  70. }
  71. return __result;
  72. }
  73. }
  74. };
  75. } // namespace __max
  76. inline namespace __cpo {
  77. inline constexpr auto max = __max::__fn{};
  78. } // namespace __cpo
  79. } // namespace ranges
  80. _LIBCPP_END_NAMESPACE_STD
  81. _LIBCPP_POP_MACROS
  82. #endif // _LIBCPP_STD_VER >= 20 &&
  83. #endif // _LIBCPP___ALGORITHM_RANGES_MAX_H