Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

89 linhas
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_MAX_HPP
  15. #define RANGES_V3_ALGORITHM_MAX_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(max)
  32. /// \brief function template \c max
  33. template<typename T, typename C = less, typename P = identity>
  34. constexpr auto RANGES_FUN_NIEBLOID(max)(
  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)) ? a : b;
  40. }
  41. /// \overload
  42. template<typename Rng, typename C = less, typename P = identity>
  43. constexpr auto RANGES_FUN_NIEBLOID(max)(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, result), invoke(proj, tmp)))
  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(max)(
  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(max)
  72. namespace cpp20
  73. {
  74. using ranges::max;
  75. }
  76. /// @}
  77. } // namespace ranges
  78. #endif // include guard