You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 satır
3.0KB

  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_MIN_HPP
  15. #define RANGES_V3_ALGORITHM_MIN_HPP
  16. #include <initializer_list>
  17. #include <range/v3/range_fwd.hpp>
  18. #include <range/v3/functional/comparisons.hpp>
  19. #include <range/v3/functional/identity.hpp>
  20. #include <range/v3/functional/invoke.hpp>
  21. #include <range/v3/iterator/concepts.hpp>
  22. #include <range/v3/iterator/traits.hpp>
  23. #include <range/v3/range/access.hpp>
  24. #include <range/v3/range/concepts.hpp>
  25. #include <range/v3/range/traits.hpp>
  26. #include <range/v3/utility/static_const.hpp>
  27. namespace ranges
  28. {
  29. /// \addtogroup group-algorithms
  30. /// @{
  31. RANGES_BEGIN_NIEBLOID(min)
  32. /// \brief function template \c min
  33. template<typename T, typename C = less, typename P = identity>
  34. constexpr auto RANGES_FUN_NIEBLOID(min)(
  35. T const & a, T const & b, C pred = C{}, P proj = P{}) //
  36. ->CPP_ret(T const &)( //
  37. requires indirect_strict_weak_order<C, projected<T const *, P>>)
  38. {
  39. return invoke(pred, invoke(proj, b), invoke(proj, a)) ? b : a;
  40. }
  41. /// \overload
  42. template<typename Rng, typename C = less, typename P = identity>
  43. constexpr auto RANGES_FUN_NIEBLOID(min)(Rng && rng, C pred = C{}, P proj = P{}) //
  44. ->CPP_ret(range_value_t<Rng>)( //
  45. requires input_range<Rng> &&
  46. indirect_strict_weak_order<C, projected<iterator_t<Rng>, P>> &&
  47. indirectly_copyable_storable<iterator_t<Rng>, range_value_t<Rng> *>)
  48. {
  49. auto first = ranges::begin(rng);
  50. auto last = ranges::end(rng);
  51. RANGES_EXPECT(first != last);
  52. range_value_t<Rng> result = *first;
  53. while(++first != last)
  54. {
  55. auto && tmp = *first;
  56. if(invoke(pred, invoke(proj, tmp), invoke(proj, result)))
  57. result = (decltype(tmp) &&)tmp;
  58. }
  59. return result;
  60. }
  61. /// \overload
  62. template<typename T, typename C = less, typename P = identity>
  63. constexpr auto RANGES_FUN_NIEBLOID(min)(
  64. std::initializer_list<T> const && rng, C pred = C{}, P proj = P{}) //
  65. ->CPP_ret(T)( //
  66. requires copyable<T> &&
  67. indirect_strict_weak_order<C, projected<T const *, P>>)
  68. {
  69. return (*this)(rng, std::move(pred), std::move(proj));
  70. }
  71. RANGES_END_NIEBLOID(min)
  72. namespace cpp20
  73. {
  74. using ranges::min;
  75. }
  76. /// @}
  77. } // namespace ranges
  78. #endif // include guard