ranges_min.h 3.5 KB

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