| 123456789101112131415161718192021222324252627282930 |
- //
- // count_if.h
- // stream
- //
- // Created by Sam Jaffe on 4/2/23.
- //
- #pragma once
- #include <stream/forward.h>
- #include <stream/detail/identity.h>
- #include <stream/detail/invoke.h>
- namespace stream::ranges {
- template <typename It, typename S, typename Pred,
- typename Proj = detail::identity>
- auto count_if(It it, S end, Pred pred, Proj proj = {}) {
- size_t result = 0;
- for (; it != end; ++it) {
- if (detail::invoke(pred, *it, proj)) { ++result; }
- }
- return result;
- }
- template <typename Stream, typename Pred, typename Proj = detail::identity>
- auto count_if(Stream const & stream, Pred pred, Proj proj = {}) {
- return count_if(stream.begin(), stream.end(), std::ref(pred), std::ref(proj));
- }
- }
|