max.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. #include <range/v3/detail/prologue.hpp>
  28. namespace ranges
  29. {
  30. /// \addtogroup group-algorithms
  31. /// @{
  32. RANGES_FUNC_BEGIN(max)
  33. /// \brief function template \c max
  34. template(typename T, typename C = less, typename P = identity)(
  35. requires indirect_strict_weak_order<C, projected<T const *, P>>)
  36. constexpr T const & RANGES_FUNC(max)(
  37. T const & a, T const & b, C pred = C{}, P proj = 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. requires input_range<Rng> AND
  44. indirect_strict_weak_order<C, projected<iterator_t<Rng>, P>> AND
  45. indirectly_copyable_storable<iterator_t<Rng>, range_value_t<Rng> *>)
  46. constexpr range_value_t<Rng> //
  47. RANGES_FUNC(max)(Rng && rng, C pred = C{}, P proj = P{}) //
  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. requires copyable<T> AND
  64. indirect_strict_weak_order<C, projected<T const *, P>>)
  65. constexpr T RANGES_FUNC(max)(
  66. std::initializer_list<T> const && rng, C pred = C{}, P proj = P{}) //
  67. {
  68. return (*this)(rng, std::move(pred), std::move(proj));
  69. }
  70. RANGES_FUNC_END(max)
  71. namespace cpp20
  72. {
  73. using ranges::max;
  74. }
  75. /// @}
  76. } // namespace ranges
  77. #include <range/v3/detail/epilogue.hpp>
  78. #endif