count.h 661 B

12345678910111213141516171819202122232425262728
  1. //
  2. // count.h
  3. // stream
  4. //
  5. // Created by Sam Jaffe on 3/30/23.
  6. //
  7. #pragma once
  8. #include <stream/forward.h>
  9. #include <stream/detail/identity.h>
  10. namespace stream::ranges {
  11. template <typename It, typename S, typename T, typename Proj = detail::identity>
  12. bool count(It it, S end, T const & value, Proj proj = {}) {
  13. size_t result = 0;
  14. for (; it != end; ++it) {
  15. if (value == std::invoke(proj, *it)) { ++result; }
  16. }
  17. return result;
  18. }
  19. template <typename Stream, typename T, typename Proj = detail::identity>
  20. bool count(Stream const & stream, T const & value, Proj proj = {}) {
  21. return count(stream.begin(), stream.end(), value, std::ref(proj));
  22. }
  23. }