您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

minmax_element.hpp 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /// \file
  2. // Range v3 library
  3. //
  4. // Copyright Eric Niebler 2014-present
  5. //
  6. // Use, modification and distribution is subject to the
  7. // Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. //
  11. // Project home: https://github.com/ericniebler/range-v3
  12. //
  13. // Implementation based on the code in libc++
  14. // http://http://libcxx.llvm.org/
  15. #ifndef RANGES_V3_ALGORITHM_MINMAX_ELEMENT_HPP
  16. #define RANGES_V3_ALGORITHM_MINMAX_ELEMENT_HPP
  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/dangling.hpp>
  27. #include <range/v3/range/traits.hpp>
  28. #include <range/v3/utility/static_const.hpp>
  29. namespace ranges
  30. {
  31. /// \addtogroup group-algorithms
  32. /// @{
  33. template<typename I>
  34. using minmax_element_result = detail::min_max_result<I, I>;
  35. RANGES_BEGIN_NIEBLOID(minmax_element)
  36. /// \brief function template \c minmax_element
  37. template<typename I, typename S, typename C = less, typename P = identity>
  38. auto RANGES_FUN_NIEBLOID(minmax_element)(
  39. I first, S last, C pred = C{}, P proj = P{}) //
  40. ->CPP_ret(minmax_element_result<I>)( //
  41. requires forward_iterator<I> && sentinel_for<S, I> &&
  42. indirect_strict_weak_order<C, projected<I, P>>)
  43. {
  44. minmax_element_result<I> result{first, first};
  45. if(first == last || ++first == last)
  46. return result;
  47. if(invoke(pred, invoke(proj, *first), invoke(proj, *result.min)))
  48. result.min = first;
  49. else
  50. result.max = first;
  51. while(++first != last)
  52. {
  53. I tmp = first;
  54. if(++first == last)
  55. {
  56. if(invoke(pred, invoke(proj, *tmp), invoke(proj, *result.min)))
  57. result.min = tmp;
  58. else if(!invoke(pred, invoke(proj, *tmp), invoke(proj, *result.max)))
  59. result.max = tmp;
  60. break;
  61. }
  62. else
  63. {
  64. if(invoke(pred, invoke(proj, *first), invoke(proj, *tmp)))
  65. {
  66. if(invoke(pred, invoke(proj, *first), invoke(proj, *result.min)))
  67. result.min = first;
  68. if(!invoke(pred, invoke(proj, *tmp), invoke(proj, *result.max)))
  69. result.max = tmp;
  70. }
  71. else
  72. {
  73. if(invoke(pred, invoke(proj, *tmp), invoke(proj, *result.min)))
  74. result.min = tmp;
  75. if(!invoke(pred, invoke(proj, *first), invoke(proj, *result.max)))
  76. result.max = first;
  77. }
  78. }
  79. }
  80. return result;
  81. }
  82. /// \overload
  83. template<typename Rng, typename C = less, typename P = identity>
  84. auto RANGES_FUN_NIEBLOID(minmax_element)(
  85. Rng && rng, C pred = C{}, P proj = P{}) //
  86. ->CPP_ret(minmax_element_result<safe_iterator_t<Rng>>)( //
  87. requires forward_range<Rng> &&
  88. indirect_strict_weak_order<C, projected<iterator_t<Rng>, P>>)
  89. {
  90. return (*this)(begin(rng), end(rng), std::move(pred), std::move(proj));
  91. }
  92. RANGES_END_NIEBLOID(minmax_element)
  93. namespace cpp20
  94. {
  95. using ranges::minmax_element;
  96. using ranges::minmax_element_result;
  97. } // namespace cpp20
  98. /// @}
  99. } // namespace ranges
  100. #endif // include guard