ranges_max.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 <__utility/move.h>
  22. #include <initializer_list>
  23. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  24. # pragma GCC system_header
  25. #endif
  26. #if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
  27. _LIBCPP_PUSH_MACROS
  28. #include <__undef_macros>
  29. _LIBCPP_BEGIN_NAMESPACE_STD
  30. namespace ranges {
  31. namespace __max {
  32. struct __fn {
  33. template <class _Tp, class _Proj = identity,
  34. indirect_strict_weak_order<projected<const _Tp*, _Proj>> _Comp = ranges::less>
  35. _LIBCPP_HIDE_FROM_ABI constexpr
  36. const _Tp& operator()(const _Tp& __a, const _Tp& __b, _Comp __comp = {}, _Proj __proj = {}) const {
  37. return std::invoke(__comp, std::invoke(__proj, __a), std::invoke(__proj, __b)) ? __b : __a;
  38. }
  39. template <copyable _Tp, class _Proj = identity,
  40. indirect_strict_weak_order<projected<const _Tp*, _Proj>> _Comp = ranges::less>
  41. _LIBCPP_HIDE_FROM_ABI constexpr
  42. _Tp operator()(initializer_list<_Tp> __il, _Comp __comp = {}, _Proj __proj = {}) const {
  43. _LIBCPP_ASSERT(__il.begin() != __il.end(), "initializer_list must contain at least one element");
  44. auto __comp_lhs_rhs_swapped = [&](auto&& __lhs, auto&& __rhs) { return std::invoke(__comp, __rhs, __lhs); };
  45. return *ranges::__min_element_impl(__il.begin(), __il.end(), __comp_lhs_rhs_swapped, __proj);
  46. }
  47. template <input_range _Rp, class _Proj = identity,
  48. indirect_strict_weak_order<projected<iterator_t<_Rp>, _Proj>> _Comp = ranges::less>
  49. requires indirectly_copyable_storable<iterator_t<_Rp>, range_value_t<_Rp>*>
  50. _LIBCPP_HIDE_FROM_ABI constexpr
  51. range_value_t<_Rp> operator()(_Rp&& __r, _Comp __comp = {}, _Proj __proj = {}) const {
  52. auto __first = ranges::begin(__r);
  53. auto __last = ranges::end(__r);
  54. _LIBCPP_ASSERT(__first != __last, "range must contain at least one element");
  55. if constexpr (forward_range<_Rp>) {
  56. auto __comp_lhs_rhs_swapped = [&](auto&& __lhs, auto&& __rhs) { return std::invoke(__comp, __rhs, __lhs); };
  57. return *ranges::__min_element_impl(std::move(__first), std::move(__last), __comp_lhs_rhs_swapped, __proj);
  58. } else {
  59. range_value_t<_Rp> __result = *__first;
  60. while (++__first != __last) {
  61. if (std::invoke(__comp, std::invoke(__proj, __result), std::invoke(__proj, *__first)))
  62. __result = *__first;
  63. }
  64. return __result;
  65. }
  66. }
  67. };
  68. } // namespace __max
  69. inline namespace __cpo {
  70. inline constexpr auto max = __max::__fn{};
  71. } // namespace __cpo
  72. } // namespace ranges
  73. _LIBCPP_END_NAMESPACE_STD
  74. _LIBCPP_POP_MACROS
  75. #endif // _LIBCPP_STD_VER > 17 && && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
  76. #endif // _LIBCPP___ALGORITHM_RANGES_MAX_H