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.

max_element.hpp 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. #ifndef RANGES_V3_ALGORITHM_MAX_ELEMENT_HPP
  14. #define RANGES_V3_ALGORITHM_MAX_ELEMENT_HPP
  15. #include <range/v3/range_fwd.hpp>
  16. #include <range/v3/functional/comparisons.hpp>
  17. #include <range/v3/functional/identity.hpp>
  18. #include <range/v3/functional/invoke.hpp>
  19. #include <range/v3/iterator/concepts.hpp>
  20. #include <range/v3/iterator/operations.hpp>
  21. #include <range/v3/iterator/traits.hpp>
  22. #include <range/v3/range/access.hpp>
  23. #include <range/v3/range/concepts.hpp>
  24. #include <range/v3/range/dangling.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_element)
  32. /// \brief function template \c max_element
  33. template<typename I, typename S, typename C = less, typename P = identity>
  34. auto RANGES_FUN_NIEBLOID(max_element)(I first, S last, C pred = C{}, P proj = P{})
  35. ->CPP_ret(I)( //
  36. requires forward_iterator<I> && sentinel_for<S, I> &&
  37. indirect_strict_weak_order<C, projected<I, P>>)
  38. {
  39. if(first != last)
  40. for(auto tmp = next(first); tmp != last; ++tmp)
  41. if(invoke(pred, invoke(proj, *first), invoke(proj, *tmp)))
  42. first = tmp;
  43. return first;
  44. }
  45. /// \overload
  46. template<typename Rng, typename C = less, typename P = identity>
  47. auto RANGES_FUN_NIEBLOID(max_element)(Rng && rng, C pred = C{}, P proj = P{})
  48. ->CPP_ret(safe_iterator_t<Rng>)( //
  49. requires forward_range<Rng> &&
  50. indirect_strict_weak_order<C, projected<iterator_t<Rng>, P>>)
  51. {
  52. return (*this)(begin(rng), end(rng), std::move(pred), std::move(proj));
  53. }
  54. RANGES_END_NIEBLOID(max_element)
  55. namespace cpp20
  56. {
  57. using ranges::max_element;
  58. }
  59. /// @}
  60. } // namespace ranges
  61. #endif // include guard