minmax.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. #include <range/v3/detail/prologue.hpp>
  29. namespace ranges
  30. {
  31. /// \addtogroup group-algorithms
  32. /// @{
  33. template<typename T>
  34. using minmax_result = detail::min_max_result<T, T>;
  35. RANGES_FUNC_BEGIN(minmax)
  36. /// \brief function template \c minmax
  37. template(typename T, typename C = less, typename P = identity)(
  38. requires indirect_strict_weak_order<C, projected<T const *, P>>)
  39. constexpr minmax_result<T const &> RANGES_FUNC(minmax)(
  40. T const & a, T const & b, C pred = C{}, P proj = 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. requires input_range<Rng> AND
  48. indirect_strict_weak_order<C, projected<iterator_t<Rng>, P>> AND
  49. indirectly_copyable_storable<iterator_t<Rng>, range_value_t<Rng> *>)
  50. constexpr minmax_result<range_value_t<Rng>> //
  51. RANGES_FUNC(minmax)(Rng && rng, C pred = C{}, P proj = P{}) //
  52. {
  53. using R = minmax_result<range_value_t<Rng>>;
  54. auto first = ranges::begin(rng);
  55. auto last = ranges::end(rng);
  56. RANGES_EXPECT(first != last);
  57. auto result = R{*first, *first};
  58. if(++first != last)
  59. {
  60. {
  61. auto && tmp = *first;
  62. if(invoke(pred, invoke(proj, tmp), invoke(proj, result.min)))
  63. result.min = (decltype(tmp) &&)tmp;
  64. else
  65. result.max = (decltype(tmp) &&)tmp;
  66. }
  67. while(++first != last)
  68. {
  69. range_value_t<Rng> tmp1 = *first;
  70. if(++first == last)
  71. {
  72. if(invoke(pred, invoke(proj, tmp1), invoke(proj, result.min)))
  73. result.min = std::move(tmp1);
  74. else if(!invoke(
  75. pred, invoke(proj, tmp1), invoke(proj, result.max)))
  76. result.max = std::move(tmp1);
  77. break;
  78. }
  79. auto && tmp2 = *first;
  80. if(invoke(pred, invoke(proj, tmp2), invoke(proj, tmp1)))
  81. {
  82. if(invoke(pred, invoke(proj, tmp2), invoke(proj, result.min)))
  83. result.min = (decltype(tmp2) &&)tmp2;
  84. if(!invoke(pred, invoke(proj, tmp1), invoke(proj, result.max)))
  85. result.max = std::move(tmp1);
  86. }
  87. else
  88. {
  89. if(invoke(pred, invoke(proj, tmp1), invoke(proj, result.min)))
  90. result.min = std::move(tmp1);
  91. if(!invoke(pred, invoke(proj, tmp2), invoke(proj, result.max)))
  92. result.max = (decltype(tmp2) &&)tmp2;
  93. }
  94. }
  95. }
  96. return result;
  97. }
  98. /// \overload
  99. template(typename T, typename C = less, typename P = identity)(
  100. requires copyable<T> AND
  101. indirect_strict_weak_order<C, projected<T const *, P>>)
  102. constexpr minmax_result<T> RANGES_FUNC(minmax)(
  103. std::initializer_list<T> const && rng, C pred = C{}, P proj = P{}) //
  104. {
  105. return (*this)(rng, std::move(pred), std::move(proj));
  106. }
  107. RANGES_FUNC_END(minmax)
  108. namespace cpp20
  109. {
  110. using ranges::minmax;
  111. using ranges::minmax_result;
  112. } // namespace cpp20
  113. /// @}
  114. } // namespace ranges
  115. #include <range/v3/detail/epilogue.hpp>
  116. #endif