Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

vor 5 Jahren
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /// \file
  2. // Range v3 library
  3. //
  4. // Copyright Eric Niebler 2014-present
  5. // Copyright Casey Carter 2015
  6. //
  7. // Use, modification and distribution is subject to the
  8. // Boost Software License, Version 1.0. (See accompanying
  9. // file LICENSE_1_0.txt or copy at
  10. // http://www.boost.org/LICENSE_1_0.txt)
  11. //
  12. // Project home: https://github.com/ericniebler/range-v3
  13. //
  14. #ifndef RANGES_V3_ALGORITHM_MINMAX_HPP
  15. #define RANGES_V3_ALGORITHM_MINMAX_HPP
  16. #include <initializer_list>
  17. #include <range/v3/range_fwd.hpp>
  18. #include <range/v3/algorithm/result_types.hpp>
  19. #include <range/v3/functional/comparisons.hpp>
  20. #include <range/v3/functional/identity.hpp>
  21. #include <range/v3/functional/invoke.hpp>
  22. #include <range/v3/iterator/concepts.hpp>
  23. #include <range/v3/iterator/traits.hpp>
  24. #include <range/v3/range/access.hpp>
  25. #include <range/v3/range/concepts.hpp>
  26. #include <range/v3/range/traits.hpp>
  27. #include <range/v3/utility/static_const.hpp>
  28. namespace ranges
  29. {
  30. /// \addtogroup group-algorithms
  31. /// @{
  32. template<typename T>
  33. using minmax_result = detail::min_max_result<T, T>;
  34. RANGES_BEGIN_NIEBLOID(minmax)
  35. /// \brief function template \c minmax
  36. template<typename T, typename C = less, typename P = identity>
  37. constexpr auto RANGES_FUN_NIEBLOID(minmax)(
  38. T const & a, T const & b, C pred = C{}, P proj = P{}) //
  39. ->CPP_ret(minmax_result<T const &>)( //
  40. requires indirect_strict_weak_order<C, projected<T const *, P>>)
  41. {
  42. using R = minmax_result<T const &>;
  43. return invoke(pred, invoke(proj, b), invoke(proj, a)) ? R{b, a} : R{a, b};
  44. }
  45. /// \overload
  46. template<typename Rng, typename C = less, typename P = identity>
  47. constexpr auto RANGES_FUN_NIEBLOID(minmax)(
  48. Rng && rng, C pred = C{}, P proj = P{}) //
  49. ->CPP_ret(minmax_result<range_value_t<Rng>>)( //
  50. requires input_range<Rng> &&
  51. indirect_strict_weak_order<C, projected<iterator_t<Rng>, P>> &&
  52. indirectly_copyable_storable<iterator_t<Rng>, range_value_t<Rng> *>)
  53. {
  54. using R = minmax_result<range_value_t<Rng>>;
  55. auto first = ranges::begin(rng);
  56. auto last = ranges::end(rng);
  57. RANGES_EXPECT(first != last);
  58. auto result = R{*first, *first};
  59. if(++first != last)
  60. {
  61. {
  62. auto && tmp = *first;
  63. if(invoke(pred, invoke(proj, tmp), invoke(proj, result.min)))
  64. result.min = (decltype(tmp) &&)tmp;
  65. else
  66. result.max = (decltype(tmp) &&)tmp;
  67. }
  68. while(++first != last)
  69. {
  70. range_value_t<Rng> tmp1 = *first;
  71. if(++first == last)
  72. {
  73. if(invoke(pred, invoke(proj, tmp1), invoke(proj, result.min)))
  74. result.min = std::move(tmp1);
  75. else if(!invoke(
  76. pred, invoke(proj, tmp1), invoke(proj, result.max)))
  77. result.max = std::move(tmp1);
  78. break;
  79. }
  80. auto && tmp2 = *first;
  81. if(invoke(pred, invoke(proj, tmp2), invoke(proj, tmp1)))
  82. {
  83. if(invoke(pred, invoke(proj, tmp2), invoke(proj, result.min)))
  84. result.min = (decltype(tmp2) &&)tmp2;
  85. if(!invoke(pred, invoke(proj, tmp1), invoke(proj, result.max)))
  86. result.max = std::move(tmp1);
  87. }
  88. else
  89. {
  90. if(invoke(pred, invoke(proj, tmp1), invoke(proj, result.min)))
  91. result.min = std::move(tmp1);
  92. if(!invoke(pred, invoke(proj, tmp2), invoke(proj, result.max)))
  93. result.max = (decltype(tmp2) &&)tmp2;
  94. }
  95. }
  96. }
  97. return result;
  98. }
  99. /// \overload
  100. template<typename T, typename C = less, typename P = identity>
  101. constexpr auto RANGES_FUN_NIEBLOID(minmax)(
  102. std::initializer_list<T> const && rng, C pred = C{}, P proj = P{}) //
  103. ->CPP_ret(minmax_result<T>)( //
  104. requires copyable<T> &&
  105. indirect_strict_weak_order<C, projected<T const *, P>>)
  106. {
  107. return (*this)(rng, std::move(pred), std::move(proj));
  108. }
  109. RANGES_END_NIEBLOID(minmax)
  110. namespace cpp20
  111. {
  112. using ranges::minmax;
  113. using ranges::minmax_result;
  114. } // namespace cpp20
  115. /// @}
  116. } // namespace ranges
  117. #endif // include guard