| 12345678910111213141516171819202122232425262728 |
- //
- // count.h
- // stream
- //
- // Created by Sam Jaffe on 3/30/23.
- //
- #pragma once
- #include <stream/forward.h>
- #include <stream/detail/identity.h>
- namespace stream::ranges {
- template <typename It, typename S, typename T, typename Proj = detail::identity>
- bool count(It it, S end, T const & value, Proj proj = {}) {
- size_t result = 0;
- for (; it != end; ++it) {
- if (value == std::invoke(proj, *it)) { ++result; }
- }
- return result;
- }
- template <typename Stream, typename T, typename Proj = detail::identity>
- bool count(Stream const & stream, T const & value, Proj proj = {}) {
- return count(stream.begin(), stream.end(), value, std::ref(proj));
- }
- }
|