Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

69 lines
2.1KB

  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_COUNT_IF_HPP
  14. #define RANGES_V3_ALGORITHM_COUNT_IF_HPP
  15. #include <utility>
  16. #include <range/v3/range_fwd.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/traits.hpp>
  21. #include <range/v3/range/access.hpp>
  22. #include <range/v3/range/concepts.hpp>
  23. #include <range/v3/range/traits.hpp>
  24. #include <range/v3/utility/static_const.hpp>
  25. namespace ranges
  26. {
  27. /// \addtogroup group-algorithms
  28. /// @{
  29. RANGES_BEGIN_NIEBLOID(count_if)
  30. /// \brief function template \c count_if
  31. template<typename I, typename S, typename R, typename P = identity>
  32. auto RANGES_FUN_NIEBLOID(count_if)(I first, S last, R pred, P proj = P{})
  33. ->CPP_ret(iter_difference_t<I>)( //
  34. requires input_iterator<I> && sentinel_for<S, I> &&
  35. indirect_unary_predicate<R, projected<I, P>>)
  36. {
  37. iter_difference_t<I> n = 0;
  38. for(; first != last; ++first)
  39. if(invoke(pred, invoke(proj, *first)))
  40. ++n;
  41. return n;
  42. }
  43. /// \overload
  44. template<typename Rng, typename R, typename P = identity>
  45. auto RANGES_FUN_NIEBLOID(count_if)(Rng && rng, R pred, P proj = P{})
  46. ->CPP_ret(iter_difference_t<iterator_t<Rng>>)( //
  47. requires input_range<Rng> &&
  48. indirect_unary_predicate<R, projected<iterator_t<Rng>, P>>)
  49. {
  50. return (*this)(begin(rng), end(rng), std::move(pred), std::move(proj));
  51. }
  52. RANGES_END_NIEBLOID(count_if)
  53. namespace cpp20
  54. {
  55. using ranges::count_if;
  56. }
  57. /// @}
  58. } // namespace ranges
  59. #endif // include guard