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.

count.hpp 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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_HPP
  14. #define RANGES_V3_ALGORITHM_COUNT_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)
  30. /// \brief function template \c count
  31. template<typename I, typename S, typename V, typename P = identity>
  32. auto RANGES_FUN_NIEBLOID(count)(I first, S last, V const & val, P proj = P{})
  33. ->CPP_ret(iter_difference_t<I>)( //
  34. requires input_iterator<I> && sentinel_for<S, I> &&
  35. indirect_relation<equal_to, projected<I, P>, V const *>)
  36. {
  37. iter_difference_t<I> n = 0;
  38. for(; first != last; ++first)
  39. if(invoke(proj, *first) == val)
  40. ++n;
  41. return n;
  42. }
  43. /// \overload
  44. template<typename Rng, typename V, typename P = identity>
  45. auto RANGES_FUN_NIEBLOID(count)(Rng && rng, V const & val, P proj = P{})
  46. ->CPP_ret(iter_difference_t<iterator_t<Rng>>)( //
  47. requires input_range<Rng> &&
  48. indirect_relation<equal_to, projected<iterator_t<Rng>, P>, V const *>)
  49. {
  50. return (*this)(begin(rng), end(rng), val, std::move(proj));
  51. }
  52. RANGES_END_NIEBLOID(count)
  53. namespace cpp20
  54. {
  55. using ranges::count;
  56. }
  57. /// @}
  58. } // namespace ranges
  59. #endif // include guard