ranges_min.h 3.4 KB

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